fix: slow and control Graphs backfill
All checks were successful
Build and Deploy / build-and-push (push) Successful in 2m18s
All checks were successful
Build and Deploy / build-and-push (push) Successful in 2m18s
This commit is contained in:
@@ -5,6 +5,48 @@ import axios from 'axios';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
type CachedOrderDetails = {
|
||||
expiresAt: number;
|
||||
fullOrderDetails: any;
|
||||
statusProcessamento: string;
|
||||
whatsappVendedor: string;
|
||||
};
|
||||
|
||||
const orderDetailsCache = new Map<string, CachedOrderDetails>();
|
||||
const configuredCacheTtlMs = Number(process.env.TINY_ORDER_DETAILS_CACHE_TTL_MS || 120000);
|
||||
const ORDER_DETAILS_CACHE_TTL_MS = Number.isFinite(configuredCacheTtlMs) ? configuredCacheTtlMs : 120000;
|
||||
|
||||
function pruneExpiredOrderDetailsCache() {
|
||||
const now = Date.now();
|
||||
for (const [orderId, cached] of orderDetailsCache.entries()) {
|
||||
if (now > cached.expiresAt) {
|
||||
orderDetailsCache.delete(orderId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getCachedOrderDetails(orderId: string) {
|
||||
const cached = orderDetailsCache.get(orderId);
|
||||
if (!cached) return null;
|
||||
|
||||
if (Date.now() > cached.expiresAt) {
|
||||
orderDetailsCache.delete(orderId);
|
||||
return null;
|
||||
}
|
||||
|
||||
return cached;
|
||||
}
|
||||
|
||||
function setCachedOrderDetails(orderId: string, cached: Omit<CachedOrderDetails, 'expiresAt'>) {
|
||||
if (ORDER_DETAILS_CACHE_TTL_MS <= 0) return;
|
||||
|
||||
pruneExpiredOrderDetailsCache();
|
||||
orderDetailsCache.set(orderId, {
|
||||
...cached,
|
||||
expiresAt: Date.now() + ORDER_DETAILS_CACHE_TTL_MS
|
||||
});
|
||||
}
|
||||
|
||||
export const handleTinyOrderUpdate = async (req: Request, res: Response): Promise<void> => {
|
||||
try {
|
||||
// 1. Security Check: Verify token from Tiny
|
||||
@@ -42,7 +84,13 @@ export const handleTinyOrderUpdate = async (req: Request, res: Response): Promis
|
||||
let statusProcessamento = "";
|
||||
let whatsappVendedor = "";
|
||||
|
||||
if (tinyApiToken) {
|
||||
const cachedDetails = getCachedOrderDetails(String(orderId));
|
||||
if (cachedDetails) {
|
||||
fullOrderDetails = cachedDetails.fullOrderDetails;
|
||||
statusProcessamento = cachedDetails.statusProcessamento;
|
||||
whatsappVendedor = cachedDetails.whatsappVendedor;
|
||||
console.log(`Using cached order details for Order ID: ${orderId}.`);
|
||||
} else if (tinyApiToken) {
|
||||
try {
|
||||
console.log(`Fetching full details for Order ID: ${orderId} from Tiny API...`);
|
||||
const params = new URLSearchParams();
|
||||
@@ -61,7 +109,7 @@ export const handleTinyOrderUpdate = async (req: Request, res: Response): Promis
|
||||
|
||||
// OPTION B: Fetch Vendor's WhatsApp
|
||||
const idVendedor = fullOrderDetails.id_vendedor;
|
||||
if (idVendedor && idVendedor !== "0") {
|
||||
if (process.env.TINY_FETCH_SELLER_DETAILS === 'true' && idVendedor && idVendedor !== "0") {
|
||||
console.log(`Fetching seller details for Seller ID: ${idVendedor}...`);
|
||||
const vendorParams = new URLSearchParams();
|
||||
vendorParams.append('token', tinyApiToken);
|
||||
@@ -80,6 +128,12 @@ export const handleTinyOrderUpdate = async (req: Request, res: Response): Promis
|
||||
console.error('Failed to fetch seller details:', JSON.stringify(vendorResponse.data?.retorno?.erros || 'Unknown API error'));
|
||||
}
|
||||
}
|
||||
|
||||
setCachedOrderDetails(String(orderId), {
|
||||
fullOrderDetails,
|
||||
statusProcessamento,
|
||||
whatsappVendedor
|
||||
});
|
||||
} else {
|
||||
console.error('Tiny API returned an error:', apiResponse.data?.retorno?.erros || 'Unknown error');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user