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,
|
|
|
|
replace: boolean = false,
|
|
|
|
update: boolean = true
|
|
|
|
): void => {
|
2025-01-19 14:53:45 +01:00
|
|
|
const clientSuitsInDbFormat = client.Suits.map(x => convertEquipment(x));
|
2025-01-19 12:33:04 +01:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
};
|