| import { updateInboxAccount } from "@midday/db/queries"; |
| import { triggerJob } from "@midday/job-client"; |
| import type { Job } from "bullmq"; |
| import { registerDynamicScheduler } from "../../schedulers/registry"; |
| import type { InboxProviderInitialSetupPayload } from "../../schemas/inbox"; |
| import { getDb } from "../../utils/db"; |
| import { generateQuarterDailyCronTag } from "../../utils/generate-cron-tag"; |
| import { BaseProcessor } from "../base"; |
|
|
| |
| |
| |
| |
| export class InitialSetupProcessor extends BaseProcessor<InboxProviderInitialSetupPayload> { |
| async process(job: Job<InboxProviderInitialSetupPayload>): Promise<{ |
| inboxAccountId: string; |
| schedulerRegistered: boolean; |
| }> { |
| const { inboxAccountId } = job.data; |
| const db = getDb(); |
|
|
| this.logger.info("Starting initial inbox setup", { inboxAccountId }); |
|
|
| |
| |
| const cronPattern = generateQuarterDailyCronTag(inboxAccountId); |
|
|
| try { |
| await registerDynamicScheduler({ |
| template: "inbox-sync-scheduler", |
| accountId: inboxAccountId, |
| cronPattern, |
| }); |
|
|
| this.logger.info("Dynamic scheduler registered for inbox account", { |
| inboxAccountId, |
| cronPattern, |
| }); |
|
|
| |
| |
| const schedulerJobKey = `inbox-sync-${inboxAccountId}`; |
|
|
| await updateInboxAccount(db, { |
| id: inboxAccountId, |
| scheduleId: schedulerJobKey, |
| }); |
|
|
| |
| await triggerJob( |
| "sync-scheduler", |
| { |
| id: inboxAccountId, |
| manualSync: true, |
| }, |
| "inbox-provider", |
| ); |
|
|
| this.logger.info("Initial inbox setup completed", { inboxAccountId }); |
|
|
| return { |
| inboxAccountId, |
| schedulerRegistered: true, |
| }; |
| } catch (error) { |
| this.logger.error("Failed to register inbox scheduler", { |
| inboxAccountId, |
| error: error instanceof Error ? error.message : "Unknown error", |
| }); |
|
|
| throw error; |
| } |
| } |
| } |
|
|