Ajuste conexao oracle & postgres

This commit is contained in:
unknown
2025-03-28 17:03:25 -03:00
parent d2ffb266db
commit b98b219e52
28 changed files with 916 additions and 1383 deletions

View File

@@ -1,6 +1,4 @@
import { Inject, Injectable } from '@nestjs/common';
import { IRedisClient } from '../core/configs/cache/IRedisClient';
import { RedisClientToken } from '../core/configs/cache/redis-client.adapter.provider';
import { Injectable, HttpException, HttpStatus, Inject } from '@nestjs/common';
import { DataConsultRepository } from './data-consult.repository';
import { StoreDto } from './dto/store.dto';
import { SellerDto } from './dto/seller.dto';
@@ -9,119 +7,73 @@ import { CustomerDto } from './dto/customer.dto';
import { ProductDto } from './dto/product.dto';
import { ILogger } from '../Log/ILogger';
const DEFAULT_CACHE_TTL = 100;
@Injectable()
export class DataConsultService {
constructor(
private readonly repository: DataConsultRepository,
@Inject(RedisClientToken)
private readonly redisClient: IRedisClient,
@Inject('LoggerService')
private readonly logger: ILogger
) {}
/**
* Método genérico para lidar com lógica de cache
* @param cacheKey - A chave a ser usada para cache
* @param fetchFn - Função para buscar dados se não estiverem no cache
* @param ttl - Tempo de vida em segundos para o cache
* @returns Os dados em cache ou recentemente buscados
*/
private async getCachedData<T>(
cacheKey: string,
fetchFn: () => Promise<T>,
ttl: number = DEFAULT_CACHE_TTL
): Promise<T> {
try {
this.logger.log(`Tentando obter dados em cache para a chave: ${cacheKey}`);
const cached = await this.redisClient.get<T>(cacheKey);
if (cached) {
this.logger.log(`Cache encontrado para a chave: ${cacheKey}`);
return cached;
}
this.logger.log(`Cache não encontrado para a chave: ${cacheKey}, buscando na origem`);
const result = await fetchFn();
try {
await this.redisClient.set<T>(cacheKey, result, ttl);
this.logger.log(`Dados armazenados em cache com sucesso para a chave: ${cacheKey}`);
} catch (cacheError) {
this.logger.warn(`Falha ao armazenar dados em cache para a chave: ${cacheKey}`);
this.logger.error('Detalhes do erro de cache:', cacheError instanceof Error ? cacheError.stack : '');
}
return result;
} catch (error) {
this.logger.error(
`Erro no método getCachedData para a chave ${cacheKey}:`,
error instanceof Error ? error.stack : ''
);
return fetchFn();
}
}
/**
* Obter todas as lojas com cache
* Obter todas as lojas
* @returns Array de StoreDto
*/
async stores(): Promise<StoreDto[]> {
this.logger.log('Buscando todas as lojas');
return this.getCachedData<StoreDto[]>(
'data-consult:stores',
() => this.repository.findStores()
);
return this.repository.findStores();
}
/**
* Obter todos os vendedores com cache
* Obter todos os vendedores
* @returns Array de SellerDto
*/
async sellers(): Promise<SellerDto[]> {
this.logger.log('Buscando todos os vendedores');
return this.getCachedData<SellerDto[]>(
'data-consult:sellers',
() => this.repository.findSellers()
);
return this.repository.findSellers();
}
/**
* Obter todos os faturamentos com cache
* Obter todos os faturamentos
* @returns Array de BillingDto
*/
async billings(): Promise<BillingDto[]> {
this.logger.log('Buscando todos os faturamentos');
return this.getCachedData<BillingDto[]>(
'data-consult:billings',
() => this.repository.findBillings()
);
return this.repository.findBillings();
}
/**
* Obter clientes filtrados por termo de pesquisa com cache
* Obter clientes filtrados por termo de pesquisa
* @param filter - Termo de pesquisa para filtrar clientes
* @returns Array de CustomerDto
*/
async customers(filter: string): Promise<CustomerDto[]> {
this.logger.log(`Buscando clientes com filtro: ${filter}`);
return this.getCachedData<CustomerDto[]>(
`data-consult:customers:${filter}`,
() => this.repository.findCustomers(filter)
);
return this.repository.findCustomers(filter);
}
/**
* Obter produtos filtrados por termo de pesquisa com cache
* Obter produtos filtrados por termo de pesquisa
* @param filter - Termo de pesquisa para filtrar produtos
* @returns Array de ProductDto
*/
async products(filter: string): Promise<ProductDto[]> {
this.logger.log(`Buscando produtos com filtro: ${filter}`);
return this.getCachedData<ProductDto[]>(
`data-consult:products:${filter}`,
() => this.repository.findProducts(filter)
);
try {
const result = await this.repository.findProducts(filter);
this.logger.log(`Produtos encontrados: ${result.length}`);
return result;
} catch (error) {
this.logger.error(
`Erro ao buscar produtos com filtro "${filter}"`,
error instanceof Error ? error.stack : ''
);
throw new HttpException(
'Erro ao buscar produtos. Tente novamente mais tarde.',
HttpStatus.INTERNAL_SERVER_ERROR
);
}
}
}