3.4 KiB
3.4 KiB
Backend: Architecture (Recommendations)
Last Updated: 2025-01-17
Phase: Phase 0 (Planning)
Status: Draft — finalize in Phase 1
Owner: Backend Architect
References:
/docs/project-overview.md/docs/backend/api-design.md/docs/backend/security.md/docs/backend/payment-flow.md
Recommendations for Phase 0. Lock decisions in Phase 1.
1. Approach & Stack
- Style: modular monolith with clear modules; containerized.
- Language/Runtime: Node.js (LTS) + TypeScript.
- Framework: Express or Fastify with modular structure and DI where helpful.
- DB: Postgres (managed: Supabase/RDS). Vector:
pgvectorfor embeddings. - Queue: BullMQ (Redis) for ingestion, categorization, notifications.
- Auth: Clerk/Auth.js (or equivalent) with tenant-aware RBAC.
- Payments: provider-hosted subscriptions; no raw card data stored.
- LLM: OpenAI API (or equivalent) via single helper (e.g.,
callLLM()), configurable model/params.
2. Modules (logical)
auth— tenants, users, roles, sessions.integrations— external connectors, OAuth2, webhooks, connection health.records— normalized record store, statuses,reasoning_traceJSONB.rules— rule definitions, evaluation order, testing, hit stats.processing— pipeline: rule engine → embeddings similarity → LLM fallback; writesPROCESSED, updates records.approvals— queues for human review, overrides, optional rule creation; logsTX_APPROVED/RULE_CREATEDwithsource_agent.reports— dashboards/exports, history.billing— provider checkout/portal, webhooks, plan enforcement per tenant.events— audit/event log (EventLog), read-only/api/eventsfor downstream agents.files/receipts— attachment storage metadata (Receiptwith file URL/mime).
3. API Layers
- HTTP API (REST) with versioning (
/api/v1). - Service layer for business logic and transactions.
- Repositories for data access; use migrations for schema evolution.
4. Infrastructure & Ops
- Environments: dev/stage/prod; Docker images; CI/CD.
- Observability: structured logging, metrics, tracing; dead-letter queues for failed jobs.
- Secrets management per environment; rotate webhook/LLM/payment provider secrets.
5. Data & Schema Notes
- Records: store raw payload + normalized fields +
reasoning_traceJSONB (model, rationale, confidence, source). - EventLog: include
source_agent(defaultbalance) and payload for auditability; ensure filters by tenant/time/type. - Embeddings: table keyed by transaction/merchant strings to support similarity search; index with
pgvector. - Multi-tenant: all core tables carry
tenantIdand enforce scoped queries;Userrole per tenant.
6. Payment & Messaging (High-Level)
- Payment provider: initiate sessions via backend; handle webhooks idempotently; map provider status to internal billing/subscription states; update tenant access.
- Notifications: optional email/webhook callbacks to surface ingestion/categorization failures; keep out of PII exposure.
7. Queues (BullMQ)
records:ingest— normalize webhook payloads, writeRecord, emitINGESTED.records:process— rule engine → embeddings similarity → LLM fallback; emitPROCESSEDwithreasoning_trace.reports:generate— build P&L/exports, emitREPORT_GENERATED.- Dead-letter queues per stream; retries with backoff; idempotent handlers keyed by external event IDs.