Skip to content

Commit

Permalink
feat: more logs on syncPurchaseOrderPaymentStatus (#308)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TextC0de authored Nov 4, 2024
1 parent feebee8 commit 336b45f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/datasources/mercadopago/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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}`,
Expand All @@ -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),
};
};
10 changes: 10 additions & 0 deletions src/datasources/stripe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
PurchaseOrderStatus,
} from "~/datasources/db/schema";
import { someMinutesIntoTheFuture } from "~/datasources/helpers";
import { Logger } from "~/logging";

const getPaymentStatusFromStripeSession = (
stripeStatus: Stripe.Response<Stripe.Checkout.Session>["payment_status"],
Expand Down Expand Up @@ -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),
Expand Down
7 changes: 7 additions & 0 deletions src/schema/purchaseOrder/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,7 @@ export const syncPurchaseOrderPaymentStatus = async ({
const stripeStatus = await getStripePaymentStatus({
paymentId: paymentPlatformReferenceID,
getStripeClient: GET_STRIPE_CLIENT,
logger,
});

poPaymentStatus = stripeStatus.paymentStatus;
Expand All @@ -1031,13 +1032,19 @@ export const syncPurchaseOrderPaymentStatus = async ({
const mercadoPagoStatus = await getMercadoPagoPayment({
purchaseOrderId: purchaseOrder.id,
getMercadoPagoClient: GET_MERCADOPAGO_CLIENT,
logger,
});

poPaymentStatus = mercadoPagoStatus.paymentStatus;

poStatus = mercadoPagoStatus.status ?? poStatus;
}

logger.info(`New purchase order ${purchaseOrderId} status`, {
poPaymentStatus,
poStatus,
});

if (
poPaymentStatus !== purchaseOrder.purchaseOrderPaymentStatus ||
poStatus !== purchaseOrder.status
Expand Down

0 comments on commit 336b45f

Please sign in to comment.