From 428a1dfb87b972386c464bd3956eda197d5b5d48 Mon Sep 17 00:00:00 2001 From: Karli Date: Mon, 22 Sep 2025 15:31:06 +0300 Subject: [PATCH] don't upsert if audit log doesn't have medipost_private_message_id, improve logging --- .../cart/montonio-callback/actions.ts | 3 +- .../medipost/medipostMessageBase.service.ts | 45 ++++++++++--------- .../medipostPrivateMessage.service.ts | 5 +++ .../src/lib/data/customer.ts | 2 +- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/app/home/(user)/(dashboard)/cart/montonio-callback/actions.ts b/app/home/(user)/(dashboard)/cart/montonio-callback/actions.ts index 7e7b22e..6ed2b99 100644 --- a/app/home/(user)/(dashboard)/cart/montonio-callback/actions.ts +++ b/app/home/(user)/(dashboard)/cart/montonio-callback/actions.ts @@ -163,8 +163,9 @@ async function sendAnalysisPackageOrderEmail({ partnerLocationName, language, }); + console.info(`Successfully sent analysis package order email to ${email}`); } catch (error) { - console.error("Failed to send email", error); + console.error(`Failed to send analysis package order email to ${email}`, error); } } diff --git a/lib/services/medipost/medipostMessageBase.service.ts b/lib/services/medipost/medipostMessageBase.service.ts index d3194e8..fe29d8c 100644 --- a/lib/services/medipost/medipostMessageBase.service.ts +++ b/lib/services/medipost/medipostMessageBase.service.ts @@ -49,27 +49,30 @@ export async function upsertMedipostActionLog({ medipostExternalOrderId?: string | null; medipostPrivateMessageId?: string | null; }) { - const { data } = await getSupabaseServerAdminClient() - .schema('medreport') - .from('medipost_actions') - .upsert( - { - action, - xml, - has_analysis_results: hasAnalysisResults, - medusa_order_id: medusaOrderId, - response_xml: responseXml, - has_error: hasError, - medipost_external_order_id: medipostExternalOrderId, - medipost_private_message_id: medipostPrivateMessageId, - }, - { - onConflict: 'medipost_private_message_id', - ignoreDuplicates: false - } - ) - .select('id') - .throwOnError(); + const recordData = { + action, + xml, + has_analysis_results: hasAnalysisResults, + medusa_order_id: medusaOrderId, + response_xml: responseXml, + has_error: hasError, + medipost_external_order_id: medipostExternalOrderId, + medipost_private_message_id: medipostPrivateMessageId, + }; + + const query = getSupabaseServerAdminClient().schema('medreport').from('medipost_actions'); + const { data } = medipostPrivateMessageId + ? await query + .upsert(recordData, { + onConflict: 'medipost_private_message_id', + ignoreDuplicates: false + }) + .select('id') + .throwOnError() + : await query + .insert(recordData) + .select('id') + .throwOnError(); const medipostActionId = data?.[0]?.id; if (!medipostActionId) { diff --git a/lib/services/medipost/medipostPrivateMessage.service.ts b/lib/services/medipost/medipostPrivateMessage.service.ts index ef3e9ac..60a4602 100644 --- a/lib/services/medipost/medipostPrivateMessage.service.ts +++ b/lib/services/medipost/medipostPrivateMessage.service.ts @@ -303,6 +303,7 @@ export async function readPrivateMessageResponse({ const hasInvalidOrderId = isNaN(analysisOrderId); if (hasInvalidOrderId || !messageResponse || !patientPersonalCode) { + console.error(`Invalid order id or message response or patient personal code, medipostExternalOrderId=${medipostExternalOrderId}, privateMessageId=${privateMessageId}`); await upsertMedipostActionLog({ action: 'sync_analysis_results_from_medipost', xml: privateMessageXml, @@ -340,6 +341,7 @@ export async function readPrivateMessageResponse({ const status = await syncPrivateMessage({ messageResponse, order: analysisOrder }); + console.info(`Successfully synced analysis results from Medipost message privateMessageId=${privateMessageId}`); await upsertMedipostActionLog({ action: 'sync_analysis_results_from_medipost', xml: privateMessageXml, @@ -473,6 +475,7 @@ export async function sendOrderToMedipost({ isMedipostError, errorMessage: e.response, }); + console.error(`Failed to send order to Medipost, medusaOrderId=${medusaOrderId}, error=${e.response}`); await upsertMedipostActionLog({ action: 'send_order_to_medipost', xml: orderXml, @@ -482,6 +485,7 @@ export async function sendOrderToMedipost({ hasError: true, }); } else { + console.error(`Failed to send order to Medipost, medusaOrderId=${medusaOrderId}, error=${e}`); await logMedipostDispatch({ medusaOrderId, isSuccess: false, @@ -498,6 +502,7 @@ export async function sendOrderToMedipost({ throw e; } + console.info(`Successfully sent order to Medipost, medusaOrderId=${medusaOrderId}`); await logMedipostDispatch({ medusaOrderId, isSuccess: true, diff --git a/packages/features/medusa-storefront/src/lib/data/customer.ts b/packages/features/medusa-storefront/src/lib/data/customer.ts index 3c05921..dfa6917 100644 --- a/packages/features/medusa-storefront/src/lib/data/customer.ts +++ b/packages/features/medusa-storefront/src/lib/data/customer.ts @@ -330,7 +330,7 @@ export async function medusaLoginOrRegister(credentials: { await medusaRegister({ email, password, name, lastName }); return await medusaLogin(email, password); } catch (registerError) { - console.error("Failed to create Medusa account for user with email=${email}", registerError); + console.error(`Failed to create Medusa account for user with email=${email}`, registerError); throw medusaError(registerError); } }