Skip to content

Commit

Permalink
feat: add partnership goals to project
Browse files Browse the repository at this point in the history
  • Loading branch information
asabotovich committed Jul 22, 2024
1 parent 9d66ea8 commit 229cf8f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
32 changes: 31 additions & 1 deletion trpc/queries/projectV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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<string>`concat("Goal"."projectId", '-', "Goal"."scopeId")::text`.as('_shortId'),
jsonBuildObject({
Expand Down Expand Up @@ -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),
]),
Expand Down Expand Up @@ -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(),
Expand Down
9 changes: 5 additions & 4 deletions trpc/router/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 229cf8f

Please sign in to comment.