adding record models for brevity
This commit is contained in:
parent
eea2dbf3b3
commit
f8cfc8e7d7
|
@ -25,7 +25,7 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "jest routes --runInBand",
|
"test": "jest routes --runInBand",
|
||||||
"test:integration": "jest workflow --runInBand",
|
"test:integration": "jest workflow --runInBand",
|
||||||
"test:watch": "jest -w",
|
"test:watch": "jest --watch",
|
||||||
"initialise": "node ../cli/bin/budi init -b local -q",
|
"initialise": "node ../cli/bin/budi init -b local -q",
|
||||||
"budi": "node ../cli/bin/budi",
|
"budi": "node ../cli/bin/budi",
|
||||||
"dev:builder": "nodemon ../cli/bin/budi run",
|
"dev:builder": "nodemon ../cli/bin/budi run",
|
||||||
|
|
|
@ -61,7 +61,7 @@ exports.fetchView = async function(ctx) {
|
||||||
ctx.body = response.rows.map(row => row.doc)
|
ctx.body = response.rows.map(row => row.doc)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.fetchModel = async function(ctx) {
|
exports.fetchModelRecords = async function(ctx) {
|
||||||
const db = new CouchDB(ctx.params.instanceId)
|
const db = new CouchDB(ctx.params.instanceId)
|
||||||
const response = await db.query(`database/all_${ctx.params.modelId}`, {
|
const response = await db.query(`database/all_${ctx.params.modelId}`, {
|
||||||
include_docs: true,
|
include_docs: true,
|
||||||
|
@ -69,6 +69,15 @@ exports.fetchModel = async function(ctx) {
|
||||||
ctx.body = response.rows.map(row => row.doc)
|
ctx.body = response.rows.map(row => row.doc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.search = async function(ctx) {
|
||||||
|
const db = new CouchDB(ctx.params.instanceId)
|
||||||
|
const response = await db.allDocs({
|
||||||
|
include_docs: true,
|
||||||
|
...ctx.request.body,
|
||||||
|
})
|
||||||
|
ctx.body = response.rows.map(row => row.doc)
|
||||||
|
}
|
||||||
|
|
||||||
exports.find = async function(ctx) {
|
exports.find = async function(ctx) {
|
||||||
const db = new CouchDB(ctx.params.instanceId)
|
const db = new CouchDB(ctx.params.instanceId)
|
||||||
const record = await db.get(ctx.params.recordId)
|
const record = await db.get(ctx.params.recordId)
|
||||||
|
|
|
@ -10,6 +10,7 @@ const {
|
||||||
instanceRoutes,
|
instanceRoutes,
|
||||||
clientRoutes,
|
clientRoutes,
|
||||||
applicationRoutes,
|
applicationRoutes,
|
||||||
|
recordRoutes,
|
||||||
modelRoutes,
|
modelRoutes,
|
||||||
viewRoutes,
|
viewRoutes,
|
||||||
staticRoutes,
|
staticRoutes,
|
||||||
|
@ -69,6 +70,9 @@ router.use(viewRoutes.allowedMethods())
|
||||||
router.use(modelRoutes.routes())
|
router.use(modelRoutes.routes())
|
||||||
router.use(modelRoutes.allowedMethods())
|
router.use(modelRoutes.allowedMethods())
|
||||||
|
|
||||||
|
router.use(recordRoutes.routes())
|
||||||
|
router.use(recordRoutes.allowedMethods())
|
||||||
|
|
||||||
router.use(userRoutes.routes())
|
router.use(userRoutes.routes())
|
||||||
router.use(userRoutes.allowedMethods())
|
router.use(userRoutes.allowedMethods())
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ const instanceRoutes = require("./instance")
|
||||||
const clientRoutes = require("./client")
|
const clientRoutes = require("./client")
|
||||||
const applicationRoutes = require("./application")
|
const applicationRoutes = require("./application")
|
||||||
const modelRoutes = require("./model")
|
const modelRoutes = require("./model")
|
||||||
|
const recordRoutes = require("./record")
|
||||||
const viewRoutes = require("./view")
|
const viewRoutes = require("./view")
|
||||||
const staticRoutes = require("./static")
|
const staticRoutes = require("./static")
|
||||||
const componentRoutes = require("./component")
|
const componentRoutes = require("./component")
|
||||||
|
@ -18,6 +19,7 @@ module.exports = {
|
||||||
instanceRoutes,
|
instanceRoutes,
|
||||||
clientRoutes,
|
clientRoutes,
|
||||||
applicationRoutes,
|
applicationRoutes,
|
||||||
|
recordRoutes,
|
||||||
modelRoutes,
|
modelRoutes,
|
||||||
viewRoutes,
|
viewRoutes,
|
||||||
staticRoutes,
|
staticRoutes,
|
||||||
|
|
|
@ -1,46 +1,10 @@
|
||||||
const Router = require("@koa/router")
|
const Router = require("@koa/router")
|
||||||
const modelController = require("../controllers/model")
|
const modelController = require("../controllers/model")
|
||||||
const recordController = require("../controllers/record")
|
|
||||||
const authorized = require("../../middleware/authorized")
|
const authorized = require("../../middleware/authorized")
|
||||||
const {
|
const { BUILDER } = require("../../utilities/accessLevels")
|
||||||
READ_MODEL,
|
|
||||||
WRITE_MODEL,
|
|
||||||
BUILDER,
|
|
||||||
} = require("../../utilities/accessLevels")
|
|
||||||
|
|
||||||
const router = Router()
|
const router = Router()
|
||||||
|
|
||||||
// records
|
|
||||||
|
|
||||||
router
|
|
||||||
.get(
|
|
||||||
"/api/:instanceId/:modelId/records",
|
|
||||||
authorized(READ_MODEL, ctx => ctx.params.modelId),
|
|
||||||
recordController.fetchModel
|
|
||||||
)
|
|
||||||
.get(
|
|
||||||
"/api/:instanceId/:modelId/records/:recordId",
|
|
||||||
authorized(READ_MODEL, ctx => ctx.params.modelId),
|
|
||||||
recordController.find
|
|
||||||
)
|
|
||||||
.post(
|
|
||||||
"/api/:instanceId/:modelId/records",
|
|
||||||
authorized(WRITE_MODEL, ctx => ctx.params.modelId),
|
|
||||||
recordController.save
|
|
||||||
)
|
|
||||||
.post(
|
|
||||||
"/api/:instanceId/:modelId/records/validate",
|
|
||||||
authorized(WRITE_MODEL, ctx => ctx.params.modelId),
|
|
||||||
recordController.validate
|
|
||||||
)
|
|
||||||
.delete(
|
|
||||||
"/api/:instanceId/:modelId/records/:recordId/:revId",
|
|
||||||
authorized(WRITE_MODEL, ctx => ctx.params.modelId),
|
|
||||||
recordController.destroy
|
|
||||||
)
|
|
||||||
|
|
||||||
// models
|
|
||||||
|
|
||||||
router
|
router
|
||||||
.get("/api/:instanceId/models", authorized(BUILDER), modelController.fetch)
|
.get("/api/:instanceId/models", authorized(BUILDER), modelController.fetch)
|
||||||
.get("/api/:instanceId/models/:id", authorized(BUILDER), modelController.find)
|
.get("/api/:instanceId/models/:id", authorized(BUILDER), modelController.find)
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
const Router = require("@koa/router")
|
||||||
|
const recordController = require("../controllers/record")
|
||||||
|
const authorized = require("../../middleware/authorized")
|
||||||
|
const { READ_MODEL, WRITE_MODEL } = require("../../utilities/accessLevels")
|
||||||
|
|
||||||
|
const router = Router()
|
||||||
|
|
||||||
|
router
|
||||||
|
.get(
|
||||||
|
"/api/:instanceId/:modelId/records",
|
||||||
|
authorized(READ_MODEL, ctx => ctx.params.modelId),
|
||||||
|
recordController.fetchModelRecords
|
||||||
|
)
|
||||||
|
.get(
|
||||||
|
"/api/:instanceId/:modelId/records/:recordId",
|
||||||
|
authorized(READ_MODEL, ctx => ctx.params.modelId),
|
||||||
|
recordController.find
|
||||||
|
)
|
||||||
|
.post("/api/:instanceId/records/search", recordController.search)
|
||||||
|
.post(
|
||||||
|
"/api/:instanceId/:modelId/records",
|
||||||
|
authorized(WRITE_MODEL, ctx => ctx.params.modelId),
|
||||||
|
recordController.save
|
||||||
|
)
|
||||||
|
.post(
|
||||||
|
"/api/:instanceId/:modelId/records/validate",
|
||||||
|
authorized(WRITE_MODEL, ctx => ctx.params.modelId),
|
||||||
|
recordController.validate
|
||||||
|
)
|
||||||
|
.delete(
|
||||||
|
"/api/:instanceId/:modelId/records/:recordId/:revId",
|
||||||
|
authorized(WRITE_MODEL, ctx => ctx.params.modelId),
|
||||||
|
recordController.destroy
|
||||||
|
)
|
||||||
|
|
||||||
|
module.exports = router
|
|
@ -97,7 +97,6 @@ describe("/models", () => {
|
||||||
instanceId: instance._id,
|
instanceId: instance._id,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("destroy", () => {
|
describe("destroy", () => {
|
||||||
|
|
|
@ -110,6 +110,30 @@ describe("/records", () => {
|
||||||
expect(res.body.find(r => r.name === record.name)).toBeDefined()
|
expect(res.body.find(r => r.name === record.name)).toBeDefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("lists records when queried by their ID", async () => {
|
||||||
|
const newRecord = {
|
||||||
|
modelId: model._id,
|
||||||
|
name: "Second Contact",
|
||||||
|
status: "new"
|
||||||
|
}
|
||||||
|
const record = await createRecord()
|
||||||
|
const secondRecord = await createRecord(newRecord)
|
||||||
|
|
||||||
|
const recordIds = [record.body._id, secondRecord.body._id]
|
||||||
|
|
||||||
|
const res = await request
|
||||||
|
.post(`/api/${instance._id}/records/search`)
|
||||||
|
.set(defaultHeaders)
|
||||||
|
.send({
|
||||||
|
keys: recordIds
|
||||||
|
})
|
||||||
|
.expect('Content-Type', /json/)
|
||||||
|
.expect(200)
|
||||||
|
|
||||||
|
expect(res.body.length).toBe(2)
|
||||||
|
expect(res.body.map(response => response._id)).toEqual(expect.arrayContaining(recordIds))
|
||||||
|
})
|
||||||
|
|
||||||
it("load should return 404 when record does not exist", async () => {
|
it("load should return 404 when record does not exist", async () => {
|
||||||
await createRecord()
|
await createRecord()
|
||||||
await request
|
await request
|
||||||
|
|
Loading…
Reference in New Issue