From 819dcae91db1a9c69892f74ed28c069bca698a12 Mon Sep 17 00:00:00 2001 From: Sainan Date: Fri, 3 Jan 2025 05:34:40 +0100 Subject: [PATCH] chore: fix garbage typings for inventory response --- src/controllers/api/inventoryController.ts | 22 ++++++++++---------- src/helpers/inventoryHelpers.ts | 8 ------- src/models/inventoryModels/inventoryModel.ts | 1 + 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/controllers/api/inventoryController.ts b/src/controllers/api/inventoryController.ts index 03c1b757..868dca5c 100644 --- a/src/controllers/api/inventoryController.ts +++ b/src/controllers/api/inventoryController.ts @@ -1,11 +1,10 @@ import { RequestHandler } from "express"; import { getAccountForRequest } from "@/src/services/loginService"; -import { toInventoryResponse } from "@/src/helpers/inventoryHelpers"; -import { Inventory, TInventoryDatabaseDocument } from "@/src/models/inventoryModels/inventoryModel"; +import { Inventory } from "@/src/models/inventoryModels/inventoryModel"; import { config } from "@/src/services/configService"; import allDialogue from "@/static/fixed_responses/allDialogue.json"; import { ILoadoutDatabase } from "@/src/types/saveLoadoutTypes"; -import { IShipInventory, equipmentKeys } from "@/src/types/inventoryTypes/inventoryTypes"; +import { IInventoryResponse, IShipInventory, equipmentKeys } from "@/src/types/inventoryTypes/inventoryTypes"; import { IPolarity, ArtifactPolarity } from "@/src/types/inventoryTypes/commonInventoryTypes"; import { ExportCustoms, @@ -26,9 +25,7 @@ export const inventoryController: RequestHandler = async (request, response) => return; } - const inventory = await Inventory.findOne({ accountOwnerId: account._id.toString() }) - .populate<{ LoadOutPresets: ILoadoutDatabase }>("LoadOutPresets") - .populate<{ Ships: IShipInventory }>("Ships"); + const inventory = await Inventory.findOne({ accountOwnerId: account._id.toString() }); if (!inventory) { response.status(400).json({ error: "inventory was undefined" }); @@ -63,14 +60,17 @@ export const inventoryController: RequestHandler = async (request, response) => inventory.InfestedFoundry.AbilityOverrideUnlockCooldown && new Date() >= inventory.InfestedFoundry.AbilityOverrideUnlockCooldown ) { - handleSubsumeCompletion(inventory as unknown as TInventoryDatabaseDocument); + handleSubsumeCompletion(inventory); await inventory.save(); } - //TODO: make a function that converts from database representation to client - const inventoryJSON = inventory.toJSON(); - - const inventoryResponse = toInventoryResponse(inventoryJSON); + const inventoryWithLoadOutPresets = await inventory.populate<{ LoadOutPresets: ILoadoutDatabase }>( + "LoadOutPresets" + ); + const inventoryWithLoadOutPresetsAndShips = await inventoryWithLoadOutPresets.populate<{ Ships: IShipInventory }>( + "Ships" + ); + const inventoryResponse = inventoryWithLoadOutPresetsAndShips.toJSON(); if (config.infiniteCredits) { inventoryResponse.RegularCredits = 999999999; diff --git a/src/helpers/inventoryHelpers.ts b/src/helpers/inventoryHelpers.ts index d85fc978..d7fa7b25 100644 --- a/src/helpers/inventoryHelpers.ts +++ b/src/helpers/inventoryHelpers.ts @@ -1,14 +1,6 @@ import { IMongoDate, IOid } from "@/src/types/commonTypes"; -import { IInventoryResponse } from "@/src/types/inventoryTypes/inventoryTypes"; import { Types } from "mongoose"; -//TODO: this needs to be addressed: a schema's toJSON is responsible for changing Oid and Date to their corresponding Response versions __id to "ItemId":{"$oid":"6450f720bc562ebf030222d4"}, and a Date to "date":{"$date":{"$numberLong":"unix timestamp"}) -export const toInventoryResponse = (inventoryDatabase: { accountOwnerId: Types.ObjectId }): IInventoryResponse => { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const { accountOwnerId, ...inventoryResponse } = inventoryDatabase; - return inventoryResponse as unknown as IInventoryResponse; -}; - export const toOid = (objectId: Types.ObjectId): IOid => { return { $oid: objectId.toString() } satisfies IOid; }; diff --git a/src/models/inventoryModels/inventoryModel.ts b/src/models/inventoryModels/inventoryModel.ts index cc04b849..ec39bb1d 100644 --- a/src/models/inventoryModels/inventoryModel.ts +++ b/src/models/inventoryModels/inventoryModel.ts @@ -1061,6 +1061,7 @@ inventorySchema.set("toJSON", { transform(_document, returnedObject) { delete returnedObject._id; delete returnedObject.__v; + delete returnedObject.accountOwnerId; const inventoryDatabase = returnedObject as IInventoryDatabase; const inventoryResponse = returnedObject as IInventoryResponse;