From 7b56c4b1a9a9f68fce33116897b086c5a15603ee Mon Sep 17 00:00:00 2001 From: Kshitija Kadam <65657373+Xitija@users.noreply.github.com> Date: Tue, 3 Sep 2024 17:37:44 +0530 Subject: [PATCH 1/9] chore : added startime validation --- src/common/pipes/event-validation.pipe.ts | 16 ++++++++++- src/modules/event/event.service.ts | 34 +++++++++++++---------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/common/pipes/event-validation.pipe.ts b/src/common/pipes/event-validation.pipe.ts index e2b0688..ed1a797 100644 --- a/src/common/pipes/event-validation.pipe.ts +++ b/src/common/pipes/event-validation.pipe.ts @@ -167,7 +167,21 @@ export class RecurringEndDateValidationPipe implements PipeTransform { throw new BadRequestException( 'Event End time does not match with Recurrence End time', ); - } // do for recurrence start time also in edit + } + + // do for recurrence start time also in edit + if (createEventDto instanceof UpdateEventDto) { + if ( + createEventDto.recurrencePattern.recurringStartDate.split( + 'T', + )[1] !== createEventDto.startDatetime.split('T')[1] + ) { + throw new BadRequestException( + 'Event Start time does not match with Recurrence Start time', + ); + } + } + // createEventDto.recurrencePattern.endCondition.value = endDate; } else if (endConditionType === EndConditionType.occurrences) { const occurrences = Number(endConditionValue); diff --git a/src/modules/event/event.service.ts b/src/modules/event/event.service.ts index e22f25c..bc33e3e 100644 --- a/src/modules/event/event.service.ts +++ b/src/modules/event/event.service.ts @@ -379,7 +379,7 @@ export class EventService { // new End date is passed if (newRecurringEnd !== oldRecurringEnd && oldEndDate < currentDate) { throw new BadRequestException( - 'End Date can not be changes beacuse it is passed away', + 'End Date can not be changes because it is passed away', ); } @@ -413,6 +413,7 @@ export class EventService { // no action on start dates but end date is different if (oldStartDate > newEndDate) { + // end date is passed is less than recurring start date throw new BadRequestException( 'End date is passed is less than recurring start date', ); @@ -620,7 +621,7 @@ export class EventService { // Frequency and interval are different // make start date as end date for old events and create new events if (oldStartDate > currentDate) { - //check newrecuurmnece startDate should be greater than cuurentDate + // check newrecurrence startDate should be greater than currentDate if (newStartDate < currentDate) { throw new BadRequestException( 'Recurrence start date must be in future', @@ -912,6 +913,17 @@ export class EventService { 'Invalid Date passed for this recurring event', ); } + + // undefined , past or equal to previously given date + if ( + updateBody.recurrencePattern.recurringStartDate == undefined || + !updateBody.recurrencePattern.recurringStartDate + ) { + // no start date is passed , make old date as start date + updateBody.recurrencePattern.recurringStartDate = + event.recurrencePattern.recurringStartDate; + } + new DateValidationPipe().transform(updateBody); new RecurringEndDateValidationPipe().transform(updateBody); @@ -929,16 +941,6 @@ export class EventService { ); } - // undefined , past or equal to previously given date - if ( - updateBody.recurrencePattern.recurringStartDate == undefined || - !updateBody.recurrencePattern.recurringStartDate - ) { - // no start date is passed , make old date as start date - updateBody.recurrencePattern.recurringStartDate = - event.recurrencePattern.recurringStartDate; - } - this.checkValidRecurrenceTimeForUpdate( endDatetime, updateBody.recurrencePattern.endCondition.value, @@ -954,17 +956,20 @@ export class EventService { event.recurrencePattern.endCondition.value, ); - const isWeekPatternChange = this.checkIfPatternIsSame( + const isWeekPatternSame = this.checkIfPatternIsSame( updateBody.recurrencePattern.daysOfWeek, event.recurrencePattern.daysOfWeek, ); + console.log(isWeekPatternSame, 'isWeekPatternSame', isDateTimeUpdate); // when date is different regenerate new events if ( updateBody.recurrencePattern && event.recurrencePattern?.frequency && - (!isDateTimeUpdate.dateSame || !isWeekPatternChange) + (!isDateTimeUpdate.dateSame || !isWeekPatternSame) ) { + console.log('hereeeeeeeeeeeeeee134566798'); + // if date is different or pattern is different eventRepetition['startTime'] = startTime; eventRepetition['endTime'] = endTime; eventRepetition['orignalEventStartTime'] = startTimeOfCurrentEvent; @@ -979,6 +984,7 @@ export class EventService { // just time is different so just update time else if (!isDateTimeUpdate.timeSame && isDateTimeUpdate.dateSame) { + console.log('hereeeeeeeeeeeeeee'); // update time in event table recurrence const recurrenceRecords = await this.eventRepetitionRepository.find({ where: { From 875399905c38c0d29564307aa3d91f1344363a37 Mon Sep 17 00:00:00 2001 From: Kshitija Kadam <65657373+Xitija@users.noreply.github.com> Date: Thu, 5 Sep 2024 18:41:13 +0530 Subject: [PATCH 2/9] chore : added validation for time check --- src/common/pipes/event-validation.pipe.ts | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/common/pipes/event-validation.pipe.ts b/src/common/pipes/event-validation.pipe.ts index ed1a797..a9fc2a3 100644 --- a/src/common/pipes/event-validation.pipe.ts +++ b/src/common/pipes/event-validation.pipe.ts @@ -160,28 +160,20 @@ export class RecurringEndDateValidationPipe implements PipeTransform { ); } + const endDateTime = endConditionValue.split('T'); + const endDate = endDateTime[0]; // recurring end date + if ( - endConditionValue.split('T')[1] !== - createEventDto.endDatetime.split('T')[1] + new Date(endConditionValue).getTime() !== + new Date( + endDate + 'T' + createEventDto.endDatetime.split('T')[1], + ).getTime() // compare with current event end time ) { throw new BadRequestException( 'Event End time does not match with Recurrence End time', ); } - // do for recurrence start time also in edit - if (createEventDto instanceof UpdateEventDto) { - if ( - createEventDto.recurrencePattern.recurringStartDate.split( - 'T', - )[1] !== createEventDto.startDatetime.split('T')[1] - ) { - throw new BadRequestException( - 'Event Start time does not match with Recurrence Start time', - ); - } - } - // createEventDto.recurrencePattern.endCondition.value = endDate; } else if (endConditionType === EndConditionType.occurrences) { const occurrences = Number(endConditionValue); From 829a77ab70bace2302177447f6e3250425253500 Mon Sep 17 00:00:00 2001 From: Kshitija Kadam <65657373+Xitija@users.noreply.github.com> Date: Thu, 5 Sep 2024 18:59:56 +0530 Subject: [PATCH 3/9] refactor : modified functions and fixed issues --- src/common/utils/functions.util.ts | 18 +- src/modules/event/event.service.ts | 377 +++++++++++++++++------------ 2 files changed, 231 insertions(+), 164 deletions(-) diff --git a/src/common/utils/functions.util.ts b/src/common/utils/functions.util.ts index 33f3d46..bd784b5 100644 --- a/src/common/utils/functions.util.ts +++ b/src/common/utils/functions.util.ts @@ -1,14 +1,16 @@ export const compareArrays = (a: any[], b: any[]): boolean => { - if (a.length !== b.length) return false; - else { - // Comparing each element of your array - for (let i = 0; i < a.length; i++) { - if (a[i] !== b[i]) { - return false; - } + if (a.length !== b.length) { + return false; + } + a.sort(); + b.sort(); + // Comparing each element of your array + for (let i = 0; i < a.length; i++) { + if (a[i] !== b[i]) { + return false; } - return true; } + return true; }; export const getNextDay = (currentDate: Date): Date => { diff --git a/src/modules/event/event.service.ts b/src/modules/event/event.service.ts index bc33e3e..8d883db 100644 --- a/src/modules/event/event.service.ts +++ b/src/modules/event/event.service.ts @@ -156,7 +156,7 @@ export class EventService { // Add isEnded key based on endDateTime const finalResult = result.map((event) => { delete event.total_count; - delete event.total_count; + const endDateTime = new Date(event.endDateTime); return { ...event, @@ -340,7 +340,7 @@ export class EventService { } } - async updateRecurringMeetings( + async updateRecurringEvents( newRecurrencePattern: RecurrencePatternDto, oldRecurrencePattern, currentEventRepetition, @@ -353,12 +353,12 @@ export class EventService { const currentDate = new Date(); const newRecurringStart = newRecurrencePattern.recurringStartDate; - const newStartDate = new Date(newRecurringStart); + const newRecStartDate = new Date(newRecurringStart); const nstartDateTime = newRecurringStart.split('T'); const nstartDate = nstartDateTime[0]; const nstartTime = nstartDateTime[1]; const oldRecurringStart = oldRecurrencePattern.recurringStartDate; - const oldStartDate = new Date(oldRecurringStart); + const oldRecStartDate = new Date(oldRecurringStart); const ostartDateTime = oldRecurringStart.split('T'); const ostartDate = ostartDateTime[0]; const ostartTime = ostartDateTime[1]; @@ -371,6 +371,7 @@ export class EventService { // const oEndDate = oldEndDate[0]; if (nstartDate === ostartDate && nstartTime !== ostartTime) { + console.log(nstartTime, ostartTime, 'nstartTime !== ostartTime'); throw new BadRequestException( 'Recurring Start Time cannot be changed pass orignal start time', ); @@ -379,7 +380,7 @@ export class EventService { // new End date is passed if (newRecurringEnd !== oldRecurringEnd && oldEndDate < currentDate) { throw new BadRequestException( - 'End Date can not be changes because it is passed away', + 'End Date cannot be changed because it is passed away', ); } @@ -393,16 +394,15 @@ export class EventService { ) ) { // new start date is passed - if (nstartDate !== ostartDate && oldStartDate < currentDate) { + if (nstartDate !== ostartDate && oldRecStartDate < currentDate) { throw new BadRequestException( - 'Start Date can not be changes beacuse it is passed away', + 'Start Date cannot be changed because it is passed away', ); } // either add or subtract events as pattern is same currentEventRepetition['recurrencePattern'] = oldRecurrencePattern; // remove lines of code and put it on out of the function - // check if new start date is greater than old start date if ( newRecurringStart === oldRecurringStart && @@ -412,7 +412,7 @@ export class EventService { // changed time of current event will take effect on following events // no action on start dates but end date is different - if (oldStartDate > newEndDate) { + if (oldRecStartDate > newEndDate) { // end date is passed is less than recurring start date throw new BadRequestException( 'End date is passed is less than recurring start date', @@ -467,7 +467,6 @@ export class EventService { currentEventRepetition.eventId, ); - // check if time of current event is modified // from current event to new end date if there are events // and if time is changed then update time for following events @@ -496,15 +495,15 @@ export class EventService { } // find out if start date is changed or end date is changed or both are changed - if (newStartDate < currentDate) { + if (newRecStartDate < currentDate) { // not possible because cannot create events in past throw error // start date remains same throw new BadRequestException( 'Cannot update events prepone not allowed for past events', ); } else if ( - newStartDate < oldStartDate && - newStartDate > currentDate && + newRecStartDate < oldRecStartDate && + newRecStartDate > currentDate && newEndDate.getTime() === oldEndDate.getTime() ) { // prepone events when new start date lies between current date and old start date @@ -542,8 +541,8 @@ export class EventService { ); return { newlyAddedEvents: true }; } else if ( - newStartDate > oldStartDate && - newStartDate > currentDate && + newRecStartDate > oldRecStartDate && + newRecStartDate > currentDate && newEndDate.getTime() === oldEndDate.getTime() ) { // postpone events when new start date is after old start date @@ -575,8 +574,8 @@ export class EventService { return { removedEvent, updateRemainingEvents: 0 }; } else if ( newEndDate.getTime() !== oldEndDate.getTime() && - newStartDate > currentDate && - newStartDate !== oldStartDate + newRecStartDate > currentDate && + newRecStartDate !== oldRecStartDate ) { // start date is changed and end date is changed // remove events @@ -620,104 +619,62 @@ export class EventService { } else { // Frequency and interval are different // make start date as end date for old events and create new events - if (oldStartDate > currentDate) { + if (oldRecStartDate > currentDate) { + // old start date is greater than current date that means event is in future // check newrecurrence startDate should be greater than currentDate - if (newStartDate < currentDate) { + console.log(newRecStartDate, currentDate, 'newStartDate, currentDate'); + if (newRecStartDate < currentDate) { throw new BadRequestException( 'Recurrence start date must be in future', ); } - const removedEvent = await this.eventRepetitionRepository.delete({ - eventId: currentEventRepetition.eventId, - }); - currentEventRepetition['recurrencePattern'] = newRecurrencePattern; - currentEventRepetition['startDatetime'] = - newRecurrencePattern.recurringStartDate; - currentEventRepetition['endDatetime'] = - currentEventRepetition['startDatetime'].split('T')[0] + - 'T' + - currentEventRepetition.endDatetime.split('T')[1]; - currentEventRepetition.recurrencePattern.recurringStartDate = - newRecurrencePattern.recurringStartDate; - currentEventRepetition.recurrencePattern.endCondition.value = - newRecurrencePattern.endCondition.value; - - currentEventRepetition.updatedAt = new Date(); - const newlyAddedEvents = await this.createRecurringEvents( + return await this.deleteOldAndRecreateNewEvents( currentEventRepetition, - currentEventRepetition.eventId, - currentEventRepetition.eventDetailId, - true, + newRecurrencePattern, ); - - const extUpdt = await this.updateEventRepetitionPattern( - currentEventRepetition.eventId, - currentEventRepetition.recurrencePattern, - ); - - return { removedEvent, newlyAddedEvents }; - // } } else { - const removedEvents = await this.removeEventsInRange( - currentEventRepetition.startDateTime, - currentEventRepetition.eventId, - ); - - // it update recuurence pattern in which update endDate - currentEventRepetition['recurrencePattern'] = oldRecurrencePattern; - currentEventRepetition.recurrencePattern.endCondition.value = - currentEventRepetition.startDatetime; - const extUpdt = await this.updateEventRepetitionPattern( - currentEventRepetition.eventId, - currentEventRepetition.recurrencePattern, + // old start date is less than current date that means event started in past + // throw new BadRequestException( + // 'Cannot update events prepone not allowed for past events', + // ); + return await this.editThisAndFollowingEvents( + currentEventRepetition, + oldRecurrencePattern, + newRecurrencePattern, ); + } + } + } - newRecurrencePattern.recurringStartDate = - currentEventRepetition.startDatetime; - currentEventRepetition['recurrencePattern'] = newRecurrencePattern; - - currentEventRepetition['startDatetime'] = - newRecurrencePattern.recurringStartDate; - - currentEventRepetition['endDatetime'] = - currentEventRepetition['startDatetime'].split('T')[0] + - 'T' + - currentEventRepetition.endDatetime.split('T')[1]; - - currentEventRepetition.recurrencePattern.recurringStartDate = - newRecurrencePattern.recurringStartDate; - currentEventRepetition.recurrencePattern.endCondition.value = - newRecurrencePattern.endCondition.value; + async createNewEventAndEventDetail( + eventId, + eventDetailId, + newRecurrencePattern, + ) { + // Create new event and eventDetail as per details of orignal event + const oldEvent = await this.eventRepository.findOne({ + where: { eventId }, + }); + oldEvent.eventId = undefined; // so that new event is created and new id is generated for it + oldEvent.createdAt = new Date(); + oldEvent.updatedAt = new Date(); - currentEventRepetition.updatedAt = new Date(); + const oldEventDetail = await this.eventDetailRepository.findOne({ + where: { eventDetailId }, + }); + oldEventDetail.eventDetailId = undefined; // so that new eventDetail is created and new id is generated for it + oldEventDetail.createdAt = new Date(); + oldEventDetail.updatedAt = new Date(); - //Create new event and eventDetail - const oldEvent = await this.eventRepository.findOne({ - where: { eventId: currentEventRepetition.eventId }, - }); - delete oldEvent.eventId; + const newEventDetail = + await this.eventDetailRepository.save(oldEventDetail); + oldEvent.eventDetailId = newEventDetail.eventDetailId; + oldEvent.recurrencePattern = newRecurrencePattern; + const newEvent = await this.eventRepository.save(oldEvent); - const oldEventDetail = await this.eventDetailRepository.findOne({ - where: { eventDetailId: currentEventRepetition.eventDetailId }, - }); - delete oldEventDetail.eventDetailId; - const newEventDetail = - await this.eventDetailRepository.save(oldEventDetail); - oldEvent.eventDetailId = newEventDetail.eventDetailId; - oldEvent.recurrencePattern = newRecurrencePattern; - const newEvent = await this.eventRepository.save(oldEvent); - currentEventRepetition.eventId = newEvent.eventId; - currentEventRepetition.eventDetailId = newEventDetail.eventDetailId; + console.log('newEvent', newEvent, 'newEventDetail', newEventDetail); - const newlyAddedEvents = await this.createRecurringEvents( - currentEventRepetition, - currentEventRepetition.eventId, - currentEventRepetition.eventDetailId, - true, - ); - return { removedEvents, newlyAddedEvents }; - } - } + return { newEvent, newEventDetail }; } async updateEventRepetitionPattern(eventId, repetitionPattern) { @@ -727,10 +684,114 @@ export class EventService { }, { recurrencePattern: repetitionPattern, + updatedAt: new Date(), }, ); } + async deleteOldAndRecreateNewEvents( + currentEventRepetition, + newRecurrencePattern, + ) { + console.log('deleteOldAndRecreateNewEvents'); + // delete old events associated with the eventId + const removedEvents = await this.eventRepetitionRepository.delete({ + eventId: currentEventRepetition.eventId, + }); + currentEventRepetition['recurrencePattern'] = newRecurrencePattern; + currentEventRepetition['startDatetime'] = + newRecurrencePattern.recurringStartDate; + currentEventRepetition['endDatetime'] = + currentEventRepetition['startDatetime'].split('T')[0] + + 'T' + + currentEventRepetition.endDatetime.split('T')[1]; + + currentEventRepetition.updatedAt = new Date(); + + // create new events + const newlyAddedEvents = await this.createRecurringEvents( + currentEventRepetition, + currentEventRepetition.eventId, + currentEventRepetition.eventDetailId, + true, + ); + + const extUpdt = await this.updateEventRepetitionPattern( + currentEventRepetition.eventId, + currentEventRepetition.recurrencePattern, + ); + + return { removedEvents, newlyAddedEvents }; + } + + async editThisAndFollowingEvents( + currentEventRepetition, + oldRecurrencePattern, + newRecurrencePattern, + ) { + console.log('editThisAndFollowingEvents'); + // remove upcoming events + const removedEvents = await this.removeEventsInRange( + currentEventRepetition.startDateTime, + currentEventRepetition.eventId, + ); + + // update recurrence pattern in which update endDate so that old event ends on new start date + // currentEventRepetition['recurrencePattern'] = oldRecurrencePattern; + // set start of new event as end of old event + console.log( + oldRecurrencePattern.endCondition.value, + 'oldRecurrencePattern.endCondition.value', + ); + + oldRecurrencePattern.endCondition.value = + currentEventRepetition.startDatetime.split('T')[0]; + // + 'T'; + // oldRecurrencePattern.endCondition.value.split('T')[1]; + + const extUpdt = await this.updateEventRepetitionPattern( + currentEventRepetition.eventId, + oldRecurrencePattern, + ); + + newRecurrencePattern.recurringStartDate = + currentEventRepetition.startDatetime; + currentEventRepetition['recurrencePattern'] = newRecurrencePattern; + + // currentEventRepetition['startDatetime'] = + // newRecurrencePattern.recurringStartDate; + + currentEventRepetition['endDatetime'] = + currentEventRepetition['startDatetime'].split('T')[0] + + 'T' + + currentEventRepetition.endDatetime.split('T')[1]; + + // currentEventRepetition.recurrencePattern.recurringStartDate = + // newRecurrencePattern.recurringStartDate; + // currentEventRepetition.recurrencePattern.endCondition.value = + // newRecurrencePattern.endCondition.value; + + currentEventRepetition.updatedAt = new Date(); + + const { newEvent, newEventDetail } = + await this.createNewEventAndEventDetail( + currentEventRepetition.eventId, + currentEventRepetition.eventDetailId, + newRecurrencePattern, + ); + + currentEventRepetition.eventId = newEvent.eventId; + currentEventRepetition.eventDetailId = newEventDetail.eventDetailId; + + const newlyAddedEvents = await this.createRecurringEvents( + currentEventRepetition, + currentEventRepetition.eventId, + currentEventRepetition.eventDetailId, + true, + ); + return { removedEvents, newlyAddedEvents }; + } + async updateEventRepetitionTime( fromDate, toDate, @@ -821,20 +882,6 @@ export class EventService { } } - checkIfPatternIsSame(newdaysOfWeek, olddaysOfWeek) { - if (newdaysOfWeek.length !== olddaysOfWeek.length) { - return false; - } - newdaysOfWeek.sort(); - olddaysOfWeek.sort(); - for (let i = 0; i <= newdaysOfWeek.length; i++) { - if (newdaysOfWeek[i] !== olddaysOfWeek[i]) { - return false; - } - } - return true; - } - checkValidRecurrenceTimeForUpdate(endDate, recurrenceEndDate) { if (endDate.split('T')[1] !== recurrenceEndDate.split('T')[1]) { throw new BadRequestException( @@ -843,36 +890,24 @@ export class EventService { } } - async handleAllEventUpdate( - updateBody: UpdateEventDto, - event: Events, - eventRepetition: EventRepetition, - ) { - const eventId = event.eventId; - const eventDetailId = event.eventDetailId; - - updateBody.isRecurring = event.isRecurring; - const { startDatetime, endDatetime } = updateBody; - - eventRepetition['startDatetime'] = startDatetime; - eventRepetition['endDatetime'] = endDatetime; - - let updateResult: UpdateResult = {}; - - const recurrenceRecords = await this.eventRepetitionRepository + async getRecurrenceRecords(eventId, eventRepetitionStartDateTime) { + return await this.eventRepetitionRepository .createQueryBuilder('eventRepetition') .innerJoinAndSelect('eventRepetition.eventDetail', 'eventDetail') .where('eventRepetition.eventId = :eventId', { eventId }) .andWhere('eventRepetition.startDateTime >= :startDateTime', { - startDateTime: eventRepetition.startDateTime, + startDateTime: eventRepetitionStartDateTime, }) .andWhere('eventDetail.status != :status', { status: 'archived' }) .getMany(); + } - //Get event which eventDetailId is diffrent from main eventDetailId from eventRepetation table[use for delete] - const startDateTimes = eventRepetition.startDateTime; - - const upcomingrecurrenceRecords = await this.eventRepetitionRepository + async getUpcomingRecurrenceRecords( + eventId, + eventDetailId, + eventRepetitionStartDateTime, + ) { + return await this.eventRepetitionRepository .createQueryBuilder('eventRepetition') .innerJoinAndSelect('eventRepetition.eventDetail', 'eventDetail') .where('eventRepetition.eventId = :eventId', { eventId }) @@ -880,10 +915,40 @@ export class EventService { eventDetailId, }) .andWhere('eventRepetition.startDateTime >= :startDateTime', { - startDateTime: startDateTimes, + startDateTime: eventRepetitionStartDateTime, }) .andWhere('eventDetail.status != :status', { status: 'archived' }) .getMany(); + } + + async handleAllEventUpdate( + updateBody: UpdateEventDto, + event: Events, + eventRepetition: EventRepetition, + ) { + const eventId = event.eventId; + const eventDetailId = event.eventDetailId; + + updateBody.isRecurring = event.isRecurring; + const { startDatetime, endDatetime } = updateBody; + + eventRepetition['startDatetime'] = startDatetime; + eventRepetition['endDatetime'] = endDatetime; + + let updateResult: UpdateResult = {}; + + const recurrenceRecords = await this.getRecurrenceRecords( + eventId, + eventRepetition.startDateTime, + ); + + // Get event which eventDetailId is diffrent from main eventDetailId from eventRepetation table[use for delete] + + const upcomingrecurrenceRecords = await this.getUpcomingRecurrenceRecords( + eventId, + eventDetailId, + eventRepetition.startDateTime, + ); // Handle recurring events if (startDatetime && endDatetime && event.isRecurring) { @@ -941,12 +1006,12 @@ export class EventService { ); } - this.checkValidRecurrenceTimeForUpdate( - endDatetime, - updateBody.recurrencePattern.endCondition.value, - // startDatetime, - // updateBody.recurrencePattern.recurringStartDate, - ); + // this.checkValidRecurrenceTimeForUpdate( + // endDatetime, + // updateBody.recurrencePattern.endCondition.value, + // // startDatetime, + // // updateBody.recurrencePattern.recurringStartDate, + // ); // compare date and time of old and new recurrence pattern const isDateTimeUpdate = this.checkIfDateIsSame( @@ -956,7 +1021,7 @@ export class EventService { event.recurrencePattern.endCondition.value, ); - const isWeekPatternSame = this.checkIfPatternIsSame( + const isWeekPatternSame = compareArrays( updateBody.recurrencePattern.daysOfWeek, event.recurrencePattern.daysOfWeek, ); @@ -970,20 +1035,18 @@ export class EventService { ) { console.log('hereeeeeeeeeeeeeee134566798'); // if date is different or pattern is different - eventRepetition['startTime'] = startTime; - eventRepetition['endTime'] = endTime; + eventRepetition['orignalEventStartTime'] = startTimeOfCurrentEvent; eventRepetition['orignalEventEndTime'] = endDateAndTimeOfCurrentEvent[1]; - updatedEvents = await this.updateRecurringMeetings( + updatedEvents = await this.updateRecurringEvents( updateBody.recurrencePattern, event.recurrencePattern, eventRepetition, ); - } - - // just time is different so just update time - else if (!isDateTimeUpdate.timeSame && isDateTimeUpdate.dateSame) { + console.log('updatedEvents', updatedEvents); + } else if (!isDateTimeUpdate.timeSame && isDateTimeUpdate.dateSame) { + // just time is different so just update time console.log('hereeeeeeeeeeeeeee'); // update time in event table recurrence const recurrenceRecords = await this.eventRepetitionRepository.find({ @@ -1125,7 +1188,7 @@ export class EventService { // create new entry for new updated record which connect all upcoming and this event if (eventRepetition.eventDetailId === event.eventDetailId) { Object.assign(existingEventDetails, updateBody); - delete existingEventDetails.eventDetailId; + existingEventDetails.eventDetailId = undefined; const saveNewEntry = await this.eventDetailRepository.save(existingEventDetails); neweventDetailsId = saveNewEntry.eventDetailId; @@ -1190,7 +1253,7 @@ export class EventService { Object.assign(repetationeventDetailexistingResult, updateBody, { eventRepetitionId: eventRepetition.eventRepetitionId, }); - delete repetationeventDetailexistingResult.eventDetailId; + repetationeventDetailexistingResult.eventDetailId = undefined; const result = await this.eventDetailRepository.save( repetationeventDetailexistingResult, ); @@ -1257,7 +1320,8 @@ export class EventService { Object.assign(existingEventDetails, updateBody, { eventRepetitionId: eventRepetition.eventRepetitionId, }); - delete existingEventDetails.eventDetailId; + existingEventDetails.eventDetailId = undefined; + const result = await this.eventDetailRepository.save(existingEventDetails); eventRepetition.eventDetailId = result.eventDetailId; @@ -1282,7 +1346,7 @@ export class EventService { Object.assign(existingEventDetails, updateBody, { eventRepetitionId: eventRepetition.eventRepetitionId, }); - delete existingEventDetails.eventDetailId; + existingEventDetails.eventDetailId = undefined; const result = await this.eventDetailRepository.save(existingEventDetails); eventRepetition.eventDetailId = result.eventDetailId; @@ -1376,6 +1440,7 @@ export class EventService { if (recurrencePattern?.endCondition?.value) { recurrencePattern.recurringStartDate = createEventDto.startDatetime; + recurrencePattern.daysOfWeek.sort(); } event.isRecurring = isRecurring; // event.recurrenceEndDate = recurrenceEndDate From 77e4759ff681c8d825a6298c030edfeaa80983c8 Mon Sep 17 00:00:00 2001 From: Kshitija Kadam <65657373+Xitija@users.noreply.github.com> Date: Thu, 5 Sep 2024 19:04:50 +0530 Subject: [PATCH 4/9] chore : minor clean up --- src/modules/event/event.service.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/modules/event/event.service.ts b/src/modules/event/event.service.ts index 8d883db..5cb621f 100644 --- a/src/modules/event/event.service.ts +++ b/src/modules/event/event.service.ts @@ -693,7 +693,6 @@ export class EventService { currentEventRepetition, newRecurrencePattern, ) { - console.log('deleteOldAndRecreateNewEvents'); // delete old events associated with the eventId const removedEvents = await this.eventRepetitionRepository.delete({ eventId: currentEventRepetition.eventId, @@ -729,7 +728,6 @@ export class EventService { oldRecurrencePattern, newRecurrencePattern, ) { - console.log('editThisAndFollowingEvents'); // remove upcoming events const removedEvents = await this.removeEventsInRange( currentEventRepetition.startDateTime, @@ -737,7 +735,6 @@ export class EventService { ); // update recurrence pattern in which update endDate so that old event ends on new start date - // currentEventRepetition['recurrencePattern'] = oldRecurrencePattern; // set start of new event as end of old event console.log( oldRecurrencePattern.endCondition.value, @@ -1033,7 +1030,6 @@ export class EventService { event.recurrencePattern?.frequency && (!isDateTimeUpdate.dateSame || !isWeekPatternSame) ) { - console.log('hereeeeeeeeeeeeeee134566798'); // if date is different or pattern is different eventRepetition['orignalEventStartTime'] = startTimeOfCurrentEvent; @@ -1047,7 +1043,7 @@ export class EventService { console.log('updatedEvents', updatedEvents); } else if (!isDateTimeUpdate.timeSame && isDateTimeUpdate.dateSame) { // just time is different so just update time - console.log('hereeeeeeeeeeeeeee'); + // update time in event table recurrence const recurrenceRecords = await this.eventRepetitionRepository.find({ where: { From f19fd963ed0da0ba04665f634f7625f1ff979328 Mon Sep 17 00:00:00 2001 From: Kshitija Kadam <65657373+Xitija@users.noreply.github.com> Date: Thu, 5 Sep 2024 19:13:16 +0530 Subject: [PATCH 5/9] chore : added types --- src/common/utils/functions.util.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/utils/functions.util.ts b/src/common/utils/functions.util.ts index bd784b5..02762d2 100644 --- a/src/common/utils/functions.util.ts +++ b/src/common/utils/functions.util.ts @@ -1,4 +1,4 @@ -export const compareArrays = (a: any[], b: any[]): boolean => { +export const compareArrays = (a: number[], b: number[]): boolean => { if (a.length !== b.length) { return false; } From 0ccc6239d9bf2f2da1ab0577fda6c9f568f4a4e2 Mon Sep 17 00:00:00 2001 From: Kshitija Kadam <65657373+Xitija@users.noreply.github.com> Date: Thu, 5 Sep 2024 19:18:34 +0530 Subject: [PATCH 6/9] refactor : improved sort --- src/common/utils/functions.util.ts | 4 ++-- src/modules/event/event.service.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/common/utils/functions.util.ts b/src/common/utils/functions.util.ts index 02762d2..709ee5d 100644 --- a/src/common/utils/functions.util.ts +++ b/src/common/utils/functions.util.ts @@ -2,8 +2,8 @@ export const compareArrays = (a: number[], b: number[]): boolean => { if (a.length !== b.length) { return false; } - a.sort(); - b.sort(); + a.sort((a, b) => a - b); + b.sort((a, b) => a - b); // Comparing each element of your array for (let i = 0; i < a.length; i++) { if (a[i] !== b[i]) { diff --git a/src/modules/event/event.service.ts b/src/modules/event/event.service.ts index 5cb621f..d0f6182 100644 --- a/src/modules/event/event.service.ts +++ b/src/modules/event/event.service.ts @@ -1436,7 +1436,7 @@ export class EventService { if (recurrencePattern?.endCondition?.value) { recurrencePattern.recurringStartDate = createEventDto.startDatetime; - recurrencePattern.daysOfWeek.sort(); + recurrencePattern.daysOfWeek.sort((a, b) => a - b); } event.isRecurring = isRecurring; // event.recurrenceEndDate = recurrenceEndDate From 4ba655aff8d5b4d80409d6b450c5d88f39c8730b Mon Sep 17 00:00:00 2001 From: Kshitija Kadam <65657373+Xitija@users.noreply.github.com> Date: Fri, 6 Sep 2024 11:43:13 +0530 Subject: [PATCH 7/9] refactor: commented attendees addition code and added update event repetition function --- src/modules/event/event.service.ts | 89 ++++++++++++------------------ 1 file changed, 34 insertions(+), 55 deletions(-) diff --git a/src/modules/event/event.service.ts b/src/modules/event/event.service.ts index d0f6182..72d7a22 100644 --- a/src/modules/event/event.service.ts +++ b/src/modules/event/event.service.ts @@ -85,14 +85,14 @@ export class EventService { // createEventDto.createdBy, // ); - // new approach of adding attendees against eventId - await this.attendeesService.createAttendeesForEvents( - createEventDto.attendees, - createdEvent.res.eventId, - createEventDto.createdBy, - ); + // TODO: new approach of adding attendees + // await this.attendeesService.createAttendeesForEvents( + // createEventDto.attendees, + // createdEvent.res.eventId, + // createEventDto.createdBy, + // ); } else { - throw new NotImplementedException(); + throw new NotImplementedException('Public events not implemented!'); // if event is public then registrationDate is required if (createEventDto.eventType === 'online') { // create online event @@ -811,6 +811,17 @@ export class EventService { ); } + async updateEventRepetition(recurrenceRecords: EventRepetition[], set) { + return await this.eventRepetitionRepository.update( + { + eventRepetitionId: In( + recurrenceRecords.map((record) => record.eventRepetitionId), + ), + }, + set, + ); + } + async removeEventsInRange(fromDate: Date, eventId: string) { const removedEvents = await this.eventRepetitionRepository.delete({ eventId: eventId, @@ -1071,14 +1082,11 @@ export class EventService { `DATE_TRUNC('day', "endDateTime") + '${endTime}'::time`; } updateDateResult.updatedAt = new Date(); - const result = await this.eventRepetitionRepository.update( - { - eventRepetitionId: In( - recurrenceRecords.map((record) => record.eventRepetitionId), - ), - }, + const result = await this.updateEventRepetition( + recurrenceRecords, updateDateResult, ); + updateResult.recurrenceUpdate = result; } } @@ -1106,13 +1114,8 @@ export class EventService { updateData.erMetaData = eventRepetition.erMetaData; updateResult.erMetaData = updateBody.erMetaData; } - - await this.eventRepetitionRepository.update( - { - eventRepetitionId: In( - recurrenceRecords.map((record) => record.eventRepetitionId), - ), - }, + const result = await this.updateEventRepetition( + recurrenceRecords, updateData, ); } @@ -1146,7 +1149,7 @@ export class EventService { } else { eventStartDate = new Date(eventRepetition.startDateTime); } - //if startrecuuring or startDate is equal to passed eventRepetationId startDate + // if startrecurring or startDate is equal to passed eventRepetationId startDate if ( eventRepetition.startDateTime.toISOString().split('T')[0] === eventStartDate.toISOString().split('T')[0] @@ -1161,14 +1164,9 @@ export class EventService { updateResult.eventDetails = result; // below code run for update of recurring event if (recurrenceRecords.length > 0) { - const result = await this.eventRepetitionRepository.update( - { - eventRepetitionId: In( - recurrenceRecords.map((record) => record.eventRepetitionId), - ), - }, - { eventDetailId: event.eventDetailId }, - ); + const result = await this.updateEventRepetition(recurrenceRecords, { + eventDetailId: event.eventDetailId, + }); } // delete eventDetail from eventDetail table if futher created single-single for upcoming session if (upcomingrecurrenceRecords.length > 0) { @@ -1180,34 +1178,20 @@ export class EventService { } } else { // Not going in this condition if event is non recurring - let neweventDetailsId; // create new entry for new updated record which connect all upcoming and this event if (eventRepetition.eventDetailId === event.eventDetailId) { Object.assign(existingEventDetails, updateBody); existingEventDetails.eventDetailId = undefined; const saveNewEntry = await this.eventDetailRepository.save(existingEventDetails); - neweventDetailsId = saveNewEntry.eventDetailId; + updateResult.eventDetails = saveNewEntry; - //repeated code - // const upcomingrecurrenceRecords = await this.eventRepetitionRepository.find({ - // where: { - // eventId: eventId, - // eventDetailId: Not(eventDetailId), - // startDateTime: MoreThanOrEqual(startDateTimes), - // }, - // }); // update eventDetail id in all places which are greater than and equal to curreitn repetation startDate in repetation table if (recurrenceRecords.length > 0) { - const result = await this.eventRepetitionRepository.update( - { - eventRepetitionId: In( - recurrenceRecords.map((record) => record.eventRepetitionId), - ), - }, - { eventDetailId: neweventDetailsId }, - ); + const result = await this.updateEventRepetition(recurrenceRecords, { + eventDetailId: saveNewEntry.eventDetailId, + }); } // delete eventDetail from eventDetail table if futher created single-single for upcoming session if (upcomingrecurrenceRecords.length > 0) { @@ -1259,14 +1243,9 @@ export class EventService { // update eventDetail id in all places which are greater than and equal to curreitn repetation startDate in repetation table if (recurrenceRecords.length > 0) { - const result = await this.eventRepetitionRepository.update( - { - eventRepetitionId: In( - recurrenceRecords.map((record) => record.eventRepetitionId), - ), - }, - { eventDetailId: neweventDetailsId }, - ); + const result = await this.updateEventRepetition(recurrenceRecords, { + eventDetailId: neweventDetailsId, + }); } } } From e8bfdaf8ef71e8e1a85515b56c36a17c2ec66704 Mon Sep 17 00:00:00 2001 From: Kshitija Kadam <65657373+Xitija@users.noreply.github.com> Date: Fri, 6 Sep 2024 11:53:58 +0530 Subject: [PATCH 8/9] chore : remove commented code --- src/modules/event/event.service.ts | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/modules/event/event.service.ts b/src/modules/event/event.service.ts index 72d7a22..75b8383 100644 --- a/src/modules/event/event.service.ts +++ b/src/modules/event/event.service.ts @@ -743,8 +743,6 @@ export class EventService { oldRecurrencePattern.endCondition.value = currentEventRepetition.startDatetime.split('T')[0]; - // + 'T'; - // oldRecurrencePattern.endCondition.value.split('T')[1]; const extUpdt = await this.updateEventRepetitionPattern( currentEventRepetition.eventId, @@ -755,19 +753,11 @@ export class EventService { currentEventRepetition.startDatetime; currentEventRepetition['recurrencePattern'] = newRecurrencePattern; - // currentEventRepetition['startDatetime'] = - // newRecurrencePattern.recurringStartDate; - currentEventRepetition['endDatetime'] = currentEventRepetition['startDatetime'].split('T')[0] + 'T' + currentEventRepetition.endDatetime.split('T')[1]; - // currentEventRepetition.recurrencePattern.recurringStartDate = - // newRecurrencePattern.recurringStartDate; - // currentEventRepetition.recurrencePattern.endCondition.value = - // newRecurrencePattern.endCondition.value; - currentEventRepetition.updatedAt = new Date(); const { newEvent, newEventDetail } = From 1b574fb98d543105e82c4480c6d149aa9ea26064 Mon Sep 17 00:00:00 2001 From: Kshitija Kadam <65657373+Xitija@users.noreply.github.com> Date: Fri, 6 Sep 2024 12:30:20 +0530 Subject: [PATCH 9/9] refactor : added database methods and removed repeat lines --- src/modules/event/event.service.ts | 82 ++++++++++++++---------------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/src/modules/event/event.service.ts b/src/modules/event/event.service.ts index 75b8383..c032132 100644 --- a/src/modules/event/event.service.ts +++ b/src/modules/event/event.service.ts @@ -289,16 +289,15 @@ export class EventService { // when id does not exist or event date is passed throw new BadRequestException(ERROR_MESSAGES.EVENT_NOT_FOUND); } - const isEventArchived = await this.eventDetailRepository.findOne({ - where: { eventDetailId: eventRepetition.eventDetailId }, - }); + const isEventArchived = await this.getEventDetails( + eventRepetition.eventDetailId, + ); if (isEventArchived.status === 'archived') { throw new BadRequestException('Event is archived you can not Edit'); } - const event = await this.eventRepository.findOne({ - where: { eventId: eventRepetition.eventId }, - }); + const event = await this.findEventById(eventRepetition.eventId); + // condition for prevent non recuring event if (!event.isRecurring && !updateBody.isMainEvent) { throw new BadRequestException( @@ -306,9 +305,7 @@ export class EventService { ); } - const eventDetail = await this.eventDetailRepository.findOne({ - where: { eventDetailId: event.eventDetailId }, - }); + const eventDetail = await this.getEventDetails(event.eventDetailId); const validationResult = this.isInvalidUpdate(updateBody, eventDetail); if (!validationResult.isValid) { @@ -564,11 +561,10 @@ export class EventService { await this.eventRepetitionRepository.delete(idsArray); } // update start date in recpattern - const newEvent = await this.eventRepository.findOne({ - where: { - eventId: currentEventRepetition.eventId, - }, - }); + const newEvent = await this.findEventById( + currentEventRepetition.eventId, + ); + newEvent.recurrencePattern = newRecurrencePattern; await this.eventRepository.save(newEvent); return { removedEvent, updateRemainingEvents: 0 }; @@ -652,16 +648,13 @@ export class EventService { newRecurrencePattern, ) { // Create new event and eventDetail as per details of orignal event - const oldEvent = await this.eventRepository.findOne({ - where: { eventId }, - }); + const oldEvent = await this.findEventById(eventId); + oldEvent.eventId = undefined; // so that new event is created and new id is generated for it oldEvent.createdAt = new Date(); oldEvent.updatedAt = new Date(); - const oldEventDetail = await this.eventDetailRepository.findOne({ - where: { eventDetailId }, - }); + const oldEventDetail = await this.getEventDetails(eventDetailId); oldEventDetail.eventDetailId = undefined; // so that new eventDetail is created and new id is generated for it oldEventDetail.createdAt = new Date(); oldEventDetail.updatedAt = new Date(); @@ -1119,9 +1112,8 @@ export class EventService { updateBody.onlineDetails || updateBody.metadata ) { - const existingEventDetails = await this.eventDetailRepository.findOne({ - where: { eventDetailId: eventDetailId }, - }); + const existingEventDetails = await this.getEventDetails(eventDetailId); + if (updateBody.onlineDetails) { Object.assign( existingEventDetails.meetingDetails, @@ -1160,11 +1152,9 @@ export class EventService { } // delete eventDetail from eventDetail table if futher created single-single for upcoming session if (upcomingrecurrenceRecords.length > 0) { - await this.eventDetailRepository.delete({ - eventDetailId: In( - upcomingrecurrenceRecords.map((record) => record.eventDetailId), - ), - }); + await this.deleteEventDetail( + upcomingrecurrenceRecords.map((record) => record.eventDetailId), + ); } } else { // Not going in this condition if event is non recurring @@ -1185,18 +1175,15 @@ export class EventService { } // delete eventDetail from eventDetail table if futher created single-single for upcoming session if (upcomingrecurrenceRecords.length > 0) { - await this.eventDetailRepository.delete({ - eventDetailId: In( - upcomingrecurrenceRecords.map((record) => record.eventDetailId), - ), - }); + await this.deleteEventDetail( + upcomingrecurrenceRecords.map((record) => record.eventDetailId), + ); } } else { //do change in existing eventDetail row [eventRepetition.eventDetails me] table const repetationeventDetailexistingResult = - await this.eventDetailRepository.findOne({ - where: { eventDetailId: eventRepetition.eventDetailId }, - }); + await this.getEventDetails(eventRepetition.eventDetailId); + let neweventDetailsId; const numberOfEntryInEventReperationTable = await this.eventRepetitionRepository.find({ @@ -1204,7 +1191,7 @@ export class EventService { }); if (updateBody.onlineDetails) { Object.assign( - repetationeventDetailexistingResult.meetingDetails, + repetationeventDetailexistingResult['meetingDetails'], updateBody.onlineDetails, ); } @@ -1261,9 +1248,8 @@ export class EventService { updateResult.repetationDetail = eventRepetition; } const eventDetailId = eventRepetition.eventDetailId; - const existingEventDetails = await this.eventDetailRepository.findOne({ - where: { eventDetailId: eventDetailId }, - }); + const existingEventDetails = await this.getEventDetails(eventDetailId); + existingEventDetails.updatedAt = new Date(); if ( @@ -1603,6 +1589,14 @@ export class EventService { return this.eventRepetitionRepository.find({ where: { eventId: eventId } }); } + async getEventDetails(eventDetailId: string): Promise { + return this.eventDetailRepository.findOne({ where: { eventDetailId } }); + } + + async findEventById(eventId: string): Promise { + return this.eventRepository.findOne({ where: { eventId } }); + } + generateEventOccurences( createEventDto: CreateEventDto, eventDetailId: string, @@ -1727,8 +1721,10 @@ export class EventService { return this.eventRepository.delete({ eventId }); } - async deleteEventDetail(eventDetailId: string): Promise { - return this.eventDetailRepository.delete({ eventDetailId }); + async deleteEventDetail(eventDetailIds: string[]): Promise { + return this.eventDetailRepository.delete({ + eventDetailId: In(eventDetailIds), + }); } async removePartiallyCreatedData( @@ -1737,7 +1733,7 @@ export class EventService { ): Promise[]> { const promises = [ this.deleteEvent(eventId), - this.deleteEventDetail(eventDetailId), + this.deleteEventDetail([eventDetailId]), ]; const responses = await Promise.allSettled(promises);