diff --git a/src/controllers/api/purchaseController.ts b/src/controllers/api/purchaseController.ts index 3602e201..652dd298 100644 --- a/src/controllers/api/purchaseController.ts +++ b/src/controllers/api/purchaseController.ts @@ -1,17 +1,47 @@ /* eslint-disable @typescript-eslint/no-unsafe-argument */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ -import { PurchaseItem } from "@/src/services/tradeService"; import { Request, Response } from "express"; +import Items from "warframe-items"; -const purchaseController = (_req: Request, res: Response): void => { - const body = JSON.parse(_req.body); - const response = PurchaseItem( - body.PurchaseParams.StoreItem, - body.PurchaseParams.Quantity, - body.PurchaseParams.UsePremium, - body.PurchaseParams.ExpectedPrice - ); - res.json({ InventoryChanges: response }); +import { parseString } from "@/src/helpers/general"; +import { PurchaseItem } from "@/src/services/tradeService"; + +import purchase from "@/static/fixed_responses/purchase.json"; + +const purchaseController = async (req: Request, res: Response): Promise => { + const accountId = req.query.accountId; + if (!accountId) { + res.status(400).json({ error: "accountId was not provided" }); + return; + } + console.log(accountId); + const body = JSON.parse(req.body); + const purchaseParams = body.PurchaseParams; + const storeItem = purchaseParams.StoreItem; + const quantity = purchaseParams.Quantity; + const usePremium = purchaseParams.UsePremium; + const price = purchaseParams.ExpectedPrice; + const items = new Items({ category: ["All"] }); + const item = items.find(i => { + return i.uniqueName == storeItem; + }); + if (item) { + const response = await PurchaseItem(parseString(accountId), item, quantity, usePremium, price); + res.json({ InventoryChanges: response }); + return; + } else { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call + const item_name = storeItem.replace("StoreItems/", ""); + const new_item = items.find(i => { + return i.uniqueName == item_name; + }); + if (new_item) { + const response = await PurchaseItem(parseString(accountId), new_item, quantity, usePremium, price); + res.json({ InventoryChanges: response }); + return; + } + } + res.json(purchase); }; export { purchaseController }; diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index 36f49d06..fd6a0651 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -2,6 +2,8 @@ import { Inventory } from "@/src/models/inventoryModel"; import new_inventory from "@/static/fixed_responses/postTutorialInventory.json"; import config from "@/config.json"; import { Types } from "mongoose"; +import { InventoryChanges } from "../types/commonTypes"; +import { parseString } from "../helpers/general"; const createInventory = async (accountOwnerId: Types.ObjectId) => { try { @@ -22,4 +24,81 @@ const createInventory = async (accountOwnerId: Types.ObjectId) => { } }; -export { createInventory }; +const UpdateInventory = async (accountId: string, inventoryChanges: InventoryChanges) => { + try { + const inventory = await Inventory.findOne({ accountOwnerId: accountId }); + if (inventory) { + if (inventoryChanges.Suits) { + inventoryChanges.Suits.forEach(item => { + if (item.ItemId && item.ItemType) { + inventory.Suits.push({ + ItemType: item.ItemType, + Configs: [], + ItemId: item.ItemId + }); + } + }); + } + if (inventoryChanges.LongGuns) { + inventoryChanges.LongGuns.forEach(item => { + if (item.ItemId && item.ItemType) { + inventory.LongGuns.push({ + ItemType: item.ItemType, + Configs: [], + ItemId: item.ItemId + }); + } + }); + } + if (inventoryChanges.Pistols) { + inventoryChanges.Pistols.forEach(item => { + if (item.ItemId && item.ItemType) { + inventory.Pistols.push({ + ItemType: item.ItemType, + Configs: [], + ItemId: item.ItemId + }); + } + }); + } + if (inventoryChanges.Melee) { + inventoryChanges.Melee.forEach(item => { + if (item.ItemId && item.ItemType) { + inventory.Melee.push({ + ItemType: item.ItemType, + Configs: [], + ItemId: item.ItemId + }); + } + }); + } + if (inventoryChanges.FlavourItems) { + inventoryChanges.FlavourItems.forEach(item => { + if (item.ItemType) { + inventory.FlavourItems.push({ + ItemType: item.ItemType + }); + } + }); + } + if (inventoryChanges.MiscItems) { + inventoryChanges.MiscItems.forEach(item => { + if (item.ItemType && item.ItemCount) { + inventory.MiscItems.push({ + ItemType: item.ItemType, + ItemCount: item.ItemCount + }); + } + }); + } + await inventory.updateOne(inventory); + } + } catch (error) { + if (error instanceof Error) { + throw new Error(`error update inventory" ${error.message}`); + } + throw new Error("error update inventory that is not of instance Error"); + } +}; + +export { createInventory, UpdateInventory }; diff --git a/src/services/tradeService.ts b/src/services/tradeService.ts index 33933258..6dcac90c 100644 --- a/src/services/tradeService.ts +++ b/src/services/tradeService.ts @@ -1,10 +1,18 @@ +/* eslint-disable @typescript-eslint/no-unsafe-call */ +/* eslint-disable @typescript-eslint/no-unsafe-assignment */ import { Item, Misc, Skin, Warframe, Weapon } from "warframe-items"; import { InventoryChanges, StoreItem } from "../types/commonTypes"; import { generateOid, parseString } from "../helpers/general"; +import { UpdateInventory } from "./inventoryService"; -const PurchaseItem = (item: Item, quantity: number, usePremium: boolean, price: number): InventoryChanges => { +const PurchaseItem = async ( + accountId: string, + item: Item, + quantity: number, + usePremium: boolean, + price: number +): Promise => { // {"InventoryChanges":{"WishlistChanges":["/Lotus/Types/StoreItems/SuitCustomizations/ColourPickerItem"],"FlavourItems":[{"ItemType":"/Lotus/Types/StoreItems/SuitCustomizations/ColourPickerItem"}],"PremiumCredits":-75}} - const newInventoryChanges: InventoryChanges = {}; if ((item as Warframe).productCategory == "Suits") { const newSuits: StoreItem[] = [ @@ -66,6 +74,8 @@ const PurchaseItem = (item: Item, quantity: number, usePremium: boolean, price: if (!usePremium) { newInventoryChanges.RegularCredits = -price; } + await UpdateInventory(accountId, newInventoryChanges); + // eslint-disable-next-line @typescript-eslint/no-unsafe-return return newInventoryChanges; };