From 229cf8f1a9b0097333e12cd4937c56d8bbfd8ea6 Mon Sep 17 00:00:00 2001 From: Anton Sabotovich Date: Thu, 18 Jul 2024 14:05:05 +0300 Subject: [PATCH] feat: add partnership goals to project --- trpc/queries/projectV2.ts | 32 +++++++++++++++++++++++++++++++- trpc/router/project.ts | 9 +++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/trpc/queries/projectV2.ts b/trpc/queries/projectV2.ts index df8d2b164..2cae5a90a 100644 --- a/trpc/queries/projectV2.ts +++ b/trpc/queries/projectV2.ts @@ -291,6 +291,12 @@ export const getUserProjectsWithGoals = (params: GetProjectsWithGoalsByIdsParams .where('A', 'in', ({ selectFrom }) => selectFrom('subs_projects').select('B')), ), ) + .with('partnership_goals', (db) => + db + .selectFrom('_partnershipProjects') + .where('B', 'in', ({ selectFrom }) => selectFrom('project_ids').select('pid')) + .select('A'), + ) .with('goals', (db) => db .selectFrom('Goal') @@ -329,6 +335,15 @@ export const getUserProjectsWithGoals = (params: GetProjectsWithGoalsByIdsParams .as('criteria'), (join) => join.onTrue(), ) + .leftJoinLateral( + ({ selectFrom }) => + selectFrom('_partnershipProjects') + .innerJoin('Project', 'Project.id', 'B') + .selectAll('Project') + .whereRef('A', '=', 'Goal.id') + .as('partnershipProject'), + (join) => join.onTrue(), + ) .select((eb) => [ sql`concat("Goal"."projectId", '-', "Goal"."scopeId")::text`.as('_shortId'), jsonBuildObject({ @@ -358,11 +373,19 @@ export const getUserProjectsWithGoals = (params: GetProjectsWithGoalsByIdsParams .else(null) .end() .as('participants'), + eb + .case() + .when(eb.fn.count('partnershipProject.id'), '>', 0) + .then(eb.fn.agg('array_agg', [sql`"partnershipProject"`]).distinct()) + .else(null) + .end() + .as('partnershipProjects'), ]) .where(({ or, eb }) => or([ eb('Goal.id', 'in', ({ selectFrom }) => selectFrom('subs_goals').select('B')), eb('Goal.projectId', 'in', ({ selectFrom }) => selectFrom('project_ids').select('pid')), + eb('Goal.id', 'in', ({ selectFrom }) => selectFrom('partnership_goals').select('A')), eb('Goal.ownerId', '=', params.activityId), eb('Goal.activityId', '=', params.activityId), ]), @@ -481,7 +504,14 @@ export const getUserProjectsWithGoals = (params: GetProjectsWithGoalsByIdsParams sql`"state"`.as('state'), sql`"priority"`.as('priority'), ]) - .whereRef('goals.projectId', '=', 'Project.id') + .where((eb) => + eb.or([ + eb('goals.id', '=', () => + selectFrom('_partnershipProjects').whereRef('B', '=', 'Project.id').select('A'), + ), + eb('goals.projectId', '=', eb.ref('Project.id')), + ]), + ) .limit(dashboardGoalByProjectLimit) .as('goal'), (join) => join.onTrue(), diff --git a/trpc/router/project.ts b/trpc/router/project.ts index 8a7bc6e54..60214c916 100644 --- a/trpc/router/project.ts +++ b/trpc/router/project.ts @@ -508,15 +508,16 @@ export const project = router({ .use(projectAccessMiddleware) .query(async ({ ctx, input: { id, goalsQuery = {} } }) => { const { activityId, role } = ctx.session.user; + const goalsWhere = { + OR: [{ projectId: id }, { partnershipProjects: { some: { id } } }], + }; const [allProjectGoals, filtredProjectGoals] = await Promise.all([ prisma.goal.count({ - where: { - projectId: id, - }, + where: goalsWhere, }), prisma.goal.findMany({ - ...goalsFilter(goalsQuery, activityId, role, { projectId: id }), + ...goalsFilter(goalsQuery, activityId, role, goalsWhere), include: getGoalDeepQuery({ activityId, role,