From d5d321b183abd672ea7d6e140918e2f63d7b087c Mon Sep 17 00:00:00 2001 From: Kshitija Kadam <65657373+Xitija@users.noreply.github.com> Date: Thu, 19 Sep 2024 06:40:35 +0000 Subject: [PATCH 1/2] refactor : fixed regression --- src/modules/event/dto/create-event.dto.ts | 2 +- src/modules/event/dto/update-event.dto.ts | 2 + src/modules/event/event.service.ts | 50 +++++++++++++++-------- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/modules/event/dto/create-event.dto.ts b/src/modules/event/dto/create-event.dto.ts index 0d56db7..c3e86dd 100644 --- a/src/modules/event/dto/create-event.dto.ts +++ b/src/modules/event/dto/create-event.dto.ts @@ -131,7 +131,7 @@ export class RecurrencePatternDto { @Validate(EndsWithZConstraint) @IsDateString({ strict: true, strictSeparator: true }) - @IsOptional() + // @IsOptional() recurringStartDate: string; @ApiProperty({ diff --git a/src/modules/event/dto/update-event.dto.ts b/src/modules/event/dto/update-event.dto.ts index 213c368..9887e4a 100644 --- a/src/modules/event/dto/update-event.dto.ts +++ b/src/modules/event/dto/update-event.dto.ts @@ -28,6 +28,8 @@ export interface UpdateResult { eventDetails?: any; repetationDetail?: any; recurrenceUpdate?: any; + updatedRecurringEvent?: any; + updatedEventDetails?: any; } export class UpdateEventDto { diff --git a/src/modules/event/event.service.ts b/src/modules/event/event.service.ts index 89a5f59..ebc4f82 100644 --- a/src/modules/event/event.service.ts +++ b/src/modules/event/event.service.ts @@ -797,13 +797,30 @@ export class EventService { eventRepetition['startDatetime'] = startDatetime; eventRepetition['endDatetime'] = endDatetime; - let updateResult; - // : UpdateResult = {}; + let updateResult: UpdateResult = {}; let updatedEvents; let eventAndEventDetails; + eventAndEventDetails = {}; + eventAndEventDetails['newEvent'] = event; + eventAndEventDetails['newEventDetail'] = eventDetail; + + if ( + (!startDatetime || !endDatetime) && + updateBody.recurrencePattern && + event.isRecurring + ) { + throw new BadRequestException('Please Provide Valid Start and End Date'); + } // Handle recurring events if (startDatetime && endDatetime && event.isRecurring) { + // check if rec is passed + if (!updateBody.recurrencePattern) { + throw new BadRequestException( + 'Recurring Pattern is missing for this event', + ); + } + const startDateTime = startDatetime.split('T'); const endDateTime = endDatetime.split('T'); const startDate = startDateTime[0]; @@ -903,7 +920,7 @@ export class EventService { updateData.erMetaData = eventRepetition.erMetaData; updateResult.erMetaData = updateBody.erMetaData; } - updateResult['updatedRecurringEvent'] = await this.updateEventRepetition( + updateResult.updatedRecurringEvent = await this.updateEventRepetition( recurrenceRecords, updateData, ); @@ -918,13 +935,14 @@ export class EventService { updateBody.onlineDetails || updateBody.metadata ) { - updateResult = await this.updateEventDetailsForRecurringEvents( - updateBody, - recurrenceRecords, - eventAndEventDetails.newEvent, - eventAndEventDetails.newEventDetail, - eventRepetition, - ); + updateResult.updatedEventDetails = + await this.updateEventDetailsForRecurringEvents( + updateBody, + recurrenceRecords, + eventAndEventDetails.newEvent, + eventAndEventDetails.newEventDetail, + eventRepetition, + ); } return updateResult; } @@ -969,13 +987,13 @@ export class EventService { await this.eventDetailRepository.save(existingEventDetails); // below code run for update of recurring event if (recurrenceRecords.length > 0) { - const updateResult = await this.updateEventRepetition( + const updatedEventRepetition = await this.updateEventRepetition( recurrenceRecords, { eventDetailId: event.eventDetailId, }, ); - updateResult['updatedEvents'] = updateResult.affected; + updateResult['updatedEvents'] = updatedEventRepetition.affected; } // delete eventDetail from eventDetail table if futher created single-single for upcoming session if (upcomingrecurrenceRecords.length > 0) { @@ -995,13 +1013,13 @@ 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 updateResult = await this.updateEventRepetition( + const updatedEventRepetition = await this.updateEventRepetition( recurrenceRecords, { eventDetailId: saveNewEntry.eventDetailId, }, ); - updateResult['updatedEvents'] = updateResult.affected; + updateResult['updatedEvents'] = updatedEventRepetition.affected; } // delete eventDetail from eventDetail table if futher created single-single for upcoming session if (upcomingrecurrenceRecords.length > 0) { @@ -1053,13 +1071,13 @@ 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 updateResult = await this.updateEventRepetition( + const updatedEventRepetition = await this.updateEventRepetition( recurrenceRecords, { eventDetailId: neweventDetailsId, }, ); - updateResult['updatedEvents'] = updateResult.affected; + updateResult['updatedEvents'] = updatedEventRepetition.affected; } } } From 4a4bce64db4b6ce382821f3eb443c253a35de867 Mon Sep 17 00:00:00 2001 From: Kshitija Kadam <65657373+Xitija@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:29:54 +0000 Subject: [PATCH 2/2] refactor : sonar suggestions --- src/modules/event/event.service.ts | 158 ++++++++++++++--------------- 1 file changed, 75 insertions(+), 83 deletions(-) diff --git a/src/modules/event/event.service.ts b/src/modules/event/event.service.ts index ebc4f82..5a302a9 100644 --- a/src/modules/event/event.service.ts +++ b/src/modules/event/event.service.ts @@ -16,6 +16,8 @@ import { MoreThanOrEqual, LessThanOrEqual, Between, + DeleteResult, + InsertResult, } from 'typeorm'; import { Events } from './entities/event.entity'; import { Response } from 'express'; @@ -35,12 +37,11 @@ import { RepetitionDetail, } from 'src/common/utils/types'; import { ConfigService } from '@nestjs/config'; -import { DeleteResult, InsertResult } from 'typeorm'; import { DateValidationPipe, RecurringEndDateValidationPipe, } from 'src/common/pipes/event-validation.pipe'; -import { compareArrays, getNextDay } from 'src/common/utils/functions.util'; +import { compareArrays } from 'src/common/utils/functions.util'; @Injectable() export class EventService { @@ -276,67 +277,61 @@ export class EventService { response: Response, ) { const apiId = API_ID.UPDATE_EVENT; - try { - // Event repetition record must not be of passed date - const currentTimestamp = new Date(); - // To do optimize both cases in one queries - const eventRepetition = await this.eventRepetitionRepository.findOne({ - where: { eventRepetitionId, startDateTime: MoreThan(currentTimestamp) }, - }); + // Event repetition record must not be of passed date + const currentTimestamp = new Date(); + // To do optimize both cases in one queries + const eventRepetition = await this.eventRepetitionRepository.findOne({ + where: { eventRepetitionId, startDateTime: MoreThan(currentTimestamp) }, + }); - if (!eventRepetition) { - // when id does not exist or event date is passed - throw new BadRequestException(ERROR_MESSAGES.EVENT_NOT_FOUND); - } - const isEventArchived = await this.getEventDetails( - eventRepetition.eventDetailId, - ); - if (isEventArchived.status === 'archived') { - throw new BadRequestException( - ERROR_MESSAGES.CANNOT_EDIT_ARCHIVED_EVENTS, - ); - } + if (!eventRepetition) { + // when id does not exist or event date is passed + throw new BadRequestException(ERROR_MESSAGES.EVENT_NOT_FOUND); + } + const isEventArchived = await this.getEventDetails( + eventRepetition.eventDetailId, + ); + if (isEventArchived.status === 'archived') { + throw new BadRequestException(ERROR_MESSAGES.CANNOT_EDIT_ARCHIVED_EVENTS); + } - const event = await this.findEventById(eventRepetition.eventId); + const event = await this.findEventById(eventRepetition.eventId); - // condition for prevent non recuring event - if (!event.isRecurring && !updateBody.isMainEvent) { - throw new BadRequestException( - ERROR_MESSAGES.CANNOT_PASS_MAIN_EVENT_FALSE, - ); - } + // condition for prevent non recuring event + if (!event.isRecurring && !updateBody.isMainEvent) { + throw new BadRequestException( + ERROR_MESSAGES.CANNOT_PASS_MAIN_EVENT_FALSE, + ); + } - const eventDetail = await this.getEventDetails(event.eventDetailId); + const eventDetail = await this.getEventDetails(event.eventDetailId); - const validationResult = this.isInvalidUpdate(updateBody, eventDetail); - if (!validationResult.isValid) { - throw new BadRequestException(validationResult.message); - } - let result; - eventRepetition.updatedAt = new Date(); - eventRepetition.updatedBy = updateBody.updatedBy; - if (updateBody?.isMainEvent) { - // Handle updates or deletions for all recurrence records - result = await this.handleAllEventUpdate( - updateBody, - event, - eventDetail, - eventRepetition, - ); - } else { - // Handle updates or deletions for a specific recurrence record - result = await this.handleSpecificRecurrenceUpdate( - updateBody, - event, - eventRepetition, - ); - } - return response - .status(HttpStatus.OK) - .json(APIResponse.success(apiId, result, 'OK')); - } catch (error) { - throw error; + const validationResult = this.isInvalidUpdate(updateBody, eventDetail); + if (!validationResult.isValid) { + throw new BadRequestException(validationResult.message); + } + let result; + eventRepetition.updatedAt = new Date(); + eventRepetition.updatedBy = updateBody.updatedBy; + if (updateBody?.isMainEvent) { + // Handle updates or deletions for all recurrence records + result = await this.handleAllEventUpdate( + updateBody, + event, + eventDetail, + eventRepetition, + ); + } else { + // Handle updates or deletions for a specific recurrence record + result = await this.handleSpecificRecurrenceUpdate( + updateBody, + event, + eventRepetition, + ); } + return response + .status(HttpStatus.OK) + .json(APIResponse.success(apiId, result, 'OK')); } async updateRecurringEvents( @@ -454,32 +449,31 @@ export class EventService { newRecurrencePattern, ); } - } else { + } else if (oldRecStartDate > currentDate) { // Pattern is different- date doesnt matter // 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 + // { + // 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( - ERROR_MESSAGES.RECURRENCE_START_DATE_IN_FUTURE, - ); - } - return await this.deleteOldAndRecreateNewEvents( - currentEventRepetition, - newRecurrencePattern, - ); - } else { - // old start date is less than current date that means event started in past - - return await this.editThisAndFollowingEvents( - currentEventRepetition, - oldRecurrencePattern, - newRecurrencePattern, + if (newRecStartDate < currentDate) { + throw new BadRequestException( + ERROR_MESSAGES.RECURRENCE_START_DATE_IN_FUTURE, ); } + return await this.deleteOldAndRecreateNewEvents( + currentEventRepetition, + newRecurrencePattern, + ); + } else { + // old start date is less than current date that means event started in past + + return await this.editThisAndFollowingEvents( + currentEventRepetition, + oldRecurrencePattern, + newRecurrencePattern, + ); } } else if ( !isDateTimeUpdate.timeSame && @@ -1393,7 +1387,6 @@ export class EventService { eventId, isEdit, ); - if (!(this.eventCreationLimit > 0)) { await this.removePartiallyCreatedData(eventId, eventDetailId); throw new BadRequestException(ERROR_MESSAGES.CREATION_LIMIT_UNAVAILABLE); @@ -1415,8 +1408,7 @@ export class EventService { .values(eventOccurences) .returning(['onlineDetails', 'erMetaData']) .execute(); - // const insertedOccurences = - // await this.eventRepetitionRepository.insert(eventOccurences); + return insertedOccurences; } } @@ -1503,9 +1495,9 @@ export class EventService { currentDay: number, daysOfWeek: DaysOfWeek[], ): number => { - for (let i = 0; i < daysOfWeek.length; i++) { - if (daysOfWeek[i] > currentDay) { - return daysOfWeek[i] - currentDay; + for (const day of daysOfWeek) { + if (day > currentDay) { + return day - currentDay; } } return 7 - currentDay + daysOfWeek[0]; // Move to the next valid week