commit
This commit is contained in:
146
src/partner-range/partner-range.service.ts
Normal file
146
src/partner-range/partner-range.service.ts
Normal file
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
https://docs.nestjs.com/providers#services
|
||||
*/
|
||||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { connectionOptions } from 'src/configs/typeorm.config';
|
||||
import { Estfaixaparceiro } from 'src/domain/entity/tables/estfaixaparceiro.entity';
|
||||
import { PartnerRange } from 'src/domain/models/partner-range.model';
|
||||
import { Connection } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class PartnerRangeService {
|
||||
|
||||
selectPartnerRange = 'SELECT ESTFAIXAPARCEIRO.ID AS "id" ' +
|
||||
' ,ESTFAIXAPARCEIRO.TIPO AS "type" ' +
|
||||
' ,CASE WHEN ESTFAIXAPARCEIRO.TIPO = \'P\' THEN \'Parceiro\' ' +
|
||||
' WHEN ESTFAIXAPARCEIRO.TIPO = \'B\' THEN \'Bella Obra\' ' +
|
||||
' WHEN ESTFAIXAPARCEIRO.TIPO = \'M\' THEN \'Mestre Jurunense\' ' +
|
||||
' ELSE \'Não Informado\' END AS "descriptionType" ' +
|
||||
' ,ESTFAIXAPARCEIRO.FAIXAINI AS "rangeIni" ' +
|
||||
' ,ESTFAIXAPARCEIRO.FAIXAFIM AS "rangeFin" ' +
|
||||
' ,ESTFAIXAPARCEIRO.PERCCOMISSAO AS "percentComiss" ' +
|
||||
' ,ESTFAIXAPARCEIRO.DTCADASTRO AS "createDate" ' +
|
||||
' ,ESTFAIXAPARCEIRO.CODFUNCCAD AS "createUserId" ' +
|
||||
' ,ESTFAIXAPARCEIRO.DTALTERACAO AS "updateDate" ' +
|
||||
' ,ESTFAIXAPARCEIRO.CODFUNCALT AS "updateUserId" ' +
|
||||
' ,USUARIO_CADASTRO.NOME AS "createUserName" ' +
|
||||
' ,USUARIO_ALTERACAO.NOME AS "updateUserName" ' +
|
||||
' FROM ESTFAIXAPARCEIRO, PCEMPR USUARIO_CADASTRO, PCEMPR USUARIO_ALTERACAO ' +
|
||||
' WHERE ESTFAIXAPARCEIRO.CODFUNCCAD = USUARIO_CADASTRO.MATRICULA (+) ' +
|
||||
' AND ESTFAIXAPARCEIRO.CODFUNCALT = USUARIO_ALTERACAO.MATRICULA (+) ';
|
||||
|
||||
async getPartnerRanges(type: string) {
|
||||
let sqlFilter = '';
|
||||
if ( type != null && type != '' && type !== 'T') {
|
||||
sqlFilter = ` AND ESTFAIXAPARCEIRO.TIPO = '${type}'`;
|
||||
}
|
||||
const connection = new Connection(connectionOptions);
|
||||
await connection.connect();
|
||||
const queryRunner = connection.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
try {
|
||||
const partnerCategory = await queryRunner
|
||||
.query(this.selectPartnerRange + sqlFilter );
|
||||
return partnerCategory;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
await connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
async getPartnerRange(id: number) {
|
||||
const connection = new Connection(connectionOptions);
|
||||
await connection.connect();
|
||||
const queryRunner = connection.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
try {
|
||||
const partnerCategory = await queryRunner
|
||||
.query(this.selectPartnerRange +
|
||||
' AND 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 createOrUpdatePartnerRange(data: PartnerRange) {
|
||||
const connection = new Connection(connectionOptions);
|
||||
await connection.connect();
|
||||
const queryRunner = connection.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
|
||||
let estfaixaparceiro = await queryRunner.manager.findOne(Estfaixaparceiro, {
|
||||
id: data.id,
|
||||
});
|
||||
await queryRunner.startTransaction();
|
||||
try {
|
||||
let id = 1;
|
||||
if (estfaixaparceiro != null) {
|
||||
id = estfaixaparceiro.id;
|
||||
} else {
|
||||
const queryId = await queryRunner.query('SELECT ESSFAIXAPARCEIRO.NEXTVAL AS "id" FROM DUAL');
|
||||
if (queryId.length > 0) {
|
||||
id = queryId[0].id;
|
||||
}
|
||||
}
|
||||
if ( estfaixaparceiro == null ) {
|
||||
estfaixaparceiro = new Estfaixaparceiro();
|
||||
estfaixaparceiro.codfunccad = data.userId;
|
||||
estfaixaparceiro.dtCadastro = new Date();
|
||||
}
|
||||
estfaixaparceiro.id = id;
|
||||
estfaixaparceiro.faixaIni = data.rangeIni;
|
||||
estfaixaparceiro.faixaFim = data.rangeFin;
|
||||
estfaixaparceiro.percComissao = data.percentComiss;
|
||||
estfaixaparceiro.codfuncalt = data.userId;
|
||||
estfaixaparceiro.tipo = data.type;
|
||||
estfaixaparceiro.dtAlteracao = new Date();
|
||||
|
||||
await queryRunner.manager.save(estfaixaparceiro);
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
data.id = id;
|
||||
|
||||
return data;
|
||||
|
||||
} catch (error) {
|
||||
await queryRunner.rollbackTransaction();
|
||||
throw error;
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
await connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
async deleteRange(id: number) {
|
||||
|
||||
const connection = new Connection(connectionOptions);
|
||||
await connection.connect();
|
||||
const queryRunner = connection.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
|
||||
|
||||
await queryRunner.startTransaction();
|
||||
try {
|
||||
const sqlDelete = "DELETE FROM ESTFAIXAPARCEIRO 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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user