Skip to content

Commit

Permalink
Merge pull request #46 from Xitija/main2
Browse files Browse the repository at this point in the history
fix : Edit Adhoc events throws error
  • Loading branch information
snehal0904 authored Sep 24, 2024
2 parents ee87a73 + f43aa56 commit a6355d4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
5 changes: 5 additions & 0 deletions src/common/utils/constants.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ export const ERROR_MESSAGES = {
CANNOT_PREPONE_PAST_EVENTS:
'Cannot update events prepone not allowed for past events',
CANNOT_EDIT_ARCHIVED_EVENTS: 'Cannot Edit archived events',
ENDTIME_DOES_NOT_MATCH:
'Event End time does not match with Recurrence Start or End time',
PROVIDE_VALID_START_AND_END_DATETIME:
'Please Provide Valid Start and End Date',
RECURRENCE_PATTERN_MISSING: 'Recurring Pattern is missing for this event',
CANNOT_PASS_MAIN_EVENT_FALSE:
'You can not pass isMainEvent false because event is non recurring',
CREATION_LIMIT_UNAVAILABLE: 'Event creation limit unavailable',
Expand Down
48 changes: 27 additions & 21 deletions src/modules/event/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,11 @@ export class EventService {

const currentDate = new Date();
const newRecurringStart = newRecurrencePattern.recurringStartDate;
const nstartDate = newRecurringStart.split('T')[0];
const newRecStartDate = new Date(newRecurringStart);

const oldRecurringStart = oldRecurrencePattern.recurringStartDate;
const ostartDate = oldRecurringStart.split('T')[0];
const oldRecStartDate = new Date(oldRecurringStart);

const newRecurringEnd = newRecurrencePattern.endCondition.value;
Expand Down Expand Up @@ -406,7 +408,7 @@ export class EventService {
currentEventRepetition['recurrencePattern'] = oldRecurrencePattern;

if (
newRecStartDate.getTime() === oldRecStartDate.getTime() &&
new Date(nstartDate).getTime() === new Date(ostartDate).getTime() && // check only date
newRecEndDate.getTime() !== oldRecEndDate.getTime()
) {
// start date and time is same
Expand Down Expand Up @@ -441,7 +443,7 @@ export class EventService {
(newRecStartDate > oldRecStartDate && newRecStartDate > currentDate)
) {
// prepone events when new start date lies between current date and old start date
// end date remains same
// end date does not matter
// add events fully and update start date in recpattern

return await this.deleteOldAndRecreateNewEvents(
Expand Down Expand Up @@ -740,9 +742,7 @@ export class EventService {

checkValidRecurrenceTimeForUpdate(endDate, recurrenceEndDate) {
if (endDate.split('T')[1] !== recurrenceEndDate.split('T')[1]) {
throw new BadRequestException(
'Event End time does not match with Recurrence Start or End time',
);
throw new BadRequestException(ERROR_MESSAGES.ENDTIME_DOES_NOT_MATCH);
}
}

Expand Down Expand Up @@ -793,25 +793,27 @@ export class EventService {

let updateResult: UpdateResult = {};
let updatedEvents;
let eventAndEventDetails;
eventAndEventDetails = {};
eventAndEventDetails['newEvent'] = event;
eventAndEventDetails['newEventDetail'] = eventDetail;
let eventAndEventDetails: {
newEvent: Events;
newEventDetail: EventDetail;
} = { newEvent: event, newEventDetail: eventDetail };

if (
(!startDatetime || !endDatetime) &&
updateBody.recurrencePattern &&
event.isRecurring
) {
throw new BadRequestException('Please Provide Valid Start and End Date');
throw new BadRequestException(
ERROR_MESSAGES.PROVIDE_VALID_START_AND_END_DATETIME,
);
}

// 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',
ERROR_MESSAGES.RECURRENCE_PATTERN_MISSING,
);
}

Expand Down Expand Up @@ -861,19 +863,17 @@ export class EventService {
!(updatedEvents.newEvent instanceof Events) &&
updatedEvents.newEvent === event.eventId
) {
eventAndEventDetails = {};
eventAndEventDetails['newEvent'] = await this.findEventById(
eventAndEventDetails.newEvent = await this.findEventById(
updatedEvents.newEvent,
);
eventAndEventDetails['newEventDetail'] = await this.getEventDetails(
eventAndEventDetails.newEventDetail = await this.getEventDetails(
updatedEvents.newEventDetail,
);
}
// else as passed from function
} else {
eventAndEventDetails = {};
eventAndEventDetails['newEvent'] = event;
eventAndEventDetails['newEventDetail'] = eventDetail;
eventAndEventDetails.newEvent = event;
eventAndEventDetails.newEventDetail = eventDetail;
}
} else if (startDatetime && endDatetime && !event.isRecurring) {
// Handle non-recurring events
Expand All @@ -886,11 +886,17 @@ export class EventService {
}

// get current first event as we regenerate new events and make other changes first event might change

let firstEventDate =
eventAndEventDetails.newEvent.recurrencePattern.recurringStartDate;

if (!firstEventDate && !event.isRecurring) {
firstEventDate = eventRepetition.startDateTime;
}

const currentFirstEvent = await this.getFirstEvent(
eventAndEventDetails.newEvent.eventId,
new Date(
eventAndEventDetails.newEvent.recurrencePattern.recurringStartDate,
), // updatedEvents.newEvent.recurrencePattern.recurringStartDate --- startDatetime
new Date(firstEventDate),
);

eventRepetition = currentFirstEvent;
Expand Down Expand Up @@ -938,7 +944,7 @@ export class EventService {
eventRepetition,
);
}
return updateResult;
return { ...updateResult, event: eventAndEventDetails.newEvent };
}

async updateEventDetailsForRecurringEvents(
Expand Down

0 comments on commit a6355d4

Please sign in to comment.