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 { 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) |
||||
|
|||||
Loading…
Reference in new issue