backend/app/controllers/api/v1/Page.controller.js
garrettmills 9beb0d2c3a
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing
Add endpoint for saving a single node to a page
2020-10-14 09:23:44 -05:00

305 lines
9.7 KiB
JavaScript

const Controller = require('libflitter/controller/Controller')
const PageModel = require('../../../models/api/Page.model')
const Node = require('../../../models/api/Node.model')
/*
* Page Controller
* -------------------------------------------------------------
* Put some description here!
*/
class Page extends Controller {
async get_page(req, res) {
const PageId = req.params.PageId
const user = req.user
const page = await PageModel.findOne({UUID: PageId})
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
if ( !(await page.is_accessible_by(user)) ) return req.security.deny()
page.level = await page.access_level_for(req.user)
return res.api(page)
}
async save_page(req, res) {
const PageId = req.params.PageId
let page;
if ( PageId ) {
page = await PageModel.findOne({UUID: PageId})
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
if ( !(await page.is_accessible_by(req.user, 'update')) ) return req.security.deny()
} else {
page = new PageModel
page.CreatedUserId = req.user.id
page.OrgUserId = req.user._id
}
if ( !req.body.Name ) return res.status(400).message('Missing required: Name').api({})
page.Name = req.body.Name
if ( 'IsPublic' in req.body ) {
page.IsPublic = !!req.body.IsPublic
}
if ( 'IsVisibleInMenu' in req.body ) {
page.IsVisibleInMenu = !!req.body.IsVisibleInMenu
}
/*let parent;
if ( !req.body.ParentId ) return res.status(400).message('Missing required: ParentId').api({})
else {
parent = await PageModel.findOne({UUID: req.body.ParentId})
if ( !parent ) return res.status(404).message('Parent page not found with that ID.').api({})
if ( !(await parent.is_accessible_by(req.user, 'update')) ) return req.security.kickout()
}*/ // TODO re-implement this to account for sharing
page.UpdatedAt = new Date
page.UpdateUserId = req.user._id
await page.save()
return res.api(page)
}
async get_nodes(req, res) {
const PageId = req.params.PageId
let page;
if ( PageId ) {
page = await PageModel.findOne({UUID: PageId})
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
if ( !(await page.is_accessible_by(req.user)) ) return req.security.deny()
}
const nodes = await Node.find({PageId: page.UUID});
const assoc_nodes = {}
nodes.forEach(node => {
assoc_nodes[node.UUID] = node
})
const return_nodes = [];
for ( const uuid of page.NodeIds ) {
return_nodes.push(assoc_nodes[uuid])
}
return res.api(return_nodes);
}
async save_node_to_page(req, res) {
const PageId = req.params.PageId
const page = await PageModel.findOne({UUID: PageId})
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
if ( !(await page.is_accessible_by(req.user, 'update')) ) return req.security.deny()
const nodes = await Node.find({PageId: page.UUID})
const assoc_nodes = {}
nodes.forEach(node => {
assoc_nodes[node.UUID] = node
})
if ( !req.body.nodeData ) {
return res.status(400).message('Missing nodeData body').api()
}
const node = req.body.nodeData;
if ( node.UUID && assoc_nodes[node.UUID] ) {
assoc_nodes[node.UUID].update_from_raw(node)
assoc_nodes[node.UUID].UpdatedAt = new Date
assoc_nodes[node.UUID].UpdateUserId = req.user._id
assoc_nodes[node.UUID].save()
return res.api(assoc_nodes[node.UUID])
} else {
const node_obj = new Node({
Type: node.Type,
Value: node.Value,
PageId,
CreatedUserId: req.user._id,
UpdateUserId: req.user._id,
})
await node_obj.save()
page.NodeIds.push(node_obj.UUID);
await page.save();
return res.api(node_obj)
}
}
async save_nodes(req, res) {
const PageId = req.params.PageId
let page;
if ( PageId ) {
page = await PageModel.findOne({UUID: PageId})
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
if ( !(await page.is_accessible_by(req.user, 'update')) ) return req.security.deny()
}
const nodes = await Node.find({PageId: page.UUID})
const assoc_nodes = {}
nodes.forEach(node => {
assoc_nodes[node.UUID] = node
})
if ( !Array.isArray(req.body) ) return res.status(400).message('Invalid request body. Should be array of nodes.').api({})
const updated_node_ids = []
for ( let node of req.body ) {
if ( node.UUID && Object.keys(assoc_nodes).includes(node.UUID) ) updated_node_ids.push(node.UUID)
}
const updated_nodes = []
for ( let node of req.body ) {
if ( node.UUID && assoc_nodes[node.UUID] ) {
assoc_nodes[node.UUID].update_from_raw(node)
assoc_nodes[node.UUID].UpdatedAt = new Date
assoc_nodes[node.UUID].UpdateUserId = req.user._id
updated_nodes.push(assoc_nodes[node.UUID])
} else {
const node_obj = new Node({
Type: node.Type,
Value: node.Value,
PageId,
CreatedUserId: req.user._id,
UpdateUserId: req.user._id,
})
await node_obj.save()
updated_nodes.push(node_obj)
}
}
// console.log('updated nodes', updated_nodes)
let delete_nodes = nodes
for ( const node of updated_nodes ) {
await node.save()
delete_nodes = delete_nodes.filter(n => {
return n.UUID !== node.UUID
})
}
for ( const node of delete_nodes ) {
await node.delete()
}
page.NodeIds = updated_nodes.map(x => x.UUID)
await page.save()
res.api(updated_nodes)
// Step 1: make sure the updated nodes are updated
// Make sure the deleted nodes are deleted
// Make sure the new nodes are created
// Make sure the order is correct
}
async create_top_level(req, res) {
if ( !req.body.name ) {
return res.status(400).message('Missing required field: name').api({})
}
const root_page = await req.user.get_root_page()
const new_page = new PageModel({
Name: req.body.name,
OrgUserId: req.user._id,
ParentId: root_page.UUID,
CreatedUserId: req.user.id,
UpdateUserId: req.user.id
})
await new_page.save()
root_page.ChildPageIds.push(new_page.UUID)
await root_page.save()
req.user.allow(`page:${new_page.UUID}`)
await req.user.save()
const starter_node = new Node({
Type: 'paragraph',
Value: {
Value: 'Click to edit...',
},
PageId: new_page.UUID,
CreatedUserId: req.user.id,
UpdateUserId: req.user.id
})
await starter_node.save()
new_page.NodeIds.push(starter_node.UUID)
await new_page.save()
return res.api(new_page)
}
async create_child(req, res) {
if ( !req.body.name ) {
return res.status(400).message('Missing required field: name').api({})
}
if ( !req.body.parentId ) {
return res.status(400).message('Missing required field: parentId').api({})
}
// Get the parent
const parent = await PageModel.findOne({UUID: req.body.parentId})
if ( !parent ) {
return res.status(404).message('Unable to find parent with that ID.').api({})
} else if ( !(await parent.is_accessible_by(req.user, 'manage')) ) {
return res.security.deny()
}
const new_page = new PageModel({
Name: req.body.name,
OrgUserId: req.user._id,
ParentId: parent.UUID,
CreatedUserId: req.user.id,
UpdateUserId: req.user.id,
})
await new_page.save()
parent.ChildPageIds.push(new_page.UUID)
await parent.save()
req.user.allow(`page:${new_page.UUID}`)
await req.user.save()
const starter_node = new Node({
Type: 'paragraph',
Value: {
Value: 'Click to edit...',
},
PageId: new_page.UUID,
CreatedUserId: req.user.id,
UpdateUserId: req.user.id
})
await starter_node.save()
new_page.NodeIds.push(starter_node.UUID)
await new_page.save()
return res.api(new_page)
}
async delete_page(req, res) {
const PageId = req.params.PageId
let page;
if ( PageId ) {
page = await PageModel.findOne({UUID: PageId})
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
if ( !(await page.is_accessible_by(req.user, 'manage')) ) return req.security.deny()
if ( page.ParentId === '0' ) return req.security.kickout()
}
page.Active = false
page.DeletedAt = new Date
await page.save()
return res.api({})
}
}
module.exports = exports = Page