refactor(worker): move job handler data access into business services - #1101
Open
realcodesiman wants to merge 1 commit into
Open
refactor(worker): move job handler data access into business services#1101realcodesiman wants to merge 1 commit into
realcodesiman wants to merge 1 commit into
Conversation
Removes direct db usage from apps/worker/src/{default,schedule,trigger,
sequence-scheduler,webhook} per .agents/rules/data-access.md. Query
bodies, chunk sizes, orderings, onConflict targets and raw SQL are moved
verbatim; only signatures change.
- new tag-channel and sequence-dispatch repositories, mac-partitions
DDL helpers, contactRepository.listForExportPage,
fileRepository.updateForWorkspace
- broadcastService gains the prepare/send/reconcile reads and writes;
promoteAfterPrepare keeps the resumeCount CAS guard
- userQuotaService.reconcileUserSelfUsage / persistMacUsed /
applyMonthlyBotMessagesReset and workspaceUsageService.loadReconcileCounts
/ upsertReconciled keep the direct-assign (not GREATEST) semantics and
the DB-before-Redis zeroing order
- triggerService / webhookService / tagService / conversationService /
flowService / inboxService / zaloIntegrationService gain the trigger
engine and sync-tag reads; the trigger-only tag helpers deliberately do
not emit or bulk-enqueue
- contactService.insertImportedContactBatch (body in
contact/insert-imported-batch.ts) holds the import transaction
isBlockedWorkspace call sites and retry semantics are unchanged.
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
db.*call fromapps/worker/src/{default,schedule,trigger,sequence-scheduler,webhook}(export/import handlers, tag sync, broadcast crons, quota reconcile, trigger engine, sequence scheduler, webhook engine) per.agents/rules/data-access.md. Bodies are moved verbatim; chunk sizes, orderings,onConflicttargets and raw SQL are unchanged.main, append-only barrel edits. The newcontactrepository also exists on feat(contacts): cover full public API for MCP, move logic into business services聽#1093; keep one copy when merging.Changes
tag-channelandsequence-dispatchrepositories,mac-partitions.tsDDL helpers,contactRepository.listForExportPage,fileRepository.updateForWorkspace,contactInboxRepository.{listByInboxPage,listContactIdsByIds},integrationMessengerRepository.{findById,findByInboxId,listByWorkspace}.broadcastService:listDueScheduled,listSendingAwaitingHandoff,findScheduledForPrepare,resolveTemplateIntegrationMessengerId,insertRecipients,promoteAfterPrepare(keeps theresumeCount = promotionEpochCAS guard and returns the boolean the handler branches on),listSendableById,listPendingRecipients,markContactFailed.userQuotaService.{reconcileUserSelfUsage,persistMacUsed,applyMonthlyBotMessagesReset}andworkspaceUsageService.{loadReconcileCounts,upsertReconciled}: direct-assign semantics (neverGREATEST),onConflictDoUpdatekept, self-pathhsethas nomacfield, DB zeroing still precedes the Redis field write (AGENTS.md invariants 11 and 12).triggerService,webhookService,tagService,conversationService,flowService,inboxService,zaloIntegrationService: trigger-engine and sync-tag reads/writes.tagService.{attachExistingToContactForTrigger,detachFromContactForTrigger}deliberately do not emit or bulk-enqueue; the worker keeps its own per-pair enqueue loops.hardDeleteSoftDeletedkeeps theisNotNull(deletedAt)guard.sequenceDispatchRepository.claimkeeps thestatus='pending'predicate + affected-row check;deleteTerminalBatchkeeps the CTE with the partition-pruningsd.workspaceId = rows.workspaceIdjoin.contactService.insertImportedContactBatch(body incontact/insert-imported-batch.ts, exposed as a normal class method) holds the import transaction verbatim.Notes for reviewers
isBlockedWorkspaceis still called at the same points inprepare-broadcastandprocess-broadcast-contacts, and was not added to the fan-out crons.packages/business/src/tag/service.ts,flow/service.tsand the two barrels will conflict trivially with the sibling worker-integration PR (refactor(worker): move integration, chat and ai-agent handler data access into business聽#1099) and feat(contacts): cover full public API for MCP, move logic into business services聽#1093; all are append-or-keep-both.Test plan
pnpm --filter @chatbotx.io/database check-types && testpnpm --filter @chatbotx.io/business check-types && test(only the pre-existingads-conversion-rule.service.test.tsfailures remain, also red onmain)pnpm --filter worker check-types && test(only the pre-existingflow-import-handler.test.tsfailures remain, also red onmain)pnpm --filter worker buildpnpm lint