implmentação swagger no modulo orders-payment

This commit is contained in:
JurTI-BR
2025-04-02 18:13:47 -03:00
parent 28a1cee876
commit 4719279ab7
36 changed files with 944 additions and 820 deletions

View File

@@ -9,25 +9,23 @@ import { ILogger } from '../Log/ILogger';
import { RedisClientToken } from '../core/configs/cache/redis-client.adapter.provider';
import { IRedisClient } from '../core/configs/cache/IRedisClient';
import { getOrSetCache } from '../shared/cache.util';
import { DataSource } from 'typeorm';
import { DATA_SOURCE } from '../core/constants';
@Injectable()
export class DataConsultService {
private readonly SELLERS_CACHE_KEY = 'data-consult:sellers';
private readonly SELLERS_TTL = 3600; // 1 hora
private readonly SELLERS_TTL = 3600;
private readonly STORES_TTL = 3600;
private readonly BILLINGS_TTL = 3600;
private readonly ALL_PRODUCTS_CACHE_KEY = 'data-consult:products:all';
private readonly ALL_PRODUCTS_TTL = 600; // 10 minutos (ajustável)
private readonly ALL_PRODUCTS_TTL = 600;
constructor(
private readonly repository: DataConsultRepository,
@Inject(RedisClientToken) private readonly redisClient: IRedisClient,
@Inject('LoggerService')
private readonly logger: ILogger,
@Inject('LoggerService') private readonly logger: ILogger,
@Inject(DATA_SOURCE) private readonly dataSource: DataSource
) {}
/**
@@ -36,7 +34,13 @@ export class DataConsultService {
*/
async stores(): Promise<StoreDto[]> {
this.logger.log('Buscando todas as lojas');
return this.repository.findStores();
try {
const stores = await this.repository.findStores();
return stores.map(store => new StoreDto(store));
} catch (error) {
this.logger.error('Erro ao buscar lojas', error);
throw new HttpException('Erro ao buscar lojas', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
/**
@@ -45,15 +49,20 @@ export class DataConsultService {
*/
async sellers(): Promise<SellerDto[]> {
this.logger.log('Buscando vendedores com cache Redis...');
return getOrSetCache<SellerDto[]>(
this.redisClient,
this.SELLERS_CACHE_KEY,
this.SELLERS_TTL,
async () => {
this.logger.log('Cache de vendedores vazio. Buscando no banco...');
return this.repository.findSellers();
}
);
try {
return getOrSetCache<SellerDto[]>(
this.redisClient,
this.SELLERS_CACHE_KEY,
this.SELLERS_TTL,
async () => {
const sellers = await this.repository.findSellers();
return sellers.map(seller => new SellerDto(seller));
}
);
} catch (error) {
this.logger.error('Erro ao buscar vendedores', error);
throw new HttpException('Erro ao buscar vendedores', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
/**
@@ -61,13 +70,16 @@ export class DataConsultService {
* @returns Array de BillingDto
*/
async billings(): Promise<BillingDto[]> {
this.logger.log('Buscando todos os faturamentos');
return this.repository.findBillings();
this.logger.log('Buscando informações de faturamento');
try {
const billings = await this.repository.findBillings();
return billings.map(billing => new BillingDto(billing));
} catch (error) {
this.logger.error('Erro ao buscar faturamento', error);
throw new HttpException('Erro ao buscar faturamento', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
/**
* Obter clientes filtrados por termo de pesquisa
* @param filter - Termo de pesquisa para filtrar clientes
@@ -75,7 +87,16 @@ export class DataConsultService {
*/
async customers(filter: string): Promise<CustomerDto[]> {
this.logger.log(`Buscando clientes com filtro: ${filter}`);
return this.repository.findCustomers(filter);
try {
if (!filter || typeof filter !== 'string') {
throw new HttpException('Filtro inválido', HttpStatus.BAD_REQUEST);
}
const customers = await this.repository.findCustomers(filter);
return customers.map(customer => new CustomerDto(customer));
} catch (error) {
this.logger.error('Erro ao buscar clientes', error);
throw new HttpException('Erro ao buscar clientes', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
/**
@@ -85,34 +106,33 @@ export class DataConsultService {
*/
async products(filter: string): Promise<ProductDto[]> {
this.logger.log(`Buscando produtos com filtro: ${filter}`);
try {
const result = await this.repository.findProducts(filter);
this.logger.log(`Produtos encontrados: ${result.length}`);
return result;
if (!filter || typeof filter !== 'string') {
throw new HttpException('Filtro inválido', HttpStatus.BAD_REQUEST);
}
const products = await this.repository.findProducts(filter);
return products.map(product => new ProductDto(product));
} 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
);
this.logger.error('Erro ao buscar produtos', error);
throw new HttpException('Erro ao buscar produtos', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
async getAllProducts(): Promise<ProductDto[]> {
this.logger.log('Buscando produtos com cache Redis...');
return getOrSetCache<ProductDto[]>(
this.redisClient,
this.ALL_PRODUCTS_CACHE_KEY,
this.ALL_PRODUCTS_TTL,
async () => {
this.logger.log('Cache de produtos vazio. Buscando no banco...');
return this.repository.findAllProducts();
}
);
this.logger.log('Buscando todos os produtos');
try {
return getOrSetCache<ProductDto[]>(
this.redisClient,
this.ALL_PRODUCTS_CACHE_KEY,
this.ALL_PRODUCTS_TTL,
async () => {
const products = await this.repository.findAllProducts();
return products.map(product => new ProductDto(product));
}
);
} catch (error) {
this.logger.error('Erro ao buscar todos os produtos', error);
throw new HttpException('Erro ao buscar produtos', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}