remedify-payments-be/src/master-config/master-config-mapping/master-config-mapping.controller.ts
2025-02-24 12:46:49 +05:30

105 lines
3.7 KiB
TypeScript

import { Body, Controller, Delete, Get, Param, Post, Put, Res } from '@nestjs/common';
import { Response } from 'express';
import { GenericResponse } from 'src/common/GenericResponse.model';
import { MasterConfigMappingService } from './master-config-mapping.service';
import MasterConfigMapping from './master-config-mapping.entity';
@Controller('master-config/mapping')
export class MasterConfigMappingController {
constructor(private masterConfigService: MasterConfigMappingService) {
}
@Get("/all")
async getAll(@Res() res: Response) {
const response = await this.masterConfigService.findAll() || [];
const httpResponse = new GenericResponse(null, response)
res.send(httpResponse);
}
@Get(':id')
async findById(@Param('id') id: number, @Res() res: Response) {
if(!id) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_ID_IN_REQ',
stackTrace: 'Request'
}, null);
res.send(response);
return;
}
const response = await this.masterConfigService.findByPk(id) || {};
const httpResponse = new GenericResponse(null, response)
res.send(httpResponse);
}
@Post('/filter')
async filter(@Body() masterConfig: MasterConfigMapping, @Res() res: Response) {
if(!masterConfig) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ',
stackTrace: 'Request'
}, null);
res.send(response);
return;
}
const response = await this.masterConfigService.filter(masterConfig) || [];
const httpResponse = new GenericResponse(null, response)
res.send(httpResponse);
}
@Post()
async insert(@Body() masterConfig: MasterConfigMapping, @Res() res: Response) {
if(!masterConfig) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ',
stackTrace: 'Request'
}, null);
res.send(response);
return;
}
delete masterConfig.id;
const response = await this.masterConfigService.upsert(masterConfig, true);
const httpResponse = new GenericResponse(null, response)
res.send(httpResponse);
}
@Put()
async update(@Body() masterConfig: MasterConfigMapping, @Res() res: Response) {
if(!masterConfig || !masterConfig.id) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ',
stackTrace: 'Request'
}, null);
res.send(response);
return;
}
const response = await this.masterConfigService.upsert(masterConfig, false);
const httpResponse = new GenericResponse(null, response)
res.send(httpResponse);
}
@Delete(':id')
async deleteById(@Param('id') id: number, @Res() res: Response) {
if(!id) {
const response = new GenericResponse({
exception: true,
exceptionSeverity: 'HIGH',
exceptionMessage: 'ERR.NO_REQ',
stackTrace: 'Request'
}, null);
res.send(response);
return;
}
const response = await this.masterConfigService.remove(id) || {};
const httpResponse = new GenericResponse(null, response)
res.send(httpResponse);
}
}