ticket-booking-be/src/refund/refund.controller.ts
2025-04-11 13:14:06 +05:30

313 lines
11 KiB
TypeScript

import { Body, Controller, Delete, Get, Param, Post, Put, Res, UsePipes, ValidationPipe } from '@nestjs/common';
import { RefundService } from './refund.service';
import { Response } from 'express';
import { GenericResponse } from '../common/GenericResponse.model';
import Refund from './refund.entity';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger';
import { RefundDTO, RefundUpdateDTO } from './refund.dto';
@ApiTags('refund')
@Controller('refund')
export class RefundController {
constructor(private refundService: RefundService) {}
@Get("/all")
@ApiOperation({ summary: 'Get all refunds' })
@ApiResponse({
status: 200,
description: 'Successfully retrieved all refunds',
})
@ApiResponse({
status: 404,
description: 'No refunds found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No refunds found"
},
"data": null
}
})
async getAllRefunds(@Res() res: Response) {
const response = await this.refundService.findAll() || [];
if(!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No refunds found'
}, null);
return res.status(404).send(errorResponse);
}
const httpResponse = new GenericResponse(null, response);
res.send(httpResponse);
}
@Get(':id')
@ApiOperation({ summary: 'Get refund by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Refund 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: 'Refund not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Refund not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully retrieved refund 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.refundService.findByPk(id) || {};
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Refund 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 refunds based on criteria' })
@ApiBody({ type: Refund, description: 'Filter criteria for refunds' })
@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 refunds found based on filter criteria',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No refunds found based on the filter criteria"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully filtered refunds',
})
async filter(@Body() refund: Refund, @Res() res: Response) {
if (!refund) {
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.refundService.filter(refund) || [];
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No refunds 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 refund' })
@ApiBody({ type: RefundDTO, description: 'Refund data to insert' })
@ApiResponse({
status: 400,
description: 'Invalid refund data',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.INVALID_DATA",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 201,
description: 'Successfully created a refund',
})
@UsePipes(new ValidationPipe({ whitelist: true}))
async insert(@Body() refund: RefundDTO, @Res() res: Response) {
if (!refund) {
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.refundService.upsert(refund, true);
const httpResponse = new GenericResponse(null, response);
res.status(201).send(httpResponse);
}
@Put()
@ApiOperation({ summary: 'Update an existing refund' })
@ApiBody({ type: RefundUpdateDTO, description: 'Refund data to update' })
@ApiResponse({
status: 400,
description: 'Invalid refund data or ID missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.INVALID_DATA",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Refund not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Refund not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully updated refund',
})
@UsePipes(new ValidationPipe({ whitelist: true, skipMissingProperties: true }))
async update(@Body() refund: RefundUpdateDTO, @Res() res: Response) {
if (!refund || !refund.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.refundService.upsert(refund, false);
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Refund with ID ${refund.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 refund by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Refund 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: 'Refund not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Refund not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully deleted refund',
})
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.refundService.remove(id) || {};
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Refund with ID ${id} not found`
}, null);
return res.status(404).send(errorResponse);
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
}
}