perf: handleChildrenEventType link old event-type with workflows using create/findMany (#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>
This commit is contained in:
Morgan
2025-12-08 13:11:21 +02:00
committed by GitHub
co-authored by morgan@cal.com <morgan@cal.com> Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
parent 4e68bcb021
commit a6578fdcd2
2 changed files with 61 additions and 30 deletions
@@ -553,6 +553,10 @@ describe("handleChildrenEventTypes", () => {
updatedAt: new Date(),
});
// Mock workflowsOnEventTypes.findMany to return empty array (no existing workflow links for old event types)
// This is needed because the new implementation uses findMany + createMany instead of upsert
prismaMock.workflowsOnEventTypes.findMany.mockResolvedValue([]);
await updateChildrenEventTypes({
eventTypeId: 1,
oldEventType: { children: [{ userId: 4 }], team: { name: "" } },
@@ -627,19 +631,23 @@ describe("handleChildrenEventTypes", () => {
},
},
});
// Verify workflowsOnEventTypes.upsert was called for existing users' workflows
expect(prismaMock.workflowsOnEventTypes.upsert).toHaveBeenCalledWith({
create: {
eventTypeId: 2,
workflowId: 11,
},
update: {},
// Verify workflowsOnEventTypes.findMany was called to check existing relationships
expect(prismaMock.workflowsOnEventTypes.findMany).toHaveBeenCalledWith({
where: {
workflowId_eventTypeId: {
eventTypeId: 2,
workflowId: 11,
},
workflowId: { in: [11] },
eventTypeId: { in: [2] },
},
select: {
workflowId: true,
eventTypeId: true,
},
});
// Verify workflowsOnEventTypes.createMany was called for existing users' workflows
// Since findMany returned empty array, all workflow-eventType pairs should be created
expect(prismaMock.workflowsOnEventTypes.createMany).toHaveBeenCalledWith({
data: [{ eventTypeId: 2, workflowId: 11 }],
skipDuplicates: false,
});
});
});
@@ -355,26 +355,49 @@ export default async function handleChildrenEventTypes({
})
);
if (currentWorkflowIds?.length) {
await prisma.$transaction(
currentWorkflowIds.flatMap((wfId) => {
return oldEventTypes.map((oEvTy) => {
return prisma.workflowsOnEventTypes.upsert({
create: {
eventTypeId: oEvTy.id,
workflowId: wfId,
},
update: {},
where: {
workflowId_eventTypeId: {
eventTypeId: oEvTy.id,
workflowId: wfId,
},
},
});
// Link workflows with old users' event types if new workflows were added
if (currentWorkflowIds?.length && oldEventTypes.length) {
await prisma.$transaction(async (tx) => {
const eventTypeIds = oldEventTypes.map((e) => e.id);
const allDesiredPairs = currentWorkflowIds.flatMap((wfId: number) =>
oldEventTypes.map((oEvTy: { id: number }) => ({
eventTypeId: oEvTy.id,
workflowId: wfId,
}))
);
const existingRelationships = await tx.workflowsOnEventTypes.findMany({
where: {
workflowId: { in: currentWorkflowIds },
eventTypeId: { in: eventTypeIds },
},
select: {
workflowId: true,
eventTypeId: true,
},
});
const existingSet = new Set(
existingRelationships.map(
(rel: { eventTypeId: number; workflowId: number }) => `${rel.eventTypeId}_${rel.workflowId}`
)
);
const newRelationshipsToCreate = allDesiredPairs.filter(
(pair: { eventTypeId: number; workflowId: number }) => {
const key = `${pair.eventTypeId}_${pair.workflowId}`;
return !existingSet.has(key);
}
);
if (newRelationshipsToCreate.length > 0) {
await tx.workflowsOnEventTypes.createMany({
data: newRelationshipsToCreate,
skipDuplicates: false,
});
})
);
}
});
}
}