From 839970a9dd7c664381a3b96a500b1eeb4c976a01 Mon Sep 17 00:00:00 2001 From: Jonathan Niles Date: Thu, 30 Jan 2020 10:40:49 +0100 Subject: [PATCH] fix(inventory): allow deletion from inventory This commit removes the foreign key constraints from the inventory_log to allow deletion from the inventory. It also fixes the http headers sent twice error in the error handler. Closes #4147. --- server/controllers/inventory/index.js | 19 +++++++++++-------- .../controllers/inventory/inventory/core.js | 7 +++---- server/models/migrations/next/migrate.sql | 4 ++++ server/models/schema.sql | 1 - 4 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 server/models/migrations/next/migrate.sql diff --git a/server/controllers/inventory/index.js b/server/controllers/inventory/index.js index d3edcbbd1e..e9c1e11907 100644 --- a/server/controllers/inventory/index.js +++ b/server/controllers/inventory/index.js @@ -208,15 +208,18 @@ function getInventoryItemsById(req, res, next) { /** - * DELETE /inventory/:uuid - * delete an inventory group + * DELETE /inventory/metadata/:uuid + * + * @description + * Delete an inventory item from the database */ -function deleteInventory(req, res, next) { - - core.remove(req.params.uuid) - .then(res.sendStatus(204)) - .catch(error => core.errorHandler(error, req, res, next)) - .done(); +async function deleteInventory(req, res, next) { + try { + await core.remove(req.params.uuid); + res.sendStatus(204); + } catch (err) { + core.errorHandler(err, req, res, next); + } } // ======================= inventory group ============================= diff --git a/server/controllers/inventory/inventory/core.js b/server/controllers/inventory/inventory/core.js index f657332c07..47d999d130 100644 --- a/server/controllers/inventory/inventory/core.js +++ b/server/controllers/inventory/inventory/core.js @@ -227,11 +227,10 @@ function getItemsMetadata(params) { } -// This function helps to delete an invetory - +// This function helps to delete an inventory function remove(_uuid) { - const sql = `DELETE FROM inventory WHERE uuid = HUID(?)`; - return db.exec(sql, _uuid); + const sql = `DELETE FROM inventory WHERE uuid = ?`; + return db.exec(sql, db.bid(_uuid)); } /** diff --git a/server/models/migrations/next/migrate.sql b/server/models/migrations/next/migrate.sql new file mode 100644 index 0000000000..6bf8dc2f6c --- /dev/null +++ b/server/models/migrations/next/migrate.sql @@ -0,0 +1,4 @@ +/* next migration rules */ + +ALTER TABLE inventory_log DROP FOREIGN KEY `inventory_log_ibfk_1`; + diff --git a/server/models/schema.sql b/server/models/schema.sql index ab287c7ef7..a62a583ae7 100644 --- a/server/models/schema.sql +++ b/server/models/schema.sql @@ -2353,7 +2353,6 @@ CREATE TABLE `inventory_log` ( PRIMARY KEY (`uuid`), KEY `inventory_uuid` (`inventory_uuid`), KEY `user_id` (`user_id`), - FOREIGN KEY (`inventory_uuid`) REFERENCES `inventory` (`uuid`), FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci;