From 336b45f67147c9a9879633dcfe50ec4c29f52fb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20Guzm=C3=A1n?= <37272605+TextC0de@users.noreply.github.com> Date: Mon, 4 Nov 2024 17:24:55 -0300 Subject: [PATCH] feat: more logs on syncPurchaseOrderPaymentStatus (#308) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hay un caso en el que el purchase order fue pagado pero el estado no se está actualizando correctamente a través del worker de sicronización de estado de compra. Estos logs permitirán debuggear mejor estos casos. --- src/datasources/mercadopago/index.ts | 16 ++++++++++++++-- src/datasources/stripe/index.ts | 10 ++++++++++ src/schema/purchaseOrder/actions.tsx | 7 +++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/datasources/mercadopago/index.ts b/src/datasources/mercadopago/index.ts index c9c0dbc7..46cf80e4 100644 --- a/src/datasources/mercadopago/index.ts +++ b/src/datasources/mercadopago/index.ts @@ -41,7 +41,7 @@ const getPaymentStatusFromMercadoPago = ( throw new Error(`Unknown payment status: ${theStatus}`); }; -const getPurchasOrderStatusFromMercadoPago = ( +const getPurchaseOrderStatusFromMercadoPago = ( mercadoPagoStatus: PaymentResponse["status"], ): (typeof purchaseOrderStatusEnum)[number] => { const theStatus = mercadoPagoStatus?.toLowerCase(); @@ -178,9 +178,11 @@ export const getMercadoPagoPreference = async ({ export const getMercadoPagoPayment = async ({ purchaseOrderId, getMercadoPagoClient, + logger, }: { purchaseOrderId: string; getMercadoPagoClient: MercadoPagoFetch; + logger: Logger; }) => { const result = await getMercadoPagoClient<{ results: PaymentResponse[] }>({ url: `/v1/payments/search?external_reference=${purchaseOrderId}`, @@ -191,8 +193,18 @@ export const getMercadoPagoPayment = async ({ ? result.results[result.results.length - 1] : null; + if (lastPaymentHistory) { + logger.info(`Payment found for PO with ID: ${purchaseOrderId}`, { + status: lastPaymentHistory.status, + }); + } else { + logger.info(`No payment history found for PO with ID: ${purchaseOrderId}`, { + response: result, + }); + } + return { paymentStatus: getPaymentStatusFromMercadoPago(lastPaymentHistory?.status), - status: getPurchasOrderStatusFromMercadoPago(lastPaymentHistory?.status), + status: getPurchaseOrderStatusFromMercadoPago(lastPaymentHistory?.status), }; }; diff --git a/src/datasources/stripe/index.ts b/src/datasources/stripe/index.ts index d86806ca..8eb867a8 100644 --- a/src/datasources/stripe/index.ts +++ b/src/datasources/stripe/index.ts @@ -5,6 +5,7 @@ import { PurchaseOrderStatus, } from "~/datasources/db/schema"; import { someMinutesIntoTheFuture } from "~/datasources/helpers"; +import { Logger } from "~/logging"; const getPaymentStatusFromStripeSession = ( stripeStatus: Stripe.Response["payment_status"], @@ -225,17 +226,26 @@ export const createStripePayment = async ({ export const getStripePaymentStatus = async ({ paymentId, getStripeClient, + logger, }: { paymentId: string; getStripeClient: () => Stripe; + logger: Logger; }) => { const stripeClient = getStripeClient(); const payment = await stripeClient.checkout.sessions.retrieve(paymentId); if (!payment) { + logger.error(`Payment not found for id: ${paymentId}`); + throw new Error(`Payment not found for id: ${paymentId}`); } + logger.info(`Payment found for id: ${paymentId}`, { + payment_status: payment.payment_status, + status: payment.status, + }); + return { paymentStatus: getPaymentStatusFromStripeSession(payment.payment_status), status: getPurchaseOrderStatusFromStripeSession(payment.status), diff --git a/src/schema/purchaseOrder/actions.tsx b/src/schema/purchaseOrder/actions.tsx index 083826b8..5e518cbd 100644 --- a/src/schema/purchaseOrder/actions.tsx +++ b/src/schema/purchaseOrder/actions.tsx @@ -1020,6 +1020,7 @@ export const syncPurchaseOrderPaymentStatus = async ({ const stripeStatus = await getStripePaymentStatus({ paymentId: paymentPlatformReferenceID, getStripeClient: GET_STRIPE_CLIENT, + logger, }); poPaymentStatus = stripeStatus.paymentStatus; @@ -1031,6 +1032,7 @@ export const syncPurchaseOrderPaymentStatus = async ({ const mercadoPagoStatus = await getMercadoPagoPayment({ purchaseOrderId: purchaseOrder.id, getMercadoPagoClient: GET_MERCADOPAGO_CLIENT, + logger, }); poPaymentStatus = mercadoPagoStatus.paymentStatus; @@ -1038,6 +1040,11 @@ export const syncPurchaseOrderPaymentStatus = async ({ poStatus = mercadoPagoStatus.status ?? poStatus; } + logger.info(`New purchase order ${purchaseOrderId} status`, { + poPaymentStatus, + poStatus, + }); + if ( poPaymentStatus !== purchaseOrder.purchaseOrderPaymentStatus || poStatus !== purchaseOrder.status