2022-09-23 01:59:28 +12:00
|
|
|
import {
|
2021-04-24 05:54:12 +12:00
|
|
|
TemplateMetadata,
|
|
|
|
TemplateBindings,
|
|
|
|
GLOBAL_OWNER,
|
2022-09-23 01:59:28 +12:00
|
|
|
} from "../../../constants"
|
|
|
|
import { getTemplates } from "../../../constants/templates"
|
|
|
|
import { tenancy, db as dbCore } from "@budibase/backend-core"
|
2021-04-22 03:42:44 +12:00
|
|
|
|
2022-09-23 01:59:28 +12:00
|
|
|
export async function save(ctx: any) {
|
|
|
|
const db = tenancy.getGlobalDB()
|
2021-04-22 05:15:57 +12:00
|
|
|
let template = ctx.request.body
|
|
|
|
if (!template.ownerId) {
|
|
|
|
template.ownerId = GLOBAL_OWNER
|
|
|
|
}
|
|
|
|
if (!template._id) {
|
2022-09-23 01:59:28 +12:00
|
|
|
template._id = dbCore.generateTemplateID(template.ownerId)
|
2021-04-22 05:15:57 +12:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 01:59:28 +12:00
|
|
|
export async function definitions(ctx: any) {
|
|
|
|
const bindings: any = {}
|
|
|
|
const info: any = {}
|
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
|
|
|
}
|
|
|
|
|
2022-09-23 01:59:28 +12:00
|
|
|
export async function fetch(ctx: any) {
|
2021-08-03 05:34:43 +12:00
|
|
|
ctx.body = await getTemplates()
|
2021-04-22 05:15:57 +12:00
|
|
|
}
|
|
|
|
|
2022-09-23 01:59:28 +12:00
|
|
|
export async function fetchByType(ctx: any) {
|
|
|
|
// @ts-ignore
|
2021-08-03 05:34:43 +12:00
|
|
|
ctx.body = await getTemplates({
|
2021-04-22 05:15:57 +12:00
|
|
|
type: ctx.params.type,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-23 01:59:28 +12:00
|
|
|
export async function fetchByOwner(ctx: any) {
|
|
|
|
// @ts-ignore
|
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
|
|
|
}
|
|
|
|
|
2022-09-23 01:59:28 +12:00
|
|
|
export async function find(ctx: any) {
|
|
|
|
// @ts-ignore
|
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
|
|
|
}
|
|
|
|
|
2022-09-23 01:59:28 +12:00
|
|
|
export async function destroy(ctx: any) {
|
|
|
|
const db = tenancy.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
|
|
|
}
|