2020-11-03 17:27:28 +01:00
|
|
|
const { getScreenParams, generateScreenID } = require("../../db/utils")
|
2022-01-10 20:33:00 +01:00
|
|
|
const { AccessController } = require("@budibase/backend-core/roles")
|
2022-01-27 19:18:31 +01:00
|
|
|
const { getAppDB } = require("@budibase/backend-core/context")
|
2020-10-13 18:40:28 +02:00
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.fetch = async ctx => {
|
2022-01-27 19:18:31 +01:00
|
|
|
const db = getAppDB()
|
2020-11-03 17:27:28 +01:00
|
|
|
|
2020-11-16 19:04:44 +01:00
|
|
|
const screens = (
|
|
|
|
await db.allDocs(
|
|
|
|
getScreenParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2021-05-04 12:32:22 +02:00
|
|
|
).rows.map(element => element.doc)
|
2020-11-16 19:04:44 +01:00
|
|
|
|
2022-01-27 19:18:31 +01:00
|
|
|
ctx.body = await new AccessController().checkScreensAccess(
|
2020-11-16 19:04:44 +01:00
|
|
|
screens,
|
2020-12-02 14:20:56 +01:00
|
|
|
ctx.user.role._id
|
2020-11-03 17:27:28 +01:00
|
|
|
)
|
2020-10-13 18:40:28 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.save = async ctx => {
|
2022-01-27 19:18:31 +01:00
|
|
|
const db = getAppDB()
|
2020-11-23 17:56:35 +01:00
|
|
|
let screen = ctx.request.body
|
2020-11-03 17:27:28 +01:00
|
|
|
|
|
|
|
if (!screen._id) {
|
2020-11-20 18:47:13 +01:00
|
|
|
screen._id = generateScreenID()
|
2020-11-03 17:27:28 +01:00
|
|
|
}
|
|
|
|
const response = await db.put(screen)
|
|
|
|
|
|
|
|
ctx.message = `Screen ${screen.name} saved.`
|
2020-12-03 13:08:33 +01:00
|
|
|
ctx.body = {
|
|
|
|
...screen,
|
|
|
|
_id: response.id,
|
|
|
|
_rev: response.rev,
|
|
|
|
}
|
2020-10-13 18:40:28 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.destroy = async ctx => {
|
2022-01-27 19:18:31 +01:00
|
|
|
const db = getAppDB()
|
2020-11-30 17:12:06 +01:00
|
|
|
await db.remove(ctx.params.screenId, ctx.params.screenRev)
|
2021-03-10 12:47:39 +01:00
|
|
|
ctx.body = {
|
|
|
|
message: "Screen deleted successfully",
|
|
|
|
}
|
2020-11-04 17:13:50 +01:00
|
|
|
ctx.status = 200
|
2020-10-13 18:40:28 +02:00
|
|
|
}
|