backend · architecture · ecowell · database · testing · security
Connecting Multiple Business Teams Through One Backend
Building a dashboard per team is the easy part. The hard part is that the same job passes through five teams: Sales books it, Accounts verifies the advance, Inventory dispatches the parts, Installation or Service does the work, and the customer expects one coherent story. This case study is about the design choices that made that hand-off reliable, not about the number of screens.

The shape of the problem
- One customer, many teams. A sale becomes a payment, an installation, a stock request, later a service ticket.
- Each team should see only its slice. A sales executive sees tickets they raised; an installation executive sees jobs assigned to them; a branch admin sees one branch; a super admin sees every branch.
- Teams depend on each other. Installation cannot promise a date if Inventory has no stock; Sales wants to know the moment Accounts verifies an advance.
- Nobody should be able to approve their own work. But the owner should not be blocked by anyone.
We solved this with one backend, one permission model, and a small number of patterns applied consistently.
Decision 1: permissions are role plus scope, enforced in two places
A person has a role (for example Inventory Manager, Inventory Executive, Branch Admin, Super Admin), a branch, and for HR-style capabilities a scope ceiling (self, branch, organisation).
- The API layer checks the role for each route.
- The database functions re-check the actor (user, employee and role must match and be active), the branch, and the organisation. Even a super admin is bound to their own organisation.
Two layers on purpose: the API check gives fast, friendly errors; the database check means a future script or a mistake in a route cannot widen access. We tested this adversarially: an executive from another branch as an assignee is refused; a manager approving their own item is refused; a branch admin asking for another branch gets “not found”, not “forbidden”, so existence is not leaked.
A small but important detail: we derive human-readable role labels from role and department (the “Installation Admin” and “Service Admin” are the same underlying role in different departments), at the backend, so every portal shows the same names without each front end guessing.
Decision 2: hand-offs are events, and notifications are rows
Every hand-off writes a notification row addressed to a specific person. The in-app bell is just a query over those rows with server-side read state. The set of recipients for an event is a rule, not a broadcast:
| Event | Who is told |
|---|---|
| Ticket status, assignee or schedule changes | The person who raised it |
| New message on a ticket | The other side of the conversation |
| Inventory marks stock unavailable (with expected date) | The job’s executive, the Installation or Service admin, Branch Admins, Super Admins (“Out of stock”) |
| Stock arrives earlier than promised | Waiting jobs in FIFO order, only while available stock still covers their need |
| Accounts verifies or rejects a payment | The sales executive, Branch Admin, Accounts Admin and every Super Admin, but not the person who verified |
Three rules made this livable:
- One notification per person per event. Recipients are merged before inserting, so someone who is both the executive and a branch admin gets one.
- Never notify the actor. People stop reading a bell that tells them about their own clicks.
- Each occurrence is its own notification. A request that is marked unavailable, restocked, then marked unavailable again must not collide with the first. De-duplication keys include the occurrence, not just the entity.
Triggers or services?
We put most hand-off notifications in database triggers and functions rather than in the API layer. Triggers catch every path that changes the data: the API, a repair script, a background job. Services only catch the paths someone remembered to wire. The price is that a trigger does not know who triggered the change unless you give it that information. We hit exactly this: a sales executive editing their own ticket notified themselves. The fix was to carry the actor on the rows that trigger notifications, and we documented the one case we could not fix yet (a reschedule by the raiser).
Decision 3: approvals are maker-checker, with a supreme role
Approvals exist wherever a mistake is expensive:
- New inventory items are inactive and have no barcode until approved. The barcode is generated at approval, so nothing can be received or scanned against an unapproved item.
- Price changes are proposals by a branch admin, approved by the super admin. Approving closes the old price row and opens a new one, so every historical booking keeps the price it was sold at.
- Access revocation disables an executive’s account and releases their open tasks, and every change is audited.
The rules we settled on:
- The maker can never be the checker for the same item.
- A manager may approve items proposed by their own executives, in their own branch; branch admins and super admins approve more broadly.
- The super admin is supreme: their own changes apply immediately and are audit-logged, and they can decide every queue. We audited this deliberately instead of assuming it.
- Approvals are idempotent: approving twice returns the same result; a decision on something that has since changed is marked superseded rather than failing mysteriously.
Decision 4: an honest timeline instead of a status field
“What happened on this ticket?” is the question every team asks. We assemble one chronological timeline per customer from ticket messages, status history, assignments, scheduling events, parts requests (including the expected-back date), and payments, each entry naming who and their role. Scope follows the same permission model: a sales executive sees only tickets they raised, internal notes are hidden from Sales, and tickets are always bound to the customer’s own branch.
Decision 5: privacy by omission
One late request sharpened a principle. Staff wanted a barcode scan to show “who has this”. We removed customer details from the scan result entirely, at the API, not just the screen. A batch label says what stock it is, where it came from (vendor, invoice) and where it went (when, how many, by whom, for which kind of job). Who the customer is lives in the customer and ticket records, which have their own permissions. If a screen does not need personal data, the API should not return it.
What went wrong along the way
- Branch live leaked. The shared “what is happening in the branch” feed originally showed every executive the whole branch, including sale amounts and payments. Found while testing as an executive. Fixed: executives see only events about their own jobs.
- Notification bells ignored the real inbox. Three portals showed other feeds, so well-built notifications never appeared. Lesson: test the last mile (does a human see it) with a real login in the recipient’s role.
- Counts included hidden test customers until the aggregates excluded soft-deleted rows.
- A role missing from one allow-list meant a newly created executive could sign in but got “Access denied” because one shared endpoint did not know the role. Adding a role touches more lists than you expect; we now grep for the role in every gate.
Principles we would keep
- Model the hand-off, not the screen. Decide who must know, and when, before drawing UI.
- Enforce twice: friendly check in the API, hard check in the database.
- Notifications are addressed rows, one per person per occurrence, never to the actor.
- Maker-checker everywhere money or stock identity is at stake, with an explicit supreme role.
- Keep history by closing rows, not editing them.
- Return less data. Privacy is easiest to guarantee at the API.
- Test as each role, on the real system. Most of our worst bugs were only visible from a specific person’s seat.