Skip to content

Commit

Permalink
Task #223114 chore: Constants for api messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Xitija committed Jul 12, 2024
1 parent 7cb74eb commit 634c04b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 18 deletions.
56 changes: 56 additions & 0 deletions src/common/utils/constants.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export const ERROR_MESSAGES = {
INVALID_REQUEST: 'Invalid request',
NOT_FOUND: 'Not found',
UNAUTHORIZED: 'Unauthorized',
FORBIDDEN: 'Forbidden',
BAD_REQUEST: 'Bad request',
INVALID_REQUEST_BODY: 'Invalid request body',
INTERNAL_SERVER_ERROR: 'Internal Server Error',
REGISTRATION_DATE_INVALID: 'Registration date must be in the future',
REGISTRATION_START_DATE_BEFORE_EVENT_DATE: 'Registration start date must be before the event start date',
REGISTRATION_END_DATE_BEFORE_EVENT_DATE: 'Registration end date must be on or before the event start date',
REGISTRATION_START_DATE_INVALID: 'Registration start date must be in the future',
REGISTRATION_END_DATE_INVALID: 'Registration end date must be in the future',
REGISTRATION_START_DATE_BEFORE_END_DATE: 'Registration start date must be before registration end date',
RECURRENCE_END_DATE_INVALID: 'Recurrence end date must be in the future',
RECURRENCE_END_DATE_BEFORE_EVENT_DATE: 'Recurrence end date must be after the event start date',
RECURRING_PATTERN_REQUIRED: 'Recurrence Pattern required for event',
REGISTRATION_START_DATE_REQUIRED: 'Registration Start Date required for event',
INVITEES_REQUIRED: 'Invitees required for private event',
INVITEES_NOT_REQUIRED: 'Invitees not required for public event',
EVENT_NOT_FOUND: 'Event not found',
EVENT_ATTENDEE_NOT_FOUND: 'Event attendee not found',
EVENT_ATTENDEE_HISTORY_NOT_FOUND: 'Event attendee history not found',
EVENT_ATTENDEE_HISTORY_ITEM_NOT_FOUND: 'Event attendee history item not found',
}

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',
EVENT_ATTENDEE_HISTORY_ITEM_CREATED: 'Event attendee history item created successfully',
EVENT_ATTENDEE_HISTORY_ITEM_UPDATED: 'Event attendee history item updated successfully',
EVENT_ATTENDEE_HISTORY_ITEM_DELETED: 'Event attendee history item deleted successfully',
}

export const API_ID = {
CREATE_EVENT: 'api.event.create',
GET_EVENT_BY_ID: 'api.event.getbyid',
GET_EVENTS: 'api.events.get',
UPDATE_EVENT: 'api.event.update',
DELETE_EVENT: 'api.event.delete',
GET_EVENT_ATTENDEES: 'api.event.attendees.get',
GET_EVENT_ATTENDEE: 'api.event.attendee.get',
CREATE_EVENT_ATTENDEE: 'api.event.attendee.create',
UPDATE_EVENT_ATTENDEE: 'api.event.attendee.update',
DELETE_EVENT_ATTENDEE: 'api.event.attendee.delete',
GET_EVENT_ATTENDEE_HISTORY: 'api.event.attendee.history.get',
GET_EVENT_ATTENDEE_HISTORY_ITEM: 'api.event.attendee.history.item.get',
CREATE_EVENT_ATTENDEE_HISTORY_ITEM: 'api.event.attendee.history.item.create',
UPDATE_EVENT_ATTENDEE_HISTORY_ITEM: 'api.event.attendee.history.item.update',
DELETE_EVENT_ATTENDEE_HISTORY_ITEM: 'api.event.attendee.history.item.delete',
}
47 changes: 29 additions & 18 deletions src/modules/event/event.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, UsePipes, Res, ValidationPipe, BadRequestException, ParseUUIDPipe } from '@nestjs/common';
import { Controller, Get, Post, Body, Patch, Param, Delete, UsePipes, Res, ValidationPipe, BadRequestException, ParseUUIDPipe, UseFilters } from '@nestjs/common';
import { EventService } from './event.service';
import { CreateEventDto } from './dto/create-event.dto';
import { UpdateEventDto } from './dto/update-event.dto';
Expand All @@ -13,65 +13,76 @@ import {
} from '@nestjs/swagger';
import { Response } from 'express';
import { SearchFilterDto } from './dto/search-event.dto';
import { DateValidationPipe, DeadlineValidationPipe, ParamsValidationPipe } from 'src/common/pipes/event-validation.pipe';
import { DateValidationPipe, RegistrationDateValidationPipe, ParamsValidationPipe } from 'src/common/pipes/event-validation.pipe';
import { ConfigService } from '@nestjs/config';
import { AllExceptionsFilter } from 'src/common/filters/exception.filter';
import { API_ID, ERROR_MESSAGES, SUCCESS_MESSAGES } from 'src/common/utils/constants.util';

