Skip to content

Commit

Permalink
Merge pull request #7 from poojakarma/Add_draft_condition_in_create_a…
Browse files Browse the repository at this point in the history
…nd_update_api

Added condition and swagger updation code
  • Loading branch information
vaivk369 authored May 30, 2024
2 parents 3544e84 + d851311 commit 16a5fdd
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 65 deletions.
53 changes: 0 additions & 53 deletions src/common/pipes/event-validation.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,6 @@
import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common';
import { CreateEventDto } from 'src/modules/event/dto/create-event.dto';

//@Injectable()
// export class EventValidationPipe implements PipeTransform {
// transform(createEventDto: CreateEventDto) {
// const startDate = new Date(createEventDto.startDatetime);
// const endDate = new Date(createEventDto.endDatetime);
// const registrationDeadline = new Date(createEventDto.registrationDeadline);
// if (endDate < startDate) {
// throw new BadRequestException('End date should be greater than or equal to start date');
// }
// if (registrationDeadline < startDate || registrationDeadline > endDate) {
// throw new BadRequestException('Registration deadline should be between start date and end date');
// }
// if (createEventDto.isRestricted) {
// const params = createEventDto.params;

// // Check if 'params' is defined and is an object
// if (!params || typeof params !== 'object') {
// throw new BadRequestException('Invalid params object');
// }

// // Check if 'cohortIds' or 'userIds' key is not present in 'params'
// if (!params.cohortIds && !params.userIds) {
// throw new BadRequestException('Either cohortIds or userIds must be provided in params');
// }

// // Check if both 'cohortIds' and 'userIds' keys are present in 'params'
// if (params.cohortIds && params.userIds) {
// throw new BadRequestException('Only one of cohortIds or userIds should be provided in params');
// }

// // Validate the UUID format of 'cohortIds' or 'userIds' values if present
// if (params.cohortIds) {
// this.validateUUIDs(params.cohortIds);
// } else if (params.userIds) {
// this.validateUUIDs(params.userIds);
// }
// }
// else if (!createEventDto.isRestricted) {
// createEventDto.params = {};
// }
// return createEventDto;
// }
// private validateUUIDs(ids: string[]) {
// const uuidRegex = /^[a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}$/i; // UUID regex pattern

// for (const id of ids) {
// if (!uuidRegex.test(id)) {
// throw new BadRequestException(`Invalid UUID format: ${id}`);
// }
// }
// }
// }

@Injectable()
export class DateValidationPipe implements PipeTransform {
transform(createEventDto: CreateEventDto) {
Expand Down
7 changes: 3 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ async function bootstrap() {
app.enableCors();
// app.setGlobalPrefix('api/v1');
const options = new DocumentBuilder()
.setTitle('Event Management API Collection')
.setDescription('APIs required for frontend apps.')
.setTitle('Event Management')
.setDescription('CRUD API')
.setVersion('1.0')
.addServer('http://localhost:3000/', 'Local environment')
.build();

const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('api-docs', app, document);
SwaggerModule.setup('api/swagger-docs', app, document);

await app.listen(3000);
}
Expand Down
2 changes: 2 additions & 0 deletions src/modules/event/dto/create-event.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ export class CreateEventDto {

updatedBy: string;

autoEnroll: boolean;

}


9 changes: 7 additions & 2 deletions src/modules/event/dto/search-event.dto.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsOptional, IsDateString, IsEnum, IsString } from "class-validator";
import { Type } from "class-transformer";
import { IsOptional, IsDateString, IsEnum, IsString, ValidateNested } from "class-validator";

export class FilterDto {

@ApiProperty({ example: '2024-01-01', description: 'Start date in YYYY-MM-DD format' })
@IsOptional()
@IsDateString()
startDate: string;

@ApiProperty({ example: '2024-01-01', description: 'Start date in YYYY-MM-DD format' })
@IsOptional()
@IsDateString()
endDate: string;

Expand All @@ -16,7 +19,7 @@ export class FilterDto {
description: 'Array of status values: live, draft, inActive'
})
@IsOptional()
@IsEnum(['live', 'draft', 'inActive'], { each: true })
@IsEnum(['live', 'draft', 'inActive'], { each: true, message: 'Status must be one of: live, draft, inActive' })
status?: string[];

@ApiProperty({
Expand All @@ -40,5 +43,7 @@ export class FilterDto {

export class SearchFilterDto {
@ApiProperty({ type: FilterDto, description: 'Filters for search' })
@ValidateNested({ each: true })
@Type(() => FilterDto)
filters: FilterDto
}
3 changes: 3 additions & 0 deletions src/modules/event/entities/event.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export class Events {
@Column({ nullable: false, default: false })
isRestricted: boolean;

@Column({ default: false })
autoEnroll: boolean;

@Column({ nullable: false, type: 'timestamp' })
startDatetime: Date;

Expand Down
31 changes: 25 additions & 6 deletions src/modules/event/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ export class EventService {
}
}
createEventDto.createdBy = userId;
createEventDto.updatedBy = userId
createEventDto.updatedBy = userId;
if (createEventDto.isRestricted === true) {
createEventDto.autoEnroll = true;
}
const created = await this.eventRespository.save(createEventDto);
// Create attendees if isRsetricted true
if (created.eventID && createEventDto.isRestricted === true) {
// Create attendees if isRsetricted true and event status is live
if (created.eventID && createEventDto.isRestricted === true && createEventDto.status == 'live') {
await this.CreateAttendeedforRestrictedEvent(createEventDto, created, userId, response)
}
return response
Expand Down Expand Up @@ -165,31 +168,47 @@ export class EventService {
}
// You can update private event again private
if (updateEventDto.isRestricted == true && event.isRestricted == true) {
throw new BadRequestException('You can not update event');
throw new BadRequestException('You can not update private event as private');
}

// Convert private event to public if status is draft
if (updateEventDto.isRestricted == false && event.isRestricted == true) {
if (event.status == 'draft') {
const result = await this.attendeesService.deleteEventAttendees(event.eventID)
event.params = {};
}
else {
throw new BadRequestException('You can not update private into public event beacuse event is live');
}
}

//if event created as draft and private and now event become live then automatic entry will go in attenddes table of private atendees
if (event.status == 'draft' && updateEventDto.status == 'live' && event.isRestricted == true) {
if (event.params && Object.keys(event.params.length > 0)) {
if (event.params.userIds) {
await this.CreateAttendeedforRestrictedEvent(event, event, userId, response)
}
else if (event.params.cohortIds) {
await this.CreateAttendeedforRestrictedEvent(event, event, userId, response)
}
}
}
Object.assign(event, updateEventDto);

//validation pipe for check start date and end date or only start date
if (updateEventDto.startDatetime && updateEventDto.endDatetime || updateEventDto.startDatetime) {
new DateValidationPipe().transform(event);
}
//validation pipe for if user want to change only end date
if (updateEventDto.endDatetime) {
const startDate = new Date(event.startDatetime);
const endDate = new Date(updateEventDto.endDatetime);
if (startDate > endDate) {
throw new BadRequestException('End date should be greater than or equal to start date')
}
}
//validation pipe for registration deadline date
new DeadlineValidationPipe().transform(event);
// validation pipe for empty param object
new ParamsValidationPipe().transform(event);
event.updatedBy = userId;
const updated_result = await this.eventRespository.save(event);
Expand Down Expand Up @@ -340,4 +359,4 @@ export class EventService {
}
}

}
}

0 comments on commit 16a5fdd

Please sign in to comment.