diff --git a/src/controllers/custom/importController.ts b/src/controllers/custom/importController.ts index 47a850f6..98ebc1fe 100644 --- a/src/controllers/custom/importController.ts +++ b/src/controllers/custom/importController.ts @@ -13,33 +13,41 @@ export const importController: RequestHandler = async (req, res) => { const request = req.body as IImportRequest; let anyKnownKey = false; + try { + const inventory = await getInventory(accountId); + if (importInventory(inventory, request.inventory)) { + anyKnownKey = true; + await inventory.save(); + } - const inventory = await getInventory(accountId); - if (importInventory(inventory, request.inventory)) { - anyKnownKey = true; - await inventory.save(); + if ("LoadOutPresets" in request.inventory && request.inventory.LoadOutPresets) { + anyKnownKey = true; + const loadout = await getLoadout(accountId); + importLoadOutPresets(loadout, request.inventory.LoadOutPresets); + await loadout.save(); + } + + if ( + request.inventory.Ship?.Rooms || // very old accounts may have Ship with { Features: [ ... ] } + "Apartment" in request.inventory || + "TailorShop" in request.inventory + ) { + anyKnownKey = true; + const personalRooms = await getPersonalRooms(accountId); + importPersonalRooms(personalRooms, request.inventory); + await personalRooms.save(); + } + + if (!anyKnownKey) { + res.send("noKnownKey").end(); + } + + broadcastInventoryUpdate(req); + } catch (e) { + console.error(e); + res.send((e as Error).message); } - - if ("LoadOutPresets" in request.inventory && request.inventory.LoadOutPresets) { - anyKnownKey = true; - const loadout = await getLoadout(accountId); - importLoadOutPresets(loadout, request.inventory.LoadOutPresets); - await loadout.save(); - } - - if ( - request.inventory.Ship?.Rooms || // very old accounts may have Ship with { Features: [ ... ] } - "Apartment" in request.inventory || - "TailorShop" in request.inventory - ) { - anyKnownKey = true; - const personalRooms = await getPersonalRooms(accountId); - importPersonalRooms(personalRooms, request.inventory); - await personalRooms.save(); - } - - res.json(anyKnownKey); - broadcastInventoryUpdate(req); + res.end(); }; interface IImportRequest { diff --git a/static/webui/script.js b/static/webui/script.js index 1222f1c5..05265556 100644 --- a/static/webui/script.js +++ b/static/webui/script.js @@ -3646,8 +3646,12 @@ function doImport() { data: JSON.stringify({ inventory: JSON.parse($("#import-inventory").val()) }) - }).then(function (anyKnownKey) { - toast(loc(anyKnownKey ? "code_succImport" : "code_nothingToDo")); + }).then(function (err) { + if (err) { + toast(err == "noKnownKey" ? loc("code_nothingToDo") : err); + } else { + toast(loc("code_succImport")); + } updateInventory(); }); } catch (e) {