From 48fe2c7757e0ad041c90606540395dc4a70df9c4 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Mon, 16 Nov 2020 22:49:19 -0600 Subject: [PATCH] Initial changes to support forms & form builder --- app/controllers/api/v1/Menu.controller.js | 15 ++++++++------- app/controllers/api/v1/Page.controller.js | 20 +++++++++++++++++++- app/enum.js | 11 +++++++++++ app/models/api/Node.model.js | 2 ++ app/models/api/Page.model.js | 2 ++ app/models/api/db/ColumnDef.model.js | 1 + 6 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 app/enum.js diff --git a/app/controllers/api/v1/Menu.controller.js b/app/controllers/api/v1/Menu.controller.js index 076d58b..2b358f1 100644 --- a/app/controllers/api/v1/Menu.controller.js +++ b/app/controllers/api/v1/Menu.controller.js @@ -1,4 +1,5 @@ const Controller = require('libflitter/controller/Controller') +const { PageType } = require('../../../enum') /* * Menu Controller @@ -26,7 +27,7 @@ class Menu extends Controller { noDelete: true, noChildren: true, virtual: true, - type: 'branch', + type: PageType.Branch, }) // Get view only shared trees @@ -39,7 +40,7 @@ class Menu extends Controller { name: tree.Name, children: await this._build_secure_menu_object(tree, req.user), level: await tree.access_level_for(req.user), - type: 'page', + type: tree.PageType || PageType.Note, }) } @@ -53,7 +54,7 @@ class Menu extends Controller { name: tree.Name, children: await this._build_secure_menu_object(tree, req.user), level: await tree.access_level_for(req.user), - type: 'page', + type: tree.PageType || PageType.Note, }) } @@ -67,7 +68,7 @@ class Menu extends Controller { name: tree.Name, children: await this._build_secure_menu_object(tree, req.user), level: await tree.access_level_for(req.user), - type: 'page', + type: tree.PageType || PageType.Note, }) } @@ -78,7 +79,7 @@ class Menu extends Controller { noDelete: true, noChildren: true, virtual: true, - type: 'branch', + type: PageType.Branch, }) return res.api(menu) @@ -93,7 +94,7 @@ class Menu extends Controller { name: child.is_shared() ? child.Name + ' ⁽ˢʰᵃʳᵉᵈ⁾' : child.Name, shared: child.is_shared(), children: [...(await child.get_menu_items(page_only)), ...(await this._build_menu_object(child, [], page_only))], - type: 'page', + type: child.PageType || PageType.Note, }) } } @@ -111,7 +112,7 @@ class Menu extends Controller { name: child.Name, children: await this._build_secure_menu_object(child, user), level: await child.access_level_for(user), - type: 'page', + type: child.PageType || PageType.Note, }) } } diff --git a/app/controllers/api/v1/Page.controller.js b/app/controllers/api/v1/Page.controller.js index 5b1c291..5cd9095 100644 --- a/app/controllers/api/v1/Page.controller.js +++ b/app/controllers/api/v1/Page.controller.js @@ -1,6 +1,7 @@ const Controller = require('libflitter/controller/Controller') const PageModel = require('../../../models/api/Page.model') const Node = require('../../../models/api/Node.model') +const { PageType } = require('../../../enum') /* * Page Controller @@ -267,13 +268,21 @@ class Page extends Controller { } const root_page = await req.user.get_root_page() + const page_type = req.body.pageType || PageType.Note + + if ( !PageType._isValid(page_type) ) { + return res.status(400) + .message('Invalid PageType.') + .api() + } 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 + UpdateUserId: req.user.id, + PageType: page_type, }) if ( req.body.UUID ) { @@ -327,12 +336,21 @@ class Page extends Controller { return res.security.deny() } + const page_type = req.body.pageType || PageType.Note + + if ( !PageType._isValid(page_type) ) { + return res.status(400) + .message('Invalid PageType.') + .api() + } + const new_page = new PageModel({ Name: req.body.name, OrgUserId: req.user._id, ParentId: parent.UUID, CreatedUserId: req.user.id, UpdateUserId: req.user.id, + PageType: page_type, }) if ( req.body.UUID ) { diff --git a/app/enum.js b/app/enum.js new file mode 100644 index 0000000..4992ed9 --- /dev/null +++ b/app/enum.js @@ -0,0 +1,11 @@ +module.exports = { + PageType: { + Note: 'page', + Form: 'form', + Branch: 'branch', + + _isValid(value) { + return ['page', 'form', 'branch'].includes(value) + } + } +} diff --git a/app/models/api/Node.model.js b/app/models/api/Node.model.js index fd1b305..c1d943b 100644 --- a/app/models/api/Node.model.js +++ b/app/models/api/Node.model.js @@ -21,6 +21,7 @@ class Node extends VersionedModel { Mode: {type: String, default: 'norm'}, Value: String, }, + AdditionalData: Object, PageId: String, CreatedAt: { type: Date, default: () => new Date }, UpdatedAt: { type: Date, default: () => new Date }, @@ -57,6 +58,7 @@ class Node extends VersionedModel { update_from_raw(data) { if ( data.Type ) this.Type = data.Type if ( data.Value ) this.Value = data.Value + if ( data.AdditionalData ) this.AdditionalData = data.AdditionalData; } async cast_to_version_data() { diff --git a/app/models/api/Page.model.js b/app/models/api/Page.model.js index d3cbc0f..a47154a 100644 --- a/app/models/api/Page.model.js +++ b/app/models/api/Page.model.js @@ -3,6 +3,7 @@ const { ObjectId } = require('mongodb') const uuid = require('uuid/v4') const ActiveScope = require('../scopes/Active.scope') +const { PageType } = require('../../enum'); /* * Page Model @@ -31,6 +32,7 @@ class Page extends VersionedModel { CreatedUserId: {type: String}, UpdateUserId: {type: String}, ChildPageIds: [String], + PageType: {type: String, default: PageType.Note}, // PageType // Menu flags noDelete: { type: Boolean, default: false }, diff --git a/app/models/api/db/ColumnDef.model.js b/app/models/api/db/ColumnDef.model.js index d008390..c13c544 100644 --- a/app/models/api/db/ColumnDef.model.js +++ b/app/models/api/db/ColumnDef.model.js @@ -39,6 +39,7 @@ class ColumnDef extends VersionedModel { return { name: this.headerName, uuid: this.UUID, + field: this.field, database_id: this.DatabaseId, type: this.Type, metadata: this.data(),