2022-01-11 08:33:00 +13:00
|
|
|
const { generateTemplateID } = require("@budibase/backend-core/db")
|
2021-04-24 05:54:12 +12:00
|
|
|
const {
|
|
|
|
TemplateMetadata,
|
|
|
|
TemplateBindings,
|
|
|
|
GLOBAL_OWNER,
|
|
|
|
} = require("../../../constants")
|
2021-08-03 05:34:43 +12:00
|
|
|
const { getTemplates } = require("../../../constants/templates")
|
2022-01-11 08:33:00 +13:00
|
|
|
const { getGlobalDB } = require("@budibase/backend-core/tenancy")
|
2021-04-22 03:42:44 +12:00
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.save = async ctx => {
|
2021-08-05 20:59:08 +12:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-05-11 01:41:52 +12:00
|
|
|
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 = {}
|
2021-07-14 22:23:24 +12:00
|
|
|
const info = {}
|
2021-05-12 01:44:43 +12:00
|
|
|
for (let template of TemplateMetadata.email) {
|
|
|
|
bindings[template.purpose] = template.bindings
|
2021-07-14 22:23:24 +12:00
|
|
|
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 = {
|
2021-07-14 22:23:24 +12:00
|
|
|
info,
|
2021-05-12 01:44:43 +12:00
|
|
|
bindings: {
|
|
|
|
...bindings,
|
|
|
|
common: Object.values(TemplateBindings),
|
|
|
|
},
|
2021-04-22 05:15:57 +12:00
|
|
|
}
|
2021-04-22 03:42:44 +12:00
|
|
|
}
|
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.fetch = async ctx => {
|
2021-08-03 05:34:43 +12:00
|
|
|
ctx.body = await getTemplates()
|
2021-04-22 05:15:57 +12:00
|
|
|
}
|
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.fetchByType = async ctx => {
|
2021-08-03 05:34:43 +12:00
|
|
|
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 => {
|
2021-08-03 05:34:43 +12:00
|
|
|
ctx.body = await getTemplates({
|
2021-04-22 05:15:57 +12:00
|
|
|
ownerId: ctx.params.ownerId,
|
|
|
|
})
|
2021-04-22 03:42:44 +12:00
|
|
|
}
|
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.find = async ctx => {
|
2021-08-03 05:34:43 +12:00
|
|
|
ctx.body = await getTemplates({
|
2021-04-22 05:15:57 +12:00
|
|
|
id: ctx.params.id,
|
|
|
|
})
|
2021-04-22 03:42:44 +12:00
|
|
|
}
|
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.destroy = async ctx => {
|
2021-08-05 20:59:08 +12:00
|
|
|
const db = getGlobalDB()
|
2021-04-23 02:26:32 +12:00
|
|
|
await db.remove(ctx.params.id, ctx.params.rev)
|
|
|
|
ctx.message = `Template ${ctx.params.id} deleted.`
|
|
|
|
ctx.status = 200
|
2021-04-22 03:42:44 +12:00
|
|
|
}
|