From e85b7682e0f4b0d529680d81f28353e86599a2eb Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 21 Apr 2021 18:15:57 +0100 Subject: [PATCH] Initial CRUD interface for templates. --- packages/auth/src/db/utils.js | 8 ++- packages/server/src/db/utils.js | 3 +- .../src/api/controllers/admin/templates.js | 72 ++++++++++++++++--- .../worker/src/api/routes/admin/templates.js | 29 ++++++-- packages/worker/src/constants/index.js | 26 +++++++ 5 files changed, 116 insertions(+), 22 deletions(-) diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index 4bd1f399cd..deff7e1519 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -80,8 +80,12 @@ exports.getTemplateParams = (ownerId, templateId, otherProps = {}) => { if (!templateId) { templateId = "" } - const base = `${DocumentTypes.TEMPLATE}${SEPARATOR}${ownerId}` - const final = templateId ? `${base}${SEPARATOR}${templateId}` : base + let final + if (templateId) { + final = templateId + } else { + final = `${DocumentTypes.TEMPLATE}${SEPARATOR}${ownerId}${SEPARATOR}` + } return { ...otherProps, startkey: final, diff --git a/packages/server/src/db/utils.js b/packages/server/src/db/utils.js index e6bcfada5b..bbed248cf8 100644 --- a/packages/server/src/db/utils.js +++ b/packages/server/src/db/utils.js @@ -107,8 +107,7 @@ exports.getRowParams = (tableId = null, rowId = null, otherProps = {}) => { return getDocParams(DocumentTypes.ROW, null, otherProps) } - const endOfKey = - rowId == null ? `${tableId}${SEPARATOR}` : `${tableId}${SEPARATOR}${rowId}` + const endOfKey = rowId == null ? `${tableId}${SEPARATOR}` : rowId return getDocParams(DocumentTypes.ROW, endOfKey, otherProps) } diff --git a/packages/worker/src/api/controllers/admin/templates.js b/packages/worker/src/api/controllers/admin/templates.js index e445f611a8..25360bd1e5 100644 --- a/packages/worker/src/api/controllers/admin/templates.js +++ b/packages/worker/src/api/controllers/admin/templates.js @@ -1,25 +1,75 @@ -// const { generateTemplateID, getTemplateParams, StaticDatabases } = require("@budibase/auth").db -// const { CouchDB } = require("../../../db") +const { generateTemplateID, getTemplateParams, StaticDatabases } = require("@budibase/auth").db +const { CouchDB } = require("../../../db") +const { TemplatePurposePretty } = require("../../../constants") -// const GLOBAL_DB = StaticDatabases.GLOBAL.name +const GLOBAL_DB = StaticDatabases.GLOBAL.name +const GLOBAL_OWNER = "global" + +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) + if (type) { + templates = templates.filter(template => template.type === type) + } + return templates +} exports.save = async ctx => { - // const db = new CouchDB(GLOBAL_DB) - // const id = generateTemplateID() - ctx.body = {} + 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 = { + purpose: TemplatePurposePretty + } } exports.fetch = async ctx => { - // const db = new CouchDB(GLOBAL_DB) - ctx.body = {} + 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, + }) } exports.find = async ctx => { - // const db = new CouchDB(GLOBAL_DB) - ctx.body = {} + ctx.body = await getTemplates({ + id: ctx.params.id, + }) } exports.destroy = async ctx => { - // const db = new CouchDB(GLOBAL_DB) + // TODO + const db = new CouchDB(GLOBAL_DB) ctx.body = {} } diff --git a/packages/worker/src/api/routes/admin/templates.js b/packages/worker/src/api/routes/admin/templates.js index 9c66b87200..756b3e7cf0 100644 --- a/packages/worker/src/api/routes/admin/templates.js +++ b/packages/worker/src/api/routes/admin/templates.js @@ -1,18 +1,33 @@ const Router = require("@koa/router") const controller = require("../../controllers/admin/templates") -// const joiValidator = require("../../../middleware/joi-validator") -// const Joi = require("joi") +const joiValidator = require("../../../middleware/joi-validator") +const Joi = require("joi") +const { TemplatePurpose, TemplateTypes } = require("../../../constants") const router = Router() -function buildTemplateSaveValidation() {} +function buildTemplateSaveValidation() { + // prettier-ignore + return joiValidator.body(Joi.object({ + _id: Joi.string().allow(null, ""), + _rev: Joi.string().allow(null, ""), + ownerId: Joi.string().allow(null, ""), + name: Joi.string().allow(null, ""), + contents: Joi.string().required(), + purpose: Joi.string().required().valid(...Object.values(TemplatePurpose)), + type: Joi.string().required().valid(...Object.values(TemplateTypes)), + }).required().unknown(true).optional()) +} router + .get("/api/admin/template/definitions", controller.definitions) .post( - "/api/admin/template/:type", + "/api/admin/template", buildTemplateSaveValidation(), controller.save ) - .get("/api/admin/template/:type", controller.fetch) - .delete("/api/admin/template/:type/:id", controller.destroy) - .get("/api/admin/template/:type/:id", controller.find) + .get("/api/admin/template", controller.fetch) + .get("/api/admin/template/:type", controller.fetchByType) + .get("/api/admin/template/:ownerId", controller.fetchByOwner) + .delete("/api/admin/template/:id", controller.destroy) + .get("/api/admin/template/:id", controller.find) diff --git a/packages/worker/src/constants/index.js b/packages/worker/src/constants/index.js index 586d69c86f..dcdad2bb3e 100644 --- a/packages/worker/src/constants/index.js +++ b/packages/worker/src/constants/index.js @@ -6,3 +6,29 @@ exports.UserStatus = { exports.Groups = { ALL_USERS: "all_users", } + +exports.TemplateTypes = { + EMAIL: "email", +} + +exports.TemplatePurpose = { + PASSWORD_RECOVERY: "password_recovery", + INVITATION: "invitation", + CUSTOM: "custom", +} + +exports.TemplatePurposePretty = [ + { + name: "Password Recovery", + value: exports.TemplatePurpose.PASSWORD_RECOVERY + }, + { + name: "New User Invitation", + value: exports.TemplatePurpose.INVITATION, + }, + { + name: "Custom", + value: exports.TemplatePurpose.CUSTOM, + } +] +