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