|
| 1 | +# DealSentry Architecture |
| 2 | + |
| 3 | +DealSentry is an AI-assisted proposal compliance and risk review system. This |
| 4 | +document explains how the pieces fit together and, more importantly, *why* — |
| 5 | +including the trade-offs that were made deliberately. |
| 6 | + |
| 7 | +## System overview |
| 8 | + |
| 9 | +```mermaid |
| 10 | +flowchart LR |
| 11 | + subgraph Browser |
| 12 | + SPA["React 18 SPA<br/>(Vite, Tailwind, shadcn/ui,<br/>TanStack Query)"] |
| 13 | + end |
| 14 | +
|
| 15 | + subgraph "Express API (Node 20, tsx)" |
| 16 | + API["REST routers<br/>/api/proposals /rules /analyze<br/>/files /integrations /oauth ..."] |
| 17 | + MW["Middleware<br/>helmet · CORS allowlist ·<br/>rate limits · session resolver"] |
| 18 | + PDF["PDF export<br/>(Puppeteer / headless Chrome)"] |
| 19 | + end |
| 20 | +
|
| 21 | + subgraph Supabase |
| 22 | + PG[("PostgreSQL<br/>+ pgvector")] |
| 23 | + REST["PostgREST API"] |
| 24 | + STORE["Storage<br/>(proposal-files bucket)"] |
| 25 | + end |
| 26 | +
|
| 27 | + subgraph Azure["Azure OpenAI"] |
| 28 | + GPT["gpt-4o<br/>(analysis + generation)"] |
| 29 | + EMB["text-embedding-ada-002<br/>(semantic search)"] |
| 30 | + end |
| 31 | +
|
| 32 | + CRM["Salesforce · HubSpot · Gmail<br/>(OAuth 2.0, demo mode available)"] |
| 33 | +
|
| 34 | + SPA -- "/api/* (same-origin,<br/>Vite proxy in dev)" --> MW --> API |
| 35 | + API -- "supabase-js (REST)" --> REST --> PG |
| 36 | + API --> STORE |
| 37 | + API --> GPT |
| 38 | + API --> EMB |
| 39 | + API <--> CRM |
| 40 | + PDF --> API |
| 41 | +``` |
| 42 | + |
| 43 | +## Key design decisions |
| 44 | + |
| 45 | +### 1. Two database access paths, on purpose |
| 46 | + |
| 47 | +- **Runtime**: the Express API talks to Supabase over **PostgREST** |
| 48 | + (`supabase-js`). No connection pool to manage, works on serverless-ish free |
| 49 | + tiers, and survives the API host and database being in different regions. |
| 50 | +- **Schema & seeds**: **Prisma** is used *only* for `db push`, `db execute` |
| 51 | + (the manual SQL in `prisma/manual/`) and `npm run seed`, over the Supabase |
| 52 | + **session-mode pooler** (port 5432 — Prisma migrations cannot run over the |
| 53 | + transaction pooler on 6543). |
| 54 | + |
| 55 | +The trade-off: no compile-time query types at runtime (PostgREST returns |
| 56 | +untyped JSON). That's the price of running well on a free tier; the routers |
| 57 | +type their own row interfaces at the boundary instead. |
| 58 | + |
| 59 | +### 2. Demo-first authentication |
| 60 | + |
| 61 | +There is deliberately **no login screen**: every request is served as a default |
| 62 | +user (`DEMO_USER_EMAIL`, falling back to the first ADMIN). The full JWT + |
| 63 | +bcrypt stack still exists (`/api/auth/login`, `/register`, |
| 64 | +`/change-password`) and valid bearer tokens win over the default identity, so |
| 65 | +real multi-user auth can be re-enabled by rendering the login page again. |
| 66 | + |
| 67 | +### 3. AI with guardrails, not AI instead of rules |
| 68 | + |
| 69 | +`/api/analyze` sends the proposal *content* (source of truth) plus the active |
| 70 | +compliance rules to gpt-4o and asks for structured findings. Hard limits |
| 71 | +(max 25% discount, 90-day payment terms, mandatory legal clauses, $10k minimum |
| 72 | +deal) are enforced twice: stated in the prompt *and* re-checked/capped in |
| 73 | +code after generation (`/api/proposals/generate` clamps the discount range). |
| 74 | +Results are normalized (`normalizeAnalysis`) before they become a RiskReport. |
| 75 | + |
| 76 | +### 4. Semantic search via pgvector |
| 77 | + |
| 78 | +Every proposal gets a 1536-dim embedding on create (best-effort — failures |
| 79 | +never block the write). Search calls the `match_proposals` SQL function |
| 80 | +(cosine distance, ivfflat index). If the extension/function is missing the API |
| 81 | +answers `available: false` and the client falls back to substring search — |
| 82 | +the feature degrades, it doesn't break. |
| 83 | + |
| 84 | +Setup lives in `prisma/manual/semantic_search.sql`; backfill with |
| 85 | +`npm run backfill:embeddings`. |
| 86 | + |
| 87 | +### 5. Integrations with a real demo mode |
| 88 | + |
| 89 | +Salesforce/HubSpot/Gmail connect over standard OAuth 2.0. With |
| 90 | +`*_DEMO_MODE=true`, connect stores `{demo: true}` credentials and **sync |
| 91 | +imports canned deals as proposals**, so the connect → sync → analyze → approve |
| 92 | +story is demonstrable without any external accounts. Real-mode sync handles |
| 93 | +token refresh and dedupes on the CRM record id. |
| 94 | + |
| 95 | +### 6. Deployment (Render, Docker) |
| 96 | + |
| 97 | +One container serves both the API and the built SPA (Express serves `dist/` in |
| 98 | +production, so CORS is a non-issue for same-origin traffic). The runtime image |
| 99 | +installs system Chromium for PDF export (`PUPPETEER_EXECUTABLE_PATH`), because |
| 100 | +the free tier's 512 MB can't afford Puppeteer's bundled download at build time. |
| 101 | +`render.yaml` is a full Blueprint — secrets are dashboard-only (`sync: false`). |
| 102 | + |
| 103 | +## Security model (current state and roadmap) |
| 104 | + |
| 105 | +| Area | Today | Next step | |
| 106 | +| --- | --- | --- | |
| 107 | +| API auth | Default-user demo mode; JWT honored when present | Re-enable login UI for multi-user | |
| 108 | +| DB access | Server-side key via supabase-js; anon key works, service-role preferred when set | Enable RLS per table; drop anon write policies | |
| 109 | +| Secrets | `.env` git-ignored; Render dashboard for prod | Rotate any key that was ever committed | |
| 110 | +| Responses | Password hashes stripped from every user payload; mutations admin-gated | Field-level DTOs | |
| 111 | + |
| 112 | +## Repository map |
| 113 | + |
| 114 | +``` |
| 115 | +server.ts Express bootstrap, static SPA serving, error handling |
| 116 | +src/api/ One router per resource + middleware/ + lib/ |
| 117 | +src/lib/ API client (frontend), supabase client (backend), utils |
| 118 | +src/pages/ components/ React SPA |
| 119 | +prisma/ schema.prisma, seed.ts, manual/*.sql (pgvector, storage) |
| 120 | +scripts/ backfill-embeddings, cleanup-integrations, migrate-auth |
| 121 | +tests/ Vitest + Supertest API tests |
| 122 | +``` |
0 commit comments