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