2021-04-22 22:48:37 +12:00
|
|
|
const {
|
|
|
|
generateTemplateID,
|
|
|
|
getTemplateParams,
|
|
|
|
StaticDatabases,
|
|
|
|
} = require("@budibase/auth").db
|
2021-04-22 05:15:57 +12:00
|
|
|
const { CouchDB } = require("../../../db")
|
2021-04-23 03:06:29 +12:00
|
|
|
const {
|
|
|
|
TemplatePurposePretty,
|
|
|
|
TemplateTypes,
|
|
|
|
EmailTemplatePurpose,
|
|
|
|
TemplatePurpose,
|
|
|
|
} = require("../../../constants")
|
2021-04-23 02:26:32 +12:00
|
|
|
const { getTemplateByPurpose } = require("../../../constants/templates")
|
2021-04-22 03:42:44 +12:00
|
|
|
|
2021-04-22 05:15:57 +12:00
|
|
|
const GLOBAL_DB = StaticDatabases.GLOBAL.name
|
|
|
|
const GLOBAL_OWNER = "global"
|
|
|
|
|
2021-04-23 02:26:32 +12:00
|
|
|
function addBaseTemplates(templates, type = null) {
|
|
|
|
let purposeList
|
|
|
|
switch (type) {
|
|
|
|
case TemplateTypes.EMAIL:
|
|
|
|
purposeList = Object.values(EmailTemplatePurpose)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
purposeList = Object.values(TemplatePurpose)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for (let purpose of purposeList) {
|
|
|
|
// check if a template exists already for purpose
|
|
|
|
if (templates.find(template => template.purpose === purpose)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
templates.push(getTemplateByPurpose(purpose))
|
|
|
|
}
|
|
|
|
return templates
|
|
|
|
}
|
|
|
|
|
2021-04-22 05:15:57 +12:00
|
|
|
async function getTemplates({ ownerId, type, id } = {}) {
|
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
|
|
|
const response = await db.allDocs(
|
|
|
|
getTemplateParams(ownerId, id, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
let templates = response.rows.map(row => row.doc)
|
2021-04-23 02:26:32 +12:00
|
|
|
// should only be one template with ID
|
|
|
|
if (id) {
|
|
|
|
return templates[0]
|
|
|
|
}
|
2021-04-22 05:15:57 +12:00
|
|
|
if (type) {
|
|
|
|
templates = templates.filter(template => template.type === type)
|
|
|
|
}
|
2021-04-23 02:26:32 +12:00
|
|
|
|
|
|
|
return addBaseTemplates(templates, type)
|
2021-04-22 05:15:57 +12:00
|
|
|
}
|
2021-04-22 03:42:44 +12:00
|
|
|
|
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 = {
|
2021-04-22 22:48:37 +12:00
|
|
|
purpose: TemplatePurposePretty,
|
2021-04-22 05:15:57 +12:00
|
|
|
}
|
2021-04-22 03:42:44 +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,
|
|
|
|
})
|
2021-04-22 03:42:44 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.find = async ctx => {
|
2021-04-22 05:15:57 +12:00
|
|
|
ctx.body = await getTemplates({
|
|
|
|
id: ctx.params.id,
|
|
|
|
})
|
2021-04-22 03:42:44 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.destroy = async ctx => {
|
2021-04-22 05:15:57 +12:00
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
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
|
|
|
}
|