merge
This commit is contained in:
@@ -18,16 +18,32 @@ export async function getExistingAnalysisResponseElements({
|
||||
return data as AnalysisResponseElement[];
|
||||
}
|
||||
|
||||
export async function createAnalysisResponseElement({
|
||||
export async function upsertAnalysisResponseElement({
|
||||
element,
|
||||
}: {
|
||||
element: Omit<AnalysisResponseElement, 'created_at' | 'updated_at' | 'id'>;
|
||||
}) {
|
||||
await getSupabaseServerAdminClient()
|
||||
const { data } = await getSupabaseServerAdminClient()
|
||||
.schema('medreport')
|
||||
.from('analysis_response_elements')
|
||||
.insert(element)
|
||||
.upsert(
|
||||
element,
|
||||
{
|
||||
onConflict: 'analysis_response_id,analysis_element_original_id',
|
||||
ignoreDuplicates: false
|
||||
}
|
||||
)
|
||||
.select('id')
|
||||
.throwOnError();
|
||||
|
||||
const analysisResponseElementId = data?.[0]?.id;
|
||||
if (!analysisResponseElementId) {
|
||||
throw new Error(
|
||||
`Failed to insert or update analysis response element (response id: ${element.analysis_response_id}, element id: ${element.analysis_element_original_id})`
|
||||
);
|
||||
}
|
||||
|
||||
return { analysisResponseElementId };
|
||||
}
|
||||
|
||||
export async function upsertAnalysisResponse({
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
'use server';
|
||||
|
||||
import { toArray } from '@/lib/utils';
|
||||
|
||||
import { getMailer } from '@kit/mailers';
|
||||
import { enhanceAction } from '@kit/next/actions';
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
import { toArray } from '@kit/shared/utils';
|
||||
|
||||
import { emailSchema } from '~/lib/validations/email.schema';
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ export async function getLatestMessage({
|
||||
);
|
||||
}
|
||||
|
||||
export async function createMedipostActionLog({
|
||||
export async function upsertMedipostActionLog({
|
||||
action,
|
||||
xml,
|
||||
hasAnalysisResults = false,
|
||||
@@ -40,8 +40,7 @@ export async function createMedipostActionLog({
|
||||
action:
|
||||
| 'send_order_to_medipost'
|
||||
| 'sync_analysis_results_from_medipost'
|
||||
| 'send_fake_analysis_results_to_medipost'
|
||||
| 'send_analysis_results_to_medipost';
|
||||
| 'send_fake_analysis_results_to_medipost';
|
||||
xml: string;
|
||||
hasAnalysisResults?: boolean;
|
||||
medusaOrderId?: string | null;
|
||||
@@ -50,19 +49,34 @@ export async function createMedipostActionLog({
|
||||
medipostExternalOrderId?: string | null;
|
||||
medipostPrivateMessageId?: string | null;
|
||||
}) {
|
||||
await getSupabaseServerAdminClient()
|
||||
const { data } = await getSupabaseServerAdminClient()
|
||||
.schema('medreport')
|
||||
.from('medipost_actions')
|
||||
.insert({
|
||||
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,
|
||||
})
|
||||
.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 medipostActionId = data?.[0]?.id;
|
||||
if (!medipostActionId) {
|
||||
throw new Error(
|
||||
`Failed to insert or update medipost action (private message id: ${medipostPrivateMessageId})`
|
||||
);
|
||||
}
|
||||
|
||||
return { medipostActionId };
|
||||
}
|
||||
|
||||
@@ -13,15 +13,16 @@ import type {
|
||||
MedipostOrderResponse,
|
||||
UuringElement,
|
||||
} from '@/packages/shared/src/types/medipost-analysis';
|
||||
import { toArray } from '@/lib/utils';
|
||||
import { toArray } from '@kit/shared/utils';
|
||||
import type { AnalysisOrder } from '~/lib/types/analysis-order';
|
||||
import type { AnalysisResponseElement } from '~/lib/types/analysis-response-element';
|
||||
import { createUserAnalysesApi } from '@/packages/features/user-analyses/src/server/api';
|
||||
|
||||
import { Tables } from '@kit/supabase/database';
|
||||
import { getSupabaseServerAdminClient } from '@/packages/supabase/src/clients/server-admin-client';
|
||||
import { getAnalysisElementsAdmin } from '../analysis-element.service';
|
||||
import { getAnalyses } from '../analyses.service';
|
||||
import { createMedipostActionLog, getLatestMessage } from './medipostMessageBase.service';
|
||||
import { upsertMedipostActionLog, getLatestMessage } from './medipostMessageBase.service';
|
||||
import { validateMedipostResponse } from './medipostValidate.service';
|
||||
import { getAnalysisOrder, updateAnalysisOrderStatus } from '../order.service';
|
||||
import { parseXML } from '../util/xml.service';
|
||||
@@ -29,7 +30,7 @@ import { composeOrderXML, OrderedAnalysisElement } from './medipostXML.service';
|
||||
import { getAccountAdmin } from '../account.service';
|
||||
import { logMedipostDispatch } from '../audit.service';
|
||||
import { MedipostValidationError } from './MedipostValidationError';
|
||||
import { createAnalysisResponseElement, getExistingAnalysisResponseElements, upsertAnalysisResponse } from '../analysis-order.service';
|
||||
import { upsertAnalysisResponseElement, getExistingAnalysisResponseElements, upsertAnalysisResponse } from '../analysis-order.service';
|
||||
|
||||
const BASE_URL = process.env.MEDIPOST_URL!;
|
||||
const USER = process.env.MEDIPOST_USER!;
|
||||
@@ -138,28 +139,25 @@ export async function getAnalysisResponseElementsForGroup({
|
||||
continue;
|
||||
}
|
||||
|
||||
const responseValueIsNumeric = responseValue !== null;
|
||||
const responseValueIsNegative = vastuseVaartus === 'Negatiivne';
|
||||
const responseValueIsWithinNorm = vastuseVaartus === 'Normi piires';
|
||||
const mappedResponse = createUserAnalysesApi(getSupabaseServerAdminClient())
|
||||
.mapUuringVastus({ uuringVastus: response });
|
||||
|
||||
results.push({
|
||||
analysis_element_original_id: analysisElementOriginalId,
|
||||
norm_lower: response.NormAlum?.['#text'] ?? null,
|
||||
norm_lower_included:
|
||||
response.NormAlum?.['@_kaasaarvatud'].toLowerCase() === 'jah',
|
||||
norm_status: response.NormiStaatus,
|
||||
norm_upper: response.NormYlem?.['#text'] ?? null,
|
||||
norm_upper_included:
|
||||
response.NormYlem?.['@_kaasaarvatud'].toLowerCase() === 'jah',
|
||||
response_time: response.VastuseAeg ?? null,
|
||||
response_value: responseValue,
|
||||
norm_lower: mappedResponse.normLower,
|
||||
norm_lower_included: mappedResponse.normLowerIncluded,
|
||||
norm_status: mappedResponse.normStatus,
|
||||
norm_upper: mappedResponse.normUpper,
|
||||
norm_upper_included: mappedResponse.normUpperIncluded,
|
||||
response_time: mappedResponse.responseTime,
|
||||
response_value: mappedResponse.responseValue,
|
||||
unit: groupUuringElement.Mootyhik ?? null,
|
||||
original_response_element: groupUuringElement,
|
||||
analysis_name: groupUuringElement.UuringNimi || groupUuringElement.KNimetus,
|
||||
comment: groupUuringElement.UuringuKommentaar ?? null,
|
||||
status: status.toString(),
|
||||
response_value_is_within_norm: responseValueIsNumeric ? null : responseValueIsWithinNorm,
|
||||
response_value_is_negative: responseValueIsNumeric ? null : responseValueIsNegative,
|
||||
response_value_is_within_norm: mappedResponse.responseValueIsWithinNorm,
|
||||
response_value_is_negative: mappedResponse.responseValueIsNegative,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -198,7 +196,7 @@ async function hasAllAnalysisResponseElements({
|
||||
}) {
|
||||
const allOrderResponseElements = await getExistingAnalysisResponseElements({ analysisResponseId });
|
||||
const expectedOrderResponseElements = order.analysis_element_ids?.length ?? 0;
|
||||
return allOrderResponseElements.length === expectedOrderResponseElements;
|
||||
return allOrderResponseElements.length >= expectedOrderResponseElements;
|
||||
}
|
||||
|
||||
export async function syncPrivateMessage({
|
||||
@@ -242,7 +240,7 @@ export async function syncPrivateMessage({
|
||||
|
||||
for (const element of newElements) {
|
||||
try {
|
||||
await createAnalysisResponseElement({
|
||||
await upsertAnalysisResponseElement({
|
||||
element: {
|
||||
...element,
|
||||
analysis_response_id: analysisResponseId,
|
||||
@@ -305,7 +303,7 @@ export async function readPrivateMessageResponse({
|
||||
const hasInvalidOrderId = isNaN(analysisOrderId);
|
||||
|
||||
if (hasInvalidOrderId || !messageResponse || !patientPersonalCode) {
|
||||
await createMedipostActionLog({
|
||||
await upsertMedipostActionLog({
|
||||
action: 'sync_analysis_results_from_medipost',
|
||||
xml: privateMessageXml,
|
||||
hasAnalysisResults: false,
|
||||
@@ -342,7 +340,7 @@ export async function readPrivateMessageResponse({
|
||||
|
||||
const status = await syncPrivateMessage({ messageResponse, order: analysisOrder });
|
||||
|
||||
await createMedipostActionLog({
|
||||
await upsertMedipostActionLog({
|
||||
action: 'sync_analysis_results_from_medipost',
|
||||
xml: privateMessageXml,
|
||||
hasAnalysisResults: true,
|
||||
@@ -475,7 +473,7 @@ export async function sendOrderToMedipost({
|
||||
isMedipostError,
|
||||
errorMessage: e.response,
|
||||
});
|
||||
await createMedipostActionLog({
|
||||
await upsertMedipostActionLog({
|
||||
action: 'send_order_to_medipost',
|
||||
xml: orderXml,
|
||||
hasAnalysisResults: false,
|
||||
@@ -489,7 +487,7 @@ export async function sendOrderToMedipost({
|
||||
isSuccess: false,
|
||||
isMedipostError,
|
||||
});
|
||||
await createMedipostActionLog({
|
||||
await upsertMedipostActionLog({
|
||||
action: 'send_order_to_medipost',
|
||||
xml: orderXml,
|
||||
hasAnalysisResults: false,
|
||||
@@ -505,7 +503,7 @@ export async function sendOrderToMedipost({
|
||||
isSuccess: true,
|
||||
isMedipostError: false,
|
||||
});
|
||||
await createMedipostActionLog({
|
||||
await upsertMedipostActionLog({
|
||||
action: 'send_order_to_medipost',
|
||||
xml: orderXml,
|
||||
hasAnalysisResults: false,
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
import {
|
||||
MaterjalideGrupp,
|
||||
} from '@/lib/types/medipost';
|
||||
import { toArray } from '@/lib/utils';
|
||||
import { toArray } from '@kit/shared/utils';
|
||||
import { uniqBy } from 'lodash';
|
||||
|
||||
import { Tables } from '@kit/supabase/database';
|
||||
|
||||
@@ -9,11 +9,6 @@ export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
|
||||
export function toArray<T>(input?: T | T[] | null): T[] {
|
||||
if (!input) return [];
|
||||
return Array.isArray(input) ? input : [input];
|
||||
}
|
||||
|
||||
export function toTitleCase(str?: string) {
|
||||
return (
|
||||
str
|
||||
|
||||
Reference in New Issue
Block a user