Skip to content

Commit

Permalink
Merge pull request #43 from Xitija/create-api-changes
Browse files Browse the repository at this point in the history
PS-2014: Regression defects on API
  • Loading branch information
snehal0904 authored Sep 19, 2024
2 parents 64d1783 + 4a4bce6 commit ee87a73
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 100 deletions.
2 changes: 1 addition & 1 deletion src/modules/event/dto/create-event.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class RecurrencePatternDto {

@Validate(EndsWithZConstraint)
@IsDateString({ strict: true, strictSeparator: true })
@IsOptional()
// @IsOptional()
recurringStartDate: string;

@ApiProperty({
Expand Down
2 changes: 2 additions & 0 deletions src/modules/event/dto/update-event.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface UpdateResult {
eventDetails?: any;
repetationDetail?: any;
recurrenceUpdate?: any;
updatedRecurringEvent?: any;
updatedEventDetails?: any;
}

export class UpdateEventDto {
Expand Down
208 changes: 109 additions & 99 deletions src/modules/event/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
MoreThanOrEqual,
LessThanOrEqual,
Between,
DeleteResult,
InsertResult,
} from 'typeorm';
import { Events } from './entities/event.entity';
import { Response } from 'express';
Expand All @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -797,13 +791,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];
Expand Down Expand Up @@ -903,7 +914,7 @@ export class EventService {
updateData.erMetaData = eventRepetition.erMetaData;
updateResult.erMetaData = updateBody.erMetaData;
}
updateResult['updatedRecurringEvent'] = await this.updateEventRepetition(
updateResult.updatedRecurringEvent = await this.updateEventRepetition(
recurrenceRecords,
updateData,
);
Expand All @@ -918,13 +929,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;
}
Expand Down Expand Up @@ -969,13 +981,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) {
Expand All @@ -995,13 +1007,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) {
Expand Down Expand Up @@ -1053,13 +1065,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;
}
}
}
Expand Down Expand Up @@ -1375,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);
Expand All @@ -1397,8 +1408,7 @@ export class EventService {
.values(eventOccurences)
.returning(['onlineDetails', 'erMetaData'])
.execute();
// const insertedOccurences =
// await this.eventRepetitionRepository.insert(eventOccurences);

return insertedOccurences;
}
}
Expand Down Expand Up @@ -1485,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
Expand Down

0 comments on commit ee87a73

Please sign in to comment.