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 authored and GitHub committed 2024-12-24 21:21:43 +05:30
1 parent e6ef4e1988
commit 9036080fcf
2 files changed
+48 -9

No files matched your search

@@ -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 } : {}),
},
},
});
})