From e38dd9e2759b36b385eed3c6848bfa4eae21a059 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Tue, 30 Jan 2024 18:13:20 +0400 Subject: [PATCH] Find projects by identity --- backend/src/services/project/project-dal.ts | 59 ++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/backend/src/services/project/project-dal.ts b/backend/src/services/project/project-dal.ts index a82fbc3af..87a10b2e7 100644 --- a/backend/src/services/project/project-dal.ts +++ b/backend/src/services/project/project-dal.ts @@ -30,7 +30,7 @@ export const projectDALFactory = (db: TDbClient) => { db.ref("name").withSchema(TableName.Environment).as("envName") ) .orderBy("createdAt", "asc", "last"); - return sqlNestRelationships({ + const nestedWorkspaces = sqlNestRelationships({ data: workspaces, key: "id", parentMapper: ({ _id, ...el }) => ({ _id, ...ProjectsSchema.parse(el) }), @@ -46,11 +46,67 @@ export const projectDALFactory = (db: TDbClient) => { } ] }); + + return nestedWorkspaces.map((workspace) => ({ + ...workspace, + organization: workspace.id + })); } catch (error) { throw new DatabaseError({ error, name: "Find all projects" }); } }; + const findAllProjectsByIdentity = async (identityId: string) => { + try { + const workspaces = await db(TableName.IdentityProjectMembership) + .where({ identityId }) + .join( + TableName.Project, + `${TableName.IdentityProjectMembership}.projectId`, + `${TableName.Project}.id` + ) + .leftJoin( + TableName.Environment, + `${TableName.Environment}.projectId`, + `${TableName.Project}.id` + ) + .select( + selectAllTableCols(TableName.Project), + db.ref("id").withSchema(TableName.Project).as("_id"), + db.ref("id").withSchema(TableName.Environment).as("envId"), + db.ref("slug").withSchema(TableName.Environment).as("envSlug"), + db.ref("name").withSchema(TableName.Environment).as("envName") + ) + .orderBy("createdAt", "asc", "last"); + + const nestedWorkspaces = sqlNestRelationships({ + data: workspaces, + key: "id", + parentMapper: ({ _id, ...el }) => ({ _id, ...ProjectsSchema.parse(el) }), + childrenMapper: [ + { + key: "envId", + label: "environments" as const, + mapper: ({ envId: id, envSlug: slug, envName: name }) => ({ + id, + slug, + name + }) + } + ] + }); + + // we need to add "organization" to each workspace entry + + return nestedWorkspaces.map((workspace) => ({ + ...workspace, + organization: workspace.id + })); + } catch (error) { + throw new DatabaseError({ error, name: "Find all projects by identity" }); + } + }; + const findProjectById = async (id: string) => { try { const workspaces = await db(TableName.ProjectMembership) @@ -96,6 +152,7 @@ export const projectDALFactory = (db: TDbClient) => { return { ...projectOrm, findAllProjects, + findAllProjectsByIdentity, findProjectById }; };