Skip to content

Conversation

@CapoMK25
Copy link
Collaborator

Brief description

This PR introduces MongoDB transactions to the ClanShop module to ensure data integrity during item purchases and the subsequent voting process. By using sessions, we guarantee that coin deductions, voting creation, and stock updates either all succeed together or fail without leaving partial data.

Change list

  • clanShop.service.ts Wrapped buyItem in a withTransaction block.
  • Integrated reserveFunds and startVoting within the same session.
  • Added explicit rollback handling via cancelTransaction on any service error.
  • clanShopVoting.processor.ts: Refactored the voting resolution logic to use transactions.
  • Ensures that if a vote is rejected, the gameCoins refund and the voting status update happen atomically.

Testing and verification

The transaction logic was verified using a "Manual Rollback Bomb" test:

Setup: Created a test clan with 5000 coins and a linked player via mongosh.

Execution: Injected a temporary throw Error immediately after the reserveFunds call in buyItem.

Result:

Postman request returned a 500 Internal Server Error as expected.

Database Verification: gameCoins remained at 5000 despite the deduction logic having been triggered, confirming the session successfully rolled back the changes.

Final Pass: Removed the forced error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

2 participants