Leave requests, purchase orders, expense claims, contract sign-offs, price overrides: every company has a process where one person asks and several others must say yes. On paper it is a form with four signature boxes. In software it is a workflow, and it is easy to build in a way that works on day one and hurts from day ninety.

The usual starting point is a status column on the request: pending, approved, rejected. It looks enough. It is not, because it cannot answer the questions people ask the first week: who approved it, who is it waiting on, what did the manager write, and what happens when the manager is on holiday.

This article is a way of modelling approvals so that those questions have answers.

Model the steps as data

Instead of putting the approval logic in the request, give each step of the approval its own record. A request has several approvals, one per step, created when the request is made:

CREATE TABLE leave_requests (
    id            BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    employee_id   BIGINT UNSIGNED NOT NULL,
    starts_on     DATE NOT NULL,
    ends_on       DATE NOT NULL,
    days          DECIMAL(5,1) NOT NULL,
    status        VARCHAR(20) NOT NULL,   -- draft, pending, approved, rejected, cancelled
    created_at    DATETIME NOT NULL
);

CREATE TABLE approvals (
    id            BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    request_id    BIGINT UNSIGNED NOT NULL,
    step          TINYINT UNSIGNED NOT NULL,       -- 1, 2, 3...
    role          VARCHAR(40) NOT NULL,            -- department_manager, hr, general_manager
    approver_id   BIGINT UNSIGNED NULL,            -- who actually decided
    status        VARCHAR(20) NOT NULL,            -- waiting, pending, approved, rejected, skipped
    note          TEXT NULL,
    decided_at    DATETIME NULL,
    UNIQUE (request_id, step)
);

Now a request is not a flag. It is a request plus a short list of steps, and every question has a place to look:

  • Who is it waiting on? The first approval that is not approved.
  • Who approved it, and when? approver_id and decided_at.
  • What did they say? note.
  • How far along is it? Count the steps.

Because each step is a row, a change in policy is a change in data: add a step for requests over a limit, skip a step for a manager's own request, route to a different role for one department.

Sequential or parallel, and say which

Some approvals must happen in order: the department manager first, then HR, then the general manager. Others can happen at once: finance and legal both look at a contract. Decide per workflow, and enforce it in the code, not in the interface.

A common trap is showing the approve button only to the right person at the right moment and trusting that. A button is not a rule. The service that records a decision must itself refuse a decision at step 3 while step 2 is still waiting, for anyone, including someone calling the API directly.

public function approve(Approval $approval, User $by, ?string $note): void
{
    DB::transaction(function () use ($approval, $by, $note) {
        $approval = Approval::lockForUpdate()->findOrFail($approval->id);

        if ($approval->status !== 'pending') {
            throw new NotYourTurn();   // already decided, or an earlier step is open
        }

        if (! $this->mayApprove($by, $approval)) {
            throw new NotAllowed();
        }

        $approval->update([
            'status'      => 'approved',
            'approver_id' => $by->id,
            'note'        => $note,
            'decided_at'  => now(),
        ]);

        $this->advance($approval->request);   // opens the next step, or finishes the request
    });
}

The status waiting for later steps and pending for the open one is what makes the check a single comparison.

The request has a state machine

The request's own status should change only in a few allowed ways. Write them down:

From To When
draft pending The employee submits it
pending approved The last step is approved
pending rejected Any step is rejected
pending cancelled The employee withdraws it, before the last step
approved cancelled Allowed only by a defined rule, and it reverses the effects

Anything else is a bug, and the service should say so by throwing, not by quietly doing something.

Effects belong to the last step

An approved leave request deducts days from a balance. An approved purchase order commits budget. These effects must happen exactly once, at the moment the request becomes approved, inside the same transaction that records the last approval.

Two things to get right:

  • Check before, and again after. Checking the balance when the request is created is a courtesy to the employee. The check that counts happens when it is approved, because the balance may have changed in between.
  • Reverse on the way back. If an approved request is cancelled, the days must come back. Treat it like inventory: write a reversing entry, do not edit history.

Delegation and absence

The question that comes in the first week: "the manager is on leave, who approves?" If the design has no answer, people will share passwords.

Give approvals a way to be delegated: a record that says "from this date to that date, A's approvals go to B". When a step opens, resolve its approver through that table, and store who actually decided. The history then shows "approved by B on behalf of A", which is honest and auditable.

Add an escalation rule for the opposite problem, a request that sits for days: after N days, remind the approver, and after more, move it up one level. Both are small jobs on a schedule.

Keep an audit trail

An approval system is judged when something goes wrong, and that is when you need to prove what happened.

  • Never delete an approval, even from a cancelled request.
  • Record who, what, when and from where for every decision.
  • Keep the policy that applied. If the approval chain changes next year, last year's requests should still show the chain they actually went through.

Tell the right person at the right time

An approval that nobody notices is a delay. When a step opens, notify its approver, by email or in the app. When the request is decided, notify the requester. Keep the notification as an effect of the state change, not as something each screen remembers to send.

A checklist

  • Each approval step is a record, with its role, approver, note and time.
  • The order of steps is enforced by the service, not by hidden buttons.
  • The request's status moves only along a written set of transitions.
  • Effects (a balance, a budget) happen once, in the last step's transaction, and are reversed on cancel.
  • Delegation and escalation exist before the first holiday.
  • Nothing is ever deleted, and the policy that applied is kept.
  • Notifications are sent from the state change.

A workflow you can query ("show me everything waiting on HR for more than three days") is worth far more than one you can only look at. If you are building leave, purchasing or contract approvals and want the model reviewed first, talk to us.