Skip to content

Commit

Permalink
Merge pull request #38 from poojakarma/edit_startDate_of_recuuring
Browse files Browse the repository at this point in the history
PS-1809: Edit startDate when endDate is same and different
  • Loading branch information
vaivk369 authored Aug 30, 2024
2 parents b7dfb8b + eae7bbc commit db2c162
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/modules/event/event.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class EventController {
constructor(
private readonly eventService: EventService,
private readonly configService: ConfigService,
) {}
) { }

@UseFilters(new AllExceptionsFilter(API_ID.CREATE_EVENT))
@Post('/create')
Expand Down Expand Up @@ -125,7 +125,7 @@ export class EventController {
@Param('id') id: string,
@Body(
new ValidationPipe({ transform: true }),
new DateValidationPipe(),
// new DateValidationPipe(),
// new RegistrationDateValidationPipe(),
// new RecurringEndDateValidationPipe(),
)
Expand Down
215 changes: 158 additions & 57 deletions src/modules/event/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {
In,
Not,
MoreThan,
LessThan,
MoreThanOrEqual,
LessThanOrEqual,
Between,
} from 'typeorm';
import { Events } from './entities/event.entity';
Expand Down Expand Up @@ -349,6 +351,38 @@ export class EventService {
);
}

const currentDate = new Date();
const newRecurringStart = newRecurrencePattern.recurringStartDate;
const newStartDate = 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 ostartDateTime = oldRecurringStart.split('T');
const ostartDate = ostartDateTime[0];
const ostartTime = ostartDateTime[1];

const newRecurringEnd = newRecurrencePattern.endCondition.value;
const oldRecurringEnd = oldRecurrencePattern.endCondition.value;
const newEndDate = new Date(newRecurringEnd);
const oldEndDate = new Date(oldRecurringEnd);
const nEndDate = newEndDate[0];
const oEndDate = oldEndDate[0];

if (nstartDate === ostartDate && nstartTime !== ostartTime) {
throw new BadRequestException(
'Recurring Start Time cannot be changed pass orignal start time',
);
}

// new End date is passed
if (newRecurringEnd !== oldRecurringEnd && oldEndDate < currentDate) {
throw new BadRequestException(
'End Date can not be changes beacuse it is passed away',
);
}

