swagger for controllers

This commit is contained in:
harshithnrao 2025-03-02 21:04:40 +05:30
parent 7966124df6
commit 66391a2ab2
12 changed files with 3253 additions and 5177 deletions

6735
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
{ {
"name": "abnandan-skeleton-be", "name": "remedify-payment-microservice",
"version": "0.0.1", "version": "0.0.1",
"description": "", "description": "",
"author": "", "author": "",
@ -23,11 +23,11 @@
"test:e2e": "jest --config ./test/jest-e2e.json" "test:e2e": "jest --config ./test/jest-e2e.json"
}, },
"dependencies": { "dependencies": {
"@nestjs-modules/mailer": "^1.10.3",
"@nestjs/common": "^10.0.0", "@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.1.1", "@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.0.0", "@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0", "@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^8.1.1",
"@nestjs/typeorm": "^10.0.1", "@nestjs/typeorm": "^10.0.1",
"dotenv": "^16.3.1", "dotenv": "^16.3.1",
"handlebars": "^4.7.8", "handlebars": "^4.7.8",

View File

@ -30,8 +30,8 @@ import { RefundsModule } from './refund/refund.module';
QuoteModule, QuoteModule,
RefundsModule, RefundsModule,
DataModule, DataModule,
MailModule, // MailModule,
MasterConfigModule, // MasterConfigModule,
ConfigModule, ConfigModule,
], ],
controllers: [AppController, AppConfigController], controllers: [AppController, AppConfigController],

View File

