fix: update transaction timeout for large managed event-types (#25944)
* fix: update transaction timeout for large managed event-types * test: add findMany mock for batch update optimization in handleChildrenEventTypes tests Co-Authored-By: [email protected] <[email protected]> * fix: reduce batch size to 50 and use skipDuplicates for workflow linkage Co-Authored-By: [email protected] <[email protected]> * test: add comprehensive tests for batch execution and retry behavior Co-Authored-By: [email protected] <[email protected]> * fix: use logger instead of console.log and fix type error Co-Authored-By: [email protected] <[email protected]> * test: update test file to mock logger instead of console.log Co-Authored-By: [email protected] <[email protected]> * fix: type issues * fixup! fix: type issues --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
This commit is contained in:
2 files changed
+389
-66
No files matched your search
@@ -2,9 +2,10 @@ import type { DeepMockProxy } from "vitest-mock-extended";
|
||||
|
||||
import { eventTypeMetaDataSchemaWithTypedApps } from "@calcom/app-store/zod-utils";
|
||||
import { sendSlugReplacementEmail } from "@calcom/emails/integration-email-service";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
import type { EventType, Prisma } from "@calcom/prisma/client";
|
||||
import { SchedulingType } from "@calcom/prisma/enums";
|
||||
import {
|
||||
allManagedEventTypeProps,
|
||||
@@ -273,7 +274,7 @@ export default async function handleChildrenEventTypes({
|
||||
|
||||
// Old users updated
|
||||
if (oldUserIds?.length) {
|
||||
// Check if there are children with existent homonym event types to send notifications
|
||||
// 1. Initial Checks and Setup
|
||||
deletedExistentEventTypes = await checkExistentEventTypes({
|
||||
updatedEventType,
|
||||
children,
|
||||
@@ -283,50 +284,42 @@ export default async function handleChildrenEventTypes({
|
||||
});
|
||||
|
||||
const { unlockedFields } = managedEventTypeValues.metadata?.managedEventConfig ?? {};
|
||||
const unlockedFieldProps = !unlockedFields
|
||||
? {}
|
||||
: Object.keys(unlockedFields).reduce((acc, key) => {
|
||||
const filteredKey =
|
||||
key === "afterBufferTime"
|
||||
? "afterEventBuffer"
|
||||
: key === "beforeBufferTime"
|
||||
? "beforeEventBuffer"
|
||||
: key;
|
||||
// @ts-expect-error Element implicitly has any type
|
||||
acc[filteredKey] = true;
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
// Add to payload all eventType values that belong to locked fields, changed or unchanged
|
||||
// Ignore from payload any eventType values that belong to unlocked fields
|
||||
// Transform unlocked fields into a lookup object with mapped keys
|
||||
const unlockedFieldProps = Object.keys(unlockedFields ?? {}).reduce((acc, key) => {
|
||||
const mappedKey =
|
||||
key === "afterBufferTime"
|
||||
? "afterEventBuffer"
|
||||
: key === "beforeBufferTime"
|
||||
? "beforeEventBuffer"
|
||||
: key;
|
||||
// @ts-expect-error Element implicitly has any type
|
||||
acc[mappedKey] = true;
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
// Prepare payload: Omit unlocked fields and the "children" property
|
||||
const updatePayload = allManagedEventTypePropsZod.omit(unlockedFieldProps).parse(eventType);
|
||||
const updatePayloadFiltered = Object.entries(updatePayload)
|
||||
.filter(([key, _]) => key !== "children")
|
||||
.reduce((newObj, [key, value]) => ({ ...newObj, [key]: value }), {});
|
||||
// Update event types for old users
|
||||
const oldEventTypes = await Promise.all(
|
||||
oldUserIds.map(async (userId) => {
|
||||
const existingEventType = await prisma.eventType.findUnique({
|
||||
where: {
|
||||
userId_parentId: {
|
||||
userId,
|
||||
parentId,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
metadata: true,
|
||||
},
|
||||
});
|
||||
|
||||
const metadata = eventTypeMetaDataSchemaWithTypedApps.parse(existingEventType?.metadata || {});
|
||||
// 2. Data Fetching Optimization
|
||||
const existingRecords = await prisma.eventType.findMany({
|
||||
where: { parentId, userId: { in: oldUserIds } },
|
||||
select: { userId: true, metadata: true },
|
||||
});
|
||||
|
||||
const metadataMap = new Map(existingRecords.map((rec) => [rec.userId, rec.metadata]));
|
||||
|
||||
// 3. Define Reusable Update Logic
|
||||
const performUpdate = async (userId: number) => {
|
||||
try {
|
||||
const rawMetadata = metadataMap.get(userId);
|
||||
const metadata = eventTypeMetaDataSchemaWithTypedApps.parse(rawMetadata || {});
|
||||
|
||||
return await prisma.eventType.update({
|
||||
where: {
|
||||
userId_parentId: {
|
||||
userId,
|
||||
parentId,
|
||||
},
|
||||
},
|
||||
where: { userId_parentId: { userId, parentId } },
|
||||
data: {
|
||||
...updatePayloadFiltered,
|
||||
rrHostSubsetEnabled: false,
|
||||
@@ -334,12 +327,7 @@ export default async function handleChildrenEventTypes({
|
||||
...("schedule" in unlockedFieldProps ? {} : { scheduleId: eventType.scheduleId || null }),
|
||||
restrictionScheduleId: null,
|
||||
useBookerTimezone: false,
|
||||
hashedLink:
|
||||
"multiplePrivateLinks" in unlockedFieldProps
|
||||
? undefined
|
||||
: {
|
||||
deleteMany: {},
|
||||
},
|
||||
hashedLink: "multiplePrivateLinks" in unlockedFieldProps ? undefined : { deleteMany: {} },
|
||||
allowReschedulingCancelledBookings:
|
||||
managedEventTypeValues.allowReschedulingCancelledBookings ?? false,
|
||||
metadata: {
|
||||
@@ -351,10 +339,46 @@ export default async function handleChildrenEventTypes({
|
||||
},
|
||||
},
|
||||
});
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
throw { userId, error };
|
||||
}
|
||||
};
|
||||
|
||||
// Link workflows with old users' event types if new workflows were added
|
||||
// 4. Batch Execution Handler
|
||||
const executeBatch = async (ids: number[]) => {
|
||||
const successes: EventType[] = [];
|
||||
const failures: { userId: number; error: Error }[] = [];
|
||||
const BATCH_SIZE = 50;
|
||||
|
||||
for (let i = 0; i < ids.length; i += BATCH_SIZE) {
|
||||
const batch = ids.slice(i, i + BATCH_SIZE);
|
||||
const results = await Promise.allSettled(batch.map(performUpdate));
|
||||
|
||||
results.forEach((res) => {
|
||||
if (res.status === "fulfilled") successes.push(res.value);
|
||||
else failures.push(res.reason);
|
||||
});
|
||||
}
|
||||
return { successes, failures };
|
||||
};
|
||||
|
||||
// 5. Run Updates & Retries
|
||||
const { successes: oldEventTypes, failures: failedUpdates } = await executeBatch(oldUserIds);
|
||||
|
||||
if (failedUpdates.length > 0) {
|
||||
logger.info(`Retrying ${failedUpdates.length} failed updates...`);
|
||||
const retry = await executeBatch(failedUpdates.map((f) => f.userId));
|
||||
oldEventTypes.push(...retry.successes);
|
||||
// Any remaining failures in retry.failures are permanent
|
||||
if (retry.failures.length > 0) {
|
||||
logger.error("handleChildrenEventType - Could not update managed event-type", {
|
||||
parentId,
|
||||
userIds: retry.failures.map((failure) => failure.userId),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 6. Workflow Linkage
|
||||
if (currentWorkflowIds?.length && oldEventTypes.length) {
|
||||
await prisma.$transaction(async (tx) => {
|
||||
const eventTypeIds = oldEventTypes.map((e) => e.id);
|
||||
@@ -393,7 +417,7 @@ export default async function handleChildrenEventTypes({
|
||||
if (newRelationshipsToCreate.length > 0) {
|
||||
await tx.workflowsOnEventTypes.createMany({
|
||||
data: newRelationshipsToCreate,
|
||||
skipDuplicates: false,
|
||||
skipDuplicates: true,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user