From 75ecabcdaf60eaa9a7619febe3810be8e76bf5ea Mon Sep 17 00:00:00 2001 From: 1921703654 <1921703654@qq.com> Date: Wed, 3 Sep 2025 02:47:37 +0800 Subject: [PATCH] Fix currency synchronization and add real-time validation --- src/controllers/api/purchaseController.ts | 32 +++++++++++++++++------ src/services/inventoryService.ts | 14 +++++++++- src/services/purchaseService.ts | 3 ++- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/controllers/api/purchaseController.ts b/src/controllers/api/purchaseController.ts index cd3e4443..f560f98f 100644 --- a/src/controllers/api/purchaseController.ts +++ b/src/controllers/api/purchaseController.ts @@ -6,12 +6,28 @@ import { getInventory } from "../../services/inventoryService.ts"; import { sendWsBroadcastTo } from "../../services/wsService.ts"; export const purchaseController: RequestHandler = async (req, res) => { - const purchaseRequest = JSON.parse(String(req.body)) as IPurchaseRequest; - const accountId = await getAccountIdForRequest(req); - const inventory = await getInventory(accountId); - const response = await handlePurchase(purchaseRequest, inventory); - await inventory.save(); - //console.log(JSON.stringify(response, null, 2)); - res.json(response); - sendWsBroadcastTo(accountId, { update_inventory: true }); + try { + const purchaseRequest = JSON.parse(String(req.body)) as IPurchaseRequest; + const accountId = await getAccountIdForRequest(req); + const inventory = await getInventory(accountId); + + const response = await handlePurchase(purchaseRequest, inventory); + await inventory.save(); + + res.json(response); + sendWsBroadcastTo(accountId, { update_inventory: true }); + } catch (error) { + if (error instanceof Error && error.message.includes('Insufficient')) { + res.status(400).json({ + error: 'INSUFFICIENT_CURRENCY', + message: error.message + }); + } else { + console.error('Purchase failed:', error); + res.status(500).json({ + error: 'PURCHASE_FAILED', + message: 'An error occurred during purchase' + }); + } + } }; diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index ddd855ca..8a6e343c 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -1254,10 +1254,18 @@ export const updateCurrency = ( inventory: TInventoryDatabaseDocument, price: number, usePremium: boolean, - inventoryChanges: IInventoryChanges = {} + inventoryChanges: IInventoryChanges = {}, + validateBalance: boolean = false ): IInventoryChanges => { if (price != 0 && isCurrencyTracked(inventory, usePremium)) { if (usePremium) { + if (validateBalance) { + const totalPlatinum = inventory.PremiumCredits + inventory.PremiumCreditsFree; + if (totalPlatinum < price) { + throw new Error(`Insufficient platinum balance. Required: ${price}, Available: ${totalPlatinum}`); + } + } + if (inventory.PremiumCreditsFree > 0) { const premiumCreditsFreeDelta = Math.min(price, inventory.PremiumCreditsFree) * -1; inventoryChanges.PremiumCreditsFree ??= 0; @@ -1269,6 +1277,10 @@ export const updateCurrency = ( inventory.PremiumCredits -= price; logger.debug(`currency changes `, { PremiumCredits: -price }); } else { + if (validateBalance && inventory.RegularCredits < price) { + throw new Error(`Insufficient credits balance. Required: ${price}, Available: ${inventory.RegularCredits}`); + } + inventoryChanges.RegularCredits ??= 0; inventoryChanges.RegularCredits -= price; inventory.RegularCredits -= price; diff --git a/src/services/purchaseService.ts b/src/services/purchaseService.ts index 4d7a0cf8..6bceae66 100644 --- a/src/services/purchaseService.ts +++ b/src/services/purchaseService.ts @@ -209,7 +209,8 @@ export const handlePurchase = async ( inventory, purchaseRequest.PurchaseParams.ExpectedPrice, purchaseRequest.PurchaseParams.UsePremium, - prePurchaseInventoryChanges + prePurchaseInventoryChanges, + true ); switch (purchaseRequest.PurchaseParams.Source) { -- 2.47.2