if (
newRecurrencePattern.frequency === Frequency.weekly &&
newRecurrencePattern.frequency === oldRecurrencePattern.frequency &&
Expand All @@ -358,38 +392,18 @@ export class EventService {
oldRecurrencePattern.daysOfWeek,
)
) {
// either add or subtract events as pattern is same
currentEventRepetition['recurrencePattern'] = oldRecurrencePattern;

const currentDate = new Date();
const currentDateTime = currentDate.toISOString();

// new start date is passed
const newRecurringStart = newRecurrencePattern.recurringStartDate;
const newStartDate = 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 ostartDateTime = oldRecurringStart.split('T');
const ostartDate = ostartDateTime[0];
const ostartTime = ostartDateTime[1];

const newRecurringEnd = newRecurrencePattern.endCondition.value;
const oldRecurringEnd = oldRecurrencePattern.endCondition.value;
const newEndDate = new Date(newRecurringEnd);
const oldEndDate = new Date(oldRecurringEnd);

// new start date is passed
// check if new start date is greater than old start date

if (nstartDate === ostartDate && nstartTime !== ostartTime) {
if (nstartDate !== ostartDate && oldStartDate < currentDate) {
throw new BadRequestException(
'Recurring Start Time cannot be changed pass orignal start time',
'Start Date can not be changes beacuse 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 &&
newRecurringEnd !== oldRecurringEnd
Expand Down Expand Up @@ -499,26 +513,66 @@ export class EventService {

currentEventRepetition['startDatetime'] =
newRecurrencePattern.recurringStartDate;
currentEventRepetition.recurrencePattern.endCondition.type = 'endDate';
currentEventRepetition.recurrencePattern.endCondition.value =
oldRecurrencePattern.endCondition.value;
// const newlyAddedEvents = await this.addEventsInRange(
// newStartDate,
// oldStartDate,
// currentEventRepetition,
// );
// console.log('--------------------------');
// console.log(newlyAddedEvents, 'newlyAddedEvents');
// finalStartDate = newStartDate;
currentEventRepetition['endDatetime'] =
currentEventRepetition['startDatetime'].split('T')[0] +
'T' +
currentEventRepetition.endDatetime.split('T')[1];
currentEventRepetition.recurrencePattern.recurringStartDate =
newRecurrencePattern.recurringStartDate;

currentEventRepetition.createdAt = new Date();
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(
currentEventRepetition,
currentEventRepetition.eventId,
currentEventRepetition.eventDetailId,
true,
);

const extUpdt = await this.updateEventRepetitionPattern(
currentEventRepetition.eventId,
currentEventRepetition.recurrencePattern,
);
return { newlyAddedEvents: true };
} else if (
newStartDate > oldStartDate &&
newStartDate > currentDate &&
newEndDate.getTime() === oldEndDate.getTime()
) {
// postpone events when new start date is after old start date
// Get all eventRepetationId which are are less than new recuurnecestartDate and delete all
const removedEvent = await this.eventRepetitionRepository.find({
select: ['eventRepetitionId'],
where: {
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.eventRepository.findOne({
where: {
eventId: currentEventRepetition.eventId,
},
});
newEvent.recurrencePattern = newRecurrencePattern;
await this.eventRepository.save(newEvent);
return { removedEvent, updateRemainingEvents: 0 };
} else if (
newEndDate.getTime() !== oldEndDate.getTime() &&
newStartDate > currentDate &&
Expand All @@ -528,18 +582,45 @@ export class EventService {
// remove events
// 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.createdAt = new Date();
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(
currentEventRepetition,
currentEventRepetition.eventId,
currentEventRepetition.eventDetailId,
true,
);

const extUpdt = await this.updateEventRepetitionPattern(
currentEventRepetition.eventId,
currentEventRepetition.recurrencePattern,
);
return { newlyAddedEvents: true };
}
} else {
// Frequency and interval are different
// make start date as end date for old events and create new events
console.log(
newRecurrencePattern.daysOfWeek,
oldRecurrencePattern.daysOfWeek,
compareArrays(
newRecurrencePattern.daysOfWeek,
oldRecurrencePattern.daysOfWeek,
),
);
throw new NotImplementedException('Pattern different');
}
}
Expand Down Expand Up @@ -586,6 +667,15 @@ export class EventService {
return removedEvents;
}

async removeEventsLessInRange(fromDate: Date, eventId: string) {
const removedEvents = await this.eventRepetitionRepository.delete({
eventId: eventId,
startDateTime: LessThanOrEqual(fromDate),
// endDateTime: MoreThanOrEqual(toDate),
});
return removedEvents;
}

checkIfDateIsSame(
newRecurrenceStartDt: string,
oldRecurrenceStartDt: string,
Expand Down Expand Up @@ -636,6 +726,20 @@ 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(
Expand All @@ -660,16 +764,6 @@ export class EventService {

let updateResult: UpdateResult = {};

// console.log(updateBody.recurrencePattern, 'new');
// console.log(event.recurrencePattern, 'old');
// Get all event which date is equal and greater than cuurent selected event date [use for update]
// const recurrenceRecordss = await this.eventRepetitionRepository.find({
// where: {
// eventId: eventId,
// startDateTime: MoreThanOrEqual(eventRepetition.startDateTime),
// },
// });

const recurrenceRecords = await this.eventRepetitionRepository
.createQueryBuilder('eventRepetition')
.innerJoinAndSelect('eventRepetition.eventDetail', 'eventDetail')
Expand Down Expand Up @@ -712,6 +806,8 @@ export class EventService {
const startDateAndTimeOfCurrentEvent = eventRepetition.startDateTime
.toISOString()
.split('T');
console.log(startDateAndTimeOfCurrentEvent, 'startDate');

const endDateAndTimeOfCurrentEvent = eventRepetition.endDateTime
.toISOString()
.split('T');
Expand Down Expand Up @@ -768,13 +864,18 @@ export class EventService {
event.recurrencePattern.endCondition.value,
);

const isWeekPatternChange = this.checkIfPatternIsSame(
updateBody.recurrencePattern.daysOfWeek,
event.recurrencePattern.daysOfWeek,
);

console.log(isDateTimeUpdate, 'isDateTimeUpdate');

// when date is different regenerate new events
if (
updateBody.recurrencePattern &&
event.recurrencePattern?.frequency &&
!isDateTimeUpdate.dateSame
(!isDateTimeUpdate.dateSame || !isWeekPatternChange)
) {
console.log(
updateBody.recurrencePattern,
Expand Down

0 comments on commit db2c162

Please sign in to comment.