From da2dcb347af0cd004a58a5d01a66875e80f7d669 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 1 Apr 2024 09:58:13 -0700 Subject: [PATCH] Fix: Restructured recursive path functions as suggested by Akhil --- backend/src/services/secret/secret-fns.ts | 101 +++++++++++----------- 1 file changed, 51 insertions(+), 50 deletions(-) diff --git a/backend/src/services/secret/secret-fns.ts b/backend/src/services/secret/secret-fns.ts index e7e7374d0..24c2bd811 100644 --- a/backend/src/services/secret/secret-fns.ts +++ b/backend/src/services/secret/secret-fns.ts @@ -74,62 +74,61 @@ type TGetPathsDTO = { interface FolderMap { [parentId: string]: TSecretFolders[]; } +const buildHierarchy = (folders: TSecretFolders[]): FolderMap => { + const map: FolderMap = {}; + map.null = []; // Initialize mapping for root directory + + folders.forEach((folder) => { + const parentId = folder.parentId || "null"; + if (!map[parentId]) { + map[parentId] = []; + } + map[parentId].push(folder); + }); + + return map; +}; + +const generatePaths = ( + map: FolderMap, + parentId: string = "null", + basePath: string = "" +): { path: string; folderId: string }[] => { + const children = map[parentId || "null"] || []; + let paths: { path: string; folderId: string }[] = []; + + children.forEach((child) => { + // Determine if this is the root folder of the environment. If no parentId is present and the name is root, it's the root folder + const isRootFolder = child.name === "root" && !child.parentId; + + // Form the current path based on the base path and the current child + // eslint-disable-next-line no-nested-ternary + const currPath = basePath === "" ? (isRootFolder ? "/" : `/${child.name}`) : `${basePath}/${child.name}`; + + paths.push({ + path: currPath, + folderId: child.id + }); // Add the current path + + // Recursively generate paths for children, passing down the formatted pathh + const childPaths = generatePaths(map, child.id, currPath); + paths = paths.concat( + childPaths.map((p) => ({ + path: p.path, + folderId: p.folderId + })) + ); + }); + + return paths; +}; export const recursivelyGetSecretPaths = ({ folderDAL, projectEnvDAL, permissionService }: TRecursivelyFetchSecretsFromFoldersArg) => { - const buildHierarchy = (folders: TSecretFolders[]): FolderMap => { - const map: FolderMap = {}; - map.null = []; // Initialize mapping for root directory - - folders.forEach((folder) => { - const parentId = folder.parentId || "null"; - if (!map[parentId]) { - map[parentId] = []; - } - map[parentId].push(folder); - }); - - return map; - }; - - const generatePaths = ( - map: FolderMap, - parentId: string = "null", - basePath: string = "" - ): { path: string; folderId: string }[] => { - const children = map[parentId || "null"] || []; - let paths: { path: string; folderId: string }[] = []; - - children.forEach((child) => { - // Determine if this is the root folder of the environment. If no parentId is present and the name is root, it's the root folder - const isRootFolder = child.name === "root" && !child.parentId; - - // Form the current path based on the base path and the current child - // eslint-disable-next-line no-nested-ternary - const currPath = basePath === "" ? (isRootFolder ? "/" : `/${child.name}`) : `${basePath}/${child.name}`; - - paths.push({ - path: currPath, - folderId: child.id - }); // Add the current path - - // Recursively generate paths for children, passing down the formatted pathh - const childPaths = generatePaths(map, child.id, currPath); - paths = paths.concat( - childPaths.map((p) => ({ - path: p.path, - folderId: p.folderId - })) - ); - }); - - return paths; - }; - - return async ({ projectId, environment, currentPath, auth }: TGetPathsDTO) => { + const getPaths = async ({ projectId, environment, currentPath, auth }: TGetPathsDTO) => { const env = await projectEnvDAL.findOne({ projectId, slug: environment @@ -175,6 +174,8 @@ export const recursivelyGetSecretPaths = ({ return allowedPaths; }; + + return getPaths; }; type TInterpolateSecretArg = {