Initial CRUD interface for templates.
This commit is contained in:
parent
ec099a9e6f
commit
e057217b48
|
@ -80,8 +80,12 @@ exports.getTemplateParams = (ownerId, templateId, otherProps = {}) => {
|
||||||
if (!templateId) {
|
if (!templateId) {
|
||||||
templateId = ""
|
templateId = ""
|
||||||
}
|
}
|
||||||
const base = `${DocumentTypes.TEMPLATE}${SEPARATOR}${ownerId}`
|
let final
|
||||||
const final = templateId ? `${base}${SEPARATOR}${templateId}` : base
|
if (templateId) {
|
||||||
|
final = templateId
|
||||||
|
} else {
|
||||||
|
final = `${DocumentTypes.TEMPLATE}${SEPARATOR}${ownerId}${SEPARATOR}`
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
...otherProps,
|
...otherProps,
|
||||||
startkey: final,
|
startkey: final,
|
||||||
|
|
|
@ -107,8 +107,7 @@ exports.getRowParams = (tableId = null, rowId = null, otherProps = {}) => {
|
||||||
return getDocParams(DocumentTypes.ROW, null, otherProps)
|
return getDocParams(DocumentTypes.ROW, null, otherProps)
|
||||||
}
|
}
|
||||||
|
|
||||||
const endOfKey =
|
const endOfKey = rowId == null ? `${tableId}${SEPARATOR}` : rowId
|
||||||
rowId == null ? `${tableId}${SEPARATOR}` : `${tableId}${SEPARATOR}${rowId}`
|
|
||||||
|
|
||||||
return getDocParams(DocumentTypes.ROW, endOfKey, otherProps)
|
return getDocParams(DocumentTypes.ROW, endOfKey, otherProps)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,25 +1,75 @@
|
||||||
// const { generateTemplateID, getTemplateParams, StaticDatabases } = require("@budibase/auth").db
|
const { generateTemplateID, getTemplateParams, StaticDatabases } = require("@budibase/auth").db
|
||||||
// const { CouchDB } = require("../../../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 => {
|
exports.save = async ctx => {
|
||||||
// const db = new CouchDB(GLOBAL_DB)
|
const db = new CouchDB(GLOBAL_DB)
|
||||||
// const id = generateTemplateID()
|
const type = ctx.params.type
|
||||||
ctx.body = {}
|
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 => {
|
exports.fetch = async ctx => {
|
||||||
// const db = new CouchDB(GLOBAL_DB)
|
ctx.body = await getTemplates()
|
||||||
ctx.body = {}
|
}
|
||||||
|
|
||||||
|
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 => {
|
exports.find = async ctx => {
|
||||||
// const db = new CouchDB(GLOBAL_DB)
|
ctx.body = await getTemplates({
|
||||||
ctx.body = {}
|
id: ctx.params.id,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.destroy = async ctx => {
|
exports.destroy = async ctx => {
|
||||||
// const db = new CouchDB(GLOBAL_DB)
|
// TODO
|
||||||
|
const db = new CouchDB(GLOBAL_DB)
|
||||||
ctx.body = {}
|
ctx.body = {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,33 @@
|
||||||
const Router = require("@koa/router")
|
const Router = require("@koa/router")
|
||||||
const controller = require("../../controllers/admin/templates")
|
const controller = require("../../controllers/admin/templates")
|
||||||
// const joiValidator = require("../../../middleware/joi-validator")
|
const joiValidator = require("../../../middleware/joi-validator")
|
||||||
// const Joi = require("joi")
|
const Joi = require("joi")
|
||||||
|
const { TemplatePurpose, TemplateTypes } = require("../../../constants")
|
||||||
|
|
||||||
const router = Router()
|
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
|
router
|
||||||
|
.get("/api/admin/template/definitions", controller.definitions)
|
||||||
.post(
|
.post(
|
||||||
"/api/admin/template/:type",
|
"/api/admin/template",
|
||||||
buildTemplateSaveValidation(),
|
buildTemplateSaveValidation(),
|
||||||
controller.save
|
controller.save
|
||||||
)
|
)
|
||||||
.get("/api/admin/template/:type", controller.fetch)
|
.get("/api/admin/template", controller.fetch)
|
||||||
.delete("/api/admin/template/:type/:id", controller.destroy)
|
.get("/api/admin/template/:type", controller.fetchByType)
|
||||||
.get("/api/admin/template/:type/:id", controller.find)
|
.get("/api/admin/template/:ownerId", controller.fetchByOwner)
|
||||||
|
.delete("/api/admin/template/:id", controller.destroy)
|
||||||
|
.get("/api/admin/template/:id", controller.find)
|
||||||
|
|
|
@ -6,3 +6,29 @@ exports.UserStatus = {
|
||||||
exports.Groups = {
|
exports.Groups = {
|
||||||
ALL_USERS: "all_users",
|
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,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue