SpaceNinjaServer/src/services/importService.ts

34 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-01-19 12:33:04 +01:00
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";
2025-01-19 14:53:45 +01:00
const convertDate = (value: IMongoDate): Date => {
2025-01-19 12:33:04 +01:00
return new Date(parseInt(value.$date.$numberLong));
};
2025-01-19 14:53:45 +01:00
const convertOptionalDate = (value: IMongoDate | undefined): Date | undefined => {
return value ? convertDate(value) : undefined;
2025-01-19 12:33:04 +01:00
};
2025-01-19 14:53:45 +01:00
const convertEquipment = (client: IEquipmentClient): IEquipmentDatabase => {
2025-01-19 12:33:04 +01:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { ItemId, ...rest } = client;
return {
...rest,
_id: new Types.ObjectId(client.ItemId.$oid),
2025-01-19 14:53:45 +01:00
InfestationDate: convertOptionalDate(client.InfestationDate),
Expiry: convertOptionalDate(client.Expiry),
UpgradesExpiry: convertOptionalDate(client.UpgradesExpiry)
2025-01-19 12:33:04 +01:00
};
};
export const importInventory = (db: TInventoryDatabaseDocument, client: IInventoryResponse): void => {
2025-01-19 14:53:45 +01:00
const clientSuitsInDbFormat = client.Suits.map(x => convertEquipment(x));
db.Suits.splice(0, db.Suits.length);
2025-01-19 12:33:04 +01:00
clientSuitsInDbFormat.forEach(suitToImport => {
db.Suits.push(suitToImport);
});
};