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 { getJSONfromString } from "@/src/helpers/stringHelpers";
|
||||||
import { getInventory, addMiscItems, updateCurrency, addRecipes } from "@/src/services/inventoryService";
|
import { getInventory, addMiscItems, updateCurrency, addRecipes } from "@/src/services/inventoryService";
|
||||||
import { IOid } from "@/src/types/commonTypes";
|
import { IOid } from "@/src/types/commonTypes";
|
||||||
import {
|
import { IConsumedSuit, IInfestedFoundry, IMiscItem, ITypeCount } from "@/src/types/inventoryTypes/inventoryTypes";
|
||||||
IConsumedSuit,
|
|
||||||
IInfestedFoundry,
|
|
||||||
IInventoryDatabaseDocument,
|
|
||||||
IMiscItem,
|
|
||||||
ITypeCount
|
|
||||||
} from "@/src/types/inventoryTypes/inventoryTypes";
|
|
||||||
import { ExportMisc, ExportRecipes } from "warframe-public-export-plus";
|
import { ExportMisc, ExportRecipes } from "warframe-public-export-plus";
|
||||||
import { getRecipe } from "@/src/services/itemDataService";
|
import { getRecipe } from "@/src/services/itemDataService";
|
||||||
|
import { TInventoryDatabaseDocument } from "@/src/models/inventoryModels/inventoryModel";
|
||||||
|
|
||||||
export const infestedFoundryController: RequestHandler = async (req, res) => {
|
export const infestedFoundryController: RequestHandler = async (req, res) => {
|
||||||
const accountId = await getAccountIdForRequest(req);
|
const accountId = await getAccountIdForRequest(req);
|
||||||
@ -239,7 +234,7 @@ interface IHelminthSubsumeRequest {
|
|||||||
Recipe: string;
|
Recipe: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const handleSubsumeCompletion = (inventory: IInventoryDatabaseDocument): ITypeCount[] => {
|
export const handleSubsumeCompletion = (inventory: TInventoryDatabaseDocument): ITypeCount[] => {
|
||||||
const [recipeType] = Object.entries(ExportRecipes).find(
|
const [recipeType] = Object.entries(ExportRecipes).find(
|
||||||
([_recipeType, recipe]) =>
|
([_recipeType, recipe]) =>
|
||||||
recipe.secretIngredientAction == "SIA_WARFRAME_ABILITY" &&
|
recipe.secretIngredientAction == "SIA_WARFRAME_ABILITY" &&
|
||||||
|
@ -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 } 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 { 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 { 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 IInventoryDatabaseDocument);
|
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;
|
||||||
|
@ -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;
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Model, Schema, Types, model } from "mongoose";
|
import { Document, Model, Schema, Types, model } from "mongoose";
|
||||||
import {
|
import {
|
||||||
IFlavourItem,
|
IFlavourItem,
|
||||||
IRawUpgrade,
|
IRawUpgrade,
|
||||||
@ -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;
|
||||||
@ -1114,3 +1115,15 @@ type InventoryDocumentProps = {
|
|||||||
type InventoryModelType = Model<IInventoryDatabase, {}, InventoryDocumentProps>;
|
type InventoryModelType = Model<IInventoryDatabase, {}, InventoryDocumentProps>;
|
||||||
|
|
||||||
export const Inventory = model<IInventoryDatabase, InventoryModelType>("Inventory", inventorySchema);
|
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 { getAccountIdForRequest } from "@/src/services/loginService";
|
||||||
import { getInventory } from "@/src/services/inventoryService";
|
import { getInventory } from "@/src/services/inventoryService";
|
||||||
import { Guild } from "@/src/models/guildModel";
|
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) => {
|
export const getGuildForRequest = async (req: Request) => {
|
||||||
const accountId = await getAccountIdForRequest(req);
|
const accountId = await getAccountIdForRequest(req);
|
||||||
@ -10,7 +10,7 @@ export const getGuildForRequest = async (req: Request) => {
|
|||||||
return await getGuildForRequestEx(req, inventory);
|
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;
|
const guildId = req.query.guildId as string;
|
||||||
if (!inventory.GuildId || inventory.GuildId.toString() != guildId) {
|
if (!inventory.GuildId || inventory.GuildId.toString() != guildId) {
|
||||||
throw new Error("Account is not in the guild that it has sent a request for");
|
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 postTutorialInventory from "@/static/fixed_responses/postTutorialInventory.json";
|
||||||
import { config } from "@/src/services/configService";
|
import { config } from "@/src/services/configService";
|
||||||
import { Types } from "mongoose";
|
import { Types } from "mongoose";
|
||||||
@ -7,7 +7,6 @@ import {
|
|||||||
IChallengeProgress,
|
IChallengeProgress,
|
||||||
IConsumable,
|
IConsumable,
|
||||||
IFlavourItem,
|
IFlavourItem,
|
||||||
IInventoryDatabaseDocument,
|
|
||||||
IMiscItem,
|
IMiscItem,
|
||||||
IMission,
|
IMission,
|
||||||
IRawUpgrade,
|
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 });
|
const inventory = await Inventory.findOne({ accountOwnerId: accountOwnerId });
|
||||||
|
|
||||||
if (!inventory) {
|
if (!inventory) {
|
||||||
@ -485,7 +484,7 @@ const isCurrencyTracked = (usePremium: boolean): boolean => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const updateCurrency = (
|
export const updateCurrency = (
|
||||||
inventory: IInventoryDatabaseDocument,
|
inventory: TInventoryDatabaseDocument,
|
||||||
price: number,
|
price: number,
|
||||||
usePremium: boolean
|
usePremium: boolean
|
||||||
): ICurrencyChanges => {
|
): ICurrencyChanges => {
|
||||||
@ -590,7 +589,7 @@ const addCrewShip = async (typeName: string, accountId: string) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const addGearExpByCategory = (
|
const addGearExpByCategory = (
|
||||||
inventory: IInventoryDatabaseDocument,
|
inventory: TInventoryDatabaseDocument,
|
||||||
gearArray: IEquipmentClient[] | undefined,
|
gearArray: IEquipmentClient[] | undefined,
|
||||||
categoryName: TEquipmentKey
|
categoryName: TEquipmentKey
|
||||||
): void => {
|
): 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;
|
const { MiscItems } = inventory;
|
||||||
|
|
||||||
itemsArray?.forEach(({ ItemCount, ItemType }) => {
|
itemsArray?.forEach(({ ItemCount, ItemType }) => {
|
||||||
@ -638,7 +637,7 @@ export const addMiscItems = (inventory: IInventoryDatabaseDocument, itemsArray:
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const addShipDecorations = (
|
export const addShipDecorations = (
|
||||||
inventory: IInventoryDatabaseDocument,
|
inventory: TInventoryDatabaseDocument,
|
||||||
itemsArray: IConsumable[] | undefined
|
itemsArray: IConsumable[] | undefined
|
||||||
): void => {
|
): void => {
|
||||||
const { ShipDecorations } = inventory;
|
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;
|
const { Consumables } = inventory;
|
||||||
|
|
||||||
itemsArray?.forEach(({ ItemCount, ItemType }) => {
|
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;
|
const { Recipes } = inventory;
|
||||||
|
|
||||||
itemsArray?.forEach(({ ItemCount, ItemType }) => {
|
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;
|
const { RawUpgrades } = inventory;
|
||||||
itemsArray?.forEach(({ ItemType, ItemCount }) => {
|
itemsArray?.forEach(({ ItemType, ItemCount }) => {
|
||||||
const itemIndex = RawUpgrades.findIndex(i => i.ItemType === ItemType);
|
const itemIndex = RawUpgrades.findIndex(i => i.ItemType === ItemType);
|
||||||
@ -700,7 +699,7 @@ export const addMods = (inventory: IInventoryDatabaseDocument, itemsArray: IRawU
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const addFusionTreasures = (
|
export const addFusionTreasures = (
|
||||||
inventory: IInventoryDatabaseDocument,
|
inventory: TInventoryDatabaseDocument,
|
||||||
itemsArray: IFusionTreasure[] | undefined
|
itemsArray: IFusionTreasure[] | undefined
|
||||||
): void => {
|
): void => {
|
||||||
const { FusionTreasures } = inventory;
|
const { FusionTreasures } = inventory;
|
||||||
@ -729,7 +728,7 @@ export const updateChallengeProgress = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const addSeasonalChallengeHistory = (
|
export const addSeasonalChallengeHistory = (
|
||||||
inventory: IInventoryDatabaseDocument,
|
inventory: TInventoryDatabaseDocument,
|
||||||
itemsArray: ISeasonChallenge[] | undefined
|
itemsArray: ISeasonChallenge[] | undefined
|
||||||
): void => {
|
): void => {
|
||||||
const category = inventory.SeasonChallengeHistory;
|
const category = inventory.SeasonChallengeHistory;
|
||||||
@ -746,7 +745,7 @@ export const addSeasonalChallengeHistory = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const addChallenges = (
|
export const addChallenges = (
|
||||||
inventory: IInventoryDatabaseDocument,
|
inventory: TInventoryDatabaseDocument,
|
||||||
itemsArray: IChallengeProgress[] | undefined
|
itemsArray: IChallengeProgress[] | undefined
|
||||||
): void => {
|
): void => {
|
||||||
const category = inventory.ChallengeProgress;
|
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 { Missions } = inventory;
|
||||||
const itemIndex = Missions.findIndex(item => item.Tag === Tag);
|
const itemIndex = Missions.findIndex(item => item.Tag === Tag);
|
||||||
|
|
||||||
|
@ -10,8 +10,6 @@ import {
|
|||||||
IEquipmentDatabase
|
IEquipmentDatabase
|
||||||
} from "@/src/types/inventoryTypes/commonInventoryTypes";
|
} 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
|
export interface IInventoryDatabase
|
||||||
extends Omit<
|
extends Omit<
|
||||||
IInventoryResponse,
|
IInventoryResponse,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user