Skip to content

Feature/31 receipt status state machine - #90

Open
Lightsul wants to merge 4 commits into
developfrom
feature/31-receipt-status-state-machine
Open

Lightsul wants to merge 4 commits into
developfrom
feature/31-receipt-status-state-machine

Conversation

@Lightsul

Copy link
Copy Markdown
Collaborator

Closes #31

Overview

Adds a status to receipts and a state machine that only allows the agreed status changes. Every change is recorded with the employee and the date.

stateDiagram-v2
    [*] --> Draft
    Draft --> PendingSupplyApproval: Submit
    PendingSupplyApproval --> PendingFinanceApproval: ApproveLevel1
    PendingSupplyApproval --> Draft: RefuseLevel1 (reason required)
    PendingFinanceApproval --> Approved: ApproveLevel2
    PendingFinanceApproval --> Draft: RefuseLevel2 (reason required)
    Approved --> Issued: Issue
    Draft --> Cancelled: Cancel (reason required)
    PendingSupplyApproval --> Cancelled: Cancel (reason required)
    PendingFinanceApproval --> Cancelled: Cancel (reason required)
    Approved --> Cancelled: Cancel (reason required)
    Issued --> Cancelled: Cancel (reason required)
    Cancelled --> [*]
Loading

Decisions taken (to confirm with Nafissa):

  • "Pending approval" is split into two statuses, one per approval level, so the status alone says who the receipt is waiting for.
  • A refusal sends the receipt back to Draft so Supply can fix and resubmit it.
  • A receipt can be cancelled at any step, not only once issued.
  • The employee who submitted a receipt cannot approve it, at either level (PRD rule).
  • A reason is required to refuse or cancel.

Core

  • ReceiptStatus, ReceiptAction and ReceiptStateMachine: the list of allowed transitions, each with its permission (from Permissions), whether it requires a reason, and whether the submitter is excluded.

Database (migration 20261123052442_AddReceiptStatus, script 0008_add_receipt_status.sql)

  • receipts.status (text, Draft by default; existing receipts become Draft).
  • receipt_status_changes: one row per transition with from_status, to_status, changed_by_user_id, changed_at and reason.
  • Checks for valid statuses, a reason when refusing or cancelling, and a real change of status. Receipts and users that appear in the history cannot be deleted.

API

  • One endpoint per action under /api/receipts/{id}/: submit, approve-level-1, refuse-level-1, approve-level-2, refuse-level-2, issue, cancel. Each one requires the permission of its transition.
  • ReceiptStatusService checks the transition, the reason and the submitter rule, then updates the status and adds the history row in one transaction. The status is only updated if it is still the one that was read, so two simultaneous approvals cannot both succeed.
  • Responses: 200 with the new status and the next allowed actions, 409 with a clear message and the allowed actions for an invalid or concurrent change, 400 for a missing reason, 403 for self-approval or a wrong role, 404 for an unknown receipt.
  • Statuses and actions are serialized as text in JSON.

Docs

  • New page docs/database/receipt-statuses.md: diagram, statuses, allowed changes, rules and API answers.
  • receipts.md, schema.md, receipts.dbml, schema.dbml, the receipts and schema diagrams, and roles-permissions.md updated.

Testing

dotnet test: 583/583 passing (Docker required for the integration tests).

Checklist

  • Code is neat, readable, and works
  • Code is commented where appropriate and well-documented
  • Commit messages follow our guidelines
  • Issue number is linked
  • Branch is linked
  • Reviewers are assigned (one of your tech leads)

Notes

  • develop is merged into this branch. Since Feature/29 business central import #88 took the 0007 script, the receipt status script is 0008.
  • After pulling, run dotnet ef database update --project src/MoissonMontreal.Api.
  • AuthenticatedApiFactory.CreateToken now takes an optional objectId, so a test can act as several employees.
  • Left for their own issues: receipt numbering on issue ([Issue]: Receipt numbering #32), replacement receipts and the approval queue.

- Add ReceiptStatus, ReceiptAction and ReceiptStateMachine in Core
- Add the status column on receipts and the receipt_status_changes table, with the AddReceiptStatus migration and its SQL script
- Add one endpoint per action; check the state machine, the reason and that the submitter does not approve, then save the status and its history in one transaction
- Return 409 with the allowed actions for an invalid or concurrent change, 400 for a missing reason, 403 for self-approval, 404 for an unknown receipt
- Document the statuses in docs/database/receipt-statuses.md, with a test that keeps the diagram in sync with the code
Renumber the receipt status SQL script to 0008, since 0007 is now the catalog quality class migration from #88.
@Lightsul Lightsul added this to the Sprint - 2 milestone Nov 23, 2026
@Lightsul Lightsul self-assigned this Nov 23, 2026
@Lightsul
Lightsul force-pushed the feature/31-receipt-status-state-machine branch from 24cd091 to d953a99 Compare November 23, 2026 06:27

@anis022 anis022 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on the state machine. I left 2 comments to fix before merging, one about a race when changing the status and one about cancelled receipts blocking their food items.

new(ReceiptStatus.PendingFinanceApproval, ReceiptAction.ApproveLevel2, ReceiptStatus.Approved, Permissions.ApproveReceiptsLevel2, ForbiddenToSubmitter: true),
new(ReceiptStatus.PendingFinanceApproval, ReceiptAction.RefuseLevel2, ReceiptStatus.Draft, Permissions.ApproveReceiptsLevel2, RequiresReason: true),
new(ReceiptStatus.Approved, ReceiptAction.Issue, ReceiptStatus.Issued, Permissions.ManageReceipts),
.. CancellableStatuses.Select(status =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cancel keeps the receipt_items rows, so with the unique index on food_item_id these items can never go on a new receipt. The cancelled receipt can't be deleted either, because of the FK from receipt_status_changes. So if an issued receipt is cancelled for a wrong quantity, the donation can never get a correct receipt. Should Cancel remove the lines, or should the one-receipt-per-item rule skip cancelled receipts?

if (updated == 0)
{
throw new ReceiptStatusChangedException(receiptId);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The status and the submitter are read before the transaction, and the UPDATE only checks status = transition.From. If the receipt is refused and resubmitted in between, the status is the same again, so the check passes and an admin can approve a receipt they submitted themselves. Can we lock the row with SELECT ... FOR UPDATE inside the transaction and read the status and submitter from there?

@anis022
anis022 removed the request for review from lauriechammah November 24, 2026 04:22
@anis022 anis022 mentioned this pull request Nov 24, 2026
5 of 6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants