Scheduling infrastructure for absolutely everyone.
fix: extract Prisma.sql queries for $queryRaw calls with Prisma.sql parameters (Prisma v6 upgrade) (#23779)
Eunjae Leedbbf3ae2a24b
diff --git a/packages/lib/server/repository/PrismaAgentRepository.ts b/packages/lib/server/repository/PrismaAgentRepository.tsindex aefaaa62954..21bec58da85 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`SELECTid,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`SELECTid,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`SELECTa.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+1const 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`SELECTa.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`SELECTid,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`SELECTa.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.tsindex 0a3f41153f1..0d7e1af1c09 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`SELECTpn.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