no point trying for fine-grained import, it's gonna become a mess, either way
This commit is contained in:
parent
03716daa62
commit
d11dfc5485
@ -8,12 +8,10 @@ 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);
|
||||
importInventory(inventory, request.inventory);
|
||||
res.json(await inventory.save());
|
||||
};
|
||||
|
||||
interface IImportRequest {
|
||||
inventory: IInventoryResponse;
|
||||
replace: boolean;
|
||||
update: boolean;
|
||||
}
|
||||
|
@ -24,27 +24,10 @@ const convertEquipment = (client: IEquipmentClient): IEquipmentDatabase => {
|
||||
};
|
||||
};
|
||||
|
||||
export const importInventory = (
|
||||
db: TInventoryDatabaseDocument,
|
||||
client: IInventoryResponse,
|
||||
replace: boolean = false,
|
||||
update: boolean = true
|
||||
): void => {
|
||||
export const importInventory = (db: TInventoryDatabaseDocument, client: IInventoryResponse): void => {
|
||||
const clientSuitsInDbFormat = client.Suits.map(x => convertEquipment(x));
|
||||
if (replace) {
|
||||
db.Suits.splice(0, db.Suits.length);
|
||||
}
|
||||
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);
|
||||
});
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user