diff --git a/src/controllers/api/claimCompletedRecipeController.ts b/src/controllers/api/claimCompletedRecipeController.ts index 237a5b87..f03895b7 100644 --- a/src/controllers/api/claimCompletedRecipeController.ts +++ b/src/controllers/api/claimCompletedRecipeController.ts @@ -83,11 +83,12 @@ export const claimCompletedRecipeController: RequestHandler = async (req, res) = ...(await updateCurrencyByAccountId(recipe.skipBuildTimePrice, true, accountId)) }; } - res.json({ - InventoryChanges: { - ...InventoryChanges, - ...(await addItem(accountId, recipe.resultType, recipe.num)).InventoryChanges - } - }); + const inventory = await getInventory(accountId); + InventoryChanges = { + ...InventoryChanges, + ...(await addItem(inventory, recipe.resultType, recipe.num)).InventoryChanges + }; + await inventory.save(); + res.json({ InventoryChanges }); } }; diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index f1c8e55c..18a4eac7 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -105,13 +105,12 @@ export const getInventory = async (accountOwnerId: string): Promise => { // Strict typing if (typeName in ExportRecipes) { - const inventory = await getInventory(accountId); const recipeChanges = [ { ItemType: typeName, @@ -119,7 +118,6 @@ export const addItem = async ( } satisfies ITypeCount ]; addRecipes(inventory, recipeChanges); - await inventory.save(); return { InventoryChanges: { Recipes: recipeChanges @@ -127,11 +125,9 @@ export const addItem = async ( }; } if (typeName in ExportResources) { - const inventory = await getInventory(accountId); if (ExportResources[typeName].productCategory == "Ships") { - const oid = await createShip(new Types.ObjectId(accountId), typeName); + const oid = await createShip(inventory.accountOwnerId, typeName); inventory.Ships.push(oid); - await inventory.save(); return { InventoryChanges: { Ships: [ @@ -143,9 +139,7 @@ export const addItem = async ( } }; } else if (ExportResources[typeName].productCategory == "CrewShips") { - const inventory = await getInventory(accountId); const inventoryChanges = addCrewShip(inventory, typeName); - await inventory.save(); return { InventoryChanges: inventoryChanges }; } else { const miscItemChanges = [ @@ -155,7 +149,6 @@ export const addItem = async ( } satisfies IMiscItem ]; addMiscItems(inventory, miscItemChanges); - await inventory.save(); return { InventoryChanges: { MiscItems: miscItemChanges @@ -164,19 +157,14 @@ export const addItem = async ( } } if (typeName in ExportCustoms) { - const inventory = await getInventory(accountId); const inventoryChanges = addSkin(inventory, typeName); - await inventory.save(); return { InventoryChanges: inventoryChanges }; } if (typeName in ExportFlavour) { - const inventory = await getInventory(accountId); const inventoryChanges = addCustomization(inventory, typeName); - await inventory.save(); return { InventoryChanges: inventoryChanges }; } if (typeName in ExportUpgrades || typeName in ExportArcanes) { - const inventory = await getInventory(accountId); const changes = [ { ItemType: typeName, @@ -184,7 +172,6 @@ export const addItem = async ( } ]; addMods(inventory, changes); - await inventory.save(); return { InventoryChanges: { RawUpgrades: changes @@ -192,7 +179,6 @@ export const addItem = async ( }; } if (typeName in ExportGear) { - const inventory = await getInventory(accountId); const consumablesChanges = [ { ItemType: typeName, @@ -200,7 +186,6 @@ export const addItem = async ( } satisfies IConsumable ]; addConsumables(inventory, consumablesChanges); - await inventory.save(); return { InventoryChanges: { Consumables: consumablesChanges @@ -213,10 +198,8 @@ export const addItem = async ( case "Powersuits": switch (typeName.substr(1).split("/")[2]) { default: { - const inventory = await getInventory(accountId); const inventoryChanges = addPowerSuit(inventory, typeName); updateSlots(inventory, InventorySlot.SUITS, 0, 1); - await inventory.save(); return { InventoryChanges: { ...inventoryChanges, @@ -229,10 +212,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(); return { InventoryChanges: { ...inventoryChanges, @@ -245,10 +226,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(); return { InventoryChanges: { ...inventoryChanges, @@ -263,11 +242,9 @@ export const addItem = async ( } break; case "Weapons": { - const inventory = await getInventory(accountId); const weaponType = getWeaponType(typeName); const inventoryChanges = addEquipment(inventory, weaponType, typeName); updateSlots(inventory, InventorySlot.WEAPONS, 0, 1); - await inventory.save(); return { InventoryChanges: { ...inventoryChanges, @@ -277,7 +254,6 @@ export const addItem = async ( } case "Objects": { // /Lotus/Objects/Tenno/Props/TnoLisetTextProjector (Note Beacon) - const inventory = await getInventory(accountId); const changes = [ { ItemType: typeName, @@ -285,7 +261,6 @@ export const addItem = async ( } satisfies IMiscItem ]; addShipDecorations(inventory, changes); - await inventory.save(); return { InventoryChanges: { ShipDecorations: changes @@ -295,10 +270,8 @@ export const addItem = async ( case "Types": switch (typeName.substr(1).split("/")[2]) { case "Sentinels": { - const inventory = await getInventory(accountId); const inventoryChanges = addSentinel(inventory, typeName); updateSlots(inventory, InventorySlot.SENTINELS, 0, 1); - await inventory.save(); return { InventoryChanges: { ...inventoryChanges, @@ -309,7 +282,6 @@ export const addItem = async ( case "Items": { switch (typeName.substr(1).split("/")[3]) { case "ShipDecos": { - const inventory = await getInventory(accountId); const changes = [ { ItemType: typeName, @@ -317,7 +289,6 @@ export const addItem = async ( } satisfies IMiscItem ]; addShipDecorations(inventory, changes); - await inventory.save(); return { InventoryChanges: { ShipDecorations: changes @@ -325,7 +296,6 @@ export const addItem = async ( }; } default: { - const inventory = await getInventory(accountId); const miscItemChanges = [ { ItemType: typeName, @@ -333,7 +303,6 @@ export const addItem = async ( } satisfies IMiscItem ]; addMiscItems(inventory, miscItemChanges); - await inventory.save(); return { InventoryChanges: { MiscItems: miscItemChanges @@ -345,7 +314,6 @@ export const addItem = async ( case "Game": if (typeName.substr(1).split("/")[3] == "Projections") { // Void Relics, e.g. /Lotus/Types/Game/Projections/T2VoidProjectionGaussPrimeDBronze - const inventory = await getInventory(accountId); const miscItemChanges = [ { ItemType: typeName, @@ -353,7 +321,6 @@ export const addItem = async ( } satisfies IMiscItem ]; addMiscItems(inventory, miscItemChanges); - await inventory.save(); return { InventoryChanges: { MiscItems: miscItemChanges diff --git a/src/services/purchaseService.ts b/src/services/purchaseService.ts index 346dc5df..cd43b5ba 100644 --- a/src/services/purchaseService.ts +++ b/src/services/purchaseService.ts @@ -204,9 +204,12 @@ export const handleStoreItemAcquisition = async ( } } switch (storeCategory) { - default: - purchaseResponse = await addItem(accountId, internalName, quantity); + default: { + const inventory = await getInventory(accountId); + purchaseResponse = await addItem(inventory, internalName, quantity); + await inventory.save(); break; + } case "Types": purchaseResponse = await handleTypesPurchase(internalName, accountId, quantity); break; @@ -279,6 +282,7 @@ const handleBoosterPackPurchase = async ( BoosterPackItems: "", InventoryChanges: {} }; + const inventory = await getInventory(accountId); for (let i = 0; i != quantity; ++i) { for (const weights of pack.rarityWeightsPerRoll) { const result = getRandomWeightedReward(pack.components, weights); @@ -288,11 +292,12 @@ const handleBoosterPackPurchase = async ( result.type.split("/Lotus/").join("/Lotus/StoreItems/") + ',{"lvl":0};'; combineInventoryChanges( purchaseResponse.InventoryChanges, - (await addItem(accountId, result.type, result.itemCount)).InventoryChanges + (await addItem(inventory, result.type, result.itemCount)).InventoryChanges ); } } } + await inventory.save(); return purchaseResponse; }; @@ -305,8 +310,12 @@ const handleTypesPurchase = async ( const typeCategory = getStoreItemTypesCategory(typesName); logger.debug(`type category ${typeCategory}`); switch (typeCategory) { - default: - return await addItem(accountId, typesName, quantity); + default: { + const inventory = await getInventory(accountId); + const resp = await addItem(inventory, typesName, quantity); + await inventory.save(); + return resp; + } case "BoosterPacks": return await handleBoosterPackPurchase(typesName, accountId, quantity); case "SlotItems":