@@ -15,7 +15,7 @@ import {
1515 Machinery ,
1616 ScheduleSlot ,
1717 notGuest ,
18- isSameDay ,
18+ isSameDayUTC ,
1919 EventInstance ,
2020 SlackMentionType
2121} from 'shared' ;
@@ -274,6 +274,7 @@ export default class CalendarService {
274274 description ?: string ,
275275 mention ?: SlackMentionType
276276 ) : Promise < Event > {
277+ if ( ! title . trim ( ) ) throw new HttpException ( 400 , 'Title cannot be only whitespace' ) ;
277278 // Validate eventTypeId
278279 const foundEventType = await prisma . event_Type . findUnique ( {
279280 where : { eventTypeId }
@@ -502,11 +503,7 @@ export default class CalendarService {
502503
503504 if ( foundEventType . sendSlackNotifications ) {
504505 const members = await prisma . user . findMany ( {
505- where : {
506- userId : {
507- in : optionalMemberIds . concat ( allRequiredMembers )
508- }
509- }
506+ where : { userId : { in : optionalMemberIds . concat ( allRequiredMembers ) } }
510507 } ) ;
511508
512509 // get the user settings for all the members invited, who are leaderingship
@@ -604,6 +601,7 @@ export default class CalendarService {
604601 zoomLink ?: string ,
605602 description ?: string
606603 ) : Promise < Event > {
604+ if ( ! title . trim ( ) ) throw new HttpException ( 400 , 'Title cannot be only whitespace' ) ;
607605 // validate eventId
608606 const foundEvent = await prisma . event . findUnique ( {
609607 where : { eventId } ,
@@ -799,13 +797,12 @@ export default class CalendarService {
799797 const edittedEvent = eventTransformer ( updatedEvent ) ;
800798
801799 if ( status === Event_Status . SCHEDULED && foundEventType . sendSlackNotifications ) {
802- await sendEventScheduledSlackNotif ( updatedEvent . notificationSlackThreads , edittedEvent ) ;
800+ await sendEventScheduledSlackNotif ( updatedEvent . notificationSlackThreads , edittedEvent , true ) ;
803801 }
804802
805803 if ( status === Event_Status . CONFIRMED && foundEventType . sendSlackNotifications ) {
806804 await sendEventConfirmationToThread ( updatedEvent . notificationSlackThreads , updatedEvent . userCreated ) ;
807805 }
808-
809806 return edittedEvent ;
810807 }
811808
@@ -941,7 +938,11 @@ export default class CalendarService {
941938 userCreatedId : true ,
942939 location : true ,
943940 dateDeleted : true ,
944- approved : true
941+ approved : true ,
942+ status : true ,
943+ title : true ,
944+ workPackages : true ,
945+ scheduledTimes : true
945946 }
946947 } ) ;
947948
@@ -1422,10 +1423,48 @@ export default class CalendarService {
14221423
14231424 if ( ! event ) throw new NotFoundException ( 'Event' , eventId ) ;
14241425 if ( event . dateDeleted ) throw new DeletedException ( 'Event' , eventId ) ;
1425-
1426- // Cannot schedule an already scheduled event
14271426 if ( event . status === Event_Status . SCHEDULED ) {
1428- throw new HttpException ( 400 , 'Event is already scheduled' ) ;
1427+ const timeSlots = await prisma . schedule_Slot . findMany ( {
1428+ where : { eventId : event . eventId }
1429+ } ) ;
1430+
1431+ // Restore the old scheduled time from confirmed members' availabilities
1432+ // so they get their time back from the old scheduled event
1433+ for ( const slot of timeSlots ) {
1434+ if ( ! slot . startTime || ! slot . endTime ) continue ;
1435+ const startHour = new Date ( slot . startTime ) . getHours ( ) ;
1436+ const endHour = new Date ( slot . endTime ) . getHours ( ) ;
1437+
1438+ for ( const member of event . confirmedMembers ) {
1439+ if ( ! member . drScheduleSettings ) continue ;
1440+ const existingAvailability = member . drScheduleSettings . availabilities . find ( ( a ) =>
1441+ isSameDayUTC ( a . dateSet , slot . startTime )
1442+ ) ;
1443+ if ( ! existingAvailability ) continue ;
1444+ // Availability index i represents local hour (10 + i); remove indices that fall within [startHour, endHour)
1445+ const returnedAvailability = Array . from ( { length : endHour - startHour } , ( _ , i ) => startHour + i - 10 ) . filter (
1446+ ( i ) => i >= 0
1447+ ) ;
1448+
1449+ const updatedAvailability = [ ...new Set ( [ ...existingAvailability . availability , ...returnedAvailability ] ) ] . sort (
1450+ ( a , b ) => a - b
1451+ ) ;
1452+
1453+ await prisma . availability . update ( {
1454+ where : { availabilityId : existingAvailability . availabilityId } ,
1455+ data : { availability : updatedAvailability }
1456+ } ) ;
1457+ }
1458+ }
1459+
1460+ await prisma . event . update ( {
1461+ where : { eventId : event . eventId } ,
1462+ data : { status : Event_Status . SCHEDULED }
1463+ } ) ;
1464+
1465+ await prisma . schedule_Slot . deleteMany ( {
1466+ where : { eventId : event . eventId }
1467+ } ) ;
14291468 }
14301469
14311470 // Only the event creator can schedule the event
@@ -1462,9 +1501,12 @@ export default class CalendarService {
14621501 allDay : false
14631502 }
14641503 } ,
1504+
1505+ initialDateScheduled : event . initialDateScheduled ?? null ,
14651506 approved : hasConflict ? Conflict_Status . PENDING : event . approved ,
14661507 approvalRequiredFromUserId : hasConflict ? conflictingEvent ?. userCreated . userId : event . approvalRequiredFromUserId
14671508 } ,
1509+
14681510 ...getEventQueryArgs ( organization . organizationId )
14691511 } ) ;
14701512
@@ -1474,7 +1516,7 @@ export default class CalendarService {
14741516 const endHour = endTime . getHours ( ) ;
14751517 for ( const member of event . confirmedMembers ) {
14761518 if ( ! member . drScheduleSettings ) continue ;
1477- const existingAvailability = member . drScheduleSettings . availabilities . find ( ( a ) => isSameDay ( a . dateSet , startTime ) ) ;
1519+ const existingAvailability = member . drScheduleSettings . availabilities . find ( ( a ) => isSameDayUTC ( a . dateSet , startTime ) ) ;
14781520 if ( ! existingAvailability ) continue ;
14791521 // Availability index i represents local hour (10 + i); remove indices that fall within [startHour, endHour)
14801522 const updatedAvailability = existingAvailability . availability . filter (
@@ -1492,9 +1534,12 @@ export default class CalendarService {
14921534 } ) ;
14931535
14941536 if ( foundEventType ?. sendSlackNotifications ) {
1495- await sendEventScheduledSlackNotif ( updatedEvent . notificationSlackThreads , eventTransformer ( updatedEvent ) ) ;
1537+ await sendEventScheduledSlackNotif (
1538+ updatedEvent . notificationSlackThreads ,
1539+ eventTransformer ( updatedEvent ) ,
1540+ event . status === Event_Status . SCHEDULED
1541+ ) ;
14961542 }
1497-
14981543 return eventTransformer ( updatedEvent ) ;
14991544 }
15001545
0 commit comments