From 7aa7500eada12033a602bf157d3c054671518fb1 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 18 Jun 2021 12:24:08 +0100 Subject: [PATCH] Prevent deleting built-in layout ID's as autoscreens depends on them --- packages/server/src/api/controllers/layout.js | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/packages/server/src/api/controllers/layout.js b/packages/server/src/api/controllers/layout.js index 4c6c7755ef..2887c6984a 100644 --- a/packages/server/src/api/controllers/layout.js +++ b/packages/server/src/api/controllers/layout.js @@ -1,4 +1,7 @@ -const { EMPTY_LAYOUT } = require("../../constants/layouts") +const { + EMPTY_LAYOUT, + BASE_LAYOUT_PROP_IDS, +} = require("../../constants/layouts") const CouchDB = require("../../db") const { generateLayoutID, getScreenParams } = require("../../db/utils") @@ -26,15 +29,19 @@ exports.destroy = async function (ctx) { const layoutId = ctx.params.layoutId, layoutRev = ctx.params.layoutRev - const layoutsUsedByScreens = ( - await db.allDocs( - getScreenParams(null, { - include_docs: true, - }) - ) - ).rows.map(element => element.doc.layoutId) - if (layoutsUsedByScreens.includes(layoutId)) { - ctx.throw(400, "Cannot delete a layout that's being used by a screen") + if (Object.values(BASE_LAYOUT_PROP_IDS).includes(layoutId)) { + ctx.throw(400, "Cannot delete a built-in layout") + } else { + const layoutsUsedByScreens = ( + await db.allDocs( + getScreenParams(null, { + include_docs: true, + }) + ) + ).rows.map(element => element.doc.layoutId) + if (layoutsUsedByScreens.includes(layoutId)) { + ctx.throw(400, "Cannot delete a layout that's being used by a screen") + } } await db.remove(layoutId, layoutRev)