Nirbhor platform architecture
A five layer Node.js API, twenty six tables, scheduled jobs that touch money, and enforcement that lives in the middleware chain.
Nirbhor manages shared bachelor flats: meals, expenses, wallets, tasks, complaints and a monthly settlement. I built the application, the API and the database.
This is the structure, and the parts of it I would defend in a review.
Five layers, every module
routes/ HTTP method, path, middleware chain. Nothing else.
validators/ express-validator rule arrays, one export per route
controllers/ read the request, call the service, send the response
services/ all business logic, throws AppError
repositories/ all Prisma queries, nothing else
Eleven modules follow this without exception: auth, flat, member, meal, wallet and payment, expense, task, complaint, notification, audit log, monthly summary.
The rule that gives it its value is the boring one. A controller contains no business logic and a service contains no Prisma call. The moment either leaks, the layers become decoration and you are reading five files to follow one request for no benefit.
Enforcement lives in the middleware chain
Four guards, composed per route:
authMiddlewareverifies the bearer token and attaches the user.requireFlatMemberconfirms an active membership of the flat in the path and attaches the member, which is where role and permissions come from.requireRolechecks that membership role.checkSettlementLockreturns423 Lockedwhen a member has a settlement in progress and the request would move their numbers.
Putting these in the chain rather than inside services is deliberate. A rule enforced per service is a rule the next endpoint forgets, and in a system that moves money between friends, forgetting is expensive.
One response shape, everywhere
{ "status": "success" | "error", "message": "...", "data": { } }Success and failure use the same envelope. HTTP codes come only from AppError or from
the success helper, so there is exactly one place that decides what a status code means.
Controllers are wrapped in an async handler that forwards rejections to a single global error handler. No try/catch scattered through controllers, and no unhandled rejection taking the process down.
Anything touching money is one transaction
Approving a household expense debits every active member's wallet, debits the flat
wallet, writes a transaction row per member and writes an audit entry. That is one
prisma.$transaction and the transaction client is passed down into the repository
calls.
The audit helper takes the transaction client as an argument on purpose. An audit log written outside the transaction can survive a rollback of the thing it describes, which is worse than not logging, because it lies with authority.
The audit log has no client facing write endpoint. It is appended by the system, read by clients.
Scheduled jobs
Three, and each one had to be designed for being run twice.
- Daily meal population. Creates each member's meal logs from their weekly preferences, so the default is correct and members only interact when they deviate.
- Month start fixed deduction. Fixed contributions come out at the start of the month, never at close. Deducting at both would double charge; deducting only at close would leave wallet balances meaningless for thirty days.
- Month close on the last day. Locks the meal pool, computes cost per point, deducts, writes a permanent summary row per member and resets contributions.
Month close is idempotent by design: it skips months already closed. A scheduled job that touches money and is not safe to re-run is an outage waiting for a deploy at the wrong time of the month.
There is also a preview endpoint that computes the entire close and writes nothing. Nobody should have to run an irreversible job to find out what it will do.
Notifications
A single internal helper writes the notification row and sends the FCM push. It is best effort and never throws, so a push failure cannot roll back the state change that caused it. Device tokens are registered on login and removed on logout.
Trade-offs
Five layers on a solo project. More files per feature. Justified because the domain is genuinely complicated and the alternative is business rules living in route handlers.
A stored wallet balance alongside a transaction history. Correct while every write is transactional, but a derived balance could not drift at all. If I rebuilt the wallet today it would be an append only ledger with the balance computed.
Automatic month close. Convenient, and it means an irreversible job runs without a human. Mitigated by idempotency, the preview endpoint and a full audit trail, but it is the decision I keep the closest eye on.