refactor: Fix N+1 queries and optimize database operations (#25813)
* refactor: prisma N+1 Queries * addressed review
This commit is contained in:
1 parent
5626bceeff
commit
7562ad2a16
5 files changed
+119
-66
No files matched your search
@@ -430,34 +430,44 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
},
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
hostGroups.map(async (group) => {
|
||||
await ctx.prisma.hostGroup.upsert({
|
||||
where: { id: group.id },
|
||||
update: { name: group.name },
|
||||
create: {
|
||||
const existingGroupsMap = new Map(existingHostGroups.map((group) => [group.id, group]));
|
||||
const newGroupsMap = new Map(hostGroups.map((group) => [group.id, group]));
|
||||
|
||||
const groupsToCreate = hostGroups.filter((group) => !existingGroupsMap.has(group.id));
|
||||
const groupsToUpdate = hostGroups.filter((group) => existingGroupsMap.has(group.id));
|
||||
const groupsToDelete = existingHostGroups.filter((existingGroup) => !newGroupsMap.has(existingGroup.id));
|
||||
|
||||
await ctx.prisma.$transaction(async (tx) => {
|
||||
// Create new groups
|
||||
if (groupsToCreate.length > 0) {
|
||||
await tx.hostGroup.createMany({
|
||||
data: groupsToCreate.map((group) => ({
|
||||
id: group.id,
|
||||
name: group.name,
|
||||
eventTypeId: id,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
// Update existing groups
|
||||
for (const group of groupsToUpdate) {
|
||||
await tx.hostGroup.update({
|
||||
where: { id: group.id },
|
||||
data: { name: group.name },
|
||||
});
|
||||
}
|
||||
|
||||
// Delete groups that are no longer in the new list
|
||||
if (groupsToDelete.length > 0) {
|
||||
await tx.hostGroup.deleteMany({
|
||||
where: {
|
||||
id: {
|
||||
in: groupsToDelete.map((group) => group.id),
|
||||
},
|
||||
},
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
const newGroupsMap = new Map(hostGroups.map((group) => [group.id, group]));
|
||||
|
||||
// Delete groups that are no longer in the new list
|
||||
const groupsToDelete = existingHostGroups.filter((existingGroup) => !newGroupsMap.has(existingGroup.id));
|
||||
|
||||
if (groupsToDelete.length > 0) {
|
||||
await ctx.prisma.hostGroup.deleteMany({
|
||||
where: {
|
||||
id: {
|
||||
in: groupsToDelete.map((group) => group.id),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (teamId && hosts) {
|
||||
|
||||
@@ -253,6 +253,22 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
|
||||
const agentRepo = new PrismaAgentRepository(prisma);
|
||||
|
||||
const stepIds = userWorkflow.steps.map((step) => step.id);
|
||||
const allReminders = await WorkflowReminderRepository.findWorkflowRemindersByStepIds(stepIds);
|
||||
|
||||
const remindersByStepId = new Map<number, (typeof allReminders)[number][]>();
|
||||
for (const reminder of allReminders) {
|
||||
const stepId = reminder.workflowStepId;
|
||||
if (stepId === null) continue;
|
||||
|
||||
let list = remindersByStepId.get(stepId);
|
||||
if (!list) {
|
||||
list = [];
|
||||
remindersByStepId.set(stepId, list);
|
||||
}
|
||||
list.push(reminder);
|
||||
}
|
||||
|
||||
// handle deleted and edited workflow steps
|
||||
await Promise.all(
|
||||
userWorkflow.steps.map(async (oldStep) => {
|
||||
@@ -271,7 +287,7 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
};
|
||||
}
|
||||
|
||||
const remindersFromStep = await WorkflowReminderRepository.findWorkflowRemindersByStepId(oldStep.id);
|
||||
const remindersFromStep = remindersByStepId.get(oldStep.id) || [];
|
||||
//step was deleted
|
||||
if (!newStep) {
|
||||
if (oldStep.action === WorkflowActions.CAL_AI_PHONE_CALL && !!oldStep.agentId) {
|
||||
|
||||
Reference in new issue
Block a user