Skip to content

Commit a6578fd

Browse files
perf: handleChildrenEventType link old event-type with workflows using create/findMany (calcom#25653)
* perf: link old event-type with workflows using create/findMany * test: fix handleChildrenEventTypes test mocks for new workflow linking implementation Update test to match the new implementation that uses findMany + createMany instead of upsert for linking workflows to old event types. - Add mock for workflowsOnEventTypes.findMany to return empty array - Update assertion from upsert to findMany + createMany Co-Authored-By: morgan@cal.com <morgan@cal.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 4e68bcb commit a6578fd

2 files changed

Lines changed: 61 additions & 30 deletions

File tree

apps/web/test/lib/handleChildrenEventTypes.test.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,10 @@ describe("handleChildrenEventTypes", () => {
553553
updatedAt: new Date(),
554554
});
555555

556+
// Mock workflowsOnEventTypes.findMany to return empty array (no existing workflow links for old event types)
557+
// This is needed because the new implementation uses findMany + createMany instead of upsert
558+
prismaMock.workflowsOnEventTypes.findMany.mockResolvedValue([]);
559+
556560
await updateChildrenEventTypes({
557561
eventTypeId: 1,
558562
oldEventType: { children: [{ userId: 4 }], team: { name: "" } },
@@ -627,19 +631,23 @@ describe("handleChildrenEventTypes", () => {
627631
},
628632
},
629633
});
630-
// Verify workflowsOnEventTypes.upsert was called for existing users' workflows
631-
expect(prismaMock.workflowsOnEventTypes.upsert).toHaveBeenCalledWith({
632-
create: {
633-
eventTypeId: 2,
634-
workflowId: 11,
635-
},
636-
update: {},
634+
// Verify workflowsOnEventTypes.findMany was called to check existing relationships
635+
expect(prismaMock.workflowsOnEventTypes.findMany).toHaveBeenCalledWith({
637636
where: {
638-
workflowId_eventTypeId: {
639-
eventTypeId: 2,
640-
workflowId: 11,
641-
},
637+
workflowId: { in: [11] },
638+
eventTypeId: { in: [2] },
642639
},
640+
select: {
641+
workflowId: true,
642+
eventTypeId: true,
643+
},
644+
});
645+
646+
// Verify workflowsOnEventTypes.createMany was called for existing users' workflows
647+
// Since findMany returned empty array, all workflow-eventType pairs should be created
648+
expect(prismaMock.workflowsOnEventTypes.createMany).toHaveBeenCalledWith({
649+
data: [{ eventTypeId: 2, workflowId: 11 }],
650+
skipDuplicates: false,
643651
});
644652
});
645653
});

packages/features/ee/managed-event-types/lib/handleChildrenEventTypes.ts

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -355,26 +355,49 @@ export default async function handleChildrenEventTypes({
355355
})
356356
);
357357

358-
if (currentWorkflowIds?.length) {
359-
await prisma.$transaction(
360-
currentWorkflowIds.flatMap((wfId) => {
361-
return oldEventTypes.map((oEvTy) => {
362-
return prisma.workflowsOnEventTypes.upsert({
363-
create: {
364-
eventTypeId: oEvTy.id,
365-
workflowId: wfId,
366-
},
367-
update: {},
368-
where: {
369-
workflowId_eventTypeId: {
370-
eventTypeId: oEvTy.id,
371-
workflowId: wfId,
372-
},
373-
},
374-
});
358+
// Link workflows with old users' event types if new workflows were added
359+
if (currentWorkflowIds?.length && oldEventTypes.length) {
360+
await prisma.$transaction(async (tx) => {
361+
const eventTypeIds = oldEventTypes.map((e) => e.id);
362+
363+
const allDesiredPairs = currentWorkflowIds.flatMap((wfId: number) =>
364+
oldEventTypes.map((oEvTy: { id: number }) => ({
365+
eventTypeId: oEvTy.id,
366+
workflowId: wfId,
367+
}))
368+
);
369+
370+
const existingRelationships = await tx.workflowsOnEventTypes.findMany({
371+
where: {
372+
workflowId: { in: currentWorkflowIds },
373+
eventTypeId: { in: eventTypeIds },
374+
},
375+
select: {
376+
workflowId: true,
377+
eventTypeId: true,
378+
},
379+
});
380+
381+
const existingSet = new Set(
382+
existingRelationships.map(
383+
(rel: { eventTypeId: number; workflowId: number }) => `${rel.eventTypeId}_${rel.workflowId}`
384+
)
385+
);
386+
387+
const newRelationshipsToCreate = allDesiredPairs.filter(
388+
(pair: { eventTypeId: number; workflowId: number }) => {
389+
const key = `${pair.eventTypeId}_${pair.workflowId}`;
390+
return !existingSet.has(key);
391+
}
392+
);
393+
394+
if (newRelationshipsToCreate.length > 0) {
395+
await tx.workflowsOnEventTypes.createMany({
396+
data: newRelationshipsToCreate,
397+
skipDuplicates: false,
375398
});
376-
})
377-
);
399+
}
400+
});
378401
}
379402
}
380403

0 commit comments

Comments
 (0)