313 lines
11 KiB
TypeScript
313 lines
11 KiB
TypeScript
import { Body, Controller, Delete, Get, Param, Post, Put, Res, UsePipes, ValidationPipe } from '@nestjs/common';
|
|
import { TicketService } from './ticket.service';
|
|
import { Response } from 'express';
|
|
import { GenericResponse } from '../common/GenericResponse.model';
|
|
import Ticket from './ticket.entity';
|
|
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger';
|
|
import { TicketDTO, TicketUpdateDTO } from './ticket.dto';
|
|
|
|
@ApiTags('ticket')
|
|
@Controller('ticket')
|
|
export class TicketController {
|
|
constructor(private ticketService: TicketService) {}
|
|
|
|
@Get("/all")
|
|
@ApiOperation({ summary: 'Get all tickets' })
|
|
@ApiResponse({
|
|
status: 200,
|
|
description: 'Successfully retrieved all tickets',
|
|
})
|
|
@ApiResponse({
|
|
status: 404,
|
|
description: 'No tickets found',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.NOT_FOUND",
|
|
"stackTrace": "No tickets found"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
async getAllTickets(@Res() res: Response) {
|
|
const response = await this.ticketService.findAll() || [];
|
|
if(!response) {
|
|
const errorResponse = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.NOT_FOUND',
|
|
stackTrace: 'No tickets found'
|
|
}, null);
|
|
return res.status(404).send(errorResponse);
|
|
}
|
|
const httpResponse = new GenericResponse(null, response);
|
|
res.send(httpResponse);
|
|
}
|
|
|
|
@Get(':id')
|
|
@ApiOperation({ summary: 'Get ticket by ID' })
|
|
@ApiParam({ name: 'id', type: Number, description: 'Ticket ID' })
|
|
@ApiResponse({
|
|
status: 400,
|
|
description: 'ID is required',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.NO_ID_REQ",
|
|
"stackTrace": "Request"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 404,
|
|
description: 'Ticket not found',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.NOT_FOUND",
|
|
"stackTrace": "Ticket not found"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 200,
|
|
description: 'Successfully retrieved ticket by ID',
|
|
})
|
|
async findById(@Param('id') id: number, @Res() res: Response) {
|
|
if (!id) {
|
|
const response = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.NO_ID_REQ',
|
|
stackTrace: 'Request'
|
|
}, null);
|
|
return res.status(400).send(response);
|
|
}
|
|
const response = await this.ticketService.findByPk(id) || {};
|
|
if (!response) {
|
|
const errorResponse = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.NOT_FOUND',
|
|
stackTrace: `Ticket with ID ${id} not found`
|
|
}, null);
|
|
return res.status(404).send(errorResponse);
|
|
}
|
|
const httpResponse = new GenericResponse(null, response);
|
|
res.status(200).send(httpResponse);
|
|
}
|
|
|
|
@Post('/filter')
|
|
@ApiOperation({ summary: 'Filter tickets based on criteria' })
|
|
@ApiBody({ type: Ticket, description: 'Filter criteria for tickets' })
|
|
@ApiResponse({
|
|
status: 400,
|
|
description: 'Invalid filter criteria',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.INVALID_CRITERIA",
|
|
"stackTrace": "Request"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 404,
|
|
description: 'No tickets found based on filter criteria',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.NOT_FOUND",
|
|
"stackTrace": "No tickets found based on the filter criteria"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 200,
|
|
description: 'Successfully filtered tickets',
|
|
})
|
|
async filter(@Body() ticket: Ticket, @Res() res: Response) {
|
|
if (!ticket) {
|
|
const response = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.INVALID_CRITERIA',
|
|
stackTrace: 'Request'
|
|
}, null);
|
|
return res.status(400).send(response);
|
|
}
|
|
const response = await this.ticketService.filter(ticket) || [];
|
|
if (!response) {
|
|
const errorResponse = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.NOT_FOUND',
|
|
stackTrace: 'No tickets found based on the filter criteria'
|
|
}, null);
|
|
return res.status(404).send(errorResponse);
|
|
}
|
|
const httpResponse = new GenericResponse(null, response);
|
|
res.status(200).send(httpResponse);
|
|
}
|
|
|
|
@Post()
|
|
@ApiOperation({ summary: 'Insert a new ticket' })
|
|
@ApiBody({ type: TicketDTO, description: 'Ticket data to insert' })
|
|
@ApiResponse({
|
|
status: 400,
|
|
description: 'Invalid ticket data',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.INVALID_DATA",
|
|
"stackTrace": "Request"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 201,
|
|
description: 'Successfully created a ticket',
|
|
})
|
|
@UsePipes(new ValidationPipe({ whitelist: true}))
|
|
async insert(@Body() ticket: TicketDTO, @Res() res: Response) {
|
|
if (!ticket) {
|
|
const response = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.INVALID_DATA',
|
|
stackTrace: 'Request'
|
|
}, null);
|
|
return res.status(400).send(response);
|
|
}
|
|
const response = await this.ticketService.upsert(ticket, true);
|
|
const httpResponse = new GenericResponse(null, response);
|
|
res.status(201).send(httpResponse);
|
|
}
|
|
|
|
@Put()
|
|
@ApiOperation({ summary: 'Update an existing ticket' })
|
|
@ApiBody({ type: TicketUpdateDTO, description: 'Ticket data to update' })
|
|
@ApiResponse({
|
|
status: 400,
|
|
description: 'Invalid ticket data or ID missing',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.INVALID_DATA",
|
|
"stackTrace": "Request"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 404,
|
|
description: 'Ticket not found',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.NOT_FOUND",
|
|
"stackTrace": "Ticket not found"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 200,
|
|
description: 'Successfully updated ticket',
|
|
})
|
|
@UsePipes(new ValidationPipe({ whitelist: true, skipMissingProperties: true }))
|
|
async update(@Body() ticket: TicketUpdateDTO, @Res() res: Response) {
|
|
if (!ticket || !ticket.id) {
|
|
const response = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.NO_ID_REQ',
|
|
stackTrace: 'Request'
|
|
}, null);
|
|
return res.status(400).send(response);
|
|
}
|
|
const response = await this.ticketService.upsert(ticket, false);
|
|
if (!response) {
|
|
const errorResponse = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.NOT_FOUND',
|
|
stackTrace: `Ticket with ID ${ticket.id} not found`
|
|
}, null);
|
|
return res.status(404).send(errorResponse);
|
|
}
|
|
const httpResponse = new GenericResponse(null, response);
|
|
res.status(200).send(httpResponse);
|
|
}
|
|
|
|
@Delete(':id')
|
|
@ApiOperation({ summary: 'Delete a ticket by ID' })
|
|
@ApiParam({ name: 'id', type: Number, description: 'Ticket ID to delete' })
|
|
@ApiResponse({
|
|
status: 400,
|
|
description: 'ID parameter is required',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.NO_ID_REQ",
|
|
"stackTrace": "Request"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 404,
|
|
description: 'Ticket not found',
|
|
example: {
|
|
"notification": {
|
|
"exception": true,
|
|
"exceptionSeverity": "HIGH",
|
|
"exceptionMessage": "ERR.NOT_FOUND",
|
|
"stackTrace": "Ticket not found"
|
|
},
|
|
"data": null
|
|
}
|
|
})
|
|
@ApiResponse({
|
|
status: 200,
|
|
description: 'Successfully deleted ticket',
|
|
})
|
|
async deleteById(@Param('id') id: number, @Res() res: Response) {
|
|
if (!id) {
|
|
const response = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.NO_ID_REQ',
|
|
stackTrace: 'Request'
|
|
}, null);
|
|
return res.status(400).send(response);
|
|
}
|
|
const response = await this.ticketService.remove(id) || {};
|
|
if (!response) {
|
|
const errorResponse = new GenericResponse({
|
|
exception: true,
|
|
exceptionSeverity: 'HIGH',
|
|
exceptionMessage: 'ERR.NOT_FOUND',
|
|
stackTrace: `Ticket with ID ${id} not found`
|
|
}, null);
|
|
return res.status(404).send(errorResponse);
|
|
}
|
|
const httpResponse = new GenericResponse(null, response);
|
|
res.status(200).send(httpResponse);
|
|
}
|
|
}
|