From f377da8a3f29c596d0967dff05c403b12f6cb935 Mon Sep 17 00:00:00 2001 From: Kshitija Kadam <65657373+Xitija@users.noreply.github.com> Date: Mon, 9 Sep 2024 17:23:38 +0530 Subject: [PATCH 1/4] refactor : minimized code and removed unnecessary repeat logic --- src/modules/event/event.service.ts | 164 +++++++++++++++-------------- 1 file changed, 85 insertions(+), 79 deletions(-) diff --git a/src/modules/event/event.service.ts b/src/modules/event/event.service.ts index c032132..1592520 100644 --- a/src/modules/event/event.service.ts +++ b/src/modules/event/event.service.ts @@ -362,7 +362,7 @@ export class EventService { const newRecurringEnd = newRecurrencePattern.endCondition.value; const oldRecurringEnd = oldRecurrencePattern.endCondition.value; - const newEndDate = new Date(newRecurringEnd); + const newRecEndDate = new Date(newRecurringEnd); const oldEndDate = new Date(oldRecurringEnd); // const nEndDate = newEndDate[0]; // const oEndDate = oldEndDate[0]; @@ -383,12 +383,7 @@ export class EventService { if ( newRecurrencePattern.frequency === Frequency.weekly && - newRecurrencePattern.frequency === oldRecurrencePattern.frequency && - newRecurrencePattern.interval === oldRecurrencePattern.interval && - compareArrays( - newRecurrencePattern.daysOfWeek, - oldRecurrencePattern.daysOfWeek, - ) + this.checkIfPatternIsSame(newRecurrencePattern, oldRecurrencePattern) ) { // new start date is passed if (nstartDate !== ostartDate && oldRecStartDate < currentDate) { @@ -399,7 +394,6 @@ export class EventService { // either add or subtract events as pattern is same currentEventRepetition['recurrencePattern'] = oldRecurrencePattern; - // remove lines of code and put it on out of the function if ( newRecurringStart === oldRecurringStart && @@ -409,7 +403,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 (oldRecStartDate > newEndDate) { + if (oldRecStartDate > newRecEndDate) { // end date is passed is less than recurring start date throw new BadRequestException( 'End date is passed is less than recurring start date', @@ -417,9 +411,14 @@ export class EventService { } // end date and time changed - if (newEndDate.getTime() > oldEndDate.getTime()) { + if (newRecEndDate.getTime() > oldEndDate.getTime()) { // add new events and update end date in recpattern // and save current event with new time + return await this.editThisAndFollowingEvents( + currentEventRepetition, + oldRecurrencePattern, + newRecurrencePattern, + ); currentEventRepetition.recurrencePattern.endCondition.value = newRecurrencePattern.endCondition.value; @@ -435,7 +434,7 @@ export class EventService { currentEventRepetition.updatedAt = new Date(); // delete old events - const removedEvents = await this.removeEventsInRange( + const removedEvents = await this.removeEventsMoreThanOrEqualToDate( currentEventRepetition.startDateTime, currentEventRepetition.eventId, ); @@ -453,14 +452,20 @@ export class EventService { ); return { removedEvents, newlyAddedEvents, updateRemainingEvents: 0 }; - } else if (newEndDate.getTime() < oldEndDate.getTime()) { + } else if (newRecEndDate.getTime() < oldEndDate.getTime()) { // remove events and update end date in recpattern + // time cannot be changed currentEventRepetition.recurrencePattern.endCondition.value = newRecurrencePattern.endCondition.value; - const removedEvents = await this.removeEventsInRange( - newEndDate, + return await this.editThisAndFollowingEvents( + currentEventRepetition, + oldRecurrencePattern, + newRecurrencePattern, + ); + const removedEvents = await this.removeEventsMoreThanOrEqualToDate( + newRecEndDate, currentEventRepetition.eventId, ); @@ -476,7 +481,7 @@ export class EventService { ) { updateRemainingEvents = await this.updateEventRepetitionTime( currentEventRepetition.startDateTime, - newEndDate, + newRecEndDate, [currentEventRepetition.eventId], currentEventRepetition.startDatetime.split('T')[1], currentEventRepetition.endDatetime.split('T')[1], @@ -501,12 +506,16 @@ export class EventService { } else if ( newRecStartDate < oldRecStartDate && newRecStartDate > currentDate && - newEndDate.getTime() === oldEndDate.getTime() + newRecEndDate.getTime() === oldEndDate.getTime() ) { // prepone events when new start date lies between current date and old start date // end date remains same // add events fully and update start date in recpattern + return await this.deleteOldAndRecreateNewEvents( + currentEventRepetition, + newRecurrencePattern, + ); currentEventRepetition['startDatetime'] = newRecurrencePattern.recurringStartDate; currentEventRepetition['endDatetime'] = @@ -517,11 +526,11 @@ export class EventService { newRecurrencePattern.recurringStartDate; currentEventRepetition.updatedAt = new Date(); - const removedEvents = await this.removeEventsLessInRange( + const removedEvents = await this.removeEventsLessThanOrEqualToDate( currentEventRepetition.startDateTime, currentEventRepetition.eventId, ); - const removedEvent = await this.removeEventsInRange( + const removedEvent = await this.removeEventsMoreThanOrEqualToDate( currentEventRepetition.startDateTime, currentEventRepetition.eventId, ); @@ -540,9 +549,16 @@ export class EventService { } else if ( newRecStartDate > oldRecStartDate && newRecStartDate > currentDate && - newEndDate.getTime() === oldEndDate.getTime() + newRecEndDate.getTime() === oldEndDate.getTime() ) { - // postpone events when new start date is after old start date + // postpone events when new start date is after old start date and is in future + // end date remains same + // remove events and update start date in recpattern + // modify time of all existing events if changed + return await this.deleteOldAndRecreateNewEvents( + currentEventRepetition, + newRecurrencePattern, + ); // Get all eventRepetationId which are are less than new recuurnecestartDate and delete all const removedEvent = await this.eventRepetitionRepository.find({ select: ['eventRepetitionId'], @@ -560,6 +576,7 @@ export class EventService { if (idsArray.length > 0) { await this.eventRepetitionRepository.delete(idsArray); } + // ****************** // update start date in recpattern const newEvent = await this.findEventById( currentEventRepetition.eventId, @@ -569,7 +586,7 @@ export class EventService { await this.eventRepository.save(newEvent); return { removedEvent, updateRemainingEvents: 0 }; } else if ( - newEndDate.getTime() !== oldEndDate.getTime() && + newRecEndDate.getTime() !== oldEndDate.getTime() && newRecStartDate > currentDate && newRecStartDate !== oldRecStartDate ) { @@ -578,39 +595,10 @@ export class EventService { // add events // update start date and end date in recpattern - 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 removedEvents = await this.removeEventsLessInRange( - currentEventRepetition.startDateTime, - currentEventRepetition.eventId, - ); - const removedEvent = await this.removeEventsInRange( - currentEventRepetition.startDateTime, - currentEventRepetition.eventId, - ); - - const newlyAddedEvents = await this.createRecurringEvents( + return await this.deleteOldAndRecreateNewEvents( currentEventRepetition, - currentEventRepetition.eventId, - currentEventRepetition.eventDetailId, - true, - ); - - const extUpdt = await this.updateEventRepetitionPattern( - currentEventRepetition.eventId, - currentEventRepetition.recurrencePattern, + newRecurrencePattern, ); - return { newlyAddedEvents: true }; } } else { // Frequency and interval are different @@ -722,7 +710,7 @@ export class EventService { newRecurrencePattern, ) { // remove upcoming events - const removedEvents = await this.removeEventsInRange( + const removedEvents = await this.removeEventsMoreThanOrEqualToDate( currentEventRepetition.startDateTime, currentEventRepetition.eventId, ); @@ -805,7 +793,7 @@ export class EventService { ); } - async removeEventsInRange(fromDate: Date, eventId: string) { + async removeEventsMoreThanOrEqualToDate(fromDate: Date, eventId: string) { const removedEvents = await this.eventRepetitionRepository.delete({ eventId: eventId, startDateTime: MoreThanOrEqual(fromDate), @@ -814,7 +802,7 @@ export class EventService { return removedEvents; } - async removeEventsLessInRange(fromDate: Date, eventId: string) { + async removeEventsLessThanOrEqualToDate(fromDate: Date, eventId: string) { const removedEvents = await this.eventRepetitionRepository.delete({ eventId: eventId, startDateTime: LessThanOrEqual(fromDate), @@ -823,6 +811,20 @@ export class EventService { return removedEvents; } + checkIfPatternIsSame(newRecurrencePattern, oldRecurrencePattern) { + if ( + newRecurrencePattern.frequency === oldRecurrencePattern.frequency && + newRecurrencePattern.interval === oldRecurrencePattern.interval && + compareArrays( + newRecurrencePattern.daysOfWeek, + oldRecurrencePattern.daysOfWeek, + ) + ) { + return true; + } + return false; + } + checkIfDateIsSame( newRecurrenceStartDt: string, oldRecurrenceStartDt: string, @@ -970,19 +972,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; - } - - new DateValidationPipe().transform(updateBody); - new RecurringEndDateValidationPipe().transform(updateBody); - if (!updateBody.recurrencePattern) { throw new BadRequestException( 'Recurrence pattern is required for recurring event', @@ -997,6 +986,19 @@ 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; + } + + new DateValidationPipe().transform(updateBody); + new RecurringEndDateValidationPipe().transform(updateBody); + // this.checkValidRecurrenceTimeForUpdate( // endDatetime, // updateBody.recurrencePattern.endCondition.value, @@ -1012,9 +1014,9 @@ export class EventService { event.recurrencePattern.endCondition.value, ); - const isWeekPatternSame = compareArrays( - updateBody.recurrencePattern.daysOfWeek, - event.recurrencePattern.daysOfWeek, + const isWeekPatternSame = this.checkIfPatternIsSame( + updateBody.recurrencePattern, + event.recurrencePattern, ); console.log(isWeekPatternSame, 'isWeekPatternSame', isDateTimeUpdate); @@ -1186,9 +1188,10 @@ export class EventService { let neweventDetailsId; const numberOfEntryInEventReperationTable = - await this.eventRepetitionRepository.find({ - where: { eventDetailId: eventRepetition.eventDetailId }, - }); + await this.getEventRepetitionOccurrences( + eventRepetition.eventDetailId, + ); + if (updateBody.onlineDetails) { Object.assign( repetationeventDetailexistingResult['meetingDetails'], @@ -1282,9 +1285,10 @@ export class EventService { } else { // check in event repetation table where existingEventDetails.eventDetailId aginst how many record exist const numberOfEntryInEventReperationTable = - await this.eventRepetitionRepository.find({ - where: { eventDetailId: existingEventDetails.eventDetailId }, - }); + await this.getEventRepetitionOccurrences( + existingEventDetails.eventDetailId, + ); + if (numberOfEntryInEventReperationTable.length === 1) { Object.assign(existingEventDetails, updateBody, { eventRepetitionId: eventRepetition.eventRepetitionId, @@ -1585,8 +1589,10 @@ export class EventService { return response; } - async getEventOccurrences(eventId: string): Promise { - return this.eventRepetitionRepository.find({ where: { eventId: eventId } }); + async getEventRepetitionOccurrences( + eventDetailId: string, + ): Promise { + return this.eventRepetitionRepository.find({ where: { eventDetailId } }); } async getEventDetails(eventDetailId: string): Promise { From a57e4d164221bc4a63684dcf9d439864ea24bad9 Mon Sep 17 00:00:00 2001 From: Kshitija Kadam <65657373+Xitija@users.noreply.github.com> Date: Wed, 11 Sep 2024 02:16:48 +0530 Subject: [PATCH 2/4] feat: create new event when date or time changed --- src/modules/event/event.service.ts | 505 ++++++++++++----------------- 1 file changed, 202 insertions(+), 303 deletions(-) diff --git a/src/modules/event/event.service.ts b/src/modules/event/event.service.ts index 1592520..9b13ee8 100644 --- a/src/modules/event/event.service.ts +++ b/src/modules/event/event.service.ts @@ -348,6 +348,15 @@ export class EventService { ); } + if ( + newRecurrencePattern.endCondition.type === EndConditionType.occurrences + ) { + // TODO: Implement end condition by occurrences + throw new NotImplementedException( + 'End condition by occurrences is not implemented yet', + ); + } + const currentDate = new Date(); const newRecurringStart = newRecurrencePattern.recurringStartDate; const newRecStartDate = new Date(newRecurringStart); @@ -367,78 +376,130 @@ export class EventService { // const nEndDate = newEndDate[0]; // const oEndDate = oldEndDate[0]; - if (nstartDate === ostartDate && nstartTime !== ostartTime) { - console.log(nstartTime, ostartTime, 'nstartTime !== ostartTime'); + // if (nstartDate === ostartDate && nstartTime !== ostartTime) { + // console.log(nstartTime, ostartTime, 'nstartTime !== ostartTime'); + // throw new BadRequestException( + // 'Recurring Start Time cannot be changed pass orignal start time', + // ); + // } + + // new End date is passed + + if (newRecEndDate < currentDate) { throw new BadRequestException( - 'Recurring Start Time cannot be changed pass orignal start time', + 'End Date cannot be changed because it is passed away', ); } - // new End date is passed - if (newRecurringEnd !== oldRecurringEnd && oldEndDate < currentDate) { + if (oldEndDate < currentDate) { throw new BadRequestException( 'End Date cannot be changed because it is passed away', ); } - if ( - newRecurrencePattern.frequency === Frequency.weekly && - this.checkIfPatternIsSame(newRecurrencePattern, oldRecurrencePattern) - ) { - // new start date is passed - if (nstartDate !== ostartDate && oldRecStartDate < currentDate) { - throw new BadRequestException( - 'Start Date cannot be changed because it is passed away', - ); - } + const isDateTimeUpdate = this.checkIfDateIsSame( + newRecurrencePattern.recurringStartDate, + oldRecurrencePattern.recurringStartDate, + newRecurrencePattern.endCondition.value, + oldRecurrencePattern.endCondition.value, + ); - // either add or subtract events as pattern is same - currentEventRepetition['recurrencePattern'] = oldRecurrencePattern; + const isWeekPatternSame = this.checkIfPatternIsSame( + newRecurrencePattern, + oldRecurrencePattern, + ); - if ( - newRecurringStart === oldRecurringStart && - newRecurringEnd !== oldRecurringEnd - ) { - // start date and time is same - // changed time of current event will take effect on following events - // no action on start dates but end date is different + console.log(isWeekPatternSame, 'isWeekPatternSame', isDateTimeUpdate); + + if (!isDateTimeUpdate.dateSame || !isWeekPatternSame) { + if (isWeekPatternSame) { + // new start date is passed + // if (nstartDate !== ostartDate && oldRecStartDate < currentDate) { + // throw new BadRequestException( + // 'Start Date cannot be changed because it is passed away', + // ); + // } + + // either add or subtract events as pattern is same + currentEventRepetition['recurrencePattern'] = oldRecurrencePattern; + + if ( + newRecurringStart === oldRecurringStart && + newRecurringEnd !== oldRecurringEnd + ) { + // start date and time is same + // changed time of current event will take effect on following events + // no action on start dates but end date is different + + if (newRecStartDate > newRecEndDate) { + // end date is passed is less than recurring start date + throw new BadRequestException( + 'End date is passed is less than recurring start date', + ); + } + + // end date and time changed + if (newRecEndDate.getTime() > oldEndDate.getTime()) { + // add new events and update end date in recpattern + // and save current event with new time + return await this.editThisAndFollowingEvents( + currentEventRepetition, + oldRecurrencePattern, + newRecurrencePattern, + ); + } else if (newRecEndDate.getTime() < oldEndDate.getTime()) { + // remove events and update end date in recpattern + // and save current event with new time + + currentEventRepetition.recurrencePattern.endCondition.value = + newRecurrencePattern.endCondition.value; + + return await this.editThisAndFollowingEvents( + currentEventRepetition, + oldRecurrencePattern, + newRecurrencePattern, + ); + } + } - if (oldRecStartDate > newRecEndDate) { - // end date is passed is less than recurring start date + // find out if start date is changed or end date is changed or both are changed + if (newRecStartDate < currentDate) { + // not possible because cannot create events in past throw error + // start date remains same throw new BadRequestException( - 'End date is passed is less than recurring start date', + 'Cannot update events prepone not allowed for past events', ); - } + } else if ( + newRecStartDate < oldRecStartDate && + newRecStartDate > currentDate && + newRecEndDate.getTime() === oldEndDate.getTime() + ) { + // prepone events when new start date lies between current date and old start date + // end date remains same + // add events fully and update start date in recpattern - // end date and time changed - if (newRecEndDate.getTime() > oldEndDate.getTime()) { - // add new events and update end date in recpattern - // and save current event with new time - return await this.editThisAndFollowingEvents( + return await this.deleteOldAndRecreateNewEvents( currentEventRepetition, - oldRecurrencePattern, newRecurrencePattern, ); - currentEventRepetition.recurrencePattern.endCondition.value = - newRecurrencePattern.endCondition.value; - currentEventRepetition['startDatetime'] = - currentEventRepetition['endDatetime'].split('T')[0] + - 'T' + - currentEventRepetition.startDatetime.split('T')[1]; + newRecurrencePattern.recurringStartDate; currentEventRepetition['endDatetime'] = - currentEventRepetition['endDatetime'].split('T')[0] + + currentEventRepetition['startDatetime'].split('T')[0] + 'T' + currentEventRepetition.endDatetime.split('T')[1]; + currentEventRepetition.recurrencePattern.recurringStartDate = + newRecurrencePattern.recurringStartDate; currentEventRepetition.updatedAt = new Date(); - - // delete old events - const removedEvents = await this.removeEventsMoreThanOrEqualToDate( + const removedEvents = await this.removeEventsLessThanOrEqualToDate( + currentEventRepetition.startDateTime, + currentEventRepetition.eventId, + ); + const removedEvent = await this.removeEventsMoreThanOrEqualToDate( currentEventRepetition.startDateTime, currentEventRepetition.eventId, ); - const newlyAddedEvents = await this.createRecurringEvents( currentEventRepetition, currentEventRepetition.eventId, @@ -450,183 +511,69 @@ export class EventService { currentEventRepetition.eventId, currentEventRepetition.recurrencePattern, ); - - return { removedEvents, newlyAddedEvents, updateRemainingEvents: 0 }; - } else if (newRecEndDate.getTime() < oldEndDate.getTime()) { - // remove events and update end date in recpattern - // time cannot be changed - - currentEventRepetition.recurrencePattern.endCondition.value = - newRecurrencePattern.endCondition.value; - - return await this.editThisAndFollowingEvents( + return { newlyAddedEvents: true }; + } else if ( + newRecStartDate > oldRecStartDate && + newRecStartDate > currentDate + // && + // newRecEndDate.getTime() === oldEndDate.getTime() + ) { + // postpone events when new start date is after old start date and is in future + // end date remains same + // remove events and update start date in recpattern + // modify time of all existing events if changed + console.log( + newRecStartDate, + currentDate, + 'newStartDate, currentDate', + 'postpone', + ); + return await this.deleteOldAndRecreateNewEvents( currentEventRepetition, - oldRecurrencePattern, newRecurrencePattern, ); - const removedEvents = await this.removeEventsMoreThanOrEqualToDate( - newRecEndDate, - currentEventRepetition.eventId, - ); - - // from current event to new end date if there are events - // and if time is changed then update time for following events - - let updateRemainingEvents; - if ( - currentEventRepetition.orignalEventStartTime !== - currentEventRepetition.startDatetime.split('T')[1] || - currentEventRepetition.orignalEventEndTime !== - currentEventRepetition.endDatetime.split('T')[1] - ) { - updateRemainingEvents = await this.updateEventRepetitionTime( - currentEventRepetition.startDateTime, - newRecEndDate, - [currentEventRepetition.eventId], - currentEventRepetition.startDatetime.split('T')[1], - currentEventRepetition.endDatetime.split('T')[1], + // Get all eventRepetationId which are are less than new recuurnecestartDate and delete all + } + } else { + // Frequency and interval are different + // make start date as end date for old events and create new events + 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 (newRecStartDate < currentDate) { + throw new BadRequestException( + 'Recurrence start date must be in future', ); } - - const extUpdt = await this.updateEventRepetitionPattern( - currentEventRepetition.eventId, - currentEventRepetition.recurrencePattern, + return await this.deleteOldAndRecreateNewEvents( + currentEventRepetition, + newRecurrencePattern, ); - return { removedEvents, updateRemainingEvents, newlyAddedEvents: 0 }; - } - } - - // find out if start date is changed or end date is changed or both are changed - 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 ( - newRecStartDate < oldRecStartDate && - newRecStartDate > currentDate && - newRecEndDate.getTime() === oldEndDate.getTime() - ) { - // prepone events when new start date lies between current date and old start date - // end date remains same - // add events fully and update start date in recpattern - - return await this.deleteOldAndRecreateNewEvents( - currentEventRepetition, - newRecurrencePattern, - ); - currentEventRepetition['startDatetime'] = - newRecurrencePattern.recurringStartDate; - currentEventRepetition['endDatetime'] = - currentEventRepetition['startDatetime'].split('T')[0] + - 'T' + - currentEventRepetition.endDatetime.split('T')[1]; - currentEventRepetition.recurrencePattern.recurringStartDate = - newRecurrencePattern.recurringStartDate; - - currentEventRepetition.updatedAt = new Date(); - const removedEvents = await this.removeEventsLessThanOrEqualToDate( - currentEventRepetition.startDateTime, - currentEventRepetition.eventId, - ); - const removedEvent = await this.removeEventsMoreThanOrEqualToDate( - currentEventRepetition.startDateTime, - currentEventRepetition.eventId, - ); - const newlyAddedEvents = await this.createRecurringEvents( - currentEventRepetition, - currentEventRepetition.eventId, - currentEventRepetition.eventDetailId, - true, - ); - - const extUpdt = await this.updateEventRepetitionPattern( - currentEventRepetition.eventId, - currentEventRepetition.recurrencePattern, - ); - return { newlyAddedEvents: true }; - } else if ( - newRecStartDate > oldRecStartDate && - newRecStartDate > currentDate && - newRecEndDate.getTime() === oldEndDate.getTime() - ) { - // postpone events when new start date is after old start date and is in future - // end date remains same - // remove events and update start date in recpattern - // modify time of all existing events if changed - return await this.deleteOldAndRecreateNewEvents( - currentEventRepetition, - newRecurrencePattern, - ); - // Get all eventRepetationId which are are less than new recuurnecestartDate and delete all - const removedEvent = await this.eventRepetitionRepository.find({ - select: ['eventRepetitionId'], - where: { - eventId: currentEventRepetition.eventId, - startDateTime: LessThan( - new Date(newRecurrencePattern.recurringStartDate), - ), - }, - }); - const idsArray = removedEvent.map( - (repetition) => repetition.eventRepetitionId, - ); - // remove events - if (idsArray.length > 0) { - await this.eventRepetitionRepository.delete(idsArray); - } - // ****************** - // update start date in recpattern - const newEvent = await this.findEventById( - currentEventRepetition.eventId, - ); + } else { + // old start date is less than current date that means event started in past - newEvent.recurrencePattern = newRecurrencePattern; - await this.eventRepository.save(newEvent); - return { removedEvent, updateRemainingEvents: 0 }; - } else if ( - newRecEndDate.getTime() !== oldEndDate.getTime() && - newRecStartDate > currentDate && - newRecStartDate !== oldRecStartDate - ) { - // start date is changed and end date is changed - // remove events - // add events - // update start date and end date in recpattern - - return await this.deleteOldAndRecreateNewEvents( - currentEventRepetition, - newRecurrencePattern, - ); - } - } else { - // Frequency and interval are different - // make start date as end date for old events and create new events - 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 - console.log(newRecStartDate, currentDate, 'newStartDate, currentDate'); - if (newRecStartDate < currentDate) { - throw new BadRequestException( - 'Recurrence start date must be in future', + return await this.editThisAndFollowingEvents( + currentEventRepetition, + oldRecurrencePattern, + newRecurrencePattern, ); } - return await this.deleteOldAndRecreateNewEvents( - currentEventRepetition, - newRecurrencePattern, - ); - } else { - // 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, - ); } + } else if (!isDateTimeUpdate.timeSame && isDateTimeUpdate.dateSame) { + // // just time is different so just update time + return await this.editThisAndFollowingEvents( + currentEventRepetition, + oldRecurrencePattern, + newRecurrencePattern, + ); + } else if (!isDateTimeUpdate.dateSame && !isDateTimeUpdate.timeSame) { + // both date and time are different + // when date is different regenerate new events + return await this.deleteOldAndRecreateNewEvents( + currentEventRepetition, + newRecurrencePattern, + ); } } @@ -723,7 +670,7 @@ export class EventService { ); oldRecurrencePattern.endCondition.value = - currentEventRepetition.startDatetime.split('T')[0]; + currentEventRepetition.startDatetime; const extUpdt = await this.updateEventRepetitionPattern( currentEventRepetition.eventId, @@ -972,60 +919,47 @@ export class EventService { ); } - if (!updateBody.recurrencePattern) { - throw new BadRequestException( - 'Recurrence pattern is required for recurring event', - ); - } else if ( - updateBody.recurrencePattern.endCondition.type === - EndConditionType.occurrences - ) { - // TODO: Implement end condition by occurrences - throw new NotImplementedException( - 'End condition by occurrences is not implemented yet', - ); - } + if (event.recurrencePattern?.frequency && updateBody.recurrencePattern) { + // throw new BadRequestException( + // 'Recurrence pattern is required for 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; - } + // 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); - - // 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( - updateBody.recurrencePattern.recurringStartDate, - event.recurrencePattern.recurringStartDate, - updateBody.recurrencePattern.endCondition.value, - event.recurrencePattern.endCondition.value, - ); + new DateValidationPipe().transform(updateBody); + new RecurringEndDateValidationPipe().transform(updateBody); - const isWeekPatternSame = this.checkIfPatternIsSame( - updateBody.recurrencePattern, - event.recurrencePattern, - ); + // this.checkValidRecurrenceTimeForUpdate( + // endDatetime, + // updateBody.recurrencePattern.endCondition.value, + // // startDatetime, + // // updateBody.recurrencePattern.recurringStartDate, + // ); - console.log(isWeekPatternSame, 'isWeekPatternSame', isDateTimeUpdate); - // when date is different regenerate new events - if ( - updateBody.recurrencePattern && - event.recurrencePattern?.frequency && - (!isDateTimeUpdate.dateSame || !isWeekPatternSame) - ) { + // compare date and time of old and new recurrence pattern + // const isDateTimeUpdate = this.checkIfDateIsSame( + // updateBody.recurrencePattern.recurringStartDate, + // event.recurrencePattern.recurringStartDate, + // updateBody.recurrencePattern.endCondition.value, + // event.recurrencePattern.endCondition.value, + // ); + + // const isWeekPatternSame = this.checkIfPatternIsSame( + // updateBody.recurrencePattern, + // event.recurrencePattern, + // ); + + // console.log(isWeekPatternSame, 'isWeekPatternSame', isDateTimeUpdate); + // // when date is different regenerate new events + // if (!isDateTimeUpdate.dateSame || !isWeekPatternSame) { // if date is different or pattern is different eventRepetition['orignalEventStartTime'] = startTimeOfCurrentEvent; @@ -1037,46 +971,11 @@ export class EventService { eventRepetition, ); console.log('updatedEvents', updatedEvents); - } else if (!isDateTimeUpdate.timeSame && isDateTimeUpdate.dateSame) { - // just time is different so just update time - - // update time in event table recurrence - const recurrenceRecords = await this.eventRepetitionRepository.find({ - where: { - eventId: eventId, - startDateTime: MoreThanOrEqual(eventRepetition.startDateTime), - }, - }); - const updateDateResult: { - startDateTime?: () => string; - endDateTime?: () => string; - updatedAt?: Date; - } = {}; - if ( - new Date(startDatetime).getTime() !== - new Date(eventRepetition.startDateTime).getTime() - ) { - updateDateResult.startDateTime = () => - `DATE_TRUNC('day', "startDateTime") + '${startTime}'::time`; - } - if ( - new Date(updateBody.endDatetime).getTime() !== - new Date(eventRepetition.endDateTime).getTime() - ) { - updateDateResult.endDateTime = () => - `DATE_TRUNC('day', "endDateTime") + '${endTime}'::time`; - } - updateDateResult.updatedAt = new Date(); - const result = await this.updateEventRepetition( - recurrenceRecords, - updateDateResult, - ); - - updateResult.recurrenceUpdate = result; + } else { + throw new BadRequestException('Recurrence pattern not found'); } - } - // Handle non-recurring events - if (startDatetime && endDatetime && !event.isRecurring) { + } else if (startDatetime && endDatetime && !event.isRecurring) { + // Handle non-recurring events new DateValidationPipe().transform(updateBody); eventRepetition.startDateTime = new Date(updateBody.startDatetime); eventRepetition.endDateTime = new Date(updateBody.endDatetime); @@ -1126,7 +1025,7 @@ export class EventService { Object.assign(existingEventDetails.metadata, updateBody.metadata); } - //below code for identify date like it is startRecuuring day or not + // below code for identify date like it is startRecurring day or not let eventStartDate; if (event.isRecurring) { eventStartDate = new Date(event.recurrencePattern.recurringStartDate); From f197e8123d35e712096d134fbd3b01ac8d6f85b6 Mon Sep 17 00:00:00 2001 From: Kshitija Kadam <65657373+Xitija@users.noreply.github.com> Date: Wed, 11 Sep 2024 10:54:46 +0530 Subject: [PATCH 3/4] chore : removed unwanted code --- src/modules/event/event.service.ts | 40 ++---------------------------- 1 file changed, 2 insertions(+), 38 deletions(-) diff --git a/src/modules/event/event.service.ts b/src/modules/event/event.service.ts index 9b13ee8..7584080 100644 --- a/src/modules/event/event.service.ts +++ b/src/modules/event/event.service.ts @@ -471,8 +471,7 @@ export class EventService { ); } else if ( newRecStartDate < oldRecStartDate && - newRecStartDate > currentDate && - newRecEndDate.getTime() === oldEndDate.getTime() + newRecStartDate > currentDate ) { // prepone events when new start date lies between current date and old start date // end date remains same @@ -482,36 +481,6 @@ export class EventService { currentEventRepetition, newRecurrencePattern, ); - currentEventRepetition['startDatetime'] = - newRecurrencePattern.recurringStartDate; - currentEventRepetition['endDatetime'] = - currentEventRepetition['startDatetime'].split('T')[0] + - 'T' + - currentEventRepetition.endDatetime.split('T')[1]; - currentEventRepetition.recurrencePattern.recurringStartDate = - newRecurrencePattern.recurringStartDate; - - currentEventRepetition.updatedAt = new Date(); - const removedEvents = await this.removeEventsLessThanOrEqualToDate( - currentEventRepetition.startDateTime, - currentEventRepetition.eventId, - ); - const removedEvent = await this.removeEventsMoreThanOrEqualToDate( - currentEventRepetition.startDateTime, - currentEventRepetition.eventId, - ); - const newlyAddedEvents = await this.createRecurringEvents( - currentEventRepetition, - currentEventRepetition.eventId, - currentEventRepetition.eventDetailId, - true, - ); - - const extUpdt = await this.updateEventRepetitionPattern( - currentEventRepetition.eventId, - currentEventRepetition.recurrencePattern, - ); - return { newlyAddedEvents: true }; } else if ( newRecStartDate > oldRecStartDate && newRecStartDate > currentDate @@ -522,12 +491,7 @@ export class EventService { // end date remains same // remove events and update start date in recpattern // modify time of all existing events if changed - console.log( - newRecStartDate, - currentDate, - 'newStartDate, currentDate', - 'postpone', - ); + return await this.deleteOldAndRecreateNewEvents( currentEventRepetition, newRecurrencePattern, From ef854520e0354cd4bed27bbf8f86cd3d9ff0a23e Mon Sep 17 00:00:00 2001 From: Kshitija Kadam <65657373+Xitija@users.noreply.github.com> Date: Wed, 11 Sep 2024 11:46:27 +0530 Subject: [PATCH 4/4] chore : resolved sonar cloud issues --- src/modules/event/event.service.ts | 80 ++---------------------------- 1 file changed, 5 insertions(+), 75 deletions(-) diff --git a/src/modules/event/event.service.ts b/src/modules/event/event.service.ts index 7584080..59b6066 100644 --- a/src/modules/event/event.service.ts +++ b/src/modules/event/event.service.ts @@ -11,9 +11,7 @@ import { InjectRepository } from '@nestjs/typeorm'; import { Repository, In, - Not, MoreThan, - LessThan, MoreThanOrEqual, LessThanOrEqual, Between, @@ -360,30 +358,14 @@ export class EventService { const currentDate = new Date(); const newRecurringStart = newRecurrencePattern.recurringStartDate; const newRecStartDate = new Date(newRecurringStart); - const nstartDateTime = newRecurringStart.split('T'); - const nstartDate = nstartDateTime[0]; - const nstartTime = nstartDateTime[1]; + const oldRecurringStart = oldRecurrencePattern.recurringStartDate; const oldRecStartDate = new Date(oldRecurringStart); - const ostartDateTime = oldRecurringStart.split('T'); - const ostartDate = ostartDateTime[0]; - const ostartTime = ostartDateTime[1]; const newRecurringEnd = newRecurrencePattern.endCondition.value; const oldRecurringEnd = oldRecurrencePattern.endCondition.value; const newRecEndDate = new Date(newRecurringEnd); const oldEndDate = new Date(oldRecurringEnd); - // const nEndDate = newEndDate[0]; - // 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', - // ); - // } - - // new End date is passed if (newRecEndDate < currentDate) { throw new BadRequestException( @@ -470,8 +452,9 @@ export class EventService { 'Cannot update events prepone not allowed for past events', ); } else if ( - newRecStartDate < oldRecStartDate && - newRecStartDate > currentDate + (newRecStartDate < oldRecStartDate && + newRecStartDate > currentDate) || + (newRecStartDate > oldRecStartDate && newRecStartDate > currentDate) ) { // prepone events when new start date lies between current date and old start date // end date remains same @@ -481,22 +464,6 @@ export class EventService { currentEventRepetition, newRecurrencePattern, ); - } else if ( - newRecStartDate > oldRecStartDate && - newRecStartDate > currentDate - // && - // newRecEndDate.getTime() === oldEndDate.getTime() - ) { - // postpone events when new start date is after old start date and is in future - // end date remains same - // remove events and update start date in recpattern - // modify time of all existing events if changed - - return await this.deleteOldAndRecreateNewEvents( - currentEventRepetition, - newRecurrencePattern, - ); - // Get all eventRepetationId which are are less than new recuurnecestartDate and delete all } } else { // Frequency and interval are different @@ -531,13 +498,6 @@ export class EventService { oldRecurrencePattern, newRecurrencePattern, ); - } else if (!isDateTimeUpdate.dateSame && !isDateTimeUpdate.timeSame) { - // both date and time are different - // when date is different regenerate new events - return await this.deleteOldAndRecreateNewEvents( - currentEventRepetition, - newRecurrencePattern, - ); } } @@ -860,8 +820,7 @@ export class EventService { const endDateTime = endDatetime.split('T'); const startDate = startDateTime[0]; const endDate = endDateTime[0]; - const startTime = startDateTime[1]; - const endTime = endDateTime[1]; + let updatedEvents; const startDateAndTimeOfCurrentEvent = eventRepetition.startDateTime @@ -884,10 +843,6 @@ export class EventService { } if (event.recurrencePattern?.frequency && updateBody.recurrencePattern) { - // throw new BadRequestException( - // 'Recurrence pattern is required for recurring event', - // ); - // undefined , past or equal to previously given date if ( updateBody.recurrencePattern.recurringStartDate == undefined || @@ -901,31 +856,6 @@ export class EventService { new DateValidationPipe().transform(updateBody); new RecurringEndDateValidationPipe().transform(updateBody); - // 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( - // updateBody.recurrencePattern.recurringStartDate, - // event.recurrencePattern.recurringStartDate, - // updateBody.recurrencePattern.endCondition.value, - // event.recurrencePattern.endCondition.value, - // ); - - // const isWeekPatternSame = this.checkIfPatternIsSame( - // updateBody.recurrencePattern, - // event.recurrencePattern, - // ); - - // console.log(isWeekPatternSame, 'isWeekPatternSame', isDateTimeUpdate); - // // when date is different regenerate new events - // if (!isDateTimeUpdate.dateSame || !isWeekPatternSame) { - // if date is different or pattern is different - eventRepetition['orignalEventStartTime'] = startTimeOfCurrentEvent; eventRepetition['orignalEventEndTime'] = endDateAndTimeOfCurrentEvent[1];