purchase item save test
This commit is contained in:
		
							parent
							
								
									90d0b1ad7d
								
							
						
					
					
						commit
						a36bc69341
					
				@ -1,17 +1,47 @@
 | 
				
			|||||||
/* eslint-disable @typescript-eslint/no-unsafe-argument */
 | 
					/* eslint-disable @typescript-eslint/no-unsafe-argument */
 | 
				
			||||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
 | 
					/* eslint-disable @typescript-eslint/no-unsafe-assignment */
 | 
				
			||||||
import { PurchaseItem } from "@/src/services/tradeService";
 | 
					 | 
				
			||||||
import { Request, Response } from "express";
 | 
					import { Request, Response } from "express";
 | 
				
			||||||
 | 
					import Items from "warframe-items";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const purchaseController = (_req: Request, res: Response): void => {
 | 
					import { parseString } from "@/src/helpers/general";
 | 
				
			||||||
    const body = JSON.parse(_req.body);
 | 
					import { PurchaseItem } from "@/src/services/tradeService";
 | 
				
			||||||
    const response = PurchaseItem(
 | 
					
 | 
				
			||||||
        body.PurchaseParams.StoreItem,
 | 
					import purchase from "@/static/fixed_responses/purchase.json";
 | 
				
			||||||
        body.PurchaseParams.Quantity,
 | 
					
 | 
				
			||||||
        body.PurchaseParams.UsePremium,
 | 
					const purchaseController = async (req: Request, res: Response): Promise<void> => {
 | 
				
			||||||
        body.PurchaseParams.ExpectedPrice
 | 
					    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 });
 | 
					        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 };
 | 
					export { purchaseController };
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,8 @@ import { Inventory } from "@/src/models/inventoryModel";
 | 
				
			|||||||
import new_inventory from "@/static/fixed_responses/postTutorialInventory.json";
 | 
					import new_inventory from "@/static/fixed_responses/postTutorialInventory.json";
 | 
				
			||||||
import config from "@/config.json";
 | 
					import config from "@/config.json";
 | 
				
			||||||
import { Types } from "mongoose";
 | 
					import { Types } from "mongoose";
 | 
				
			||||||
 | 
					import { InventoryChanges } from "../types/commonTypes";
 | 
				
			||||||
 | 
					import { parseString } from "../helpers/general";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const createInventory = async (accountOwnerId: Types.ObjectId) => {
 | 
					const createInventory = async (accountOwnerId: Types.ObjectId) => {
 | 
				
			||||||
    try {
 | 
					    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 };
 | 
				
			||||||
 | 
				
			|||||||
@ -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 { Item, Misc, Skin, Warframe, Weapon } from "warframe-items";
 | 
				
			||||||
import { InventoryChanges, StoreItem } from "../types/commonTypes";
 | 
					import { InventoryChanges, StoreItem } from "../types/commonTypes";
 | 
				
			||||||
import { generateOid, parseString } from "../helpers/general";
 | 
					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> => {
 | 
				
			||||||
    // {"InventoryChanges":{"WishlistChanges":["/Lotus/Types/StoreItems/SuitCustomizations/ColourPickerItem"],"FlavourItems":[{"ItemType":"/Lotus/Types/StoreItems/SuitCustomizations/ColourPickerItem"}],"PremiumCredits":-75}}
 | 
					    // {"InventoryChanges":{"WishlistChanges":["/Lotus/Types/StoreItems/SuitCustomizations/ColourPickerItem"],"FlavourItems":[{"ItemType":"/Lotus/Types/StoreItems/SuitCustomizations/ColourPickerItem"}],"PremiumCredits":-75}}
 | 
				
			||||||
 | 
					 | 
				
			||||||
    const newInventoryChanges: InventoryChanges = {};
 | 
					    const newInventoryChanges: InventoryChanges = {};
 | 
				
			||||||
    if ((item as Warframe).productCategory == "Suits") {
 | 
					    if ((item as Warframe).productCategory == "Suits") {
 | 
				
			||||||
        const newSuits: StoreItem[] = [
 | 
					        const newSuits: StoreItem[] = [
 | 
				
			||||||
@ -66,6 +74,8 @@ const PurchaseItem = (item: Item, quantity: number, usePremium: boolean, price:
 | 
				
			|||||||
    if (!usePremium) {
 | 
					    if (!usePremium) {
 | 
				
			||||||
        newInventoryChanges.RegularCredits = -price;
 | 
					        newInventoryChanges.RegularCredits = -price;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    await UpdateInventory(accountId, newInventoryChanges);
 | 
				
			||||||
 | 
					    // eslint-disable-next-line @typescript-eslint/no-unsafe-return
 | 
				
			||||||
    return newInventoryChanges;
 | 
					    return newInventoryChanges;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user