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