From dbbf3ae2a24b4fe9b9fb153f7fe7d427e3709254 Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Fri, 12 Sep 2025 00:18:12 +0200 Subject: [PATCH] fix: extract Prisma.sql queries for $queryRaw calls with Prisma.sql parameters (Prisma v6 upgrade) (#23779) * fix: extract Prisma.sql queries for calls with Prisma.sql parameters - Extract SQL queries to separate Prisma.sql variables before passing to - Fix PrismaPhoneNumberRepository.ts findManyWithUserAccess and findPhoneNumbersFromUserId methods - Fix PrismaAgentRepository.ts multiple methods with whereCondition parameters - Fix PermissionRepository.ts checkRolePermissions and getTeamIdsWithPermissions methods - Fix virtual-queues.ts getUserRelevantTeamRoutingForms method - Fix tasker/repository.ts hasNewerScanTaskForStepId method - Add missing Prisma imports where needed - Fix lint warnings: replace any with JsonValue and use explicit select for permissions - Skip primitive parameter cases as instructed - Compatible with Prisma v6 requirements Co-Authored-By: eunjae@cal.com * revert unnecessary changes --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../repository/PrismaAgentRepository.ts | 24 ++++++++++++++----- .../repository/PrismaPhoneNumberRepository.ts | 4 +++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/lib/server/repository/PrismaAgentRepository.ts b/packages/lib/server/repository/PrismaAgentRepository.ts index aefaaa6295..21bec58da8 100644 --- a/packages/lib/server/repository/PrismaAgentRepository.ts +++ b/packages/lib/server/repository/PrismaAgentRepository.ts @@ -90,7 +90,7 @@ export class PrismaAgentRepository { whereCondition = Prisma.sql`id = ${agentId} AND "userId" = ${userId}`; } - const agents = await prisma.$queryRaw<_AgentRawResult[]>` + const query = Prisma.sql` SELECT id, name, @@ -105,6 +105,8 @@ export class PrismaAgentRepository { LIMIT 1 `; + const agents = await prisma.$queryRaw<_AgentRawResult[]>(query); + return agents.length > 0 ? agents[0] : null; } @@ -126,7 +128,7 @@ export class PrismaAgentRepository { whereCondition = Prisma.sql`"providerAgentId" = ${providerAgentId} AND "userId" = ${userId}`; } - const agents = await prisma.$queryRaw<_AgentRawResult[]>` + const query = Prisma.sql` SELECT id, name, @@ -141,6 +143,8 @@ export class PrismaAgentRepository { LIMIT 1 `; + const agents = await prisma.$queryRaw<_AgentRawResult[]>(query); + return agents.length > 0 ? agents[0] : null; } @@ -227,7 +231,7 @@ export class PrismaAgentRepository { } } - const agents = await prisma.$queryRaw<_AgentRawResult[]>` + const query = Prisma.sql` SELECT a.id, a.name, @@ -251,6 +255,8 @@ export class PrismaAgentRepository { ORDER BY a."teamId" ASC, a."createdAt" DESC `; + const agents = await prisma.$queryRaw<_AgentRawResult[]>(query); + // Get phone numbers for each agent in a separate query to avoid N+1 const agentIds = agents.map((agent) => agent.id); const phoneNumbers = @@ -343,7 +349,7 @@ export class PrismaAgentRepository { whereCondition = Prisma.sql`a.id = ${id} AND a."userId" = ${userId}`; } - const agents = await prisma.$queryRaw<_AgentRawResult[]>` + const query = Prisma.sql` SELECT a.id, a.name, @@ -366,6 +372,8 @@ export class PrismaAgentRepository { LIMIT 1 `; + const agents = await prisma.$queryRaw<_AgentRawResult[]>(query); + if (agents.length === 0) { return null; } @@ -464,7 +472,7 @@ export class PrismaAgentRepository { whereCondition = Prisma.sql`id = ${id} AND "userId" = ${userId}`; } - const agents = await prisma.$queryRaw<_AgentRawResult[]>` + const query = Prisma.sql` SELECT id, name, @@ -479,6 +487,8 @@ export class PrismaAgentRepository { LIMIT 1 `; + const agents = await prisma.$queryRaw<_AgentRawResult[]>(query); + return agents.length > 0 ? agents[0] : null; } @@ -494,7 +504,7 @@ export class PrismaAgentRepository { whereCondition = Prisma.sql`a.id = ${id} AND a."userId" = ${userId}`; } - const agents = await prisma.$queryRaw<_AgentRawResult[]>` + const query = Prisma.sql` SELECT a.id, a.name, @@ -509,6 +519,8 @@ export class PrismaAgentRepository { LIMIT 1 `; + const agents = await prisma.$queryRaw<_AgentRawResult[]>(query); + if (agents.length === 0) { return null; } diff --git a/packages/lib/server/repository/PrismaPhoneNumberRepository.ts b/packages/lib/server/repository/PrismaPhoneNumberRepository.ts index 0a3f41153f..0d7e1af1c0 100644 --- a/packages/lib/server/repository/PrismaPhoneNumberRepository.ts +++ b/packages/lib/server/repository/PrismaPhoneNumberRepository.ts @@ -354,7 +354,7 @@ export class PrismaPhoneNumberRepository { } } - const phoneNumbers = await prisma.$queryRaw<_PhoneNumberRawResult[]>` + const query = Prisma.sql` SELECT pn.id, pn."phoneNumber", @@ -373,6 +373,8 @@ export class PrismaPhoneNumberRepository { ORDER BY pn."createdAt" DESC `; + const phoneNumbers = await prisma.$queryRaw<_PhoneNumberRawResult[]>(query); + const phoneNumberIds = phoneNumbers.map((pn) => pn.id); const agents = phoneNumberIds.length > 0