Repository navigation
Conversation
- 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.
24cd091 to
d953a99
Compare
anis022
left a comment
There was a problem hiding this comment.
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 => |
There was a problem hiding this comment.
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); | ||
| } |
There was a problem hiding this comment.
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?
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 --> [*]Decisions taken (to confirm with Nafissa):
Core
ReceiptStatus,ReceiptActionandReceiptStateMachine: the list of allowed transitions, each with its permission (fromPermissions), whether it requires a reason, and whether the submitter is excluded.Database (migration
20261123052442_AddReceiptStatus, script0008_add_receipt_status.sql)receipts.status(text,Draftby default; existing receipts becomeDraft).receipt_status_changes: one row per transition withfrom_status,to_status,changed_by_user_id,changed_atandreason.API
/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.ReceiptStatusServicechecks 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.Docs
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, androles-permissions.mdupdated.Testing
dotnet test: 583/583 passing (Docker required for the integration tests).Checklist
Notes
developis merged into this branch. Since Feature/29 business central import #88 took the0007script, the receipt status script is0008.dotnet ef database update --project src/MoissonMontreal.Api.AuthenticatedApiFactory.CreateTokennow takes an optionalobjectId, so a test can act as several employees.