diff --git a/packages/types/src/api/web/global/index.ts b/packages/types/src/api/web/global/index.ts index 8359d8cd1a..36dce60b6c 100644 --- a/packages/types/src/api/web/global/index.ts +++ b/packages/types/src/api/web/global/index.ts @@ -8,3 +8,4 @@ export * from "./oldMigration" export * from "./email" export * from "./role" export * from "./self" +export * from "./template" diff --git a/packages/types/src/api/web/global/template.ts b/packages/types/src/api/web/global/template.ts new file mode 100644 index 0000000000..0e79cbe62d --- /dev/null +++ b/packages/types/src/api/web/global/template.ts @@ -0,0 +1,30 @@ +import { Document, Template } from "../../../documents" + +export interface TemplateDefinition { + name: string + description: string + category: string +} + +export interface TemplateBinding { + name: string + description: string +} + +export interface FetchTemplateDefinitionResponse { + info: Record + bindings: Record +} + +export interface SaveTemplateRequest extends Template {} +export interface SaveTemplateResponse extends Template {} + +export type FetchTemplateResponse = Template[] +export type FetchTemplateByTypeResponse = Template[] +export type FetchTemplateByOwnerIDResponse = Template[] + +export interface FindTemplateResponse extends Template {} + +export interface DeleteTemplateResponse { + message: string +} diff --git a/packages/worker/src/api/controllers/global/templates.ts b/packages/worker/src/api/controllers/global/templates.ts index 0abce704c7..5fba215e96 100644 --- a/packages/worker/src/api/controllers/global/templates.ts +++ b/packages/worker/src/api/controllers/global/templates.ts @@ -3,10 +3,25 @@ import { TemplateBindings, GLOBAL_OWNER, } from "../../../constants" -import { getTemplates } from "../../../constants/templates" +import { getTemplateByID, getTemplates } from "../../../constants/templates" import { tenancy, db as dbCore } from "@budibase/backend-core" +import { + DeleteTemplateResponse, + FetchTemplateByOwnerIDResponse, + FetchTemplateByTypeResponse, + FetchTemplateDefinitionResponse, + FetchTemplateResponse, + FindTemplateResponse, + SaveTemplateRequest, + SaveTemplateResponse, + TemplateBinding, + TemplateDefinition, + UserCtx, +} from "@budibase/types" -export async function save(ctx: any) { +export async function save( + ctx: UserCtx +) { const db = tenancy.getGlobalDB() let template = ctx.request.body if (!template.ownerId) { @@ -23,9 +38,11 @@ export async function save(ctx: any) { } } -export async function definitions(ctx: any) { - const bindings: any = {} - const info: any = {} +export async function definitions( + ctx: UserCtx +) { + const bindings: Record = {} + const info: Record = {} for (let template of TemplateMetadata.email) { bindings[template.purpose] = template.bindings info[template.purpose] = { @@ -44,34 +61,33 @@ export async function definitions(ctx: any) { } } -export async function fetch(ctx: any) { +export async function fetch(ctx: UserCtx) { ctx.body = await getTemplates() } -export async function fetchByType(ctx: any) { - // @ts-ignore +export async function fetchByType( + ctx: UserCtx +) { ctx.body = await getTemplates({ type: ctx.params.type, }) } -export async function fetchByOwner(ctx: any) { +export async function fetchByOwner( + ctx: UserCtx +) { // @ts-ignore ctx.body = await getTemplates({ ownerId: ctx.params.ownerId, }) } -export async function find(ctx: any) { - // @ts-ignore - ctx.body = await getTemplates({ - id: ctx.params.id, - }) +export async function find(ctx: UserCtx) { + ctx.body = await getTemplateByID(ctx.params.id) } -export async function destroy(ctx: any) { +export async function destroy(ctx: UserCtx) { const db = tenancy.getGlobalDB() await db.remove(ctx.params.id, ctx.params.rev) - ctx.message = `Template ${ctx.params.id} deleted.` - ctx.status = 200 + ctx.body = { message: `Template ${ctx.params.id} deleted.` } } diff --git a/packages/worker/src/constants/templates/index.ts b/packages/worker/src/constants/templates/index.ts index 43c5b0870f..b2e98afe58 100644 --- a/packages/worker/src/constants/templates/index.ts +++ b/packages/worker/src/constants/templates/index.ts @@ -48,8 +48,21 @@ export function addBaseTemplates(templates: Template[], type?: string) { export async function getTemplates({ ownerId, type, - id, -}: { ownerId?: string; type?: string; id?: string } = {}) { +}: { ownerId?: string; type?: string } = {}) { + const db = tenancy.getGlobalDB() + const response = await db.allDocs