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) andAccountingSystem(one Zoho organisation or Tally company). IntegrationConnectionstays a credential vault and health signal.IntegrationRegistrystays the catalogue (categoriespaymentandaccounting).- Data produced through an account references the record by id: gateway
Payouts,GatewaySettlementMappingandOrderPaymentTransactioncarrypaymentAccountId;ErpAccountMappingandErpSyncLogcarryaccountingSystemId. (Gateway payouts moved onto the shared payout spine in ADR-0005.) - Status follows ADR-0002:
INACTIVEonly by a person,SUSPENDEDonly 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 routeChannel → Seller → LegalEntity → AccountingSystem, falling back to the system without an entity. - Dependencies run one way:
finance/settlementdepends onfinance/payment-accountandfinance/accounting-system, which own their ports. Integrations implement the ports.
Alternatives
- Keep standalone connections and add
connectionIdto 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.