1
0
Fork 0
mirror of synced 2024-08-17 19:11:52 +12:00
budibase/packages/worker/src/api/controllers/admin/templates.js

65 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-04-23 04:58:14 +12:00
const { generateTemplateID, StaticDatabases } = require("@budibase/auth").db
2021-04-22 05:15:57 +12:00
const { CouchDB } = require("../../../db")
2021-04-23 04:58:14 +12:00
const { TemplateMetadata, TemplateBindings } = require("../../../constants")
const { getTemplates } = require("../../../constants/templates")
2021-04-22 05:15:57 +12:00
const GLOBAL_DB = StaticDatabases.GLOBAL.name
const GLOBAL_OWNER = "global"
2021-04-22 03:46:51 +12:00
exports.save = async ctx => {
2021-04-22 05:15:57 +12:00
const db = new CouchDB(GLOBAL_DB)
const type = ctx.params.type
let template = ctx.request.body
if (!template.ownerId) {
template.ownerId = GLOBAL_OWNER
}
if (!template._id) {
template._id = generateTemplateID(template.ownerId)
}
const response = await db.put({
...template,
type,
})
ctx.body = {
...template,
_rev: response.rev,
}
}
exports.definitions = async ctx => {
ctx.body = {
purpose: TemplateMetadata,
bindings: Object.values(TemplateBindings),
2021-04-22 05:15:57 +12:00
}
}
exports.fetch = async ctx => {
2021-04-22 05:15:57 +12:00
ctx.body = await getTemplates()
}
exports.fetchByType = async ctx => {
ctx.body = await getTemplates({
type: ctx.params.type,
})
}
exports.fetchByOwner = async ctx => {
ctx.body = await getTemplates({
ownerId: ctx.params.ownerId,
})
}
exports.find = async ctx => {
2021-04-22 05:15:57 +12:00
ctx.body = await getTemplates({
id: ctx.params.id,
})
}
exports.destroy = async ctx => {
2021-04-22 05:15:57 +12:00
const db = new CouchDB(GLOBAL_DB)
await db.remove(ctx.params.id, ctx.params.rev)
ctx.message = `Template ${ctx.params.id} deleted.`
ctx.status = 200
}