Task #21 - add page controller and page/node api endpoints
parent
7c8bcd928f
commit
617a77b809
@ -0,0 +1,43 @@
|
||||
const Middleware = require('libflitter/middleware/Middleware')
|
||||
|
||||
/*
|
||||
* GuaranteeRootNode Middleware
|
||||
* -------------------------------------------------------------
|
||||
* Put some description here!
|
||||
*/
|
||||
class GuaranteeRootNode extends Middleware {
|
||||
static get services() {
|
||||
return [...super.services, 'models']
|
||||
}
|
||||
|
||||
/*
|
||||
* Run the middleware test.
|
||||
* This method is required by all Flitter middleware.
|
||||
* It should either call the next function in the stack,
|
||||
* or it should handle the response accordingly.
|
||||
*/
|
||||
async test(req, res, next, args = {}){
|
||||
const Page = this.models.get('api:Page')
|
||||
if ( req.user ) {
|
||||
const root_page = await Page.findOne({ OrgUserId: req.user._id, ParentId: "0" })
|
||||
if ( !root_page ) {
|
||||
const new_page = new Page({
|
||||
Name: `${req.user.uid} virtual root`,
|
||||
OrgUserId: req.user._id,
|
||||
ParentId: "0",
|
||||
CreatedUserId: req.user.id,
|
||||
UpdateUserId: req.user.id,
|
||||
})
|
||||
|
||||
await new_page.save()
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Call the next function in the stack.
|
||||
*/
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = GuaranteeRootNode
|
Loading…
Reference in new issue