forked from tsai/budibase
5 changed files with 116 additions and 22 deletions
@ -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 = {} |
|||
} |
|||
|
|||
@ -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) |
|||
|
|||
Loading…
Reference in new issue