chore: do addItem on inventory document, not accountId #699

Merged
Sainan merged 11 commits from additem-inplace into main 2025-01-03 15:25:09 -08:00
3 changed files with 16 additions and 16 deletions
Showing only changes of commit ef4364760a - Show all commits

View File

@ -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:");

View File

@ -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<void> => {
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 => {

View File

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