@@ -276,56 +276,58 @@ 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+
279280 // Update event types for old users
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- } ) ;
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+ } ) ;
293293
294- const metadata = eventTypeMetaDataSchemaWithTypedApps . parse ( existingEventType ?. metadata || { } ) ;
294+ const updatedOldEventTypesData = oldEventTypes . map ( ( et ) => {
295+ const metadata = eventTypeMetaDataSchemaWithTypedApps . parse ( et ?. metadata || { } ) ;
295296
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- ) ;
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+ } ) ;
320+
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+ } ) ;
328329
330+ // create workflows for old users if new workflows were added
329331 if ( currentWorkflowIds ?. length ) {
330332 await prisma . $transaction (
331333 currentWorkflowIds . flatMap ( ( wfId ) => {
0 commit comments