Prevent deleting built-in layout ID's as autoscreens depends on them

This commit is contained in:
Andrew Kingston 2021-06-18 12:24:08 +01:00
parent eb388c51c6
commit 7aa7500ead
1 changed files with 17 additions and 10 deletions

View File

@ -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 CouchDB = require("../../db")
const { generateLayoutID, getScreenParams } = require("../../db/utils") const { generateLayoutID, getScreenParams } = require("../../db/utils")
@ -26,6 +29,9 @@ exports.destroy = async function (ctx) {
const layoutId = ctx.params.layoutId, const layoutId = ctx.params.layoutId,
layoutRev = ctx.params.layoutRev layoutRev = ctx.params.layoutRev
if (Object.values(BASE_LAYOUT_PROP_IDS).includes(layoutId)) {
ctx.throw(400, "Cannot delete a built-in layout")
} else {
const layoutsUsedByScreens = ( const layoutsUsedByScreens = (
await db.allDocs( await db.allDocs(
getScreenParams(null, { getScreenParams(null, {
@ -36,6 +42,7 @@ exports.destroy = async function (ctx) {
if (layoutsUsedByScreens.includes(layoutId)) { if (layoutsUsedByScreens.includes(layoutId)) {
ctx.throw(400, "Cannot delete a layout that's being used by a screen") ctx.throw(400, "Cannot delete a layout that's being used by a screen")
} }
}
await db.remove(layoutId, layoutRev) await db.remove(layoutId, layoutRev)
ctx.body = { message: "Layout deleted successfully" } ctx.body = { message: "Layout deleted successfully" }