@Controller('event/v1')
@ApiTags('Create Event')
export class EventController {
constructor(private readonly eventService: EventService) { }
constructor(private readonly eventService: EventService,
private readonly configService: ConfigService,
) { }

@UseFilters(new AllExceptionsFilter(API_ID.CREATE_EVENT))
@Post('/create')
@ApiBody({ type: CreateEventDto })
@UsePipes(new DateValidationPipe, new DeadlineValidationPipe, new ParamsValidationPipe, new ValidationPipe({ transform: true }))
@UsePipes(new ValidationPipe({ transform: true }), new DateValidationPipe, new RegistrationDateValidationPipe, new ParamsValidationPipe)
@ApiCreatedResponse({
description: 'Created Event',
description: SUCCESS_MESSAGES.EVENT_CREATED,
})
@ApiBadRequestResponse({ description: 'Invalid request' })
@ApiInternalServerErrorResponse({ description: 'Server Error.' })
@ApiBadRequestResponse({ description: ERROR_MESSAGES.INVALID_REQUEST_BODY })
@ApiInternalServerErrorResponse({ description: ERROR_MESSAGES.INTERNAL_SERVER_ERROR })
async create(@Body() createEventDto: CreateEventDto, @Res() response: Response,) {
const userId = '016badad-22b0-4566-88e9-aab1b35b1dfc'; // later come from JWT-token
this.configService;
return this.eventService.createEvent(createEventDto, userId, response);
}

@UseFilters(new AllExceptionsFilter(API_ID.GET_EVENTS))
@Post('/list')
@ApiBody({ type: SearchFilterDto })
@ApiInternalServerErrorResponse({ description: 'Server Error.' })
@ApiInternalServerErrorResponse({ description: ERROR_MESSAGES.INTERNAL_SERVER_ERROR })
@UsePipes(new ValidationPipe({ transform: true }))
@ApiOkResponse({
description: 'Searched',
status: 200
})
async findAll(@Res() response: Response, @Body() requestBody: SearchFilterDto) {
return this.eventService.getEvents(response, requestBody);
// return this.eventService.getEvents(response, requestBody);
}

@UseFilters(new AllExceptionsFilter(API_ID.GET_EVENT_BY_ID))
@Get('/:id')
@ApiOkResponse({
description: 'Get event details by id',
status: 200
})
@ApiInternalServerErrorResponse({ description: 'Server Error.' })
@ApiInternalServerErrorResponse({ description: ERROR_MESSAGES.INTERNAL_SERVER_ERROR })
findOne(@Param('id', ParseUUIDPipe) id: string, @Res() response: Response) {
return this.eventService.getEventByID(id, response);
// return this.eventService.getEventByID(id, response);
}

@UseFilters(new AllExceptionsFilter(API_ID.UPDATE_EVENT))
@Patch('/:id')
@ApiBody({ type: UpdateEventDto })
@ApiResponse({ status: 200, description: 'Event updated successfully' })
@ApiInternalServerErrorResponse({ description: 'Server Error.' })
@ApiResponse({ status: 200, description: SUCCESS_MESSAGES.EVENT_UPDATED })
@ApiInternalServerErrorResponse({ description: ERROR_MESSAGES.INTERNAL_SERVER_ERROR })
@UsePipes(new ValidationPipe({ transform: true }))
updateEvent(@Param('id', ParseUUIDPipe) id: string, @Body() updateEventDto: UpdateEventDto, @Res() response: Response) {
if (!updateEventDto || Object.keys(updateEventDto).length === 0) {
throw new BadRequestException('Please do not pass empty body')
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, userId, response);
// return this.eventService.updateEvent(id, updateEventDto, userId, response);
}

@UseFilters(new AllExceptionsFilter(API_ID.DELETE_EVENT))
@Delete('/:id')
@ApiResponse({ status: 200, description: 'Event deleted successfully' })
@ApiResponse({ status: 404, description: 'Event not found' })
@ApiResponse({ status: 200, description: SUCCESS_MESSAGES.EVENT_DELETED })
@ApiResponse({ status: 404, description: SUCCESS_MESSAGES.EVENT_NOT_FOUND })
deleteEvent(@Param('id', ParseUUIDPipe) id: string, @Res() response: Response) {
return this.eventService.deleteEvent(id, response);
// return this.eventService.deleteEvent(id, response);
}
}

0 comments on commit 634c04b

Please sign in to comment.