From 335f3f7d372156c91e6f47a9c1418c88d9f0eede Mon Sep 17 00:00:00 2001 From: Sheen Capadngan Date: Tue, 17 Sep 2024 18:52:30 +0800 Subject: [PATCH] misc: removed hacky approach --- .../group-project/group-project-dal.ts | 112 ++---------------- .../group-project/group-project-service.ts | 10 +- 2 files changed, 10 insertions(+), 112 deletions(-) diff --git a/backend/src/services/group-project/group-project-dal.ts b/backend/src/services/group-project/group-project-dal.ts index 8370ff881..05724df74 100644 --- a/backend/src/services/group-project/group-project-dal.ts +++ b/backend/src/services/group-project/group-project-dal.ts @@ -10,113 +10,15 @@ export type TGroupProjectDALFactory = ReturnType; export const groupProjectDALFactory = (db: TDbClient) => { const groupProjectOrm = ormify(db, TableName.GroupProjectMembership); - const findByGroupSlugAndProject = async ( - { groupSlug, projectId }: { groupSlug: string; projectId: string }, - tx?: Knex - ) => { - try { - // this is a workaround so that the order of group roles is preserved as a necessary - // requirement for our terraform provider - - /* Approach: - 1. First we query the group project membership along with the attached roles - 2. Then we have a separate query to fetch the custom role details which we manually augment below - */ - const rawGroupResult = await (tx || db.replicaNode())(TableName.GroupProjectMembership) - .where(`${TableName.GroupProjectMembership}.projectId`, projectId) - .where(`${TableName.Groups}.slug`, "=", groupSlug) - .join(TableName.Groups, `${TableName.GroupProjectMembership}.groupId`, `${TableName.Groups}.id`) - .join( - TableName.GroupProjectMembershipRole, - `${TableName.GroupProjectMembershipRole}.projectMembershipId`, - `${TableName.GroupProjectMembership}.id` - ) - .select( - db.ref("id").withSchema(TableName.GroupProjectMembership), - db.ref("createdAt").withSchema(TableName.GroupProjectMembership), - db.ref("updatedAt").withSchema(TableName.GroupProjectMembership), - db.ref("id").as("groupId").withSchema(TableName.Groups), - db.ref("name").as("groupName").withSchema(TableName.Groups), - db.ref("slug").as("groupSlug").withSchema(TableName.Groups), - db.ref("id").withSchema(TableName.GroupProjectMembership), - db.ref("role").withSchema(TableName.GroupProjectMembershipRole), - db.ref("id").withSchema(TableName.GroupProjectMembershipRole).as("membershipRoleId"), - db.ref("customRoleId").withSchema(TableName.GroupProjectMembershipRole), - db.ref("temporaryMode").withSchema(TableName.GroupProjectMembershipRole), - db.ref("isTemporary").withSchema(TableName.GroupProjectMembershipRole), - db.ref("temporaryRange").withSchema(TableName.GroupProjectMembershipRole), - db.ref("temporaryAccessStartTime").withSchema(TableName.GroupProjectMembershipRole), - db.ref("temporaryAccessEndTime").withSchema(TableName.GroupProjectMembershipRole) - ); - - // IMPORTANT NOTE: we do this separately because this breaks the order of the group project roles - const customRoleDetails = await (tx || db.replicaNode())(TableName.ProjectRoles) - .whereIn("id", rawGroupResult.map((entry) => entry.customRoleId) as string[]) - .select("id", "slug", "name"); - - const groupProjectMembershipRoleToCustomRole: Record = {}; - customRoleDetails.forEach( - // eslint-disable-next-line no-return-assign - (entry) => - (groupProjectMembershipRoleToCustomRole[entry.id] = { - name: entry.name, - slug: entry.slug - }) - ); - - const members = sqlNestRelationships({ - data: rawGroupResult, - parentMapper: ({ groupId, groupName, id, createdAt, updatedAt }) => ({ - id, - groupId, - createdAt, - updatedAt, - group: { - id: groupId, - name: groupName, - slug: groupSlug - } - }), - key: "id", - childrenMapper: [ - { - label: "roles" as const, - key: "membershipRoleId", - mapper: ({ - role, - customRoleId, - membershipRoleId, - temporaryRange, - temporaryMode, - temporaryAccessEndTime, - temporaryAccessStartTime, - isTemporary - }) => ({ - id: membershipRoleId, - role, - customRoleId, - customRoleName: customRoleId && groupProjectMembershipRoleToCustomRole[customRoleId]?.name, - customRoleSlug: customRoleId && groupProjectMembershipRoleToCustomRole[customRoleId]?.slug, - temporaryRange, - temporaryMode, - temporaryAccessEndTime, - temporaryAccessStartTime, - isTemporary - }) - } - ] - }); - - return members[0]; - } catch (error) { - throw new DatabaseError({ error, name: "FindByGroupSlugAndProject" }); - } - }; - - const findByProjectId = async (projectId: string, tx?: Knex) => { + const findByProjectId = async (projectId: string, filter?: { groupSlug?: string }, tx?: Knex) => { try { const docs = await (tx || db.replicaNode())(TableName.GroupProjectMembership) .where(`${TableName.GroupProjectMembership}.projectId`, projectId) + .where((qb) => { + if (filter?.groupSlug) { + void qb.where(`${TableName.Groups}.slug`, "=", filter.groupSlug); + } + }) .join(TableName.Groups, `${TableName.GroupProjectMembership}.groupId`, `${TableName.Groups}.id`) .join( TableName.GroupProjectMembershipRole, @@ -324,5 +226,5 @@ export const groupProjectDALFactory = (db: TDbClient) => { return members; }; - return { ...groupProjectOrm, findByProjectId, findByUserId, findAllProjectGroupMembers, findByGroupSlugAndProject }; + return { ...groupProjectOrm, findByProjectId, findByUserId, findAllProjectGroupMembers }; }; diff --git a/backend/src/services/group-project/group-project-service.ts b/backend/src/services/group-project/group-project-service.ts index ebf8025ef..b8195cf53 100644 --- a/backend/src/services/group-project/group-project-service.ts +++ b/backend/src/services/group-project/group-project-service.ts @@ -28,10 +28,7 @@ import { } from "./group-project-types"; type TGroupProjectServiceFactoryDep = { - groupProjectDAL: Pick< - TGroupProjectDALFactory, - "findOne" | "transaction" | "create" | "delete" | "findByProjectId" | "findByGroupSlugAndProject" - >; + groupProjectDAL: Pick; groupProjectMembershipRoleDAL: Pick< TGroupProjectMembershipRoleDALFactory, "create" | "transaction" | "insertMany" | "delete" @@ -401,9 +398,8 @@ export const groupProjectServiceFactory = ({ ); ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Read, ProjectPermissionSub.Groups); - const groupMembership = await groupProjectDAL.findByGroupSlugAndProject({ - groupSlug, - projectId: project.id + const [groupMembership] = await groupProjectDAL.findByProjectId(project.id, { + groupSlug }); return groupMembership;