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 accountId = await getAccountIdForRequest(req);
|
||||||
const inventory = await getInventory(accountId);
|
const inventory = await getInventory(accountId);
|
||||||
const request = JSON.parse(String(req.body)) as IImportRequest;
|
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());
|
res.json(await inventory.save());
|
||||||
};
|
};
|
||||||
|
|
||||||
interface IImportRequest {
|
interface IImportRequest {
|
||||||
inventory: IInventoryResponse;
|
inventory: IInventoryResponse;
|
||||||
replace: boolean;
|
|
||||||
update: boolean;
|
|
||||||
}
|
}
|
||||||
|
@ -24,27 +24,10 @@ const convertEquipment = (client: IEquipmentClient): IEquipmentDatabase => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const importInventory = (
|
export const importInventory = (db: TInventoryDatabaseDocument, client: IInventoryResponse): void => {
|
||||||
db: TInventoryDatabaseDocument,
|
|
||||||
client: IInventoryResponse,
|
|
||||||
replace: boolean = false,
|
|
||||||
update: boolean = true
|
|
||||||
): void => {
|
|
||||||
const clientSuitsInDbFormat = client.Suits.map(x => convertEquipment(x));
|
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 => {
|
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);
|
db.Suits.push(suitToImport);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user