Skip to content

Commit

Permalink
fix(server): issue related to join[]
Browse files Browse the repository at this point in the history
  • Loading branch information
overbit committed Apr 9, 2024
1 parent 9ad22a6 commit 0bbe3ca
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export class UsageInsightsService {
private readonly prisma: PrismaService
) {}

private projectIdsFilter = (projectIds: string[]) =>
projectIds.length > 0
? Prisma.sql`AND r."projectId" IN (${Prisma.join(projectIds)})`
: Prisma.empty;

async countLinesOfCode({
projectIds,
startDate,
Expand Down Expand Up @@ -91,7 +96,7 @@ export class UsageInsightsService {
JOIN "Resource" r ON b."resourceId" = r."id"
WHERE b."createdAt" >= ${startDate}
AND b."createdAt" <= ${endDate}
AND r."projectId" IN (${Prisma.join(projectIds)})
${this.projectIdsFilter(projectIds)}
GROUP BY year, month, time_group
ORDER BY year, month, time_group;
`;
Expand All @@ -112,8 +117,8 @@ export class UsageInsightsService {
FROM "EntityVersion" ev
JOIN "Entity" e ON ev."entityId" = e."id"
JOIN "Resource" r ON e."resourceId" = r."id"
WHERE r."projectId" IN (${Prisma.join(projectIds)})
AND ev."updatedAt" >= ${startDate} AND ev."updatedAt" <= ${endDate}
WHERE ev."updatedAt" >= ${startDate} AND ev."updatedAt" <= ${endDate}
${this.projectIdsFilter(projectIds)}
GROUP BY year, month, time_group
ORDER BY year, month, time_group;
`;
Expand All @@ -131,7 +136,8 @@ export class UsageInsightsService {
timeGroup,
}: BlockChangesArgs): Promise<UsageInsights> {
let results: QueryRawResult[];
// Prisma query row doesn't work as expected with enums, so we use switch case instead of one query with ${blokType}

// Prisma query raw doesn't work as expected with enums, so we use switch case instead of one query with ${blockType}
switch (blockType) {
case EnumBlockType.ModuleAction:
results = await this.prisma.$queryRaw`
Expand All @@ -140,7 +146,7 @@ export class UsageInsightsService {
JOIN "Block" b ON bv."blockId" = b."id"
JOIN "Resource" r ON b."resourceId" = r."id"
WHERE b."blockType" = 'ModuleAction'
AND r."projectId" IN (${Prisma.join(projectIds)})
${this.projectIdsFilter(projectIds)}
AND bv."updatedAt" >= ${startDate} AND bv."updatedAt" <= ${endDate}
GROUP BY year, month, time_group
ORDER BY year, month, time_group;
Expand All @@ -153,7 +159,7 @@ export class UsageInsightsService {
JOIN "Block" b ON bv."blockId" = b."id"
JOIN "Resource" r ON b."resourceId" = r."id"
WHERE b."blockType" = 'PluginInstallation'
AND r."projectId" IN (${Prisma.join(projectIds)})
${this.projectIdsFilter(projectIds)}
AND bv."updatedAt" >= ${startDate} AND bv."updatedAt" <= ${endDate}
GROUP BY year, month, time_group
ORDER BY year, month, time_group;
Expand Down

0 comments on commit 0bbe3ca

Please sign in to comment.