Skip to content

Commit 068eb91

Browse files
committed
More performance updates
1 parent 477d1a8 commit 068eb91

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/api/my-review/myReview.service.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export class MyReviewService {
156156
whereFragments.push(Prisma.sql`c.status = 'ACTIVE'`);
157157
}
158158

159-
const joins: Prisma.Sql[] = [];
159+
const baseJoins: Prisma.Sql[] = [];
160160

161161
if (!adminUser) {
162162
if (!normalizedUserId) {
@@ -165,7 +165,7 @@ export class MyReviewService {
165165
);
166166
}
167167

168-
joins.push(
168+
baseJoins.push(
169169
Prisma.sql`
170170
LEFT JOIN resources."Resource" r
171171
ON r."challengeId" = c.id
@@ -177,7 +177,7 @@ export class MyReviewService {
177177

178178
whereFragments.push(Prisma.sql`r."challengeId" IS NOT NULL`);
179179
} else {
180-
joins.push(
180+
baseJoins.push(
181181
Prisma.sql`
182182
LEFT JOIN resources."Resource" r
183183
ON r."challengeId" = c.id
@@ -188,10 +188,13 @@ export class MyReviewService {
188188
);
189189
}
190190

191-
joins.push(
191+
baseJoins.push(
192192
Prisma.sql`
193193
LEFT JOIN challenges."ChallengeType" ct ON ct.id = c."typeId"
194194
`,
195+
);
196+
197+
const metricJoins: Prisma.Sql[] = [
195198
Prisma.sql`
196199
LEFT JOIN LATERAL (
197200
SELECT
@@ -279,7 +282,7 @@ export class MyReviewService {
279282
AND apr."resourceId" = r.id
280283
WHERE rv_pending."resourceId" = r.id
281284
AND apr.id IS NULL
282-
) AS "hasPendingAppealResponses"
285+
) AS "hasPendingAppealResponses"
283286
) pending_appeals ON TRUE
284287
`,
285288
Prisma.sql`
@@ -309,7 +312,13 @@ export class MyReviewService {
309312
) AS "hasAIReview"
310313
) cr ON TRUE
311314
`,
315+
];
316+
317+
const joinClause = joinSqlFragments(
318+
[...baseJoins, ...metricJoins],
319+
Prisma.sql``,
312320
);
321+
const countJoinClause = joinSqlFragments(baseJoins, Prisma.sql``);
313322

314323
if (challengeTypeId) {
315324
whereFragments.push(Prisma.sql`c."typeId" = ${challengeTypeId}`);
@@ -331,7 +340,6 @@ export class MyReviewService {
331340
);
332341
}
333342

334-
const joinClause = joinSqlFragments(joins, Prisma.sql``);
335343
const whereClause = joinSqlFragments(whereFragments, Prisma.sql` AND `);
336344

337345
const phaseEndExpression = Prisma.sql`
@@ -410,7 +418,7 @@ export class MyReviewService {
410418
const countQuery = Prisma.sql`
411419
SELECT COUNT(DISTINCT c.id) AS "total"
412420
FROM challenges."Challenge" c
413-
${joinClause}
421+
${countJoinClause}
414422
WHERE ${whereClause}
415423
`;
416424

0 commit comments

Comments
 (0)