chore: fix garbage typings for inventory response

This commit is contained in:
Sainan 2025-01-03 05:34:40 +01:00
parent de02dadfc6
commit 819dcae91d
3 changed files with 12 additions and 19 deletions

View File

@ -1,11 +1,10 @@
import { RequestHandler } from "express"; import { RequestHandler } from "express";
import { getAccountForRequest } from "@/src/services/loginService"; import { getAccountForRequest } from "@/src/services/loginService";
import { toInventoryResponse } from "@/src/helpers/inventoryHelpers"; import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
import { Inventory, TInventoryDatabaseDocument } from "@/src/models/inventoryModels/inventoryModel";
import { config } from "@/src/services/configService"; import { config } from "@/src/services/configService";
import allDialogue from "@/static/fixed_responses/allDialogue.json"; import allDialogue from "@/static/fixed_responses/allDialogue.json";
import { ILoadoutDatabase } from "@/src/types/saveLoadoutTypes"; 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 { IPolarity, ArtifactPolarity } from "@/src/types/inventoryTypes/commonInventoryTypes";
import { import {
ExportCustoms, ExportCustoms,
@ -26,9 +25,7 @@ export const inventoryController: RequestHandler = async (request, response) =>
return; return;
} }
const inventory = await Inventory.findOne({ accountOwnerId: account._id.toString() }) const inventory = await Inventory.findOne({ accountOwnerId: account._id.toString() });
.populate<{ LoadOutPresets: ILoadoutDatabase }>("LoadOutPresets")
.populate<{ Ships: IShipInventory }>("Ships");
if (!inventory) { if (!inventory) {
response.status(400).json({ error: "inventory was undefined" }); response.status(400).json({ error: "inventory was undefined" });
@ -63,14 +60,17 @@ export const inventoryController: RequestHandler = async (request, response) =>
inventory.InfestedFoundry.AbilityOverrideUnlockCooldown && inventory.InfestedFoundry.AbilityOverrideUnlockCooldown &&
new Date() >= inventory.InfestedFoundry.AbilityOverrideUnlockCooldown new Date() >= inventory.InfestedFoundry.AbilityOverrideUnlockCooldown
) { ) {
handleSubsumeCompletion(inventory as unknown as TInventoryDatabaseDocument); handleSubsumeCompletion(inventory);
await inventory.save(); await inventory.save();
} }
//TODO: make a function that converts from database representation to client const inventoryWithLoadOutPresets = await inventory.populate<{ LoadOutPresets: ILoadoutDatabase }>(
const inventoryJSON = inventory.toJSON(); "LoadOutPresets"
);
const inventoryResponse = toInventoryResponse(inventoryJSON); const inventoryWithLoadOutPresetsAndShips = await inventoryWithLoadOutPresets.populate<{ Ships: IShipInventory }>(
"Ships"
);
const inventoryResponse = inventoryWithLoadOutPresetsAndShips.toJSON<IInventoryResponse>();
if (config.infiniteCredits) { if (config.infiniteCredits) {
inventoryResponse.RegularCredits = 999999999; inventoryResponse.RegularCredits = 999999999;

View File

@ -1,14 +1,6 @@
import { IMongoDate, IOid } from "@/src/types/commonTypes"; import { IMongoDate, IOid } from "@/src/types/commonTypes";
import { IInventoryResponse } from "@/src/types/inventoryTypes/inventoryTypes";
import { Types } from "mongoose"; 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 => { export const toOid = (objectId: Types.ObjectId): IOid => {
return { $oid: objectId.toString() } satisfies IOid; return { $oid: objectId.toString() } satisfies IOid;
}; };

View File

@ -1061,6 +1061,7 @@ inventorySchema.set("toJSON", {
transform(_document, returnedObject) { transform(_document, returnedObject) {
delete returnedObject._id; delete returnedObject._id;
delete returnedObject.__v; delete returnedObject.__v;
delete returnedObject.accountOwnerId;
const inventoryDatabase = returnedObject as IInventoryDatabase; const inventoryDatabase = returnedObject as IInventoryDatabase;
const inventoryResponse = returnedObject as IInventoryResponse; const inventoryResponse = returnedObject as IInventoryResponse;