initial importService for Suits array

This commit is contained in:
Sainan 2025-01-19 12:33:04 +01:00
parent b19fda66a2
commit b41ce3751c
5 changed files with 82 additions and 5 deletions

View File

@ -0,0 +1,19 @@
import { importInventory } from "@/src/services/importService";
import { getInventory } from "@/src/services/inventoryService";
import { getAccountIdForRequest } from "@/src/services/loginService";
import { IInventoryResponse } from "@/src/types/inventoryTypes/inventoryTypes";
import { RequestHandler } from "express";
export const importController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req);
const inventory = await getInventory(accountId);
const request = JSON.parse(String(req.body)) as IImportRequest;
importInventory(inventory, request.inventory, request.replace, request.update);
res.json(await inventory.save());
};
interface IImportRequest {
inventory: IInventoryResponse;
replace: boolean;
update: boolean;
}

View File

@ -9,6 +9,7 @@ import { renameAccountController } from "@/src/controllers/custom/renameAccountC
import { createAccountController } from "@/src/controllers/custom/createAccountController";
import { addItemsController } from "@/src/controllers/custom/addItemsController";
import { importController } from "@/src/controllers/custom/importController";
import { getConfigDataController } from "@/src/controllers/custom/getConfigDataController";
import { updateConfigDataController } from "@/src/controllers/custom/updateConfigDataController";
@ -24,6 +25,7 @@ customRouter.get("/renameAccount", renameAccountController);
customRouter.post("/createAccount", createAccountController);
customRouter.post("/addItems", addItemsController);
customRouter.post("/import", importController);
customRouter.get("/config", getConfigDataController);
customRouter.post("/config", updateConfigDataController);

View File

@ -0,0 +1,50 @@
import { Types } from "mongoose";
import { IEquipmentClient, IEquipmentDatabase } from "../types/inventoryTypes/commonInventoryTypes";
import { IMongoDate } from "../types/commonTypes";
import { IInventoryResponse } from "../types/inventoryTypes/inventoryTypes";
import { TInventoryDatabaseDocument } from "../models/inventoryModels/inventoryModel";
const importDate = (value: IMongoDate): Date => {
return new Date(parseInt(value.$date.$numberLong));
};
const importOptionalDate = (value: IMongoDate | undefined): Date | undefined => {
return value ? importDate(value) : undefined;
};
const importEquipment = (client: IEquipmentClient): IEquipmentDatabase => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { ItemId, ...rest } = client;
return {
...rest,
_id: new Types.ObjectId(client.ItemId.$oid),
InfestationDate: importOptionalDate(client.InfestationDate),
Expiry: importOptionalDate(client.Expiry),
UpgradesExpiry: importOptionalDate(client.UpgradesExpiry)
};
};
export const importInventory = (
db: TInventoryDatabaseDocument,
client: IInventoryResponse,
replace: boolean = false,
update: boolean = true
): void => {
const clientSuitsInDbFormat = client.Suits.map(x => importEquipment(x));
if (replace) {
db.Suits.splice(0, db.Suits.length);
}
clientSuitsInDbFormat.forEach(suitToImport => {
if (update) {
const index = db.Suits.findIndex(x => x._id == suitToImport._id);
if (index != -1) {
db.Suits.splice(index, 1);
}
} else {
if (db.Suits.id(suitToImport._id)) {
return;
}
}
db.Suits.push(suitToImport);
});
};

View File

@ -78,8 +78,11 @@ export interface IEquipmentSelection {
hide?: boolean;
}
export interface IEquipmentClient extends Omit<IEquipmentDatabase, "_id" | "UpgradesExpiry"> {
export interface IEquipmentClient
extends Omit<IEquipmentDatabase, "_id" | "InfestationDate" | "Expiry" | "UpgradesExpiry"> {
ItemId: IOid;
InfestationDate?: IMongoDate;
Expiry?: IMongoDate;
UpgradesExpiry?: IMongoDate;
}
@ -106,12 +109,12 @@ export interface IEquipmentDatabase {
CustomizationSlotPurchases?: number;
UpgradeType?: string;
UpgradeFingerprint?: string;
InfestationDate?: IMongoDate;
InfestationDate?: Date;
InfestationDays?: number;
InfestationType?: string;
ModularParts?: string[];
UnlockLevel?: number;
Expiry?: IMongoDate;
Expiry?: Date;
SkillTree?: string;
OffensiveUpgrade?: string;
DefensiveUpgrade?: string;

View File

@ -7,7 +7,8 @@ import {
IItemConfig,
IOperatorConfigClient,
IEquipmentSelection,
IEquipmentDatabase
IEquipmentDatabase,
IEquipmentClient
} from "@/src/types/inventoryTypes/commonInventoryTypes";
export interface IInventoryDatabase
@ -23,6 +24,7 @@ export interface IInventoryDatabase
| "BlessingCooldown"
| "Ships"
| "WeaponSkins"
| "Suits"
> {
accountOwnerId: Types.ObjectId;
Created: Date;
@ -35,6 +37,7 @@ export interface IInventoryDatabase
BlessingCooldown: Date;
Ships: Types.ObjectId[];
WeaponSkins: IWeaponSkinDatabase[];
Suits: IEquipmentDatabase[];
}
export interface IQuestKeyDatabase {
@ -163,7 +166,7 @@ export interface IInventoryResponse extends IDailyAffiliations {
ChallengeProgress: IChallengeProgress[];
RawUpgrades: IRawUpgrade[];
ReceivedStartingGear: boolean;
Suits: IEquipmentDatabase[];
Suits: IEquipmentClient[];
LongGuns: IEquipmentDatabase[];
Pistols: IEquipmentDatabase[];
Melee: IEquipmentDatabase[];