Skip to content

feat(worker): add scheduled job to refresh zalo oauth tokens daily - #552

Merged
sung17 merged 1 commit into
mainfrom
feat/zalo-refresh-token-worker
Jun 9, 2026
Merged

feat(worker): add scheduled job to refresh zalo oauth tokens daily#552
sung17 merged 1 commit into
mainfrom
feat/zalo-refresh-token-worker

Conversation

@sung17

@sung17 sung17 commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Adds a new BullMQ scheduled job (refreshZaloTokens) that runs daily at 02:00 UTC to refresh OAuth access tokens for all Zalo integrations
  • Introduces ZaloIntegrationService in @chatbotx.io/business with findAll and updateAuth methods to keep DB access out of the worker layer
  • Registers the new job type in @chatbotx.io/worker-config so it is type-safe across the monorepo

Changes

  • packages/worker-config/src/queues/schedule/index.ts — new refreshZaloTokens job constant and ScheduleJobRefreshZaloTokens type
  • packages/business/src/integration-zalo/service.ts — new ZaloIntegrationService (findAll, updateAuth)
  • packages/business/src/integration-zalo/index.ts — re-exports the new service
  • apps/worker/src/schedule/handlers/refresh-zalo-tokens.ts — iterates integrations and refreshes tokens, logs per-entry results and skips entries with no refresh token
  • apps/worker/src/schedule/handlers/register-schedules.ts — registers the cron scheduler (0 2 * * *)
  • apps/worker/src/schedule/worker.ts — routes refreshZaloTokens job type to the handler

Test plan

  • pnpm lint passes
  • pnpm --filter worker check-types passes
  • pnpm --filter @chatbotx.io/business check-types passes
  • Deploy to staging and confirm the job appears in the BullMQ dashboard
  • Manually trigger the job and verify Zalo tokens update in the database
  • Confirm integrations with no refreshToken are skipped with a warning log, not crashed

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 9, 2026 09:31
@github-actions github-actions Bot added the feature New feature or request label Jun 9, 2026
@sung17
sung17 merged commit 9fad512 into main Jun 9, 2026
8 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 refreshZaloTokens as a new schedule job type in @chatbotx.io/worker-config.
  • Introduce ZaloIntegrationService in @chatbotx.io/business to 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),
},
})
viethung0o0 pushed a commit that referenced this pull request Sep 7, 2026
)

Co-authored-by: Deathgiver <anonymous@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants