Skip to content

Commit

Permalink
Merge pull request #33 from Xitija/create-api-changes
Browse files Browse the repository at this point in the history
PS - 1289 feat : event edit when start date is same and end date is different
  • Loading branch information
vaivk369 authored Aug 28, 2024
2 parents 8dc8084 + 6c0b7e8 commit bbc6662
Show file tree
Hide file tree
Showing 8 changed files with 580 additions and 65 deletions.
38 changes: 33 additions & 5 deletions src/common/pipes/event-validation.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,38 @@ import {
ValidatorConstraintInterface,
} from 'class-validator';
import { EndConditionType } from '../utils/types';
import { UpdateEventDto } from 'src/modules/event/dto/update-event.dto';

@Injectable()
export class DateValidationPipe implements PipeTransform {
transform(createEventDto: CreateEventDto) {
transform(createEventDto: CreateEventDto | UpdateEventDto) {
console.log(
createEventDto,
'createEventDto',
createEventDto.startDatetime,
createEventDto.endDatetime,
);
const eventStartDate = createEventDto.startDatetime.split('T')[0];
const eventEndDate = createEventDto.endDatetime.split('T')[0];

const startDate = new Date(createEventDto.startDatetime);
const endDate = new Date(createEventDto.endDatetime);
const currentDate = new Date();

console.log(
startDate,
'start',
endDate,
'end',
currentDate,
createEventDto.isRecurring,
);
if (eventStartDate !== eventEndDate && createEventDto.isRecurring) {
throw new BadRequestException(
ERROR_MESSAGES.MULTIDAY_EVENT_NOT_RECURRING,
);
}
if (startDate <= currentDate) {
if (startDate <= currentDate || endDate <= currentDate) {
throw new BadRequestException(ERROR_MESSAGES.START_DATE_INVALID);
}
if (endDate < startDate) {
Expand Down Expand Up @@ -102,7 +117,7 @@ export class RegistrationDateValidationPipe implements PipeTransform {
}

export class RecurringEndDateValidationPipe implements PipeTransform {
transform(createEventDto: CreateEventDto) {
transform(createEventDto: CreateEventDto | UpdateEventDto) {
if (createEventDto.isRecurring) {
const endConditionValue =
createEventDto.recurrencePattern?.endCondition?.value;
Expand All @@ -115,7 +130,7 @@ export class RecurringEndDateValidationPipe implements PipeTransform {
);
}

if (endConditionType === 'endDate') {
if (endConditionType === EndConditionType.endDate) {
const recurrenceEndDate = new Date(endConditionValue);

const dateValid =
Expand All @@ -135,11 +150,24 @@ export class RecurringEndDateValidationPipe implements PipeTransform {
);
}

if (recurrenceEndDate <= startDate) {
if (
recurrenceEndDate <= startDate &&
createEventDto instanceof CreateEventDto
) {
console.log('recurrenceEndDate', recurrenceEndDate);
throw new BadRequestException(
ERROR_MESSAGES.RECURRENCE_END_DATE_AFTER_EVENT_DATE,
);
}

if (
endConditionValue.split('T')[1] !==
createEventDto.endDatetime.split('T')[1]
) {
throw new BadRequestException(
'Event End time does not match with Recurrence End time',
);
} // do for recurrence start time also in edit
// createEventDto.recurrencePattern.endCondition.value = endDate;
} else if (endConditionType === EndConditionType.occurrences) {
const occurrences = Number(endConditionValue);
Expand Down
3 changes: 1 addition & 2 deletions src/common/utils/constants.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const ERROR_MESSAGES = {
BAD_REQUEST: 'Bad request',
INVALID_REQUEST_BODY: 'Invalid request body',
INTERNAL_SERVER_ERROR: 'Internal Server Error',
START_DATE_INVALID: 'Start date must be today or a future date',
START_DATE_INVALID: 'Start and End date must be today or a future date',
END_DATE_INVALID: 'End date time should be greater than start date time',
REGISTRATION_DATE_INVALID: 'Registration date must be in the future',
REGISTRATION_START_DATE_BEFORE_EVENT_DATE:
Expand Down Expand Up @@ -48,7 +48,6 @@ export const SUCCESS_MESSAGES = {
EVENT_CREATED: 'Event created successfully',
EVENT_UPDATED: 'Event updated successfully',
EVENT_DELETED: 'Event deleted successfully',
EVENT_NOT_FOUND: 'Event not found',
EVENT_ATTENDEE_CREATED: 'Event attendee created successfully',
EVENT_ATTENDEE_UPDATED: 'Event attendee updated successfully',
EVENT_ATTENDEE_DELETED: 'Event attendee deleted successfully',
Expand Down
22 changes: 22 additions & 0 deletions src/common/utils/functions.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const compareArrays = (a: any[], b: any[]): boolean => {
if (a.length !== b.length) return false;
else {
// Comparing each element of your array
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
};

export const getNextDay = (currentDate: Date): Date => {
// Create a new date object based on the current date
const nextDay = new Date(currentDate);

// Add one day
nextDay.setDate(currentDate.getDate() + 1);

return nextDay;
};
3 changes: 3 additions & 0 deletions src/modules/event/dto/create-event.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ export class RecurrencePatternDto {
// @IsOptional()
// dayOfMonth: number;

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

@ApiProperty({
Expand Down
31 changes: 25 additions & 6 deletions src/modules/event/dto/update-event.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { MeetingDetails } from 'src/common/utils/types';
import { Transform, Type } from 'class-transformer';
import { UrlWithProviderValidator } from 'src/common/utils/validation.util';
import { RecurrencePatternDto } from './create-event.dto';
import { MeetingDetailsDto } from './create-event.dto';
export interface UpdateResult {
onlineDetails?: any;
Expand Down Expand Up @@ -129,7 +130,7 @@ export class UpdateEventDto {
})
@ValidateIf((o) => o.endDatetime !== undefined)
@IsDateString()
startDatetime: Date;
startDatetime: string;

@ApiProperty({
type: String,
Expand All @@ -138,7 +139,7 @@ export class UpdateEventDto {
})
@ValidateIf((o) => o.startDatetime !== undefined)
@IsDateString()
endDatetime: Date;
endDatetime: string;

@ApiProperty({
type: String,
Expand Down Expand Up @@ -168,6 +169,17 @@ export class UpdateEventDto {
@IsLatitude()
latitude: number;

@ApiProperty({
type: RecurrencePatternDto,
description: 'recurrencePattern',
example: { frequency: 'daily', interval: 1 },
})
@IsObject()
@ValidateNested({ each: true })
@Type(() => RecurrencePatternDto)
@IsOptional()
recurrencePattern: RecurrencePatternDto;

@ApiProperty({
type: String,
description: 'Online Provider',
Expand All @@ -179,17 +191,24 @@ export class UpdateEventDto {
@IsIn(['Zoom', 'GoogleMeet'])
onlineProvider: string;

@IsString()
@IsOptional()
createdBy: string;
// @IsString()
// @IsOptional()
// createdBy: string;

@ApiProperty({
type: String,
description: 'updatedBy',
example: 'eff008a8-2573-466d-b877-fddf6a4fc13e',
})
@IsString()
@IsOptional()
// @IsOptional()
updatedBy: string;

@IsOptional()
updateAt: Date;

isRecurring: boolean;

// Validation to ensure if isMainEvent is true, title or status must be provided
@ValidateIf(
(o) =>
Expand Down
3 changes: 2 additions & 1 deletion src/modules/event/entities/event.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export class Events {
recurrenceEndDate: Date;

@Column({ type: 'jsonb' })
recurrencePattern: object;
// recurrencePattern: object;
recurrencePattern: any;

@CreateDateColumn({
type: 'timestamptz',
Expand Down
20 changes: 15 additions & 5 deletions src/modules/event/event.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,33 @@ export class EventController {
@ApiInternalServerErrorResponse({
description: ERROR_MESSAGES.INTERNAL_SERVER_ERROR,
})
@UsePipes(new ValidationPipe({ transform: true }))
// @UsePipes(
// new ValidationPipe({ transform: true }),
// new DateValidationPipe(),
// // new RegistrationDateValidationPipe(),
// new RecurringEndDateValidationPipe(),
// )
updateEvent(
@Param('id', ParseUUIDPipe) id: string,
@Body() updateEventDto: UpdateEventDto,
@Param('id') id: string,
@Body(
new ValidationPipe({ transform: true }),
new DateValidationPipe(),
// new RegistrationDateValidationPipe(),
// new RecurringEndDateValidationPipe(),
)
updateEventDto: UpdateEventDto,
@Res() response: Response,
) {
if (!updateEventDto || Object.keys(updateEventDto).length === 0) {
throw new BadRequestException(ERROR_MESSAGES.INVALID_REQUEST_BODY);
}
const userId = '01455719-e84f-4bc8-8efa-7024874ade08'; // later come from JWT-token
return this.eventService.updateEvent(id, updateEventDto, response);
}

@UseFilters(new AllExceptionsFilter(API_ID.DELETE_EVENT))
@Delete('/:id')
@ApiResponse({ status: 200, description: SUCCESS_MESSAGES.EVENT_DELETED })
@ApiResponse({ status: 404, description: SUCCESS_MESSAGES.EVENT_NOT_FOUND })
@ApiResponse({ status: 404, description: ERROR_MESSAGES.EVENT_NOT_FOUND })
deleteEvent(
@Param('id', ParseUUIDPipe) id: string,
@Res() response: Response,
Expand Down
Loading

0 comments on commit bbc6662

Please sign in to comment.