Fix currency synchronization and add real-time validation
Some checks failed
Build / build (pull_request) Failing after 1m13s

This commit is contained in:
1921703654 2025-09-03 02:47:37 +08:00
parent 3fedc701f1
commit 75ecabcdaf
3 changed files with 39 additions and 10 deletions

View File

@ -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'
});
}
}
};

View File

@ -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;

View File

@ -209,7 +209,8 @@ export const handlePurchase = async (
inventory,
purchaseRequest.PurchaseParams.ExpectedPrice,
purchaseRequest.PurchaseParams.UsePremium,
prePurchaseInventoryChanges
prePurchaseInventoryChanges,
true
);
switch (purchaseRequest.PurchaseParams.Source) {