Skip to content

Fix critical sale status and backup restore regressions#59

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-investigation-6ea2
Draft

Fix critical sale status and backup restore regressions#59
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-investigation-6ea2

Conversation

@cursor

@cursor cursor Bot commented May 30, 2026

Copy link
Copy Markdown

Bug and impact

  • Completed/cancelled sales could still be modified through legacy item endpoints, causing duplicate inventory changes or unpaid additions to completed sales.
  • Draft sale completion updated member stats before balance validation and did not lock member rows, allowing corrupted member spend/points on insufficient balance.
  • Backup restore used loaddata without clearing existing rows, producing a mixed database state instead of the selected snapshot.
  • Sale creation rechecked stock outside the transaction only, leaving a race window for overselling.

Root cause

  • The new sale status field was not enforced across all legacy sale item/cancel/complete paths.
  • The draft completion path did not use the same atomic balance handling as the one-step sale creation path.
  • Django fixture loading upserts rows and does not remove rows absent from the fixture.
  • Inventory rows were not locked and revalidated at the point of deduction.

Fix

  • Restrict sale item create/delete and cancel/complete actions to valid sale statuses and make cancel idempotency-safe with row locks.
  • Make draft sale completion atomic, lock sale/member rows, validate balances before stats writes, and record balance transactions.
  • Add a cancel confirmation template and pass payment method choices explicitly to the complete template.
  • Flush the database before loading a backup snapshot and skip restore log creation if the restoring user no longer exists after restore.
  • Lock and recheck inventory rows inside the sale creation transaction before decrementing stock.

Validation

  • Passed: python3 manage.py test inventory.tests.test_sale_status inventory.tests.test_sales_balance_payment inventory.tests.test_views.BackupViewSecurityTest inventory.tests.test_services.MemberServiceTest
  • Broader related run: python3 manage.py test inventory.tests.test_sale_status inventory.tests.test_sales_balance_payment inventory.tests.test_services inventory.tests.test_models inventory.tests.test_integration still shows two pre-existing integration failures around legacy no-product sale creation and missing inventory-check permission; unrelated targeted coverage passed.
Open in Web View Automation 

Co-authored-by: zhtyyx <zhtyyx@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant