feat: adiciona validações e testes para métodos stores() e sellers()
- Adiciona validações para garantir que dados retornados têm propriedades necessárias - Valida se resultado do repository é array válido - Filtra dados incompletos ou com propriedades vazias - Adiciona testes que expõem problemas e validam correções - Melhora tratamento de erros com logging adequado
This commit is contained in:
@@ -35,35 +35,65 @@ export class DataConsultService {
|
||||
@Inject(DATA_SOURCE) private readonly dataSource: DataSource
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Obter todas as lojas
|
||||
* @returns Array de StoreDto
|
||||
*/
|
||||
async stores(): Promise<StoreDto[]> {
|
||||
this.logger.log('Buscando todas as lojas');
|
||||
try {
|
||||
const stores = await this.repository.findStores();
|
||||
return stores.map(store => new StoreDto(store));
|
||||
|
||||
if (stores === null || stores === undefined) {
|
||||
throw new HttpException('Resultado inválido do repositório', HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
const storesArray = Array.isArray(stores) ? stores : [stores];
|
||||
|
||||
return storesArray
|
||||
.filter(store => {
|
||||
if (!store || typeof store !== 'object') {
|
||||
return false;
|
||||
}
|
||||
const hasId = store.id !== undefined && store.id !== null && store.id !== '';
|
||||
const hasName = store.name !== undefined && store.name !== null && store.name !== '';
|
||||
const hasStore = store.store !== undefined && store.store !== null && store.store !== '';
|
||||
return hasId && hasName && hasStore;
|
||||
})
|
||||
.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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Obter todos os vendedores
|
||||
* @returns Array de SellerDto
|
||||
*/
|
||||
async sellers(): Promise<SellerDto[]> {
|
||||
this.logger.log('Buscando vendedores com cache Redis...');
|
||||
try {
|
||||
return getOrSetCache<SellerDto[]>(
|
||||
return await 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));
|
||||
try {
|
||||
const sellers = await this.repository.findSellers();
|
||||
|
||||
if (sellers === null || sellers === undefined) {
|
||||
throw new HttpException('Resultado inválido do repositório', HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
const sellersArray = Array.isArray(sellers) ? sellers : [sellers];
|
||||
|
||||
return sellersArray
|
||||
.filter(seller => {
|
||||
if (!seller || typeof seller !== 'object') {
|
||||
return false;
|
||||
}
|
||||
const hasId = seller.id !== undefined && seller.id !== null && seller.id !== '';
|
||||
const hasName = seller.name !== undefined && seller.name !== null && seller.name !== '';
|
||||
return hasId && hasName;
|
||||
})
|
||||
.map(seller => new SellerDto(seller));
|
||||
} catch (error) {
|
||||
this.logger.error('Erro ao buscar vendedores', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
@@ -72,9 +102,6 @@ export class DataConsultService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns Array de BillingDto
|
||||
*/
|
||||
async billings(): Promise<BillingDto[]> {
|
||||
this.logger.log('Buscando informações de faturamento');
|
||||
try {
|
||||
@@ -86,11 +113,6 @@ export class DataConsultService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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}`);
|
||||
try {
|
||||
@@ -105,11 +127,6 @@ export class DataConsultService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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}`);
|
||||
try {
|
||||
@@ -142,10 +159,6 @@ export class DataConsultService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Obter todas as transportadoras cadastradas
|
||||
* @returns Array de CarrierDto
|
||||
*/
|
||||
async getAllCarriers(): Promise<CarrierDto[]> {
|
||||
this.logger.log('Buscando todas as transportadoras');
|
||||
try {
|
||||
@@ -168,11 +181,6 @@ export class DataConsultService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Obter transportadoras por período de data
|
||||
* @param query - Filtros de data e filial
|
||||
* @returns Array de CarrierDto
|
||||
*/
|
||||
async getCarriersByDate(query: FindCarriersDto): Promise<CarrierDto[]> {
|
||||
this.logger.log(`Buscando transportadoras por período: ${JSON.stringify(query)}`);
|
||||
try {
|
||||
@@ -189,11 +197,6 @@ export class DataConsultService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Obter transportadoras de um pedido específico
|
||||
* @param orderId - ID do pedido
|
||||
* @returns Array de CarrierDto
|
||||
*/
|
||||
async getOrderCarriers(orderId: number): Promise<CarrierDto[]> {
|
||||
this.logger.log(`Buscando transportadoras do pedido: ${orderId}`);
|
||||
try {
|
||||
@@ -209,10 +212,6 @@ export class DataConsultService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Obter todas as regiões cadastradas
|
||||
* @returns Array de RegionDto
|
||||
*/
|
||||
async getRegions(): Promise<RegionDto[]> {
|
||||
this.logger.log('Buscando todas as regiões');
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user