@ -3,19 +3,81 @@ import { Response } from 'express';
import { InvoicesService } from './invoice.service'; import { InvoicesService } from './invoice.service';
import { GenericResponse } from 'src/common/GenericResponse.model'; import { GenericResponse } from 'src/common/GenericResponse.model';
import Invoice from './invoice.entity'; import Invoice from './invoice.entity';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger';
@ApiTags('invoices')
@Controller('invoice') @Controller('invoice')
export class InvoicesController { export class InvoicesController {
constructor(private invoiceService: InvoicesService) { } constructor(private invoiceService: InvoicesService) {}
@Get("/all") @Get("/all")
@ApiOperation({ summary: 'Get all invoices' })
@ApiResponse({
status: 200,
description: 'Successfully retrieved all invoices',
})
@ApiResponse({
status: 404,
description: 'No invoices found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No invoices found"
},
"data": null
}
})
async getAll(@Res() res: Response) { async getAll(@Res() res: Response) {
const response = await this.invoiceService.findAll() || []; const response = await this.invoiceService.findAll();
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No invoices found'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Get(':id') @Get(':id')
@ApiOperation({ summary: 'Get invoice by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Invoice ID' })
@ApiResponse({
status: 400,
description: 'ID parameter is missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NO_ID_IN_REQ",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Invoice not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Invoice not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully retrieved invoice',
})
async findById(@Param('id') id: number, @Res() res: Response) { async findById(@Param('id') id: number, @Res() res: Response) {
if (!id) { if (!id) {
const response = new GenericResponse({ const response = new GenericResponse({
@ -24,80 +86,232 @@ export class InvoicesController {
exceptionMessage: 'ERR.NO_ID_IN_REQ', exceptionMessage: 'ERR.NO_ID_IN_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.invoiceService.findByPk(id) || {}; const response = await this.invoiceService.findByPk(id);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Invoice with ID ${id} not found`
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Post('/filter') @Post('/filter')
async filter(@Body() user: Invoice, @Res() res: Response) { @ApiOperation({ summary: 'Filter invoices based on criteria' })
if (!user) { @ApiBody({ type: Invoice, description: 'Invoice filter criteria' })
@ApiResponse({
status: 404,
description: 'No invoices matching criteria found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No invoices found matching criteria"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully filtered invoices',
})
async filter(@Body() invoice: Invoice, @Res() res: Response) {
if (!invoice) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ', exceptionMessage: 'ERR.NO_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.invoiceService.filter(user) || []; const response = await this.invoiceService.filter(invoice);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No invoices found matching criteria'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Post() @Post()
async insert(@Body() user: Invoice, @Res() res: Response) { @ApiOperation({ summary: 'Insert a new invoice' })
if (!user) { @ApiBody({ type: Invoice, description: 'Invoice data to insert' })
@ApiResponse({
status: 400,
description: 'Failed to insert invoice',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Invoice not created"
},
"data": null
}
})
@ApiResponse({
status: 201,
description: 'Invoice successfully created',
})
async insert(@Body() invoice: Invoice, @Res() res: Response) {
if (!invoice) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ', exceptionMessage: 'ERR.NO_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
delete user.id; delete invoice.id;
const response = await this.invoiceService.upsert(user, true); const response = await this.invoiceService.upsert(invoice, true);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_CREATED',
stackTrace: 'Invoice not created'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(201).send(httpResponse);
} }
@Put() @Put()
async update(@Body() user: Invoice, @Res() res: Response) { @ApiOperation({ summary: 'Update an existing invoice' })
if (!user || !user.id) { @ApiBody({ type: Invoice, description: 'Invoice data to update' })
@ApiResponse({
status: 400,
description: 'Failed to update invoice',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Invoice not updated"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Invoice not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Invoice not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Invoice successfully updated',
})
async update(@Body() invoice: Invoice, @Res() res: Response) {
if (!invoice || !invoice.id) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ', exceptionMessage: 'ERR.NO_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.invoiceService.upsert(user, false); const response = await this.invoiceService.upsert(invoice, false);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'Invoice not updated'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Delete(':id') @Delete(':id')
@ApiOperation({ summary: 'Delete an invoice by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Invoice ID to delete' })
@ApiResponse({
status: 400,
description: 'ID parameter is missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NO_ID_IN_REQ",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Invoice not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Invoice not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Invoice successfully deleted',
})
async deleteById(@Param('id') id: number, @Res() res: Response) { async deleteById(@Param('id') id: number, @Res() res: Response) {
if (!id) { if (!id) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ', exceptionMessage: 'ERR.NO_ID_IN_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.invoiceService.remove(id) || {}; const response = await this.invoiceService.remove(id);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Invoice with ID ${id} not found`
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
} }

View File

@ -1,5 +1,5 @@
import { MailerModule } from '@nestjs-modules/mailer'; // import { MailerModule } from '@nestjs-modules/mailer';
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter'; // import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { MailService } from './mail.service'; import { MailService } from './mail.service';
import { join } from 'path'; import { join } from 'path';
@ -7,21 +7,21 @@ import { Utility } from 'src/common/Utility';
@Module({ @Module({
imports: [ imports: [
MailerModule.forRoot({ // MailerModule.forRoot({
// transport: 'smtps://user@example.com:topsecret@smtp.example.com', // // transport: 'smtps://user@example.com:topsecret@smtp.example.com',
// or // // or
transport: Utility.mailConfig.transport, // transport: Utility.mailConfig.transport,
defaults: Utility.mailConfig.defaults, // defaults: Utility.mailConfig.defaults,
template: { // template: {
dir: join(__dirname, 'templates'), // dir: join(__dirname, 'templates'),
adapter: new HandlebarsAdapter(), // or new PugAdapter() or new EjsAdapter() // adapter: new HandlebarsAdapter(), // or new PugAdapter() or new EjsAdapter()
options: { // options: {
strict: true, // strict: true,
}, // },
}, // },
}), // }),
], ],
providers: [MailService], // providers: [MailService],
exports: [MailService], // 👈 export for DI // exports: [MailService], // 👈 export for DI
}) })
export class MailModule {} export class MailModule {}

View File

@ -1,19 +1,19 @@
import { MailerService } from '@nestjs-modules/mailer'; // import { MailerService } from '@nestjs-modules/mailer';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
@Injectable() @Injectable()
export class MailService { export class MailService {
constructor(private mailerService: MailerService) { } // constructor(private mailerService: MailerService) { }
async sendEmail(templateName: string, subject: string, context: any, toEmail: string, ccEmails?: string[], bccEmails?: string[]) { // async sendEmail(templateName: string, subject: string, context: any, toEmail: string, ccEmails?: string[], bccEmails?: string[]) {
await this.mailerService.sendMail({ // await this.mailerService.sendMail({
to: toEmail, // to: toEmail,
cc: ccEmails, // cc: ccEmails,
bcc: bccEmails, // bcc: bccEmails,
subject: subject, // subject: subject,
template: templateName, // `.hbs` extension is appended automatically // template: templateName, // `.hbs` extension is appended automatically
context, // context,
}) // })
} // }
} }

View File

@ -3,14 +3,18 @@ import { AppModule } from './app.module';
import * as bodyParser from 'body-parser'; import * as bodyParser from 'body-parser';
import * as configMaster from './app-config/config.json'; import * as configMaster from './app-config/config.json';
import { Utility } from './common/Utility'; import { Utility } from './common/Utility';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
async function bootstrap() { async function bootstrap() {
Utility.appPort = configMaster.local.appConfig.port; Utility.appPort = configMaster.local.appConfig.port;
Utility.mailConfig = configMaster.local.mailConfig; Utility.mailConfig = configMaster.local.mailConfig;
Utility.fileConfig = configMaster.local.fileConfig; Utility.fileConfig = configMaster.local.fileConfig;
const app = await NestFactory.create(AppModule, { cors: true }); const app = await NestFactory.create(AppModule, { cors: true });
app.use(bodyParser.json({limit: '50mb'})); const config = new DocumentBuilder().setTitle('Payment API').setVersion('1.0').addTag('payments').build();
app.use(bodyParser.urlencoded({limit: '50mb', extended: true})); const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
await app.listen(Utility.appPort); await app.listen(Utility.appPort);
} }
bootstrap(); bootstrap();

View File

@ -2,102 +2,310 @@ import { Body, Controller, Delete, Get, Param, Post, Put, Res } from '@nestjs/co
import { Response } from 'express'; import { Response } from 'express';
import { OffersRedemptionsService } from './offers-redemption.service'; import { OffersRedemptionsService } from './offers-redemption.service';
import { GenericResponse } from 'src/common/GenericResponse.model'; import { GenericResponse } from 'src/common/GenericResponse.model';
import OffersRedemption from './offers-redemption.entity'; import OffersRedemption from './offers-redemption.entity';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger';
@ApiTags('offers-redemption')
@Controller('offers-redemption') @Controller('offers-redemption')
export class OffersRedemptionsController { export class OffersRedemptionsController {
constructor(private offersRedemptionService: OffersRedemptionsService) {} constructor(private offersRedemptionService: OffersRedemptionsService) {}
@Get("/all") @Get("/all")
@ApiOperation({ summary: 'Get all offers redemptions' })
@ApiResponse({ status: 200, description: 'Successfully retrieved all offers redemptions' })
@ApiResponse({
status: 404,
description: 'No offers redemption found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No offers redemptions found"
},
"data": null
}
})
async getAllCompanys(@Res() res: Response) { async getAllCompanys(@Res() res: Response) {
const response = await this.offersRedemptionService.findAll() || []; const response = await this.offersRedemptionService.findAll() || [];
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No offers redemptions found'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Get(':id') @Get(':id')
@ApiOperation({ summary: 'Get an offer redemption by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Offer Redemption ID' })
@ApiResponse({
status: 400,
description: 'ID parameter is missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NO_ID_REQ",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Offer redemption not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No offer redemption found matching criteria"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully retrieved offer redemption',
})
async findById(@Param('id') id: number, @Res() res: Response) { async findById(@Param('id') id: number, @Res() res: Response) {
if(!id) { if (!id) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.offersRedemptionService.findByPk(id) || {}; const response = await this.offersRedemptionService.findByPk(id);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Offer redemption with ID ${id} not found`
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Post('/filter') @Post('/filter')
@ApiOperation({ summary: 'Filter offers redemptions' })
@ApiBody({ type: OffersRedemption })
@ApiResponse({
status: 404,
description: 'Offer redemption not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No offer redemption found matching criteria"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully filtered offers redemptions',
})
async filter(@Body() offersRedemption: OffersRedemption, @Res() res: Response) { async filter(@Body() offersRedemption: OffersRedemption, @Res() res: Response) {
if(!offersRedemption) { if (!offersRedemption) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.offersRedemptionService.findOne(offersRedemption) || {}; const response = await this.offersRedemptionService.findOne(offersRedemption);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No offer redemption found matching criteria'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Post() @Post()
@ApiOperation({ summary: 'Insert a new offer redemption' })
@ApiBody({ type: OffersRedemption })
@ApiResponse({
status: 400,
description: 'Failed to insert offer redemption',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Offer redemption not created"
},
"data": null
}
})
@ApiResponse({
status: 201,
description: 'Offer redemption successfully created',
})
async insert(@Body() offersRedemption: OffersRedemption, @Res() res: Response) { async insert(@Body() offersRedemption: OffersRedemption, @Res() res: Response) {
if(!offersRedemption) { if (!offersRedemption) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
delete offersRedemption.id; delete offersRedemption.id;
const response = await this.offersRedemptionService.upsert(offersRedemption, true); const response = await this.offersRedemptionService.upsert(offersRedemption, true);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'Offer redemption not created'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(201).send(httpResponse);
} }
@Put() @Put()
@ApiOperation({ summary: 'Update an existing offer redemption' })
@ApiBody({ type: OffersRedemption })
@ApiResponse({
status: 400,
description: 'Offer redemption not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Offer redemption not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Offer redemption not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Offer redemption successfully updated',
})
async update(@Body() offersRedemption: OffersRedemption, @Res() res: Response) { async update(@Body() offersRedemption: OffersRedemption, @Res() res: Response) {
if(!offersRedemption || !offersRedemption.id) { if (!offersRedemption || !offersRedemption.id) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.offersRedemptionService.upsert(offersRedemption, false); const response = await this.offersRedemptionService.upsert(offersRedemption, false);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'Offer redemption not found'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Delete(':id') @Delete(':id')
@ApiOperation({ summary: 'Delete an offer redemption by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Offer Redemption ID' })
@ApiResponse({
status: 400,
description: 'ID parameter is missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NO_ID_REQ",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Offer redemption not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Offer redemption not found"
},
"data": null
}
})
@ApiResponse({ status: 200, description: 'Offer redemption successfully deleted' })
async deleteById(@Param('id') id: number, @Res() res: Response) { async deleteById(@Param('id') id: number, @Res() res: Response) {
if(!id) { if (!id) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.offersRedemptionService.remove(id) || {}; const response = await this.offersRedemptionService.remove(id);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Offer redemption with ID ${id} not found`
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
} }

View File

@ -1,103 +1,311 @@
import { Body, Controller, Delete, Get, Param, Post, Put, Res } from '@nestjs/common'; import { Body, Controller, Delete, Get, Param, Post, Put, Res } from '@nestjs/common';
import { OfferService } from './offers.service';
import { Response } from 'express'; import { Response } from 'express';
import { OfferService } from './offers.service';
import { GenericResponse } from 'src/common/GenericResponse.model'; import { GenericResponse } from 'src/common/GenericResponse.model';
import Offer from './offers.entity'; import Offer from './offers.entity';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger';
@ApiTags('offers')
@Controller('offers') @Controller('offers')
export class OfferController { export class OfferController {
constructor(private offersService: OfferService) {} constructor(private offersService: OfferService) {}
@Get("/all") @Get("/all")
@ApiOperation({ summary: 'Get all offers' })
@ApiResponse({ status: 200, description: 'Successfully retrieved all offers' })
@ApiResponse({
status: 404,
description: 'No offers found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No offers found"
},
"data": null
}
})
async getAllOffers(@Res() res: Response) { async getAllOffers(@Res() res: Response) {
const response = await this.offersService.findAll() || []; const response = await this.offersService.findAll() || [];
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No offers found'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Get(':id') @Get(':id')
@ApiOperation({ summary: 'Get an offer by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Offer ID' })
@ApiResponse({
status: 400,
description: 'ID parameter is missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NO_ID_REQ",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Offer not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Offer not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully retrieved offer',
})
async findById(@Param('id') id: number, @Res() res: Response) { async findById(@Param('id') id: number, @Res() res: Response) {
if(!id) { if (!id) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.offersService.findByPk(id) || {}; const response = await this.offersService.findByPk(id);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Offer with ID ${id} not found`
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Post('/filter') @Post('/filter')
@ApiOperation({ summary: 'Filter offers' })
@ApiBody({ type: Offer })
@ApiResponse({
status: 404,
description: 'No offers found matching criteria',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No offers found matching criteria"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully filtered offers',
})
async filter(@Body() offers: Offer, @Res() res: Response) { async filter(@Body() offers: Offer, @Res() res: Response) {
if(!offers) { if (!offers) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.offersService.filter(offers) || {}; const response = await this.offersService.filter(offers);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No offers found matching criteria'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Post() @Post()
@ApiOperation({ summary: 'Create a new offer' })
@ApiBody({ type: Offer })
@ApiResponse({
status: 400,
description: 'Failed to create offer',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Offer not created"
},
"data": null
}
})
@ApiResponse({
status: 201,
description: 'Offer successfully created',
})
async insert(@Body() offers: Offer, @Res() res: Response) { async insert(@Body() offers: Offer, @Res() res: Response) {
if(!offers) { if (!offers) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
delete offers.id; delete offers.id;
const response = await this.offersService.upsert(offers, true); const response = await this.offersService.upsert(offers, true);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'Offer not created'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(201).send(httpResponse);
} }
@Put() @Put()
@ApiOperation({ summary: 'Update an existing offer' })
@ApiBody({ type: Offer })
@ApiResponse({
status: 400,
description: 'Offer not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Offer not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Offer not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Offer successfully updated',
})
async update(@Body() offers: Offer, @Res() res: Response) { async update(@Body() offers: Offer, @Res() res: Response) {
if(!Offer || !offers.id) { if (!offers || !offers.id) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.offersService.upsert(offers, false); const response = await this.offersService.upsert(offers, false);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'Offer not found'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Delete(':id') @Delete(':id')
@ApiOperation({ summary: 'Delete an offer by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Offer ID' })
@ApiResponse({
status: 400,
description: 'ID parameter is missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NO_ID_REQ",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Offer not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Offer not found"
},
"data": null
}
})
@ApiResponse({ status: 200, description: 'Offer successfully deleted' })
async deleteById(@Param('id') id: number, @Res() res: Response) { async deleteById(@Param('id') id: number, @Res() res: Response) {
if(!id) { if (!id) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.offersService.remove(id) || {}; const response = await this.offersService.remove(id);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Offer with ID ${id} not found`
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
} }

View File

@ -2,20 +2,60 @@ import { Body, Controller, Delete, Get, Param, Post, Put, Res } from '@nestjs/co
import { PaymentService } from './payment.service'; import { PaymentService } from './payment.service';
import { Response } from 'express'; import { Response } from 'express';
import { GenericResponse } from '../common/GenericResponse.model'; import { GenericResponse } from '../common/GenericResponse.model';
import Payment from './payment.entity'; import Payment from './payment.entity';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger';
@ApiTags('payments')
@Controller('payments') @Controller('payments')
export class PaymentController { export class PaymentController {
constructor(private paymentService: PaymentService) { } constructor(private paymentService: PaymentService) { }
@Get("/all") @Get("/all")
@ApiOperation({ summary: 'Get all payments' })
@ApiResponse({
status: 200,
description: 'Successfully retrieved all payments',
})
async getAllPayments(@Res() res: Response) { async getAllPayments(@Res() res: Response) {
const response = await this.paymentService.findAll() || []; const response = await this.paymentService.findAll() || [];
const httpResponse = new GenericResponse(null, response) const httpResponse = new GenericResponse(null, response);
res.send(httpResponse); res.status(200).send(httpResponse);
} }
@Get(':id') @Get(':id')
@ApiOperation({ summary: 'Get a payment by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Payment ID' })
@ApiResponse({
status: 400,
description: 'Payment not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Payment not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No payments found matching criteria"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully retrieved payment',
})
async findById(@Param('id') id: number, @Res() res: Response) { async findById(@Param('id') id: number, @Res() res: Response) {
if (!id) { if (!id) {
const response = new GenericResponse({ const response = new GenericResponse({
@ -24,15 +64,58 @@ export class PaymentController {
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.paymentService.findByPk(id) || {}; const response = await this.paymentService.findByPk(id);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Payment with ID ${id} not found`
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Post('/filter') @Post('/filter')
@ApiOperation({ summary: 'Filter payments' })
@ApiBody({ type: Payment })
@ApiResponse({
status: 400,
description: 'Payment not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Payment not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No payments found matching criteria"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully filtered payments',
})
async filter(@Body() payment: Payment, @Res() res: Response) { async filter(@Body() payment: Payment, @Res() res: Response) {
if (!payment) { if (!payment) {
const response = new GenericResponse({ const response = new GenericResponse({
@ -41,15 +124,63 @@ export class PaymentController {
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.paymentService.filter(payment) || []; const response = await this.paymentService.filter(payment);
const httpResponse = new GenericResponse(null, response) if (response && response.length === 0) {
const successResponse = new GenericResponse(null, []);
res.status(200).send(successResponse);
return;
}
if (!response) {
const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `No payments found matching criteria`
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse); res.status(200).send(httpResponse);
} }
@Post() @Post()
@ApiOperation({ summary: 'Insert a new payment' })
@ApiBody({ type: Payment })
@ApiResponse({
status: 400,
description: 'Payment not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Payment not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Payment not found"
},
"data": null
}
})
@ApiResponse({
status: 201,
description: 'Payment successfully created',
})
async insert(@Body() payment: Payment, @Res() res: Response) { async insert(@Body() payment: Payment, @Res() res: Response) {
if (!payment) { if (!payment) {
const response = new GenericResponse({ const response = new GenericResponse({
@ -58,16 +189,59 @@ export class PaymentController {
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
delete payment.id; delete payment.id;
const response = await this.paymentService.upsert(payment, true); const response = await this.paymentService.upsert(payment, true);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Payment not found`
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(201).send(httpResponse);
} }
@Put() @Put()
@ApiOperation({ summary: 'Update an existing payment' })
@ApiBody({ type: Payment })
@ApiResponse({
status: 400,
description: 'Payment not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Payment not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Payment not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Payment successfully updated',
})
async update(@Body() payment: Payment, @Res() res: Response) { async update(@Body() payment: Payment, @Res() res: Response) {
if (!payment || !payment.id) { if (!payment || !payment.id) {
const response = new GenericResponse({ const response = new GenericResponse({
@ -76,15 +250,54 @@ export class PaymentController {
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.paymentService.upsert(payment, false); const response = await this.paymentService.upsert(payment, false);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Payment not found`
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Delete(':id') @Delete(':id')
@ApiOperation({ summary: 'Delete a payment by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Payment ID' })
@ApiResponse({
status: 400,
description: 'Payment not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Payment not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Payment with ID 1 not found"
},
"data": null
}
})
@ApiResponse({ status: 200, description: 'Payment successfully deleted' })
async deleteById(@Param('id') id: number, @Res() res: Response) { async deleteById(@Param('id') id: number, @Res() res: Response) {
if (!id) { if (!id) {
const response = new GenericResponse({ const response = new GenericResponse({
@ -93,13 +306,21 @@ export class PaymentController {
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.paymentService.remove(id) || {}; const response = await this.paymentService.remove(id);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Payment with ID ${id} not found`
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
} }

View File

@ -3,101 +3,309 @@ import { Response } from 'express';
import { GenericResponse } from 'src/common/GenericResponse.model'; import { GenericResponse } from 'src/common/GenericResponse.model';
import { PlanUsageService } from './plan-usage.service'; import { PlanUsageService } from './plan-usage.service';
import PlanUsage from './plan-usage.entity'; import PlanUsage from './plan-usage.entity';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger';
@ApiTags('plan-usage')
@Controller('plans-usage') @Controller('plans-usage')
export class PlanUsageController { export class PlanUsageController {
constructor(private itemsService: PlanUsageService) {} constructor(private itemsService: PlanUsageService) { }
@Get("/all") @Get("/all")
@ApiOperation({ summary: 'Get all plan usage' })
@ApiResponse({ status: 200, description: 'Successfully retrieved all plan usage' })
async getAll(@Res() res: Response) { async getAll(@Res() res: Response) {
const response = await this.itemsService.findAll() || []; const response = await this.itemsService.findAll();
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No plan usage found'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Get(':id') @Get(':id')
@ApiOperation({ summary: 'Get a plan usage by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Plan Usage ID' })
@ApiResponse({
status: 400,
description: 'ID parameter is missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NO_ID_REQ",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Plan usage not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No plan usage found matching criteria"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully retrieved plan usage',
})
async findById(@Param('id') id: number, @Res() res: Response) { async findById(@Param('id') id: number, @Res() res: Response) {
if(!id) { if (!id) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.itemsService.findByPk(id) || {}; const response = await this.itemsService.findByPk(id);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Plan usage with ID ${id} not found`
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Post('/filter') @Post('/filter')
@ApiOperation({ summary: 'Filter plan usage' })
@ApiBody({ type: PlanUsage })
@ApiResponse({
status: 400,
description: 'Plan usage not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Plan usage not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No plan usage found matching criteria"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully filtered plan usage',
})
async filter(@Body() item: PlanUsage, @Res() res: Response) { async filter(@Body() item: PlanUsage, @Res() res: Response) {
if(!item) { if (!item) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.itemsService.filter(item) || {}; const response = await this.itemsService.filter(item);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No plan usage found matching criteria'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Post() @Post()
@ApiOperation({ summary: 'Insert a new plan usage' })
@ApiBody({ type: PlanUsage })
@ApiResponse({
status: 400,
description: 'Plan usage not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 201,
description: 'Plan usage successfully created',
})
async insert(@Body() item: PlanUsage, @Res() res: Response) { async insert(@Body() item: PlanUsage, @Res() res: Response) {
if(!item) { if (!item) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
delete item.id; delete item.id;
const response = await this.itemsService.upsert(item, true); const response = await this.itemsService.upsert(item, true);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'Plan usage not created'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(201).send(httpResponse);
} }
@Put() @Put()
@ApiOperation({ summary: 'Update an existing plan usage' })
@ApiBody({ type: PlanUsage })
@ApiResponse({
status: 400,
description: 'Plan usage not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Plan usage not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Plan usage not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Plan usage successfully updated',
})
async update(@Body() item: PlanUsage, @Res() res: Response) { async update(@Body() item: PlanUsage, @Res() res: Response) {
if(!item || !item.id) { if (!item || !item.id) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.itemsService.upsert(item, false); const response = await this.itemsService.upsert(item, false);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'Plan usage not found'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Delete(':id') @Delete(':id')
@ApiOperation({ summary: 'Delete a plan usage by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Plan Usage ID' })
@ApiResponse({
status: 400,
description: 'ID parameter is missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NO_ID_REQ",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Plan usage not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Plan usage not found"
},
"data": null
}
})
@ApiResponse({ status: 200, description: 'Plan usage successfully deleted' })
async deleteById(@Param('id') id: number, @Res() res: Response) { async deleteById(@Param('id') id: number, @Res() res: Response) {
if(!id) { if (!id) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_REQ', exceptionMessage: 'ERR.NO_ID_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.itemsService.remove(id) || {}; const response = await this.itemsService.remove(id);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Plan usage with ID ${id} not found`
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
} }

View File

@ -1,108 +1,318 @@
import { Body, Controller, Delete, Get, Param, Post, Put, Res } from '@nestjs/common'; import { Body, Controller, Delete, Get, Param, Post, Put, Res } from '@nestjs/common';
import { Response } from 'express'; import { Response } from 'express';
import { GenericResponse } from 'src/common/GenericResponse.model'; import { GenericResponse } from 'src/common/GenericResponse.model';
import Plan from './plans.entity'; import Plan from './plans.entity';
import { PlansService } from './plans.service'; import { PlansService } from './plans.service';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger';
@ApiTags('plans')
@Controller('plans') @Controller('plans')
export class PlansController { export class PlansController {
constructor(private planService?: PlansService) { constructor(private planService: PlansService) { }
if(!planService) {
planService = new PlansService();
}
}
@Get("/all") @Get("/all")
@ApiOperation({ summary: 'Get all plans' })
@ApiResponse({
status: 200,
description: 'Successfully retrieved all plans',
})
@ApiResponse({
status: 404,
description: 'No plans found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No plans found"
},
"data": null
}
})
async plangetAll(@Res() res: Response) { async plangetAll(@Res() res: Response) {
const response = await this.planService.findAll() || []; const response = await this.planService.findAll() || [];
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No plans found'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Get(':id') @Get(':id')
@ApiOperation({ summary: 'Get a plan by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Plan ID' })
@ApiResponse({
status: 400,
description: 'ID parameter is missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NO_ID_IN_REQ",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Plan not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Plan not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully retrieved plan',
})
async planFindById(@Param('id') id: number, @Res() res: Response) { async planFindById(@Param('id') id: number, @Res() res: Response) {
if(!id) { if (!id) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_IN_REQ', exceptionMessage: 'ERR.NO_ID_IN_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.planService.findByPk(id) || {}; const response = await this.planService.findByPk(id);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Plan with ID ${id} not found`
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Post('/filter') @Post('/filter')
async planFilter(@Body() user: Plan, @Res() res: Response) { @ApiOperation({ summary: 'Filter plans based on criteria' })
if(!user) { @ApiBody({ type: Plan })
@ApiResponse({
status: 404,
description: 'No plans found matching criteria',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "No plans found matching criteria"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Successfully filtered plans',
})
async planFilter(@Body() plan: Plan, @Res() res: Response) {
if (!plan) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ', exceptionMessage: 'ERR.NO_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.planService.filter(user) || []; const response = await this.planService.filter(plan);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'No plans found matching criteria'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Post() @Post()
async planInsert(@Body() user: Plan, @Res() res: Response) { @ApiOperation({ summary: 'Create a new plan' })
if(!user) { @ApiBody({ type: Plan })
@ApiResponse({
status: 400,
description: 'Failed to create plan',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_CREATED",
"stackTrace": "Plan not created"
},
"data": null
}
})
@ApiResponse({
status: 201,
description: 'Plan successfully created',
})
async planInsert(@Body() plan: Plan, @Res() res: Response) {
if (!plan) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ', exceptionMessage: 'ERR.NO_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
delete user.id; delete plan.id;
const response = await this.planService.upsert(user, true); const response = await this.planService.upsert(plan, true);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_CREATED',
stackTrace: 'Plan not created'
}, null);
res.status(400).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(201).send(httpResponse);
} }
@Put() @Put()
async planUpdate(@Body() user: Plan, @Res() res: Response) { @ApiOperation({ summary: 'Update an existing plan' })
if(!user || !user.id) { @ApiBody({ type: Plan })
@ApiResponse({
status: 400,
description: 'Failed to update plan',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_UPDATED",
"stackTrace": "Plan not updated"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Plan not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Plan not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Plan successfully updated',
})
async planUpdate(@Body() plan: Plan, @Res() res: Response) {
if (!plan || !plan.id) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ', exceptionMessage: 'ERR.NO_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.planService.upsert(user, false); const response = await this.planService.upsert(plan, false);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: 'Plan not found'
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
@Delete(':id') @Delete(':id')
@ApiOperation({ summary: 'Delete a plan by ID' })
@ApiParam({ name: 'id', type: Number, description: 'Plan ID' })
@ApiResponse({
status: 400,
description: 'ID parameter is missing',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NO_ID_IN_REQ",
"stackTrace": "Request"
},
"data": null
}
})
@ApiResponse({
status: 404,
description: 'Plan not found',
example: {
"notification": {
"exception": true,
"exceptionSeverity": "HIGH",
"exceptionMessage": "ERR.NOT_FOUND",
"stackTrace": "Plan not found"
},
"data": null
}
})
@ApiResponse({
status: 200,
description: 'Plan successfully deleted',
})
async planDeleteById(@Param('id') id: number, @Res() res: Response) { async planDeleteById(@Param('id') id: number, @Res() res: Response) {
if(!id) { if (!id) {
const response = new GenericResponse({ const response = new GenericResponse({
exception: true, exception: true,
exceptionSeverity: 'HIGH', exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ', exceptionMessage: 'ERR.NO_ID_IN_REQ',
stackTrace: 'Request' stackTrace: 'Request'
}, null); }, null);
res.send(response); res.status(400).send(response);
return; return;
} }
const response = await this.planService.remove(id) || {}; const response = await this.planService.remove(id);
const httpResponse = new GenericResponse(null, response) if (!response) {
res.send(httpResponse); const errorResponse = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NOT_FOUND',
stackTrace: `Plan with ID ${id} not found`
}, null);
res.status(404).send(errorResponse);
return;
}
const httpResponse = new GenericResponse(null, response);
res.status(200).send(httpResponse);
} }
} }