2020-11-01 16:32:54 +01:00
|
|
|
const CouchDB = require("../../db")
|
2020-11-03 17:27:28 +01:00
|
|
|
const { getScreenParams, generateScreenID } = require("../../db/utils")
|
2020-11-16 19:04:44 +01:00
|
|
|
const { AccessController } = require("../../utilities/security/accessLevels")
|
2020-10-13 18:40:28 +02:00
|
|
|
|
|
|
|
exports.fetch = async ctx => {
|
2020-11-16 19:04:44 +01:00
|
|
|
const appId = ctx.user.appId
|
|
|
|
const db = new CouchDB(appId)
|
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,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).rows.map(element => element.doc)
|
|
|
|
|
|
|
|
ctx.body = await new AccessController(appId).checkScreensAccess(
|
|
|
|
screens,
|
|
|
|
ctx.user.accessLevel._id
|
2020-11-03 17:27:28 +01:00
|
|
|
)
|
2020-10-13 18:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.save = async ctx => {
|
2020-11-03 17:27:28 +01:00
|
|
|
const appId = ctx.user.appId
|
|
|
|
const db = new CouchDB(appId)
|
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.`
|
|
|
|
ctx.body = response
|
2020-10-13 18:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.destroy = async ctx => {
|
2020-11-03 17:27:28 +01:00
|
|
|
const db = new CouchDB(ctx.user.appId)
|
|
|
|
await db.remove(ctx.params.screenId, ctx.params.revId)
|
|
|
|
ctx.message = "Screen deleted successfully"
|
2020-11-04 17:13:50 +01:00
|
|
|
ctx.status = 200
|
2020-10-13 18:40:28 +02:00
|
|
|
}
|