@@ -276,58 +276,56 @@ export default async function handleChildrenEventTypes({
276276 const updatePayloadFiltered = Object . entries ( updatePayload )
277277 . filter ( ( [ key , _ ] ) => key !== "children" )
278278 . reduce ( ( newObj , [ key , value ] ) => ( { ...newObj , [ key ] : value } ) , { } ) ;
279-
280279 // Update event types for old users
281- const oldEventTypes = await prisma . eventType . findMany ( {
282- where : {
283- parentId : parentId ,
284- userId : { in : oldUserIds } ,
285- } ,
286- select : {
287- id : true ,
288- metadata : true ,
289- userId : true ,
290- parentId : true ,
291- } ,
292- } ) ;
293-
294- const updatedOldEventTypesData = oldEventTypes . map ( ( et ) => {
295- const metadata = eventTypeMetaDataSchemaWithTypedApps . parse ( et ?. metadata || { } ) ;
280+ const oldEventTypes = await Promise . all (
281+ oldUserIds . map ( async ( userId ) => {
282+ const existingEventType = await prisma . eventType . findUnique ( {
283+ where : {
284+ userId_parentId : {
285+ userId,
286+ parentId,
287+ } ,
288+ } ,
289+ select : {
290+ metadata : true ,
291+ } ,
292+ } ) ;
296293
297- return {
298- ...updatePayloadFiltered ,
299- hidden : children ?. find ( ( ch ) => ch . owner . id === et . userId ) ?. hidden ?? false ,
300- ...( "schedule" in unlockedFieldProps ? { } : { scheduleId : eventType . scheduleId || null } ) ,
301- restrictionScheduleId : null ,
302- useBookerTimezone : false ,
303- hashedLink :
304- "multiplePrivateLinks" in unlockedFieldProps
305- ? undefined
306- : {
307- deleteMany : { } ,
308- } ,
309- allowReschedulingCancelledBookings :
310- managedEventTypeValues . allowReschedulingCancelledBookings ?? false ,
311- metadata : {
312- ...( eventType . metadata as Prisma . JsonObject ) ,
313- ...( metadata ?. multipleDuration && "length" in unlockedFieldProps
314- ? { multipleDuration : metadata . multipleDuration }
315- : { } ) ,
316- ...( metadata ?. apps && "apps" in unlockedFieldProps ? { apps : metadata . apps } : { } ) ,
317- } ,
318- } ;
319- } ) ;
294+ const metadata = eventTypeMetaDataSchemaWithTypedApps . parse ( existingEventType ?. metadata || { } ) ;
320295
321- // Update old event types data
322- await prisma . eventType . updateMany ( {
323- where : {
324- userId : { in : oldUserIds } ,
325- parentId : parentId ,
326- } ,
327- data : updatedOldEventTypesData ,
328- } ) ;
296+ return await prisma . eventType . update ( {
297+ where : {
298+ userId_parentId : {
299+ userId,
300+ parentId,
301+ } ,
302+ } ,
303+ data : {
304+ ...updatePayloadFiltered ,
305+ hidden : children ?. find ( ( ch ) => ch . owner . id === userId ) ?. hidden ?? false ,
306+ ...( "schedule" in unlockedFieldProps ? { } : { scheduleId : eventType . scheduleId || null } ) ,
307+ restrictionScheduleId : null ,
308+ useBookerTimezone : false ,
309+ hashedLink :
310+ "multiplePrivateLinks" in unlockedFieldProps
311+ ? undefined
312+ : {
313+ deleteMany : { } ,
314+ } ,
315+ allowReschedulingCancelledBookings :
316+ managedEventTypeValues . allowReschedulingCancelledBookings ?? false ,
317+ metadata : {
318+ ...( eventType . metadata as Prisma . JsonObject ) ,
319+ ...( metadata ?. multipleDuration && "length" in unlockedFieldProps
320+ ? { multipleDuration : metadata . multipleDuration }
321+ : { } ) ,
322+ ...( metadata ?. apps && "apps" in unlockedFieldProps ? { apps : metadata . apps } : { } ) ,
323+ } ,
324+ } ,
325+ } ) ;
326+ } )
327+ ) ;
329328
330- // create workflows for old users if new workflows were added
331329 if ( currentWorkflowIds ?. length ) {
332330 await prisma . $transaction (
333331 currentWorkflowIds . flatMap ( ( wfId ) => {
0 commit comments