1
0
Fork 0
mirror of synced 2024-07-15 03:05:57 +12:00
budibase/packages/worker/src/api/controllers/global/templates.ts

78 lines
1.7 KiB
TypeScript
Raw Normal View History

import {
2021-04-24 05:54:12 +12:00
TemplateMetadata,
TemplateBindings,
GLOBAL_OWNER,
} from "../../../constants"
import { getTemplates } from "../../../constants/templates"
import { tenancy, db as dbCore } from "@budibase/backend-core"
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) {
template._id = dbCore.generateTemplateID(template.ownerId)
2021-04-22 05:15:57 +12:00
}
const response = await db.put(template)
2021-04-22 05:15:57 +12:00
ctx.body = {
...template,
_rev: response.rev,
}
}
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
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
}
}
export async function fetch(ctx: any) {
ctx.body = await getTemplates()
2021-04-22 05:15:57 +12:00
}
export async function fetchByType(ctx: any) {
// @ts-ignore
ctx.body = await getTemplates({
2021-04-22 05:15:57 +12:00
type: ctx.params.type,
})
}
export async function fetchByOwner(ctx: any) {
// @ts-ignore
ctx.body = await getTemplates({
2021-04-22 05:15:57 +12:00
ownerId: ctx.params.ownerId,
})
}
export async function find(ctx: any) {
// @ts-ignore
ctx.body = await getTemplates({
2021-04-22 05:15:57 +12:00
id: ctx.params.id,
})
}
export async function destroy(ctx: any) {
const db = tenancy.getGlobalDB()
await db.remove(ctx.params.id, ctx.params.rev)
ctx.message = `Template ${ctx.params.id} deleted.`
ctx.status = 200
}