Skip to content

refactor(builder): move messenger, instagram and whatsapp data access into business - #1100

Open
realcodesiman wants to merge 1 commit into
mainfrom
refactor/data-access-builder-meta-channels
Open

refactor(builder): move messenger, instagram and whatsapp data access into business#1100
realcodesiman wants to merge 1 commit into
mainfrom
refactor/data-access-builder-meta-channels

Conversation

@realcodesiman

Copy link
Copy Markdown
Contributor

Summary

  • Removes every direct db.* call from the Meta channel integrations in the builder (Messenger, Instagram, WhatsApp: connect, disconnect, update, queries, template/flow sync) and the shared OAuth callback, per .agents/rules/data-access.md.
  • One of seven parallel scope PRs following feat(contacts): cover full public API for MCP, move logic into business services #1093 (contacts); based on main, one-line barrel append only (./whatsapp-message-template, also added by the automation PR; keep one).

Changes

  • Messenger/Instagram: connectMessengerPage / connectInstagramAccount hold the connect transaction (DB work only); deleteWithCleanup keeps the teardown → tag-channel → CAPI → integration → inbox order in one transaction; updateSettings, listForWorkspace, syncMessageTemplates; message-template reads/deletes on integrationMessengerRepository.
  • WhatsApp: connectPhoneNumber (signup session consumed in the same transaction, unique violation → typed ChatbotXException the action maps to its translated strings), deleteWithCleanup (reuses coexistService.tearDownForIntegration, which adds a channel filter; safe because integrationId is unique), markWebhookVerified/markWebhookOverrideOk, the client-safe column allowlist moved into integrationWhatsappRepository.listClientSafeByWorkspaceId (pinned by a test that auth/capiAccessToken stay excluded), whatsappFlowRepository.syncForIntegration and a new whatsapp-message-template repository.
  • integrationService.createFromOAuthCallback for app/integrations/[...integration]/callback.ts.

Behavior changes

  • Meta API calls that used to run inside the DB transaction now run outside it in update-messenger-action, update-instagram-action and both Instagram select-account actions (business does not depend on the channel SDKs). Guards run before the write. The Instagram native-login webhook subscribe now runs before the DB write, so a failed subscribe still prevents the connect without leaving an orphaned row, matching the Messenger select-page action. Pinned by apps/builder/__tests__/instagram-select-account-ordering.test.ts.
  • deleteWithCleanup for Messenger/Instagram lives in sibling files (integration-{messenger,instagram}/disconnect.ts) to keep the existing service test's import graph intact.
  • The workspace-less audit call in the WhatsApp disconnect action is preserved as-is.

Test plan

  • pnpm --filter @chatbotx.io/database check-types && test
  • pnpm --filter @chatbotx.io/business check-types && test (only the pre-existing ads-conversion-rule.service.test.ts failures remain, also red on main)
  • pnpm --filter builder check-types && test
  • pnpm lint
  • Manual: connect + disconnect a Messenger page, an Instagram account (both variants) and a WhatsApp number; sync WhatsApp flows/templates; clone a Messenger template across workspaces; complete a Google Sheets OAuth callback

… into business

Removes direct db usage from the Meta channel integrations (Messenger,
Instagram, WhatsApp) and the shared OAuth callback per
.agents/rules/data-access.md.

- messenger/instagram: connectMessengerPage / connectInstagramAccount
  hold the connect transaction; deleteWithCleanup keeps the teardown ->
  tag-channel -> capi -> integration -> inbox order in one transaction;
  updateSettings, listForWorkspace, syncMessageTemplates and the
  message-template repository reads/deletes
- whatsapp: connectPhoneNumber (signup session consumed in the same
  transaction, unique violation mapped to a typed exception),
  deleteWithCleanup, markWebhookVerified/OverrideOk, client-safe list
  columns moved into the repository, flow/template sync repositories
- integrationService.createFromOAuthCallback for the callback route

Meta API calls that used to run inside the connect/update transactions
now run outside them (business does not depend on the channel SDKs);
the Instagram native-login webhook subscribe runs before the DB write so
a failed subscribe still prevents the connect, matching the Messenger
action. Pinned by instagram-select-account-ordering.test.ts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Refactor or performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant