Initial CRUD interface for templates.

This commit is contained in:
mike12345567 2021-04-21 18:15:57 +01:00
parent 15223080d5
commit e85b7682e0
5 changed files with 116 additions and 22 deletions

View File

@ -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,

View File

@ -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)
}

View File

@ -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 = {}
}

View File

@ -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)

View File

@ -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,
}
]