forked from OpenWF/SpaceNinjaServer
chore: cleanup inventory types (#691)
This commit is contained in:
parent
69734ea101
commit
e6432b5052
@ -3,15 +3,10 @@ import { getAccountIdForRequest } from "@/src/services/loginService";
|
||||
import { getJSONfromString } from "@/src/helpers/stringHelpers";
|
||||
import { getInventory, addMiscItems, updateCurrency, addRecipes } from "@/src/services/inventoryService";
|
||||
import { IOid } from "@/src/types/commonTypes";
|
||||
import {
|
||||
IConsumedSuit,
|
||||
IInfestedFoundry,
|
||||
IInventoryDatabaseDocument,
|
||||
IMiscItem,
|
||||
ITypeCount
|
||||
} from "@/src/types/inventoryTypes/inventoryTypes";
|
||||
import { IConsumedSuit, IInfestedFoundry, IMiscItem, ITypeCount } from "@/src/types/inventoryTypes/inventoryTypes";
|
||||
import { ExportMisc, ExportRecipes } from "warframe-public-export-plus";
|
||||
import { getRecipe } from "@/src/services/itemDataService";
|
||||
import { TInventoryDatabaseDocument } from "@/src/models/inventoryModels/inventoryModel";
|
||||
|
||||
export const infestedFoundryController: RequestHandler = async (req, res) => {
|
||||
const accountId = await getAccountIdForRequest(req);
|
||||
@ -239,7 +234,7 @@ interface IHelminthSubsumeRequest {
|
||||
Recipe: string;
|
||||
}
|
||||
|
||||
export const handleSubsumeCompletion = (inventory: IInventoryDatabaseDocument): ITypeCount[] => {
|
||||
export const handleSubsumeCompletion = (inventory: TInventoryDatabaseDocument): ITypeCount[] => {
|
||||
const [recipeType] = Object.entries(ExportRecipes).find(
|
||||
([_recipeType, recipe]) =>
|
||||
recipe.secretIngredientAction == "SIA_WARFRAME_ABILITY" &&
|
||||
|
@ -1,11 +1,10 @@
|
||||
import { RequestHandler } from "express";
|
||||
import { getAccountForRequest } from "@/src/services/loginService";
|
||||
import { toInventoryResponse } from "@/src/helpers/inventoryHelpers";
|
||||
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 { IInventoryDatabaseDocument, 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 IInventoryDatabaseDocument);
|
||||
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<IInventoryResponse>();
|
||||
|
||||
if (config.infiniteCredits) {
|
||||
inventoryResponse.RegularCredits = 999999999;
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Model, Schema, Types, model } from "mongoose";
|
||||
import { Document, Model, Schema, Types, model } from "mongoose";
|
||||
import {
|
||||
IFlavourItem,
|
||||
IRawUpgrade,
|
||||
@ -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;
|
||||
@ -1114,3 +1115,15 @@ type InventoryDocumentProps = {
|
||||
type InventoryModelType = Model<IInventoryDatabase, {}, InventoryDocumentProps>;
|
||||
|
||||
export const Inventory = model<IInventoryDatabase, InventoryModelType>("Inventory", inventorySchema);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
export type TInventoryDatabaseDocument = Document<unknown, {}, IInventoryDatabase> &
|
||||
Omit<
|
||||
IInventoryDatabase & {
|
||||
_id: Types.ObjectId;
|
||||
} & {
|
||||
__v: number;
|
||||
},
|
||||
keyof InventoryDocumentProps
|
||||
> &
|
||||
InventoryDocumentProps;
|
||||
|
@ -2,7 +2,7 @@ import { Request } from "express";
|
||||
import { getAccountIdForRequest } from "@/src/services/loginService";
|
||||
import { getInventory } from "@/src/services/inventoryService";
|
||||
import { Guild } from "@/src/models/guildModel";
|
||||
import { IInventoryDatabaseDocument } from "../types/inventoryTypes/inventoryTypes";
|
||||
import { TInventoryDatabaseDocument } from "@/src/models/inventoryModels/inventoryModel";
|
||||
|
||||
export const getGuildForRequest = async (req: Request) => {
|
||||
const accountId = await getAccountIdForRequest(req);
|
||||
@ -10,7 +10,7 @@ export const getGuildForRequest = async (req: Request) => {
|
||||
return await getGuildForRequestEx(req, inventory);
|
||||
};
|
||||
|
||||
export const getGuildForRequestEx = async (req: Request, inventory: IInventoryDatabaseDocument) => {
|
||||
export const getGuildForRequestEx = async (req: Request, inventory: TInventoryDatabaseDocument) => {
|
||||
const guildId = req.query.guildId as string;
|
||||
if (!inventory.GuildId || inventory.GuildId.toString() != guildId) {
|
||||
throw new Error("Account is not in the guild that it has sent a request for");
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
|
||||
import { Inventory, TInventoryDatabaseDocument } from "@/src/models/inventoryModels/inventoryModel";
|
||||
import postTutorialInventory from "@/static/fixed_responses/postTutorialInventory.json";
|
||||
import { config } from "@/src/services/configService";
|
||||
import { Types } from "mongoose";
|
||||
@ -7,7 +7,6 @@ import {
|
||||
IChallengeProgress,
|
||||
IConsumable,
|
||||
IFlavourItem,
|
||||
IInventoryDatabaseDocument,
|
||||
IMiscItem,
|
||||
IMission,
|
||||
IRawUpgrade,
|
||||
@ -95,7 +94,7 @@ export const combineInventoryChanges = (InventoryChanges: IInventoryChanges, del
|
||||
}
|
||||
};
|
||||
|
||||
export const getInventory = async (accountOwnerId: string) => {
|
||||
export const getInventory = async (accountOwnerId: string): Promise<TInventoryDatabaseDocument> => {
|
||||
const inventory = await Inventory.findOne({ accountOwnerId: accountOwnerId });
|
||||
|
||||
if (!inventory) {
|
||||
@ -485,7 +484,7 @@ const isCurrencyTracked = (usePremium: boolean): boolean => {
|
||||
};
|
||||
|
||||
export const updateCurrency = (
|
||||
inventory: IInventoryDatabaseDocument,
|
||||
inventory: TInventoryDatabaseDocument,
|
||||
price: number,
|
||||
usePremium: boolean
|
||||
): ICurrencyChanges => {
|
||||
@ -590,7 +589,7 @@ const addCrewShip = async (typeName: string, accountId: string) => {
|
||||
};
|
||||
|
||||
const addGearExpByCategory = (
|
||||
inventory: IInventoryDatabaseDocument,
|
||||
inventory: TInventoryDatabaseDocument,
|
||||
gearArray: IEquipmentClient[] | undefined,
|
||||
categoryName: TEquipmentKey
|
||||
): void => {
|
||||
@ -622,7 +621,7 @@ const addGearExpByCategory = (
|
||||
});
|
||||
};
|
||||
|
||||
export const addMiscItems = (inventory: IInventoryDatabaseDocument, itemsArray: IMiscItem[] | undefined): void => {
|
||||
export const addMiscItems = (inventory: TInventoryDatabaseDocument, itemsArray: IMiscItem[] | undefined): void => {
|
||||
const { MiscItems } = inventory;
|
||||
|
||||
itemsArray?.forEach(({ ItemCount, ItemType }) => {
|
||||
@ -638,7 +637,7 @@ export const addMiscItems = (inventory: IInventoryDatabaseDocument, itemsArray:
|
||||
};
|
||||
|
||||
export const addShipDecorations = (
|
||||
inventory: IInventoryDatabaseDocument,
|
||||
inventory: TInventoryDatabaseDocument,
|
||||
itemsArray: IConsumable[] | undefined
|
||||
): void => {
|
||||
const { ShipDecorations } = inventory;
|
||||
@ -655,7 +654,7 @@ export const addShipDecorations = (
|
||||
});
|
||||
};
|
||||
|
||||
export const addConsumables = (inventory: IInventoryDatabaseDocument, itemsArray: IConsumable[] | undefined): void => {
|
||||
export const addConsumables = (inventory: TInventoryDatabaseDocument, itemsArray: IConsumable[] | undefined): void => {
|
||||
const { Consumables } = inventory;
|
||||
|
||||
itemsArray?.forEach(({ ItemCount, ItemType }) => {
|
||||
@ -670,7 +669,7 @@ export const addConsumables = (inventory: IInventoryDatabaseDocument, itemsArray
|
||||
});
|
||||
};
|
||||
|
||||
export const addRecipes = (inventory: IInventoryDatabaseDocument, itemsArray: ITypeCount[] | undefined): void => {
|
||||
export const addRecipes = (inventory: TInventoryDatabaseDocument, itemsArray: ITypeCount[] | undefined): void => {
|
||||
const { Recipes } = inventory;
|
||||
|
||||
itemsArray?.forEach(({ ItemCount, ItemType }) => {
|
||||
@ -685,7 +684,7 @@ export const addRecipes = (inventory: IInventoryDatabaseDocument, itemsArray: IT
|
||||
});
|
||||
};
|
||||
|
||||
export const addMods = (inventory: IInventoryDatabaseDocument, itemsArray: IRawUpgrade[] | undefined): void => {
|
||||
export const addMods = (inventory: TInventoryDatabaseDocument, itemsArray: IRawUpgrade[] | undefined): void => {
|
||||
const { RawUpgrades } = inventory;
|
||||
itemsArray?.forEach(({ ItemType, ItemCount }) => {
|
||||
const itemIndex = RawUpgrades.findIndex(i => i.ItemType === ItemType);
|
||||
@ -700,7 +699,7 @@ export const addMods = (inventory: IInventoryDatabaseDocument, itemsArray: IRawU
|
||||
};
|
||||
|
||||
export const addFusionTreasures = (
|
||||
inventory: IInventoryDatabaseDocument,
|
||||
inventory: TInventoryDatabaseDocument,
|
||||
itemsArray: IFusionTreasure[] | undefined
|
||||
): void => {
|
||||
const { FusionTreasures } = inventory;
|
||||
@ -729,7 +728,7 @@ export const updateChallengeProgress = async (
|
||||
};
|
||||
|
||||
export const addSeasonalChallengeHistory = (
|
||||
inventory: IInventoryDatabaseDocument,
|
||||
inventory: TInventoryDatabaseDocument,
|
||||
itemsArray: ISeasonChallenge[] | undefined
|
||||
): void => {
|
||||
const category = inventory.SeasonChallengeHistory;
|
||||
@ -746,7 +745,7 @@ export const addSeasonalChallengeHistory = (
|
||||
};
|
||||
|
||||
export const addChallenges = (
|
||||
inventory: IInventoryDatabaseDocument,
|
||||
inventory: TInventoryDatabaseDocument,
|
||||
itemsArray: IChallengeProgress[] | undefined
|
||||
): void => {
|
||||
const category = inventory.ChallengeProgress;
|
||||
@ -763,7 +762,7 @@ export const addChallenges = (
|
||||
});
|
||||
};
|
||||
|
||||
const addMissionComplete = (inventory: IInventoryDatabaseDocument, { Tag, Completes }: IMission): void => {
|
||||
const addMissionComplete = (inventory: TInventoryDatabaseDocument, { Tag, Completes }: IMission): void => {
|
||||
const { Missions } = inventory;
|
||||
const itemIndex = Missions.findIndex(item => item.Tag === Tag);
|
||||
|
||||
|
@ -10,8 +10,6 @@ import {
|
||||
IEquipmentDatabase
|
||||
} from "@/src/types/inventoryTypes/commonInventoryTypes";
|
||||
|
||||
//Document extends will be deleted soon. TODO: delete and migrate uses to ...
|
||||
export interface IInventoryDatabaseDocument extends IInventoryDatabase, Document {}
|
||||
export interface IInventoryDatabase
|
||||
extends Omit<
|
||||
IInventoryResponse,
|
||||
|
Loading…
x
Reference in New Issue
Block a user