feat(worker): add scheduled job to refresh zalo oauth tokens daily - #552
Merged
Conversation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adds a new scheduled BullMQ job in the worker to refresh OAuth access tokens for all Zalo integrations daily, while introducing a business-layer service to keep DB access out of the worker layer and extending the worker-config schedule job typing for monorepo safety.
Changes:
- Add
refreshZaloTokensas a new schedule job type in@chatbotx.io/worker-config. - Introduce
ZaloIntegrationServicein@chatbotx.io/businessto list Zalo integrations and update their auth payloads. - Register and route a daily cron (02:00) scheduled job in the worker to refresh tokens for each Zalo integration.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/worker-config/src/queues/schedule/index.ts | Adds the new refreshZaloTokens schedule job constant and union type entry. |
| packages/business/src/integration-zalo/service.ts | Adds a business-layer service for listing integrations and updating stored auth JSON. |
| packages/business/src/integration-zalo/index.ts | Re-exports the new Zalo integration service from the domain barrel. |
| apps/worker/src/schedule/handlers/refresh-zalo-tokens.ts | Implements the scheduled handler that refreshes tokens and persists updated auth. |
| apps/worker/src/schedule/handlers/register-schedules.ts | Registers the cron schedule for refreshZaloTokens (0 2 * * *). |
| apps/worker/src/schedule/worker.ts | Routes the new schedule job type to the new handler. |
Comment on lines
+15
to
+33
| const auth = integration.auth as ZaloAuthValue | ||
| if (!auth.tokens.refreshToken) { | ||
| logger.warn( | ||
| `[refreshZaloTokens] id=${integration.id} skipped: no refreshToken`, | ||
| ) | ||
| continue | ||
| } | ||
|
|
||
| const newTokens = await refreshAccessToken(auth, auth.tokens.refreshToken) | ||
|
|
||
| await zaloIntegrationService.updateAuth(integration.id, { | ||
| ...auth, | ||
| tokens: { | ||
| ...auth.tokens, | ||
| accessToken: newTokens.access_token, | ||
| refreshToken: newTokens.refresh_token, | ||
| expiresAt: calculateExpiresAt(newTokens.expires_in), | ||
| }, | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Changes
Test plan