Skip to main content

ADR-0004: Finance accounts own their connection

Status​

Accepted. PaymentAccount and AccountingSystem exist in 55-finance-accounts.prisma, each with a connectionId, and are created from Integrations.

Context​

Channels, couriers and vendors are typed business records that own an IntegrationConnection through connectionId. Two finance integrations did not follow that pattern:

  • Tenant payment gateways were stored as a standalone connection looked up by tenant and integration code. That allowed one account per gateway per tenant, gateway data referenced only a gateway code string, and there was no status lifecycle.
  • Zoho Books and Tally Prime had no stored connection. Zoho credentials were sent in every request body and kept only in browser state, so no background push was possible.

Decision​

  • Every external finance account is a typed record that owns its connection: PaymentAccount (one merchant account at one gateway) and AccountingSystem (one Zoho organisation or Tally company).
  • IntegrationConnection stays a credential vault and health signal. IntegrationRegistry stays the catalogue (categories payment and accounting).
  • Data produced through an account references the record by id: gateway Payouts, GatewaySettlementMapping and OrderPaymentTransaction carry paymentAccountId; ErpAccountMapping and ErpSyncLog carry accountingSystemId. (Gateway payouts moved onto the shared payout spine in ADR-0005.)
  • Status follows ADR-0002: INACTIVE only by a person, SUSPENDED only by the circuit breaker (5 consecutive failed syncs or pushes).
  • Accounts are connected and managed in Integrations. Settlements only performs day-to-day work on existing accounts.
  • A payment account is not tied to a store. Gateway payment ids are unique per gateway, so matching needs no routing, and the account is stamped on an order payment when a line from it matches.
  • An accounting system optionally belongs to a LegalEntity; journals route Channel → Seller → LegalEntity → AccountingSystem, falling back to the system without an entity.
  • Dependencies run one way: finance/settlement depends on finance/payment-account and finance/accounting-system, which own their ports. Integrations implement the ports.

Alternatives​

  • Keep standalone connections and add connectionId to lines and payouts. Rejected: it makes the credential vault a business entity, with no status lifecycle and no home for non-secret settings, and history breaks when credentials move to a new connection.
  • PaymentAccount.channelId. Rejected: tenants use one gateway account across several stores.
  • A channel-to-account routing table now. Deferred: not needed while payment ids are unique per gateway and the order-number fallback refuses ambiguous matches. Revisit if a tenant must pin stores to different accounts of the same gateway.
  • One accounting system per tenant. Rejected: groups with several GSTIN entities keep separate books.
  • One generic partner table. Rejected: loses typed relations and breaks the existing channel/courier/vendor pattern.

Consequences​

  • Tenants can hold several accounts of one gateway and several accounting systems.
  • Settlement history survives credential rotation and account removal (accounts are soft-deleted).
  • Zoho journals can be pushed nightly; Tally journals can reach an office PC through the connector agent.
  • Two more feature modules use DatabaseService, and two more one-way cross-feature pairs exist; the architecture baseline was raised for both.