170 lines
5.3 KiB
TypeScript
170 lines
5.3 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { DataSource } from 'typeorm';
|
|
import { createOracleConfig } from '../core/configs/typeorm.oracle.config';
|
|
import { StoreDto } from './dto/store.dto';
|
|
import { SellerDto } from './dto/seller.dto';
|
|
import { BillingDto } from './dto/billing.dto';
|
|
import { CustomerDto } from './dto/customer.dto';
|
|
import { ProductDto } from './dto/product.dto';
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
@Injectable()
|
|
export class DataConsultRepository {
|
|
private readonly dataSource: DataSource;
|
|
|
|
constructor(private readonly configService: ConfigService) {
|
|
this.dataSource = new DataSource(createOracleConfig(configService));
|
|
this.dataSource.initialize();
|
|
}
|
|
|
|
private async executeQuery<T>(sql: string, params: any[] = []): Promise<T> {
|
|
const queryRunner = this.dataSource.createQueryRunner();
|
|
await queryRunner.connect();
|
|
try {
|
|
const result = await queryRunner.query(sql, params);
|
|
return result as T;
|
|
} finally {
|
|
await queryRunner.release();
|
|
}
|
|
}
|
|
|
|
async findStores(): Promise<StoreDto[]> {
|
|
const sql = `
|
|
SELECT PCFILIAL.CODIGO as "id",
|
|
PCFILIAL.RAZAOSOCIAL as "name",
|
|
PCFILIAL.CODIGO || ' - ' || PCFILIAL.FANTASIA as "store"
|
|
FROM PCFILIAL
|
|
WHERE PCFILIAL.CODIGO NOT IN ('99', '69')
|
|
ORDER BY TO_NUMBER(PCFILIAL.CODIGO)
|
|
`;
|
|
return this.executeQuery<StoreDto[]>(sql);
|
|
}
|
|
|
|
async findSellers(): Promise<SellerDto[]> {
|
|
const sql = `
|
|
SELECT PCUSUARI.CODUSUR as "id",
|
|
PCUSUARI.NOME as "name"
|
|
FROM PCUSUARI
|
|
WHERE PCUSUARI.DTTERMINO IS NULL
|
|
AND PCUSUARI.TIPOVEND NOT IN ('P')
|
|
AND (PCUSUARI.BLOQUEIO IS NULL OR PCUSUARI.BLOQUEIO = 'N')
|
|
ORDER BY PCUSUARI.NOME
|
|
`;
|
|
return this.executeQuery<SellerDto[]>(sql);
|
|
}
|
|
|
|
|
|
async findBillings(): Promise<BillingDto[]> {
|
|
const sql = `
|
|
SELECT PCCOB.CODCOB as "id",
|
|
PCCOB.CODCOB || ' - ' || PCCOB.COBRANCA as "description"
|
|
FROM PCCOB
|
|
WHERE PCCOB.CODCOB NOT IN ('DEVP', 'DEVT', 'DESD')
|
|
ORDER BY PCCOB.COBRANCA
|
|
`;
|
|
return this.executeQuery<BillingDto[]>(sql);
|
|
}
|
|
|
|
async findCustomers(filter: string): Promise<CustomerDto[]> {
|
|
if (!filter || typeof filter !== 'string') return [];
|
|
|
|
const cleanedNumeric = filter.replace(/[^\d]/g, '');
|
|
const likeFilter = filter.toUpperCase().replace('@', '%') + '%';
|
|
|
|
const queries = [
|
|
{
|
|
sql: `
|
|
SELECT PCCLIENT.CODCLI as "id",
|
|
PCCLIENT.CODCLI || ' - ' || PCCLIENT.CLIENTE ||
|
|
' ( ' || REGEXP_REPLACE(PCCLIENT.CGCENT, '[^0-9]', '') || ' )' as "name"
|
|
FROM PCCLIENT
|
|
WHERE PCCLIENT.CODCLI = :1
|
|
ORDER BY PCCLIENT.CLIENTE
|
|
`,
|
|
params: [cleanedNumeric],
|
|
},
|
|
{
|
|
sql: `
|
|
SELECT PCCLIENT.CODCLI as "id",
|
|
PCCLIENT.CODCLI || ' - ' || PCCLIENT.CLIENTE ||
|
|
' ( ' || REGEXP_REPLACE(PCCLIENT.CGCENT, '[^0-9]', '') || ' )' as "name"
|
|
FROM PCCLIENT
|
|
WHERE REGEXP_REPLACE(PCCLIENT.CGCENT, '[^0-9]', '') = :1
|
|
ORDER BY PCCLIENT.CLIENTE
|
|
`,
|
|
params: [cleanedNumeric],
|
|
},
|
|
{
|
|
sql: `
|
|
SELECT PCCLIENT.CODCLI as "id",
|
|
PCCLIENT.CODCLI || ' - ' || PCCLIENT.CLIENTE ||
|
|
' ( ' || REGEXP_REPLACE(PCCLIENT.CGCENT, '[^0-9]', '') || ' )' as "name"
|
|
FROM PCCLIENT
|
|
WHERE UPPER(PCCLIENT.CLIENTE) LIKE :1
|
|
ORDER BY PCCLIENT.CLIENTE
|
|
`,
|
|
params: [likeFilter],
|
|
},
|
|
];
|
|
|
|
for (const { sql, params } of queries) {
|
|
const result = await this.executeQuery<CustomerDto[]>(sql, params);
|
|
if (result.length > 0) {
|
|
return result;
|
|
}
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
|
|
async findProducts(filter: string): Promise<ProductDto[]> {
|
|
const cleanedFilter = filter.replace(/[^\d]/g, '');
|
|
const likeFilter = filter + '%';
|
|
|
|
const queries = [
|
|
{
|
|
sql: `
|
|
SELECT PCPRODUT.CODPROD as "id",
|
|
PCPRODUT.CODPROD || ' - ' || PCPRODUT.DESCRICAO ||
|
|
' ( ' || REGEXP_REPLACE(PCPRODUT.CODAUXILIAR, '[^0-9]', '') || ' )' as "description"
|
|
FROM PCPRODUT
|
|
WHERE PCPRODUT.CODPROD = ?
|
|
ORDER BY PCPRODUT.DESCRICAO
|
|
`,
|
|
params: [cleanedFilter],
|
|
},
|
|
{
|
|
sql: `
|
|
SELECT PCPRODUT.CODPROD as "id",
|
|
PCPRODUT.CODPROD || ' - ' || PCPRODUT.DESCRICAO ||
|
|
' ( ' || REGEXP_REPLACE(PCPRODUT.CODAUXILIAR, '[^0-9]', '') || ' )' as "description"
|
|
FROM PCPRODUT
|
|
WHERE PCPRODUT.CODAUXILIAR = ?
|
|
ORDER BY PCPRODUT.DESCRICAO
|
|
`,
|
|
params: [cleanedFilter],
|
|
},
|
|
{
|
|
sql: `
|
|
SELECT PCPRODUT.CODPROD as "id",
|
|
PCPRODUT.CODPROD || ' - ' || PCPRODUT.DESCRICAO ||
|
|
' ( ' || REGEXP_REPLACE(PCPRODUT.CODAUXILIAR, '[^0-9]', '') || ' )' as "description"
|
|
FROM PCPRODUT
|
|
WHERE PCPRODUT.DESCRICAO LIKE ?
|
|
ORDER BY PCPRODUT.DESCRICAO
|
|
`,
|
|
params: [likeFilter],
|
|
},
|
|
];
|
|
|
|
for (const { sql, params } of queries) {
|
|
const result = await this.executeQuery<ProductDto[]>(sql, params);
|
|
if (result.length > 0) {
|
|
return result;
|
|
}
|
|
}
|
|
|
|
return [];
|
|
}
|
|
}
|