Finishing up routing structure, now available on /api/routing.

This commit is contained in:
mike12345567 2020-11-17 18:12:21 +00:00
parent e793c46097
commit 3252b2d630
1 changed files with 33 additions and 2 deletions

View File

@ -2,8 +2,38 @@ const { getRoutingInfo } = require("../../utilities/routing")
const { AccessController } = require("../../utilities/security/accessLevels") const { AccessController } = require("../../utilities/security/accessLevels")
async function getRoutingStructure(appId) { async function getRoutingStructure(appId) {
let baseRouting = await getRoutingInfo(appId) const screenRoutes = await getRoutingInfo(appId)
return baseRouting const routing = {}
for (let screenRoute of screenRoutes) {
const fullpath = screenRoute.routing.route
// replace the first value with the home route
const subpaths = ["/"].concat(fullpath.split("/").splice(1))
// special case for when it is simply the home route "/", this creates a weird scenario
if (subpaths[1] === "") {
subpaths.splice(1, 1)
}
const accessLevel = screenRoute.routing.accessLevelId
// iterate through the tree initially to flesh out all the required subpaths
let currentPath = routing,
nextSubpath = routing
for (let subpath of subpaths) {
if (!nextSubpath[subpath]) {
nextSubpath[subpath] = {
subpaths: {},
}
}
currentPath = nextSubpath ? nextSubpath : currentPath[subpath]
nextSubpath = currentPath[subpath].subpaths
}
const correctPath = currentPath[subpaths[subpaths.length - 1]]
if (!correctPath.screens) {
correctPath.screens = {}
}
correctPath.screens[accessLevel] = screenRoute.id
correctPath.fullpath = fullpath
}
return { routes: routing }
} }
exports.fetch = async ctx => { exports.fetch = async ctx => {
@ -14,4 +44,5 @@ exports.clientFetch = async ctx => {
const routing = getRoutingStructure(ctx.appId) const routing = getRoutingStructure(ctx.appId)
// use the access controller to pick which access level is applicable to this user // use the access controller to pick which access level is applicable to this user
const accessController = new AccessController(ctx.appId) const accessController = new AccessController(ctx.appId)
// TODO: iterate through the routes and pick which the user can access
} }