fix: metadata is overwritten for child managed eventType when updating parent (#18059)

* fix: metadata is overwirten

* Update

* type error

* Update

* fix test

* fix type error
This commit is contained in:
Anik Dhabal Babu
2024-12-24 21:21:43 +05:30
committed by GitHub
parent e6ef4e1988
commit 9036080fcf
2 changed files with 48 additions and 9 deletions
@@ -7,6 +7,7 @@ import { describe, expect, it, vi } from "vitest";
import updateChildrenEventTypes from "@calcom/features/ee/managed-event-types/lib/handleChildrenEventTypes";
import { buildEventType } from "@calcom/lib/test/builder";
import type { Prisma } from "@calcom/prisma/client";
import { SchedulingType } from "@calcom/prisma/enums";
import type { CompleteEventType, CompleteWorkflowsOnEventTypes } from "@calcom/prisma/zod";
const mockFindFirstEventType = (data?: Partial<CompleteEventType>) => {
@@ -401,7 +402,27 @@ describe("handleChildrenEventTypes", () => {
} as CompleteWorkflowsOnEventTypes,
],
});
prismaMock.$transaction.mockResolvedValue([{ id: 2 }]);
// Mock the event type that will be returned for existing users
const mockUpdatedEventType = {
id: 2,
userId: 4,
timeZone: "UTC",
teamId: 1,
autoTranslateDescriptionEnabled: false,
secondaryEmailId: null,
schedulingType: SchedulingType.MANAGED,
requiresBookerEmailVerification: false,
lockTimeZoneToggleOnBookingPage: false,
useEventTypeDestinationCalendarEmail: false,
workflows: [],
parentId: 1,
locations: [],
...evType,
};
prismaMock.eventType.update.mockResolvedValue(mockUpdatedEventType);
await updateChildrenEventTypes({
eventTypeId: 1,
oldEventType: { children: [{ userId: 4 }], team: { name: "" } },
@@ -430,11 +451,7 @@ describe("handleChildrenEventTypes", () => {
parentId: 1,
userId: 5,
users: {
connect: [
{
id: 5,
},
],
connect: [{ id: 5 }],
},
workflows: {
create: [{ workflowId: 11 }],
@@ -7,6 +7,7 @@ import { getTranslation } from "@calcom/lib/server/i18n";
import type { PrismaClient } from "@calcom/prisma";
import { SchedulingType } from "@calcom/prisma/enums";
import { _EventTypeModel } from "@calcom/prisma/zod";
import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
import { allManagedEventTypeProps, unlockedManagedEventTypeProps } from "@calcom/prisma/zod-utils";
interface handleChildrenEventTypesProps {
@@ -225,9 +226,23 @@ export default async function handleChildrenEventTypes({
.filter(([key, _]) => key !== "children")
.reduce((newObj, [key, value]) => ({ ...newObj, [key]: value }), {});
// Update event types for old users
const oldEventTypes = await prisma.$transaction(
oldUserIds.map((userId) => {
return prisma.eventType.update({
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 = EventTypeMetaDataSchema.parse(existingEventType?.metadata || {});
return await prisma.eventType.update({
where: {
userId_parentId: {
userId,
@@ -242,6 +257,13 @@ export default async function handleChildrenEventTypes({
: {
deleteMany: {},
},
metadata: {
...(eventType.metadata as Prisma.JsonObject),
...(metadata?.multipleDuration && "length" in unlockedFieldProps
? { multipleDuration: metadata.multipleDuration }
: {}),
...(metadata?.apps && "apps" in unlockedFieldProps ? { apps: metadata.apps } : {}),
},
},
});
})