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

76 lines
1.6 KiB
JavaScript
Raw Normal View History

const { generateTemplateID } = require("@budibase/backend-core/db")
2021-04-24 05:54:12 +12:00
const {
TemplateMetadata,
TemplateBindings,
GLOBAL_OWNER,
} = require("../../../constants")
const { getTemplates } = require("../../../constants/templates")
const { getGlobalDB } = require("@budibase/backend-core/tenancy")
2021-05-04 22:32:22 +12:00
exports.save = async ctx => {
const db = getGlobalDB()
2021-04-22 05:15:57 +12:00
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 = {}
const info = {}
2021-05-12 01:44:43 +12:00
for (let template of TemplateMetadata.email) {
bindings[template.purpose] = template.bindings
info[template.purpose] = {
name: template.name,
description: template.description,
category: template.category,
}
2021-05-12 01:44:43 +12:00
}
2021-04-22 05:15:57 +12:00
ctx.body = {
info,
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 => {
ctx.body = await getTemplates()
2021-04-22 05:15:57 +12:00
}
2021-05-04 22:32:22 +12:00
exports.fetchByType = async ctx => {
ctx.body = await getTemplates({
2021-04-22 05:15:57 +12:00
type: ctx.params.type,
})
}
2021-05-04 22:32:22 +12:00
exports.fetchByOwner = async ctx => {
ctx.body = await getTemplates({
2021-04-22 05:15:57 +12:00
ownerId: ctx.params.ownerId,
})
}
2021-05-04 22:32:22 +12:00
exports.find = async ctx => {
ctx.body = await getTemplates({
2021-04-22 05:15:57 +12:00
id: ctx.params.id,
})
}
2021-05-04 22:32:22 +12:00
exports.destroy = async ctx => {
const db = getGlobalDB()
await db.remove(ctx.params.id, ctx.params.rev)
ctx.message = `Template ${ctx.params.id} deleted.`
ctx.status = 200
}