initial importService for Suits array
This commit is contained in:
parent
b19fda66a2
commit
b41ce3751c
19
src/controllers/custom/importController.ts
Normal file
19
src/controllers/custom/importController.ts
Normal 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;
|
||||||
|
}
|
@ -9,6 +9,7 @@ import { renameAccountController } from "@/src/controllers/custom/renameAccountC
|
|||||||
|
|
||||||
import { createAccountController } from "@/src/controllers/custom/createAccountController";
|
import { createAccountController } from "@/src/controllers/custom/createAccountController";
|
||||||
import { addItemsController } from "@/src/controllers/custom/addItemsController";
|
import { addItemsController } from "@/src/controllers/custom/addItemsController";
|
||||||
|
import { importController } from "@/src/controllers/custom/importController";
|
||||||
|
|
||||||
import { getConfigDataController } from "@/src/controllers/custom/getConfigDataController";
|
import { getConfigDataController } from "@/src/controllers/custom/getConfigDataController";
|
||||||
import { updateConfigDataController } from "@/src/controllers/custom/updateConfigDataController";
|
import { updateConfigDataController } from "@/src/controllers/custom/updateConfigDataController";
|
||||||
@ -24,6 +25,7 @@ customRouter.get("/renameAccount", renameAccountController);
|
|||||||
|
|
||||||
customRouter.post("/createAccount", createAccountController);
|
customRouter.post("/createAccount", createAccountController);
|
||||||
customRouter.post("/addItems", addItemsController);
|
customRouter.post("/addItems", addItemsController);
|
||||||
|
customRouter.post("/import", importController);
|
||||||
|
|
||||||
customRouter.get("/config", getConfigDataController);
|
customRouter.get("/config", getConfigDataController);
|
||||||
customRouter.post("/config", updateConfigDataController);
|
customRouter.post("/config", updateConfigDataController);
|
||||||
|
50
src/services/importService.ts
Normal file
50
src/services/importService.ts
Normal 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);
|
||||||
|
});
|
||||||
|
};
|
@ -78,8 +78,11 @@ export interface IEquipmentSelection {
|
|||||||
hide?: boolean;
|
hide?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IEquipmentClient extends Omit<IEquipmentDatabase, "_id" | "UpgradesExpiry"> {
|
export interface IEquipmentClient
|
||||||
|
extends Omit<IEquipmentDatabase, "_id" | "InfestationDate" | "Expiry" | "UpgradesExpiry"> {
|
||||||
ItemId: IOid;
|
ItemId: IOid;
|
||||||
|
InfestationDate?: IMongoDate;
|
||||||
|
Expiry?: IMongoDate;
|
||||||
UpgradesExpiry?: IMongoDate;
|
UpgradesExpiry?: IMongoDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,12 +109,12 @@ export interface IEquipmentDatabase {
|
|||||||
CustomizationSlotPurchases?: number;
|
CustomizationSlotPurchases?: number;
|
||||||
UpgradeType?: string;
|
UpgradeType?: string;
|
||||||
UpgradeFingerprint?: string;
|
UpgradeFingerprint?: string;
|
||||||
InfestationDate?: IMongoDate;
|
InfestationDate?: Date;
|
||||||
InfestationDays?: number;
|
InfestationDays?: number;
|
||||||
InfestationType?: string;
|
InfestationType?: string;
|
||||||
ModularParts?: string[];
|
ModularParts?: string[];
|
||||||
UnlockLevel?: number;
|
UnlockLevel?: number;
|
||||||
Expiry?: IMongoDate;
|
Expiry?: Date;
|
||||||
SkillTree?: string;
|
SkillTree?: string;
|
||||||
OffensiveUpgrade?: string;
|
OffensiveUpgrade?: string;
|
||||||
DefensiveUpgrade?: string;
|
DefensiveUpgrade?: string;
|
||||||
|
@ -7,7 +7,8 @@ import {
|
|||||||
IItemConfig,
|
IItemConfig,
|
||||||
IOperatorConfigClient,
|
IOperatorConfigClient,
|
||||||
IEquipmentSelection,
|
IEquipmentSelection,
|
||||||
IEquipmentDatabase
|
IEquipmentDatabase,
|
||||||
|
IEquipmentClient
|
||||||
} from "@/src/types/inventoryTypes/commonInventoryTypes";
|
} from "@/src/types/inventoryTypes/commonInventoryTypes";
|
||||||
|
|
||||||
export interface IInventoryDatabase
|
export interface IInventoryDatabase
|
||||||
@ -23,6 +24,7 @@ export interface IInventoryDatabase
|
|||||||
| "BlessingCooldown"
|
| "BlessingCooldown"
|
||||||
| "Ships"
|
| "Ships"
|
||||||
| "WeaponSkins"
|
| "WeaponSkins"
|
||||||
|
| "Suits"
|
||||||
> {
|
> {
|
||||||
accountOwnerId: Types.ObjectId;
|
accountOwnerId: Types.ObjectId;
|
||||||
Created: Date;
|
Created: Date;
|
||||||
@ -35,6 +37,7 @@ export interface IInventoryDatabase
|
|||||||
BlessingCooldown: Date;
|
BlessingCooldown: Date;
|
||||||
Ships: Types.ObjectId[];
|
Ships: Types.ObjectId[];
|
||||||
WeaponSkins: IWeaponSkinDatabase[];
|
WeaponSkins: IWeaponSkinDatabase[];
|
||||||
|
Suits: IEquipmentDatabase[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IQuestKeyDatabase {
|
export interface IQuestKeyDatabase {
|
||||||
@ -163,7 +166,7 @@ export interface IInventoryResponse extends IDailyAffiliations {
|
|||||||
ChallengeProgress: IChallengeProgress[];
|
ChallengeProgress: IChallengeProgress[];
|
||||||
RawUpgrades: IRawUpgrade[];
|
RawUpgrades: IRawUpgrade[];
|
||||||
ReceivedStartingGear: boolean;
|
ReceivedStartingGear: boolean;
|
||||||
Suits: IEquipmentDatabase[];
|
Suits: IEquipmentClient[];
|
||||||
LongGuns: IEquipmentDatabase[];
|
LongGuns: IEquipmentDatabase[];
|
||||||
Pistols: IEquipmentDatabase[];
|
Pistols: IEquipmentDatabase[];
|
||||||
Melee: IEquipmentDatabase[];
|
Melee: IEquipmentDatabase[];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user