Fix waste posts that overdraw a lot split across reasons - #36
Draft
cursor[bot] wants to merge 1 commit into
Draft
Conversation
post_waste_event checked each line against full on-hand, so two lines on the same lot (spoilage + damage) could both pass and then drive quantity_on_hand negative. Sum requested qty per inventory row before posting, reject over-allocation, and guard the deduct UPDATE so a race still rolls back. Co-authored-by: ezzademir <ezzademir@users.noreply.github.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This was referenced Aug 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug and impact
post_waste_eventchecked each line against the lot’s full on-hand quantity, not the combined quantity of every line targeting that row.Trigger: Record waste → add two lines on the same lot (for example 8 spoilage + 8 damage) when only 10 are available. Each line looks valid. Submit.
Impact: The RPC posted the event and deducted both lines, driving
quantity_on_handnegative. Staff had no available-qty hint on the picker, so this is an everyday split-reason workflow, not a contrived edge case.Root cause
Validation locked the inventory row and compared
available >= this_line.qtyindependently. The deduct pass then subtracted each line with no remaining-available guard. Stock-take already rejects duplicate inventory ids; waste never summed them.Fix
20260828110000_waste_event_sum_duplicate_lines.sql: sum requested qty per hub/outlet inventory id before writes; reject withinsufficient_*_availableif the combined qty does not fit. DeductUPDATEs require remaining available ≥ line qty andRAISEon miss so a race rolls back (does notRETURN success:falseafter writes).stafffor hub; staff or assigned-outlet supervisor for outlet).Validation
npm test: 31 passed (4 new tests for summing / over-allocation).npm run typecheckandnpm run buildclean.npm run lint: pre-existing DateFilter / FinishedGoodsLotLabel warnings only.post_waste_eventbody (identifier-match rewrite + supervisor patch). Could not exercise the RPC against a live database.