Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/api/my-review/myReview.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class MyReviewService {
whereFragments.push(Prisma.sql`c.status = 'ACTIVE'`);
}

const joins: Prisma.Sql[] = [];
const baseJoins: Prisma.Sql[] = [];

if (!adminUser) {
if (!normalizedUserId) {
Expand All @@ -165,7 +165,7 @@ export class MyReviewService {
);
}

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

whereFragments.push(Prisma.sql`r."challengeId" IS NOT NULL`);
} else {
joins.push(
baseJoins.push(
Prisma.sql`
LEFT JOIN resources."Resource" r
ON r."challengeId" = c.id
Expand All @@ -188,10 +188,13 @@ export class MyReviewService {
);
}

joins.push(
baseJoins.push(
Prisma.sql`
LEFT JOIN challenges."ChallengeType" ct ON ct.id = c."typeId"
`,
);

const metricJoins: Prisma.Sql[] = [
Prisma.sql`
LEFT JOIN LATERAL (
SELECT
Expand Down Expand Up @@ -279,7 +282,7 @@ export class MyReviewService {
AND apr."resourceId" = r.id
WHERE rv_pending."resourceId" = r.id
AND apr.id IS NULL
) AS "hasPendingAppealResponses"
) AS "hasPendingAppealResponses"
) pending_appeals ON TRUE
`,
Prisma.sql`
Expand Down Expand Up @@ -309,7 +312,13 @@ export class MyReviewService {
) AS "hasAIReview"
) cr ON TRUE
`,
];

const joinClause = joinSqlFragments(
[...baseJoins, ...metricJoins],
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ correctness]
The joinSqlFragments function is used to concatenate SQL fragments without a separator. Ensure that this is intentional and that no separator is needed between the baseJoins and metricJoins. If a separator is required, this could lead to incorrect SQL syntax.

Prisma.sql``,
);
const countJoinClause = joinSqlFragments(baseJoins, Prisma.sql``);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ correctness]
The countJoinClause only includes baseJoins. Verify that excluding metricJoins from the count query is intentional. If metrics affect the count, this could lead to incorrect results.


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

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

const phaseEndExpression = Prisma.sql`
Expand Down Expand Up @@ -410,7 +418,7 @@ export class MyReviewService {
const countQuery = Prisma.sql`
SELECT COUNT(DISTINCT c.id) AS "total"
FROM challenges."Challenge" c
${joinClause}
${countJoinClause}
WHERE ${whereClause}
`;

Expand Down