diff --git a/src/controllers/api/inventorySlotsController.ts b/src/controllers/api/inventorySlotsController.ts index 0c95c89d..cb49ddb4 100644 --- a/src/controllers/api/inventorySlotsController.ts +++ b/src/controllers/api/inventorySlotsController.ts @@ -1,5 +1,5 @@ import { getAccountIdForRequest } from "@/src/services/loginService"; -import { updateCurrencyByAccountId } from "@/src/services/inventoryService"; +import { getInventory, updateCurrency } from "@/src/services/inventoryService"; import { RequestHandler } from "express"; import { updateSlots } from "@/src/services/inventoryService"; import { InventorySlot } from "@/src/types/inventoryTypes/inventoryTypes"; @@ -26,8 +26,10 @@ export const inventorySlotsController: RequestHandler = async (req, res) => { //TODO: check which slot was purchased because pvpBonus is also possible - const currencyChanges = await updateCurrencyByAccountId(20, true, accountId); - await updateSlots(accountId, InventorySlot.PVE_LOADOUTS, 1, 1); + const inventory = await getInventory(accountId); + const currencyChanges = updateCurrency(inventory, 20, true); + updateSlots(inventory, InventorySlot.PVE_LOADOUTS, 1, 1); + await inventory.save(); //console.log({ InventoryChanges: currencyChanges }, " added loadout changes:"); diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index d3320a58..f1c8e55c 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -215,8 +215,8 @@ export const addItem = async ( default: { const inventory = await getInventory(accountId); const inventoryChanges = addPowerSuit(inventory, typeName); + updateSlots(inventory, InventorySlot.SUITS, 0, 1); await inventory.save(); - await updateSlots(accountId, InventorySlot.SUITS, 0, 1); return { InventoryChanges: { ...inventoryChanges, @@ -231,8 +231,8 @@ export const addItem = async ( case "Archwing": { const inventory = await getInventory(accountId); const inventoryChanges = addSpaceSuit(inventory, typeName); + updateSlots(inventory, InventorySlot.SPACESUITS, 0, 1); await inventory.save(); - await updateSlots(accountId, InventorySlot.SPACESUITS, 0, 1); return { InventoryChanges: { ...inventoryChanges, @@ -247,8 +247,8 @@ export const addItem = async ( case "EntratiMech": { const inventory = await getInventory(accountId); const inventoryChanges = addMechSuit(inventory, typeName); + updateSlots(inventory, InventorySlot.MECHSUITS, 0, 1); await inventory.save(); - await updateSlots(accountId, InventorySlot.MECHSUITS, 0, 1); return { InventoryChanges: { ...inventoryChanges, @@ -266,8 +266,8 @@ export const addItem = async ( const inventory = await getInventory(accountId); const weaponType = getWeaponType(typeName); const inventoryChanges = addEquipment(inventory, weaponType, typeName); + updateSlots(inventory, InventorySlot.WEAPONS, 0, 1); await inventory.save(); - await updateSlots(accountId, InventorySlot.WEAPONS, 0, 1); return { InventoryChanges: { ...inventoryChanges, @@ -297,8 +297,8 @@ export const addItem = async ( case "Sentinels": { const inventory = await getInventory(accountId); const inventoryChanges = addSentinel(inventory, typeName); + updateSlots(inventory, InventorySlot.SENTINELS, 0, 1); await inventory.save(); - await updateSlots(accountId, InventorySlot.SENTINELS, 0, 1); return { InventoryChanges: { ...inventoryChanges, @@ -485,22 +485,18 @@ export const addSpaceSuit = ( return inventoryChanges; }; -export const updateSlots = async ( - accountId: string, +export const updateSlots = ( + inventory: TInventoryDatabaseDocument, slotName: SlotNames, slotAmount: number, extraAmount: number -): Promise => { - const inventory = await getInventory(accountId); - +): void => { inventory[slotName].Slots += slotAmount; if (inventory[slotName].Extra === undefined) { inventory[slotName].Extra = extraAmount; } else { inventory[slotName].Extra += extraAmount; } - - await inventory.save(); }; const isCurrencyTracked = (usePremium: boolean): boolean => { diff --git a/src/services/purchaseService.ts b/src/services/purchaseService.ts index 8496934d..346dc5df 100644 --- a/src/services/purchaseService.ts +++ b/src/services/purchaseService.ts @@ -248,7 +248,9 @@ const handleSlotPurchase = async ( const slotName = slotPurchaseNameToSlotName[slotPurchaseName].name; const slotsPerPurchase = slotPurchaseNameToSlotName[slotPurchaseName].slotsPerPurchase; - await updateSlots(accountId, slotName, slotsPerPurchase, slotsPerPurchase); + const inventory = await getInventory(accountId); + updateSlots(inventory, slotName, slotsPerPurchase, slotsPerPurchase); + await inventory.save(); logger.debug(`added ${slotsPerPurchase} slot ${slotName}`);