From cee37178dfa73a0814be6eb0b50d85e53c95c1ff Mon Sep 17 00:00:00 2001 From: Karli Date: Wed, 17 Sep 2025 11:15:44 +0300 Subject: [PATCH] feat(MED-161): create analysis-order service --- lib/services/analysis-order.service.ts | 17 +++++++++++++++++ .../medipost/medipostPrivateMessage.service.ts | 10 +++------- lib/types/analysis-response-element.ts | 3 +++ 3 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 lib/services/analysis-order.service.ts create mode 100644 lib/types/analysis-response-element.ts diff --git a/lib/services/analysis-order.service.ts b/lib/services/analysis-order.service.ts new file mode 100644 index 0000000..e76ca16 --- /dev/null +++ b/lib/services/analysis-order.service.ts @@ -0,0 +1,17 @@ +import { getSupabaseServerAdminClient } from "@/packages/supabase/src/clients/server-admin-client"; +import type { AnalysisResponseElement } from "../types/analysis-response-element"; + +export async function getExistingAnalysisResponseElements({ + analysisResponseId, +}: { + analysisResponseId: number; +}) { + const { data } = await getSupabaseServerAdminClient() + .schema('medreport') + .from('analysis_response_elements') + .select('*') + .eq('analysis_response_id', analysisResponseId) + .throwOnError(); + + return data as AnalysisResponseElement[]; +} diff --git a/lib/services/medipost/medipostPrivateMessage.service.ts b/lib/services/medipost/medipostPrivateMessage.service.ts index ab4d406..b6216b8 100644 --- a/lib/services/medipost/medipostPrivateMessage.service.ts +++ b/lib/services/medipost/medipostPrivateMessage.service.ts @@ -22,6 +22,7 @@ import { composeOrderXML, OrderedAnalysisElement } from './medipostXML.service'; import { getAccountAdmin } from '../account.service'; import { logMedipostDispatch } from '../audit.service'; import { MedipostValidationError } from './MedipostValidationError'; +import { getExistingAnalysisResponseElements } from '../analysis-order.service'; const BASE_URL = process.env.MEDIPOST_URL!; const USER = process.env.MEDIPOST_USER!; @@ -152,14 +153,9 @@ export async function syncPrivateMessage({ ); } - const { data: allOrderResponseElements } = await supabase - .schema('medreport') - .from('analysis_response_elements') - .select('*') - .eq('analysis_response_id', analysisResponseId) - .throwOnError(); + const existingAnalysisResponseElements = await getExistingAnalysisResponseElements({ analysisResponseId }); const expectedOrderResponseElements = order.analysis_element_ids?.length ?? 0; - if (allOrderResponseElements.length !== expectedOrderResponseElements) { + if (existingAnalysisResponseElements.length !== expectedOrderResponseElements) { return { isPartial: true }; } diff --git a/lib/types/analysis-response-element.ts b/lib/types/analysis-response-element.ts new file mode 100644 index 0000000..db5b20f --- /dev/null +++ b/lib/types/analysis-response-element.ts @@ -0,0 +1,3 @@ +import type { Database } from "@/packages/supabase/src/database.types"; + +export type AnalysisResponseElement = Database['medreport']['Tables']['analysis_response_elements']['Row'];