From e20a26a84dba98885f34fcf629e452743d62960a Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 17 Nov 2020 18:12:21 +0000 Subject: [PATCH] Finishing up routing structure, now available on /api/routing. --- .../server/src/api/controllers/routing.js | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/packages/server/src/api/controllers/routing.js b/packages/server/src/api/controllers/routing.js index 3be2e3fd41..bd5d991dfa 100644 --- a/packages/server/src/api/controllers/routing.js +++ b/packages/server/src/api/controllers/routing.js @@ -2,8 +2,38 @@ const { getRoutingInfo } = require("../../utilities/routing") const { AccessController } = require("../../utilities/security/accessLevels") async function getRoutingStructure(appId) { - let baseRouting = await getRoutingInfo(appId) - return baseRouting + const screenRoutes = await getRoutingInfo(appId) + 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 => { @@ -14,4 +44,5 @@ exports.clientFetch = async ctx => { const routing = getRoutingStructure(ctx.appId) // use the access controller to pick which access level is applicable to this user const accessController = new AccessController(ctx.appId) + // TODO: iterate through the routes and pick which the user can access }