From 5f0ca6bdaeb17c84714f9da6ea684e3ca62c097d Mon Sep 17 00:00:00 2001 From: Danel Kungla Date: Sat, 20 Sep 2025 18:12:34 +0300 Subject: [PATCH 1/3] add ai_tables --- supabase/migrations/20250920154900_ai_tables.sql | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 supabase/migrations/20250920154900_ai_tables.sql diff --git a/supabase/migrations/20250920154900_ai_tables.sql b/supabase/migrations/20250920154900_ai_tables.sql new file mode 100644 index 0000000..d3afa99 --- /dev/null +++ b/supabase/migrations/20250920154900_ai_tables.sql @@ -0,0 +1,10 @@ +create table if not exists medreport.ai_responses ( + id uuid unique not null default extensions.uuid_generate_v4 (), + account_id uuid references medreport.accounts(id) on delete cascade not null, + prompt_name varchar(50) not null, + prompt_id varchar(50) not null, + input jsonb not null, + response jsonb not null, + created_at timestamp with time zone not null default now(), + latest_data_change timestamp with time zone not null +); \ No newline at end of file From 428a1dfb87b972386c464bd3956eda197d5b5d48 Mon Sep 17 00:00:00 2001 From: Karli Date: Mon, 22 Sep 2025 15:31:06 +0300 Subject: [PATCH 2/3] 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); } } From f47a9e75e292b52e556bd3c8a5aa2b6a1ac718c8 Mon Sep 17 00:00:00 2001 From: Karli Date: Mon, 22 Sep 2025 15:54:59 +0300 Subject: [PATCH 3/3] update types matching in test responses --- .../analysis-results/test/test-responses.ts | 104 +++++++++++++----- lib/services/medusaCart.service.ts | 2 - 2 files changed, 75 insertions(+), 31 deletions(-) diff --git a/app/home/(user)/(dashboard)/analysis-results/test/test-responses.ts b/app/home/(user)/(dashboard)/analysis-results/test/test-responses.ts index 89c58b2..b232eca 100644 --- a/app/home/(user)/(dashboard)/analysis-results/test/test-responses.ts +++ b/app/home/(user)/(dashboard)/analysis-results/test/test-responses.ts @@ -26,7 +26,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "1744-2" } }, @@ -46,7 +46,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "1920-8" } }, @@ -66,7 +66,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "1988-5" } }, @@ -86,7 +86,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "57747-8" } }, @@ -106,7 +106,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "2276-4" } }, @@ -126,7 +126,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "14771-0" } }, @@ -146,7 +146,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": false, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "59156-0" } }, @@ -166,7 +166,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": true, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "59156-0" } }, @@ -186,7 +186,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "13955-0" } }, @@ -206,7 +206,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "14646-4" } }, @@ -226,7 +226,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "2000-8" } }, @@ -246,7 +246,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "59158-6" } }, @@ -266,7 +266,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "14647-2" } }, @@ -286,7 +286,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "14682-9" } }, @@ -306,7 +306,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "22748-8" } }, @@ -326,7 +326,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "58805-3" } }, @@ -346,7 +346,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "2601-3" } }, @@ -367,7 +367,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "70204-3" } }, @@ -387,7 +387,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "14798-3" } }, @@ -408,7 +408,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "14927-8" } }, @@ -428,7 +428,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "3016-3" } }, @@ -448,7 +448,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "22664-7" } }, @@ -468,7 +468,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "50561-0" } }, @@ -489,7 +489,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "60493-4" } }, @@ -509,7 +509,7 @@ const big1: AnalysisTestResponse = { "responseValueIsWithinNorm": true, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "60025-4" }, } @@ -535,7 +535,7 @@ const big2: AnalysisTestResponse = { "responseValueIsWithinNorm": null, "normLowerIncluded": false, "normUpperIncluded": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "1988-5" } }, @@ -555,6 +555,8 @@ const big2: AnalysisTestResponse = { "responseValue": 150, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "718-7" }, { @@ -567,6 +569,8 @@ const big2: AnalysisTestResponse = { "responseValue": 45, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "4544-3" }, { @@ -579,6 +583,8 @@ const big2: AnalysisTestResponse = { "responseValue": 5, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "6690-2" }, { @@ -591,6 +597,8 @@ const big2: AnalysisTestResponse = { "responseValue": 5, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "789-8" }, { @@ -603,6 +611,8 @@ const big2: AnalysisTestResponse = { "responseValue": 85, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "787-2" }, { @@ -615,6 +625,8 @@ const big2: AnalysisTestResponse = { "responseValue": 30, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "785-6" }, { @@ -627,6 +639,8 @@ const big2: AnalysisTestResponse = { "responseValue": 355, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "786-4" }, { @@ -639,6 +653,8 @@ const big2: AnalysisTestResponse = { "responseValue": 15, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "788-0" }, { @@ -651,6 +667,8 @@ const big2: AnalysisTestResponse = { "responseValue": 255, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "777-3" }, { @@ -663,6 +681,8 @@ const big2: AnalysisTestResponse = { "responseValue": 0.2, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "51637-7" }, { @@ -675,6 +695,8 @@ const big2: AnalysisTestResponse = { "responseValue": 10, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "32623-1" }, { @@ -687,6 +709,8 @@ const big2: AnalysisTestResponse = { "responseValue": 15, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "32207-3" }, { @@ -699,6 +723,8 @@ const big2: AnalysisTestResponse = { "responseValue": 0.05, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "704-7" }, { @@ -711,6 +737,8 @@ const big2: AnalysisTestResponse = { "responseValue": 0.05, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "711-2" }, { @@ -723,6 +751,8 @@ const big2: AnalysisTestResponse = { "responseValue": 5, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "751-8" }, { @@ -735,6 +765,8 @@ const big2: AnalysisTestResponse = { "responseValue": 0.5, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "742-7" }, { @@ -747,6 +779,8 @@ const big2: AnalysisTestResponse = { "responseValue": 1.5, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "731-0" }, { @@ -759,6 +793,8 @@ const big2: AnalysisTestResponse = { "responseValue": 0, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "51584-1" }, { @@ -771,6 +807,8 @@ const big2: AnalysisTestResponse = { "responseValue": 0, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "38518-7" }, { @@ -779,8 +817,12 @@ const big2: AnalysisTestResponse = { "normStatus": 0, "responseTime": "2025-09-12 14:02:04", "responseValue": 0, + "normUpper": null, + "normLower": null, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "771-6" }, { @@ -789,8 +831,12 @@ const big2: AnalysisTestResponse = { "normStatus": 0, "responseTime": "2025-09-12 14:02:04", "responseValue": 0, + "normUpper": null, + "normLower": null, "normLowerIncluded": false, "normUpperIncluded": false, + "responseValueIsNegative": null, + "responseValueIsWithinNorm": null, "analysisElementOriginalId": "58413-6" } ], @@ -804,7 +850,7 @@ const big2: AnalysisTestResponse = { "normUpperIncluded": false, "responseValueIsNegative": false, "responseValueIsWithinNorm": false, - "status": "4", + "status": 4, "analysisElementOriginalId": "57021-8" } }, @@ -825,7 +871,7 @@ const big2: AnalysisTestResponse = { "normUpperIncluded": false, "responseValueIsNegative": false, "responseValueIsWithinNorm": false, - "status": "5", + "status": 5, "analysisElementOriginalId": "43583-4" } }, @@ -846,7 +892,7 @@ const big2: AnalysisTestResponse = { "normUpperIncluded": false, "responseValueIsNegative": null, "responseValueIsWithinNorm": null, - "status": "4", + "status": 4, "analysisElementOriginalId": "60493-4" } } diff --git a/lib/services/medusaCart.service.ts b/lib/services/medusaCart.service.ts index c33ed0d..8d256b6 100644 --- a/lib/services/medusaCart.service.ts +++ b/lib/services/medusaCart.service.ts @@ -9,8 +9,6 @@ import { z } from 'zod'; import { getSupabaseServerClient } from '@kit/supabase/server-client'; -import { requireUserInServerComponent } from '../server/require-user-in-server-component'; - const env = () => z .object({