2020-12-05 00:16:07 +01:00
|
|
|
const { EMPTY_LAYOUT } = require("../../constants/layouts")
|
2020-11-30 17:12:06 +01:00
|
|
|
const CouchDB = require("../../db")
|
|
|
|
const { generateLayoutID, getScreenParams } = require("../../db/utils")
|
2020-11-23 15:07:18 +01:00
|
|
|
|
|
|
|
exports.save = async function(ctx) {
|
|
|
|
const db = new CouchDB(ctx.user.appId)
|
2020-11-23 17:56:35 +01:00
|
|
|
let layout = ctx.request.body
|
2020-11-23 15:07:18 +01:00
|
|
|
|
2020-12-05 00:16:07 +01:00
|
|
|
if (!layout.props) {
|
|
|
|
layout = {
|
|
|
|
...layout,
|
|
|
|
...EMPTY_LAYOUT,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 17:56:35 +01:00
|
|
|
layout._id = layout._id || generateLayoutID()
|
2020-12-07 21:29:41 +01:00
|
|
|
const response = await db.put(layout)
|
|
|
|
layout._rev = response.rev
|
|
|
|
|
|
|
|
ctx.body = layout
|
2020-11-23 15:07:18 +01:00
|
|
|
ctx.status = 200
|
|
|
|
}
|
2020-11-30 17:12:06 +01:00
|
|
|
|
|
|
|
exports.destroy = async function(ctx) {
|
|
|
|
const db = new CouchDB(ctx.user.appId)
|
|
|
|
const layoutId = ctx.params.layoutId,
|
|
|
|
layoutRev = ctx.params.layoutRev
|
|
|
|
|
|
|
|
const layoutsUsedByScreens = (
|
|
|
|
await db.allDocs(
|
|
|
|
getScreenParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2020-12-05 00:16:07 +01:00
|
|
|
).rows.map(element => element.doc.layoutId)
|
2020-12-01 17:22:06 +01:00
|
|
|
if (layoutsUsedByScreens.includes(layoutId)) {
|
2020-11-30 17:12:06 +01:00
|
|
|
ctx.throw(400, "Cannot delete a base layout")
|
|
|
|
}
|
|
|
|
|
|
|
|
await db.remove(layoutId, layoutRev)
|
|
|
|
ctx.message = "Layout deleted successfully"
|
|
|
|
ctx.status = 200
|
|
|
|
}
|