Rah CRM
The system where I run my software business: clients, proposals, contracts, hours, invoices, servers and the AI agents that work with me, all in one place.
- Role
- Product, architecture and development
- When
- September 2026 to date
- Team
- Solo, with AI agents on the code
- Status
- Live and in daily use
- 31 phases shipped to production, each with a PRD and a journey
- 103 migrations applied in production, on real customer data
- 1,053 API tests, running against a real Postgres
- 391 end-to-end browser tests, with accessibility checks
Working with several clients at once, the business was scattered: contacts on WhatsApp, proposals in a document, hours in Kimai, tasks in Planka, invoices in a spreadsheet and passwords somewhere else. Checking whether a client was up to date meant opening five tabs.
Off-the-shelf CRMs cover sales and stop there. None of them connects the commercial side with technical delivery: hours that become invoices, contracts that become projects, servers that need attention and the AI agents that now write a good part of the code.
The challengeBuild, alone and quickly, a system that holds client data, contracts and money, with the security of a paid product and without turning into a pile of screens nobody uses.
The Choices That Held Up the Growth
- 01
Docs Before Code
Every phase starts with a PRD, a journey and a prompt for each screen. A screen only becomes code after its image is approved, and the result is compared against it. That is what let 31 phases ship without the system becoming a patchwork.
- 02
Rules on the Server, UI in Next
The FastAPI backend owns every business rule. Next.js 16 only draws the interface and talks to the API through a client generated from OpenAPI. If the contract changes and the client does not, the pipeline blocks the deploy.
- 03
Paid-product Security
Database-backed sessions with a protected cookie, CSRF and mandatory two-factor authentication, following OWASP ASVS level 2. Sensitive actions, such as creating an API token, ask for the password and code again.
- 04
A Queue Inside Postgres
Reminders, Kimai sync, alerts and AI routines run on a queue that lives in Postgres itself (Procrastinate). One less piece to maintain, no Redis, and no lost jobs when the server restarts.
- 05
AI That Helps, Not Decides
AI drafts proposals, builds contracts from the clause library and summarizes the day. Prices never come from it, and whatever it suggests stays flagged until someone confirms. External agents reach the CRM through MCP, with a token and per-tool permissions.
- 06
A Workspace on Every Row
Every table has had a workspace since day one. Only I use it today, but the path to a multi-customer product is already in the database, not in a future refactor.
- 07
WhatsApp on a Third-party Engine, with Our Own Screens
Conversations run on Chatwoot behind the scenes, through its API only: it connects the numbers (official and unofficial) and stores the messages, while every screen belongs to the CRM. An adapter of our own isolates the vendor, the webhook arrives signed with HMAC and a repeated message is never duplicated. There are chatbot flows with handoff to a human. It is live, waiting for the first number to be connected.
- 08
Proposals and Contracts That Build Themselves
Proposals are made of blocks, with a preview of the real PDF, versions, a public link and acceptance with optional items. An accepted proposal becomes a contract with clauses from the library and a signing link. AI drafts the contract from a plain request and revises the whole document block by block, showing the diff so each change can be accepted one at a time.
- 09
An AI Hub Built for Continuous Improvement
Every agent has a purpose and a health record (context, quota, failures). Every skill has a score for adherence and problem rate, and the system opens signals by itself: unused skill, expensive index, missed opportunity, skill that underperforms. Each fix goes to an improvement board with before and after measured.
- 10
Several AI Providers Behind a Single Door
Anthropic, OpenAI, Gemini, Groq and OpenRouter come in through the same contract. Each feature on screen picks its connection and model, respects the daily quota and the monthly cap, falls back to a backup when the main one fails and records the cost. On OpenRouter, the request already carries free backup models.
The Business in Your Pocket
The CRM is responsive end to end. On the phone you can see what is overdue, mark an invoice as paid, move a deal through the pipeline and keep an eye on the AI between meetings.
-
01 Home. The same dashboard on the phone, in cards that fit the screen.
-
02 Today. Today brings together what is overdue and what is due now, from invoices to tasks.
-
03 Pipeline. The whole pipeline on the phone too, with totals at the top.
-
04 Invoice. An overdue invoice: send a reminder on WhatsApp or record the payment right there.
-
05 Receivables. What comes in, when, and who is late, at a glance.
-
06 AI hub. AI agent usage, tracked from anywhere.
From First Contact to Paid Invoice
On the desktop the CRM covers the whole cycle: sales pipeline, proposal, weekly hours, cash forecast and the hub that tracks the server AI agents.
* Screenshots from a local environment running the production version with fictional data. No company, person or amount shown here is real.
Under the Hood
A system with contracts, client passwords and money cannot rely on hidden buttons. The protections live in the API, and each one is tested.
Stack
- Python 3.14
- FastAPI
- SQLAlchemy 2
- PostgreSQL
- Procrastinate
- Next.js 16
- React 19
- TypeScript
- Tailwind CSS 4
- TanStack Query
- MinIO
- MCP
- Docker
- Azure DevOps
Safeguards
- Mandatory two-factor authentication, with code reuse blocked and throttling per account and per IP
- Password and code asked again to create an API token or change the AI hub
- Links with a password or key pasted in the wrong field are rejected before reaching the database
- Attachments go straight to MinIO through signed URLs, with the file type checked by its first bytes
- Integration credentials encrypted with AES-GCM and external addresses checked against SSRF
- Insert-only audit trail, enforced by a trigger in the database itself
- CSV export protected against formula injection, and gitleaks blocking secrets at commit time
Tests and Quality
- pytest
- Testcontainers
- Hypothesis
- mypy
- Vitest
- Testing Library
- MSW
- Playwright
- axe
- API tested with pytest against a real Postgres in a container, mypy in strict mode and property tests with Hypothesis
- UI tested with Vitest, Testing Library and MSW, with request types generated from the API contract
- Full journeys in Playwright with accessibility checks by axe, which already caught real contrast issues that got fixed
- Azure DevOps pipeline that checks the contract, runs only the side that changed and applies the migration before swapping the API
What You Would Find in the Repository
The code is private, so here is what is inside it: the practices, where they live and a number to check. Feel free to ask about any of them.
Architecture
-
Feature folders
api: 40 modules · web: 39 featuresEach business topic is one folder with route, service, schema and model together, in the API and in the UI. Changing contracts does not mean opening ten folders.
-
Rules live in the API
docs/01-arquitetura/adr/ · 7 ADRsNext.js only renders: no Server Actions, no API routes, no database access. The decision is recorded in an ADR, with the reasoning.
-
Ports and adapters where vendors change
agents/llm/port.py · 5 adaptersAI and WhatsApp sit behind interfaces of our own. Swapping Chatwoot for another engine, or adding an AI provider, touches no screen and no business rule.
-
Monorepo with the contract in the middle
apps/api · apps/web · packages/api-client · e2eAPI, UI, generated client and E2E in one repository, so a contract change moves on both sides together.
Types and Validation, in Two Layers
-
Static types on both sides
mypy strict · tsc strictPython with mypy in strict mode and TypeScript with strict on. A type error does not reach the commit.
-
Runtime validation at the edge
schemas.py in every moduleEvery API input goes through a Pydantic schema and comes back with the exact field that failed. The type says what should arrive; validation checks what did.
-
Frontend typed by the contract
export_openapi.py --checkRequest types are generated from the API OpenAPI. If the contract changes and the client does not, pre-commit and the pipeline block it.
-
AI output is validated too
contracts/ai.py · proposals/revise.pyWhatever the AI returns goes through the same block validator: known types and variables only, limited size. Text from the user is reference material, not an instruction.
Tests
-
TDD where there is math
docs/01-arquitetura/testes.md §7Hour value, balance, due dates and authentication were born with the test before the code: red, green, refactor.
-
API tested against real Postgres
1,053 tests · pytest -n 3Tests start Postgres in a container, no fake database, and run in parallel in the pipeline. Property tests with Hypothesis catch the case nobody thought of.
-
Component tests for the UI
893 cases in 134 filesVitest, Testing Library and MSW mocking the API, next to each screen.
-
End-to-end journeys
391 tests in 48 specsPlaywright walks the full flows in the browser and axe checks accessibility on every screen. It has already caught real contrast and focus bugs.
-
Migrations and AI are tested as well
test_migrations.py · agents/evals/Each migration goes up, is compared with the models, goes down and up again. Agents are evaluated with a different model acting as judge, with weighted criteria.
Database
-
Versioned migrations
103 migrationsThe schema only changes through Alembic migrations, and the pipeline applies them before swapping the API version.
-
Ready for many customers
ADR 0005 · workspace_idEvery table has the workspace since the first migration. Becoming a multi-customer product does not mean redoing the database.
-
Concurrency handled
FOR NO KEY UPDATE · 21 filesRow locks where two actions can collide, with the right kind of lock so it does not fight the audit foreign key. A repeated webhook hits a unique key and is not duplicated.
-
Fast search with no extra service
p95 from 1.75 s to 122 msFuzzy search with pg_trgm inside Postgres, tuned until it fit the agreed time with 10,000 companies.
-
Rules in the database too
CHECK in 79 migrations · partial index in 24Check constraints and partial indexes keep the rule even if the code gets it wrong, and the audit trail only accepts inserts, enforced by a trigger.
Delivery and Operations
-
A gate before every commit
.pre-commit-config.yamlRuff with security rules, mypy, ESLint, tsc, the affected tests, the API contract and gitleaks run on every commit.
-
A pipeline that tests only what changed
azure-pipelines.ymlIn Azure DevOps, a UI-only change does not run the API suite, and the migration runs before the deploy. API, worker and server agent ship separately.
-
Lean containers, no root
USER app · multi-stageMulti-stage images running as their own user, with memory and CPU caps on the server.
-
Logs you can query
structlog · 43 filesStructured JSON logs with the request context, instead of loose prints.
AI in the Development Process
-
Documents before code
docs/03-fases · 37 foldersEvery phase has a PRD, a journey with a checklist and the prompt for each screen. AI writes code from a reviewed document, not from a loose request.
-
Several agents, each with its role
BRIEFING.md per phaseOne agent plans, reviews and writes the backend and the end-to-end tests; another implements screens from a briefing; a third reads the screen designs. Every diff is reviewed before the commit.
-
Skills as practice guides
ai/scores.pyAgents load practice guides per task: security, tests, database, LLM evaluation. The CRM itself measures whether each guide is being used and whether it helps.
-
A second opinion on sensitive parts
docs/01-arquitetura/seguranca.mdThe authentication design and code were reviewed by a different model, with a different bias: 13 findings in the design and 8 in the code, all fixed before going live.
Where It Stands
The CRM is live and it is where I work every day: real clients, proposals, contracts, hour closings and invoices all go through it.
Next up are the lead funnel and campaigns (phases already written) and, further on, opening the system to other people with their own workspace.
Have a Similar Project in Mind?
I like problems with real people on the other side. Tell me what you need.