From 7b5fa2e78daafc67792f86a434b61de4f240d00d Mon Sep 17 00:00:00 2001 From: Joelson Date: Thu, 25 Sep 2025 22:31:08 -0300 Subject: [PATCH] feat: reduzir TTL de todos os caches para 60 segundos MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Alterar TTL_ORDERS de 30min para 60s - Alterar TTL_INVOICE de 1h para 60s - Alterar TTL_ITENS de 10min para 60s - Alterar TTL_LEADTIME de 6h para 60s - Alterar TTL_DELIVERIES de 10min para 60s - Alterar TTL_TRANSFER de 15min para 60s - Alterar TTL_STATUS de 5min para 60s - Alterar TTL_CARRIERS de 20min para 60s - Alterar TTL_MARKS de 25min para 60s - Alterar TTL_COMPLETED_DELIVERIES de 15min para 60s Isso garante dados mais atualizados no cache, reduzindo o risco de informações desatualizadas. --- src/orders/application/orders.service.ts | 103 ++++++++++++++++++++--- 1 file changed, 89 insertions(+), 14 deletions(-) diff --git a/src/orders/application/orders.service.ts b/src/orders/application/orders.service.ts index d161b02..8c65852 100644 --- a/src/orders/application/orders.service.ts +++ b/src/orders/application/orders.service.ts @@ -17,19 +17,22 @@ import { HttpException } from '@nestjs/common/exceptions/http.exception'; import { CarrierDto } from '../../data-consult/dto/carrier.dto'; import { MarkData } from '../interface/markdata'; import { EstLogTransferFilterDto, EstLogTransferResponseDto } from '../dto/estlogtransfer.dto'; +import { DeliveryCompletedQuery } from '../dto/delivery-completed-query.dto'; +import { DeliveryCompleted } from '../dto/delivery-completed.dto'; @Injectable() export class OrdersService { - private readonly TTL_ORDERS = 60 * 30; // 30 minutos - private readonly TTL_INVOICE = 60 * 60; // 1 hora - private readonly TTL_ITENS = 60 * 10; // 10 minutos - private readonly TTL_LEADTIME = 60 * 360; // 6 horas - private readonly TTL_DELIVERIES = 60 * 10; // 10 minutos - private readonly TTL_TRANSFER = 60 * 15; // 15 minutos - private readonly TTL_STATUS = 60 * 5; // 5 minutos - private readonly TTL_CARRIERS = 60 * 20; // 20 minutos - private readonly TTL_MARKS = 60 * 25; // 25 minutos + private readonly TTL_ORDERS = 60; // 60 segundos + private readonly TTL_INVOICE = 60; // 60 segundos + private readonly TTL_ITENS = 60; // 60 segundos + private readonly TTL_LEADTIME = 60; // 60 segundos + private readonly TTL_DELIVERIES = 60; // 60 segundos + private readonly TTL_TRANSFER = 60; // 60 segundos + private readonly TTL_STATUS = 60; // 60 segundos + private readonly TTL_CARRIERS = 60; // 60 segundos + private readonly TTL_MARKS = 60; // 60 segundos + private readonly TTL_COMPLETED_DELIVERIES = 60; // 60 segundos constructor( private readonly ordersRepository: OrdersRepository, @@ -41,11 +44,34 @@ export class OrdersService { */ async findOrders(query: FindOrdersDto) { const key = `orders:query:${this.hashObject(query)}`; + return getOrSetCache( this.redisClient, key, this.TTL_ORDERS, - () => this.ordersRepository.findOrders(query), + async () => { + const orders = await this.ordersRepository.findOrders(query); + + if (query.includeCompletedDeliveries) { + for (const order of orders) { + const deliveryQuery = { + orderNumber: order.invoiceNumber, + limit: 10, + offset: 0 + }; + + try { + const deliveries = await this.ordersRepository.getCompletedDeliveries(deliveryQuery); + order.completedDeliveries = deliveries; + } catch (error) { + // Se houver erro, definir como array vazio + order.completedDeliveries = []; + } + } + } + + return orders; + }, ); } @@ -196,11 +222,46 @@ export class OrdersService { }); } - async getOrderDelivery(orderId: string): Promise { - const key = `orders:delivery:${orderId}`; + async getOrderDelivery(orderId: string, includeCompletedDeliveries: boolean = false): Promise { + const key = `orders:delivery:${orderId}:${includeCompletedDeliveries}`; - return getOrSetCache(this.redisClient, key, this.TTL_DELIVERIES, () => - this.ordersRepository.getOrderDelivery(orderId), + return getOrSetCache( + this.redisClient, + key, + this.TTL_DELIVERIES, + async () => { + const orderDelivery = await this.ordersRepository.getOrderDelivery(orderId); + + if (!orderDelivery) { + return null; + } + + if (includeCompletedDeliveries) { + try { + // Buscar entregas realizadas usando o transactionId do pedido + // Primeiro precisamos obter o NUMTRANSVENDA do pedido + const transactionId = await this.ordersRepository.getOrderTransactionId(orderId); + + if (transactionId) { + const deliveryQuery = { + transactionId: transactionId, + limit: 10, + offset: 0 + }; + + const deliveries = await this.ordersRepository.getCompletedDeliveriesByTransactionId(deliveryQuery); + orderDelivery.completedDeliveries = deliveries; + } else { + orderDelivery.completedDeliveries = []; + } + } catch (error) { + // Se houver erro, definir como array vazio + orderDelivery.completedDeliveries = []; + } + } + + return orderDelivery; + } ); } @@ -326,4 +387,18 @@ export class OrdersService { return await this.ordersRepository.getMarksByName(markName); }); } + + /** + * Buscar entregas realizadas com cache baseado nos filtros + */ + async getCompletedDeliveries(query: DeliveryCompletedQuery): Promise { + const key = `orders:completed-deliveries:${this.hashObject(query)}`; + + return getOrSetCache( + this.redisClient, + key, + this.TTL_COMPLETED_DELIVERIES, + () => this.ordersRepository.getCompletedDeliveries(query), + ); + } } \ No newline at end of file