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

72 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-04-23 04:58:14 +12:00
const { generateTemplateID, StaticDatabases } = require("@budibase/auth").db
2021-05-07 05:02:44 +12:00
const CouchDB = require("../../../db")
2021-04-24 05:54:12 +12:00
const {
TemplateMetadata,
TemplateBindings,
GLOBAL_OWNER,
} = require("../../../constants")
const { getTemplates } = require("../../../constants/templates")
2021-04-22 05:15:57 +12:00
const GLOBAL_DB = StaticDatabases.GLOBAL.name
2021-05-04 22:32:22 +12:00
exports.save = async ctx => {
2021-04-22 05:15:57 +12:00
const db = new CouchDB(GLOBAL_DB)
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)
2021-04-22 05:15:57 +12:00
ctx.body = {
...template,
_rev: response.rev,
}
}
2021-05-04 22:32:22 +12:00
exports.definitions = async ctx => {
2021-05-12 01:44:43 +12:00
const bindings = {}
for (let template of TemplateMetadata.email) {
bindings[template.purpose] = template.bindings
}
2021-04-22 05:15:57 +12:00
ctx.body = {
2021-05-12 01:44:43 +12:00
bindings: {
...bindings,
common: Object.values(TemplateBindings),
},
2021-04-22 05:15:57 +12:00
}
}
2021-05-04 22:32:22 +12:00
exports.fetch = async ctx => {
2021-04-22 05:15:57 +12:00
ctx.body = await getTemplates()
}
2021-05-04 22:32:22 +12:00
exports.fetchByType = async ctx => {
2021-04-22 05:15:57 +12:00
ctx.body = await getTemplates({
type: ctx.params.type,
})
}
2021-05-04 22:32:22 +12:00
exports.fetchByOwner = async ctx => {
2021-04-22 05:15:57 +12:00
ctx.body = await getTemplates({
ownerId: ctx.params.ownerId,
})
}
2021-05-04 22:32:22 +12:00
exports.find = async ctx => {
2021-04-22 05:15:57 +12:00
ctx.body = await getTemplates({
id: ctx.params.id,
})
}
2021-05-04 22:32:22 +12:00
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
}