Key Tiny order cache by status to stop forwarding stale situacao
All checks were successful
Build and Deploy / build-and-push (push) Successful in 1m23s
All checks were successful
Build and Deploy / build-and-push (push) Successful in 1m23s
Tiny fires one webhook per status change, often under a minute apart (e.g. "Preparando envio" -> "Pronto para envio"). The 120s order details cache was keyed only by order ID, so the second webhook reused the first fetch and forwarded the previous situacao to n8n, which then skipped the message (notably the pickup "Pronto para envio" notification). Key the cache and in-flight dedupe by order ID + webhook codigoSituacao, so a status change always refetches while same-status duplicates still share one Tiny call. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -152,10 +152,18 @@ async function fetchOrderDetailsFromTiny(orderId: string, tinyApiToken: string):
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getOrFetchOrderDetails(orderId: string, tinyApiToken: string): Promise<OrderDetailsResult | null> {
|
// Cache per order *and* status: Tiny fires one webhook per status change, often seconds
|
||||||
const cachedDetails = getCachedOrderDetails(orderId);
|
// apart (e.g. "Preparando envio" -> "Pronto para envio"), and a cache keyed only by order
|
||||||
|
// would forward the previous status. Same-status duplicates still share one fetch.
|
||||||
|
function orderDetailsCacheKey(orderId: string, webhookStatus: string) {
|
||||||
|
return `${orderId}:${webhookStatus}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getOrFetchOrderDetails(orderId: string, webhookStatus: string, tinyApiToken: string): Promise<OrderDetailsResult | null> {
|
||||||
|
const cacheKey = orderDetailsCacheKey(orderId, webhookStatus);
|
||||||
|
const cachedDetails = getCachedOrderDetails(cacheKey);
|
||||||
if (cachedDetails) {
|
if (cachedDetails) {
|
||||||
console.log(`Using cached order details for Order ID: ${orderId}.`);
|
console.log(`Using cached order details for Order ID: ${orderId} (status ${webhookStatus || 'unknown'}).`);
|
||||||
return {
|
return {
|
||||||
fullOrderDetails: cachedDetails.fullOrderDetails,
|
fullOrderDetails: cachedDetails.fullOrderDetails,
|
||||||
statusProcessamento: cachedDetails.statusProcessamento,
|
statusProcessamento: cachedDetails.statusProcessamento,
|
||||||
@@ -163,24 +171,24 @@ async function getOrFetchOrderDetails(orderId: string, tinyApiToken: string): Pr
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const existingFetch = inFlightOrderDetails.get(orderId);
|
const existingFetch = inFlightOrderDetails.get(cacheKey);
|
||||||
if (existingFetch) {
|
if (existingFetch) {
|
||||||
console.log(`Waiting for in-flight Tiny fetch for Order ID: ${orderId}.`);
|
console.log(`Waiting for in-flight Tiny fetch for Order ID: ${orderId} (status ${webhookStatus || 'unknown'}).`);
|
||||||
return existingFetch;
|
return existingFetch;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fetchPromise = fetchOrderDetailsFromTiny(orderId, tinyApiToken)
|
const fetchPromise = fetchOrderDetailsFromTiny(orderId, tinyApiToken)
|
||||||
.then(result => {
|
.then(result => {
|
||||||
if (result) {
|
if (result) {
|
||||||
setCachedOrderDetails(orderId, result);
|
setCachedOrderDetails(cacheKey, result);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
inFlightOrderDetails.delete(orderId);
|
inFlightOrderDetails.delete(cacheKey);
|
||||||
});
|
});
|
||||||
|
|
||||||
inFlightOrderDetails.set(orderId, fetchPromise);
|
inFlightOrderDetails.set(cacheKey, fetchPromise);
|
||||||
return fetchPromise;
|
return fetchPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,7 +231,8 @@ export const handleTinyOrderUpdate = async (req: Request, res: Response): Promis
|
|||||||
|
|
||||||
if (tinyApiToken) {
|
if (tinyApiToken) {
|
||||||
try {
|
try {
|
||||||
const details = await getOrFetchOrderDetails(String(orderId), tinyApiToken);
|
const webhookStatus = String(payload.dados?.codigoSituacao || payload.dados?.descricaoSituacao || '').trim();
|
||||||
|
const details = await getOrFetchOrderDetails(String(orderId), webhookStatus, tinyApiToken);
|
||||||
if (details) {
|
if (details) {
|
||||||
fullOrderDetails = details.fullOrderDetails;
|
fullOrderDetails = details.fullOrderDetails;
|
||||||
statusProcessamento = details.statusProcessamento;
|
statusProcessamento = details.statusProcessamento;
|
||||||
|
|||||||
Reference in New Issue
Block a user