This commit is contained in:
Felipe Batista
2025-01-27 17:44:27 -03:00
commit 47e7f75720
238 changed files with 36425 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
/*
https://docs.nestjs.com/controllers#controllers
*/
import { Body, Controller, Delete, Get, HttpException, HttpStatus, Param, Post, Query } from '@nestjs/common';
import { PartnerCategoryService } from './partner-category.service';
import { PartnerCategory } from '../domain/models/partner-category.model';
import { ResultModel } from 'src/domain/models/result.model';
import { ApiTags } from '@nestjs/swagger';
@ApiTags('Partner')
@Controller('api/v1/partner/category')
export class PartnerCategoryController {
constructor(
private readonly partnerCategoryService: PartnerCategoryService,
){}
@Get()
getPartnersCategory(@Query() query){
let type = 'T';
let description = '';
try {
type = query['type'];
if ( query['type'] != null ) {
type = query['type'];
}
if ( query['name'] != null ) {
description = query['description'];
}
return this.partnerCategoryService.getPartnersCategory(type, description);
}
catch(error){
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@Get(':id')
getPartnersCategoryById(@Param('id') id: number){
try {
return this.partnerCategoryService.getPartnerCategory(id);
}
catch(error){
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@Post('create')
createPartner(@Body() data: PartnerCategory) {
try {
return this.partnerCategoryService.createOrUpdatePartnerCategory(data);
}
catch(error){
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
@Delete('delete/:id')
async deleteCategory(@Param('id') id: number) {
try {
await this.partnerCategoryService.deleteCategory(id);
return new ResultModel(true, 'Categoria excluída com sucesso!', null, null);
}
catch(error){
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}
}
}

View File

@@ -0,0 +1,17 @@
import { PartnerCategoryService } from './partner-category.service';
/*
https://docs.nestjs.com/modules
*/
import { Module } from '@nestjs/common';
import { PartnerCategoryController } from './partner-category.controller';
@Module({
imports: [],
controllers: [
PartnerCategoryController,
],
providers: [
PartnerCategoryService,],
})
export class PartnerCategoryModule { }

View File

@@ -0,0 +1,148 @@
/*
https://docs.nestjs.com/providers#services
*/
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { connectionOptions } from 'src/configs/typeorm.config';
import { Estcategoriaparceiro } from 'src/domain/entity/tables/estcategoriaparceiro.entity';
import { PartnerCategory } from 'src/domain/models/partner-category.model';
import { Connection } from 'typeorm';
@Injectable()
export class PartnerCategoryService {
async getPartnersCategory(type: string, description: string) {
const connection = new Connection(connectionOptions);
await connection.connect();
const queryRunner = connection.createQueryRunner();
await queryRunner.connect();
try {
const partnerCategory = await queryRunner
.query('SELECT ESTCATEGORIAPARCEIRO.ID as "id" ' +
' ,ESTCATEGORIAPARCEIRO.TIPO as "partnerType" ' +
' ,CASE WHEN ESTCATEGORIAPARCEIRO.TIPO = \'P\' THEN \'PARCEIRO\' ' +
' WHEN ESTCATEGORIAPARCEIRO.TIPO = \'B\' THEN \'BELLA OBRA\' ' +
' WHEN ESTCATEGORIAPARCEIRO.TIPO = \'M\' THEN \'MESTRE JURUNENSE\' ' +
' ELSE \'OUTROS\' END as "descriptionType" ' +
' ,ESTCATEGORIAPARCEIRO.DESCRICAO as "name" ' +
' ,ESTCATEGORIAPARCEIRO.TIPOPAGTO as "paymentType" ' +
' ,CASE WHEN ESTCATEGORIAPARCEIRO.TIPOPAGTO = \'C\' ' +
' THEN \'Crédito\' ELSE \'Contas a pagar\' END as "descriptionPayment" ' +
' FROM ESTCATEGORIAPARCEIRO ' +
' WHERE ( ESTCATEGORIAPARCEIRO.TIPO = :1 OR :2 = \'T\' )' +
" AND ESTCATEGORIAPARCEIRO.DESCRICAO LIKE :3||'%' ", [type, type, description] );
return partnerCategory;
} catch (error) {
throw error;
} finally {
await queryRunner.release();
await connection.close();
}
}
async getPartnerCategory(id: number) {
const connection = new Connection(connectionOptions);
await connection.connect();
const queryRunner = connection.createQueryRunner();
await queryRunner.connect();
try {
const partnerCategory = await queryRunner
.query('SELECT ESTCATEGORIAPARCEIRO.ID as "id" ' +
' ,ESTCATEGORIAPARCEIRO.TIPO as "partnerType" ' +
' ,CASE WHEN ESTCATEGORIAPARCEIRO.TIPO = \'P\' THEN \'PARCEIRO\' ' +
' WHEN ESTCATEGORIAPARCEIRO.TIPO = \'B\' THEN \'BELLA OBRA\' ' +
' WHEN ESTCATEGORIAPARCEIRO.TIPO = \'M\' THEN \'MESTRE JURUNENSE\' ' +
' ELSE \'OUTROS\' END as "descriptionType" ' +
' ,ESTCATEGORIAPARCEIRO.DESCRICAO as "name" ' +
' ,ESTCATEGORIAPARCEIRO.TIPOPAGTO as "paymentType" ' +
' ,CASE WHEN ESTCATEGORIAPARCEIRO.TIPOPAGTO = \'C\' ' +
' THEN \'Crédito\' ELSE \'Contas a pagar\' END as "descriptionPayment" ' +
' FROM ESTCATEGORIAPARCEIRO ' +
' WHERE ESTCATEGORIAPARCEIRO.ID = :1', [id]);
return partnerCategory[0];
} catch (error) {
console.log('Erro ao consultar parceiro.');
throw error;
} finally {
await queryRunner.release();
await connection.close();
}
}
async createOrUpdatePartnerCategory(data: PartnerCategory) {
const partnerCategory = await this.getPartnerCategory(data.id);
const connection = new Connection(connectionOptions);
await connection.connect();
const queryRunner = connection.createQueryRunner();
await queryRunner.connect();
let estCategoriaParceiro = await queryRunner.manager.findOne(Estcategoriaparceiro, {
id: data.id,
});
await queryRunner.startTransaction();
try {
let id = 1;
if (partnerCategory != null) {
id = partnerCategory.id;
} else {
const queryId = await queryRunner.query('SELECT ESSCATEGORIAPARCEIRO.NEXTVAL AS "id" FROM DUAL');
if (queryId.length > 0) {
id = queryId[0].id;
}
}
if ( estCategoriaParceiro == null ) {
estCategoriaParceiro = new Estcategoriaparceiro();
}
estCategoriaParceiro.id = id;
estCategoriaParceiro.descricao = data.name;
estCategoriaParceiro.tipo = data.partnerType;
estCategoriaParceiro.tipopagto = data.paymentType;
await queryRunner.manager.save(estCategoriaParceiro);
await queryRunner.commitTransaction();
data.id = id;
return data;
} catch (error) {
await queryRunner.rollbackTransaction();
throw error;
} finally {
await queryRunner.release();
await connection.close();
}
}
async deleteCategory(id: number) {
const connection = new Connection(connectionOptions);
await connection.connect();
const queryRunner = connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
const parceiroCategory = await queryRunner.query('SELECT COUNT(1) as "qt" FROM ESTPARCEIRO WHERE CODCATEGORIA = :id', [id]);
if ( parceiroCategory.qt > 0) {
throw new HttpException('Existem parceiros vinculados nesta categoria, exclusao não permitida.', HttpStatus.BAD_REQUEST);
}
const sqlDelete = "DELETE FROM ESTCATEGORIAPARCEIRO WHERE ID = :ID";
await queryRunner.query(sqlDelete, [id]);
await queryRunner.commitTransaction();
} catch (error) {
await queryRunner.rollbackTransaction();
throw error;
} finally {
await queryRunner.release();
await connection.close();
}
}
}