merge
This commit is contained in:
@@ -9,11 +9,7 @@ import { AnalysisElement, createAnalysisElement, getAnalysisElements } from '~/l
|
||||
import { createCodes } from '~/lib/services/codes.service';
|
||||
import { getLatestPublicMessageListItem } from '~/lib/services/medipost/medipostPublicMessage.service';
|
||||
import type { ICode } from '~/lib/types/code';
|
||||
|
||||
function toArray<T>(input?: T | T[] | null): T[] {
|
||||
if (!input) return [];
|
||||
return Array.isArray(input) ? input : [input];
|
||||
}
|
||||
import { toArray } from '@kit/shared/utils';
|
||||
|
||||
const WRITE_XML_TO_FILE = false as boolean;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import { getAnalysisOrder } from "~/lib/services/order.service";
|
||||
import { composeOrderTestResponseXML, sendPrivateMessageTestResponse } from "~/lib/services/medipost/medipostTest.service";
|
||||
import { retrieveOrder } from "@lib/data";
|
||||
import { getAccountAdmin } from "~/lib/services/account.service";
|
||||
import { createMedipostActionLog } from "~/lib/services/medipost/medipostMessageBase.service";
|
||||
import { upsertMedipostActionLog } from "~/lib/services/medipost/medipostMessageBase.service";
|
||||
import { getOrderedAnalysisIds } from "~/lib/services/medusaOrder.service";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
@@ -35,7 +35,7 @@ export async function POST(request: Request) {
|
||||
});
|
||||
|
||||
try {
|
||||
await createMedipostActionLog({
|
||||
await upsertMedipostActionLog({
|
||||
action: 'send_fake_analysis_results_to_medipost',
|
||||
xml: messageXml,
|
||||
medusaOrderId,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import React from 'react';
|
||||
import Link from 'next/link';
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
@@ -53,47 +54,44 @@ export default async function AnalysisResultsPage({
|
||||
}
|
||||
|
||||
const orderedAnalysisElements = analysisResponse.orderedAnalysisElements;
|
||||
const hasOrderedAnalysisElements = orderedAnalysisElements.length > 0;
|
||||
const isPartialStatus = analysisResponse.order.status === 'PARTIAL_ANALYSIS_RESPONSE';
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageHeader />
|
||||
<PageBody className="gap-4">
|
||||
<div className="mt-8 flex flex-col justify-between gap-4 sm:flex-row sm:items-center sm:gap-0">
|
||||
<div>
|
||||
<h4>
|
||||
<Trans i18nKey="analysis-results:pageTitle" />
|
||||
</h4>
|
||||
<p className="text-muted-foreground text-sm">
|
||||
{analysisResponse?.elements &&
|
||||
analysisResponse.elements?.length > 0 ? (
|
||||
<Trans i18nKey="analysis-results:description" />
|
||||
) : (
|
||||
<Trans i18nKey="analysis-results:descriptionEmpty" />
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
<PageHeader
|
||||
title={<Trans i18nKey="analysis-results:pageTitle" />}
|
||||
description={hasOrderedAnalysisElements ? (
|
||||
isPartialStatus
|
||||
? <Trans i18nKey="analysis-results:descriptionPartial" />
|
||||
: <Trans i18nKey="analysis-results:description" />
|
||||
) : (
|
||||
<Trans i18nKey="analysis-results:descriptionEmpty" />
|
||||
)}
|
||||
>
|
||||
<div>
|
||||
<Button asChild>
|
||||
<Link href={pathsConfig.app.orderAnalysisPackage}>
|
||||
<Trans i18nKey="analysis-results:orderNewAnalysis" />
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
</PageHeader>
|
||||
<PageBody className="gap-4 pt-4">
|
||||
<div className="flex flex-col gap-4">
|
||||
<h4>
|
||||
<h5 className="break-all">
|
||||
<Trans
|
||||
i18nKey="analysis-results:orderTitle"
|
||||
values={{ orderNumber: analysisResponse.order.medusaOrderId }}
|
||||
/>
|
||||
</h4>
|
||||
<h5>
|
||||
<Trans
|
||||
i18nKey={`orders:status.${analysisResponse.order.status}`}
|
||||
/>
|
||||
</h5>
|
||||
<h6>
|
||||
<Trans i18nKey={`orders:status.${analysisResponse.order.status}`} />
|
||||
<ButtonTooltip
|
||||
content={`${analysisResponse.order.createdAt ? new Date(analysisResponse?.order?.createdAt).toLocaleString() : ''}`}
|
||||
className="ml-6"
|
||||
/>
|
||||
</h5>
|
||||
</h6>
|
||||
</div>
|
||||
{analysisResponse?.summary?.value && (
|
||||
<div>
|
||||
@@ -106,7 +104,16 @@ export default async function AnalysisResultsPage({
|
||||
<div className="flex flex-col gap-2">
|
||||
{orderedAnalysisElements ? (
|
||||
orderedAnalysisElements.map((element, index) => (
|
||||
<Analysis key={index} element={element} />
|
||||
<React.Fragment key={element.analysisIdOriginal}>
|
||||
<Analysis element={element} />
|
||||
{element.results?.nestedElements?.map((nestedElement, nestedIndex) => (
|
||||
<Analysis
|
||||
key={`nested-${nestedElement.analysisElementOriginalId}-${nestedIndex}`}
|
||||
nestedElement={nestedElement}
|
||||
isNestedElement
|
||||
/>
|
||||
))}
|
||||
</React.Fragment>
|
||||
))
|
||||
) : (
|
||||
<div className="text-muted-foreground text-sm">
|
||||
|
||||
@@ -3,13 +3,10 @@ import { useMemo } from 'react';
|
||||
import { ArrowDown } from 'lucide-react';
|
||||
|
||||
import { cn } from '@kit/ui/utils';
|
||||
import { AnalysisResultDetailsElementResults } from '@/packages/features/accounts/src/types/analysis-results';
|
||||
import type { AnalysisResultDetailsElementResults } from '@/packages/features/user-analyses/src/types/analysis-results';
|
||||
import { AnalysisResultLevel } from '@/packages/features/user-analyses/src/types/analysis-results';
|
||||
|
||||
export enum AnalysisResultLevel {
|
||||
NORMAL = 0,
|
||||
WARNING = 1,
|
||||
CRITICAL = 2,
|
||||
}
|
||||
type AnalysisResultLevelBarResults = Pick<AnalysisResultDetailsElementResults, 'normLower' | 'normUpper' | 'responseValue'>;
|
||||
|
||||
const Level = ({
|
||||
isActive = false,
|
||||
@@ -50,7 +47,7 @@ const Level = ({
|
||||
)}
|
||||
|
||||
{color === 'success' && typeof normRangeText === 'string' && (
|
||||
<p className={cn("absolute bottom-[-18px] left-3/8 text-xs text-muted-foreground font-bold", {
|
||||
<p className={cn("absolute bottom-[-18px] left-3/8 text-xs text-muted-foreground font-bold whitespace-nowrap", {
|
||||
'opacity-60': isActive,
|
||||
})}>
|
||||
{normRangeText}
|
||||
@@ -60,28 +57,19 @@ const Level = ({
|
||||
);
|
||||
};
|
||||
|
||||
export const AnalysisLevelBarSkeleton = () => {
|
||||
return (
|
||||
<div className="mt-4 flex h-3 w-[60%] sm:w-[35%] max-w-[360px] gap-1 sm:mt-0">
|
||||
<Level color="gray-200" />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const AnalysisLevelBar = ({
|
||||
level,
|
||||
results,
|
||||
results: {
|
||||
normLower: lower,
|
||||
normUpper: upper,
|
||||
responseValue: value,
|
||||
},
|
||||
normRangeText,
|
||||
}: {
|
||||
level: AnalysisResultLevel;
|
||||
results: AnalysisResultDetailsElementResults;
|
||||
results: AnalysisResultLevelBarResults;
|
||||
normRangeText: string | null;
|
||||
}) => {
|
||||
|
||||
const { normLower: lower, normUpper: upper, responseValue: value, normStatus } = results;
|
||||
const normLowerIncluded = results?.normLowerIncluded || false;
|
||||
const normUpperIncluded = results?.normUpperIncluded || false;
|
||||
|
||||
// Calculate arrow position based on value within normal range
|
||||
const arrowLocation = useMemo(() => {
|
||||
// If no response value, center the arrow
|
||||
@@ -147,8 +135,6 @@ const AnalysisLevelBar = ({
|
||||
|
||||
// Show appropriate levels based on available norm bounds
|
||||
const hasLowerBound = lower !== null;
|
||||
const isLowerBoundZero = hasLowerBound && lower === 0;
|
||||
console.info('isLowerBoundZero', results.analysisElementOriginalId, { isLowerBoundZero, hasLowerBound, lower });
|
||||
const hasUpperBound = upper !== null;
|
||||
|
||||
// Determine which section the value falls into
|
||||
@@ -157,34 +143,10 @@ const AnalysisLevelBar = ({
|
||||
const isValueInNormalRange = !isValueBelowLower && !isValueAboveUpper;
|
||||
|
||||
const [first, second, third] = useMemo(() => {
|
||||
if (!hasLowerBound) {
|
||||
return [
|
||||
{
|
||||
isActive: isNormal,
|
||||
color: "success",
|
||||
isFirst: true,
|
||||
normRangeText,
|
||||
...(isNormal ? { arrowLocation } : {}),
|
||||
},
|
||||
{
|
||||
isActive: isWarning,
|
||||
color: "warning",
|
||||
...(isWarning ? { arrowLocation } : {}),
|
||||
},
|
||||
{
|
||||
isActive: isCritical,
|
||||
color: "destructive",
|
||||
isLast: true,
|
||||
...(isCritical ? { arrowLocation } : {}),
|
||||
},
|
||||
] as const;
|
||||
}
|
||||
|
||||
return [
|
||||
const [warning, normal, critical] = [
|
||||
{
|
||||
isActive: isWarning,
|
||||
color: "warning",
|
||||
isFirst: true,
|
||||
...(isWarning ? { arrowLocation } : {}),
|
||||
},
|
||||
{
|
||||
@@ -200,10 +162,30 @@ const AnalysisLevelBar = ({
|
||||
...(isCritical ? { arrowLocation } : {}),
|
||||
},
|
||||
] as const;
|
||||
|
||||
if (!hasLowerBound) {
|
||||
return [
|
||||
{ ...normal, isFirst: true },
|
||||
warning,
|
||||
critical,
|
||||
] as const;
|
||||
}
|
||||
|
||||
return [
|
||||
{ ...warning, isFirst: true },
|
||||
normal,
|
||||
{ ...critical, isLast: true },
|
||||
] as const;
|
||||
}, [isValueBelowLower, isValueAboveUpper, isValueInNormalRange, arrowLocation, normRangeText, isNormal, isWarning, isCritical]);
|
||||
|
||||
return (
|
||||
<div className="mt-4 flex h-3 w-[60%] sm:w-[35%] max-w-[360px] gap-1 sm:mt-0">
|
||||
<div className={cn(
|
||||
"flex h-3 gap-1",
|
||||
"mt-4 sm:mt-0",
|
||||
"w-[60%] sm:w-[35%]",
|
||||
"min-w-[50vw] sm:min-w-auto",
|
||||
"max-w-[360px]",
|
||||
)}>
|
||||
<Level {...first} />
|
||||
<Level {...second} />
|
||||
<Level {...third} />
|
||||
|
||||
@@ -2,33 +2,59 @@
|
||||
|
||||
import React, { useMemo, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
import { AnalysisResultDetailsElement } from '@/packages/features/accounts/src/types/analysis-results';
|
||||
import { format } from 'date-fns';
|
||||
import { Info } from 'lucide-react';
|
||||
|
||||
import type {
|
||||
AnalysisResultDetailsElement,
|
||||
AnalysisResultsDetailsElementNested,
|
||||
} from '@/packages/features/user-analyses/src/types/analysis-results';
|
||||
import { AnalysisResultLevel } from '@/packages/features/user-analyses/src/types/analysis-results';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
import { cn } from '@kit/ui/utils';
|
||||
|
||||
import AnalysisLevelBar, {
|
||||
AnalysisResultLevel,
|
||||
} from './analysis-level-bar';
|
||||
|
||||
export enum AnalysisStatus {
|
||||
NORMAL = 0,
|
||||
MEDIUM = 1,
|
||||
HIGH = 2,
|
||||
}
|
||||
import AnalysisLevelBar from './analysis-level-bar';
|
||||
|
||||
const Analysis = ({
|
||||
element,
|
||||
element: elementOriginal,
|
||||
nestedElement,
|
||||
isNestedElement = false,
|
||||
}: {
|
||||
element: AnalysisResultDetailsElement;
|
||||
element?: AnalysisResultDetailsElement;
|
||||
nestedElement?: AnalysisResultsDetailsElementNested;
|
||||
isNestedElement?: boolean;
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const element = (() => {
|
||||
if (isNestedElement) {
|
||||
return nestedElement!;
|
||||
}
|
||||
return elementOriginal!;
|
||||
})();
|
||||
const results: AnalysisResultDetailsElement['results'] = useMemo(() => {
|
||||
if (isNestedElement) {
|
||||
const nestedElement = element as AnalysisResultsDetailsElementNested;
|
||||
return {
|
||||
analysisElementOriginalId: nestedElement.analysisElementOriginalId,
|
||||
normLower: nestedElement.normLower,
|
||||
normUpper: nestedElement.normUpper,
|
||||
normStatus: nestedElement.normStatus,
|
||||
responseTime: nestedElement.responseTime,
|
||||
responseValue: nestedElement.responseValue,
|
||||
responseValueIsNegative: nestedElement.responseValueIsNegative,
|
||||
responseValueIsWithinNorm: nestedElement.responseValueIsWithinNorm,
|
||||
normLowerIncluded: nestedElement.normLowerIncluded,
|
||||
normUpperIncluded: nestedElement.normUpperIncluded,
|
||||
unit: nestedElement.unit,
|
||||
status: nestedElement.status,
|
||||
nestedElements: [],
|
||||
};
|
||||
}
|
||||
return (element as AnalysisResultDetailsElement).results;
|
||||
}, [element, isNestedElement]);
|
||||
|
||||
const name = element.analysisName || '';
|
||||
const results = element.results;
|
||||
|
||||
const hasIsWithinNorm = results?.responseValueIsWithinNorm !== null;
|
||||
const hasIsNegative = results?.responseValueIsNegative !== null;
|
||||
@@ -58,21 +84,16 @@ const Analysis = ({
|
||||
return responseValue;
|
||||
})();
|
||||
const unit = results?.unit || '';
|
||||
const normLower = results?.normLower;
|
||||
const normUpper = results?.normUpper;
|
||||
const normLower = results?.normLower ?? null;
|
||||
const normUpper = results?.normUpper ?? null;
|
||||
const normStatus = results?.normStatus ?? null;
|
||||
|
||||
const [showTooltip, setShowTooltip] = useState(false);
|
||||
const analysisResultLevel = useMemo(() => {
|
||||
if (!results) {
|
||||
if (normStatus === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (results.responseValue === null || results.responseValue === undefined) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const normStatus = results.normStatus;
|
||||
|
||||
switch (normStatus) {
|
||||
case 1:
|
||||
return AnalysisResultLevel.WARNING;
|
||||
@@ -82,17 +103,24 @@ const Analysis = ({
|
||||
default:
|
||||
return AnalysisResultLevel.NORMAL;
|
||||
}
|
||||
}, [results]);
|
||||
}, [normStatus]);
|
||||
|
||||
const isCancelled = Number(results?.status) === 5;
|
||||
const hasNestedElements = results?.nestedElements.length > 0;
|
||||
const nestedElements = results?.nestedElements ?? null;
|
||||
const hasNestedElements = Array.isArray(nestedElements) && nestedElements.length > 0;
|
||||
|
||||
const normRangeText = normLower !== null ? `${normLower} - ${normUpper || ''}` : null;
|
||||
const normRangeText = (() => {
|
||||
if (normLower === null && normUpper === null) {
|
||||
return null;
|
||||
}
|
||||
return `${normLower ?? '...'} - ${normUpper ?? '...'}`;
|
||||
})();
|
||||
const hasTextualResponse = hasIsNegative || hasIsWithinNorm;
|
||||
|
||||
return (
|
||||
<div className="border-border rounded-lg border px-5">
|
||||
<div className={cn("border-border rounded-lg border px-5", { 'ml-8': isNestedElement })}>
|
||||
<div className="flex flex-col items-center justify-between gap-2 pt-3 pb-6 sm:py-3 sm:h-[65px] sm:flex-row sm:gap-0">
|
||||
<div className="flex items-center gap-2 font-semibold">
|
||||
<div className={cn("flex items-center gap-2 font-semibold", { 'font-bold': isNestedElement })}>
|
||||
{name}
|
||||
{results?.responseTime && (
|
||||
<div
|
||||
@@ -127,10 +155,18 @@ const Analysis = ({
|
||||
{isCancelled || !results || hasNestedElements ? null : (
|
||||
<>
|
||||
<div className="flex items-center gap-3 sm:ml-auto">
|
||||
<div className="font-semibold">{value}</div>
|
||||
<div
|
||||
className={cn('font-semibold', {
|
||||
'text-yellow-600': hasTextualResponse && analysisResultLevel === AnalysisResultLevel.WARNING,
|
||||
'text-red-600': hasTextualResponse && analysisResultLevel === AnalysisResultLevel.CRITICAL,
|
||||
'text-green-600': hasTextualResponse && analysisResultLevel === AnalysisResultLevel.NORMAL,
|
||||
})}
|
||||
>
|
||||
{value}
|
||||
</div>
|
||||
<div className="text-muted-foreground text-sm">{unit}</div>
|
||||
</div>
|
||||
{!(hasIsNegative || hasIsWithinNorm) && (
|
||||
{!hasTextualResponse && (
|
||||
<>
|
||||
<div className="text-muted-foreground mx-8 flex flex-col-reverse gap-2 text-center text-sm sm:block sm:gap-0">
|
||||
{normRangeText}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { AnalysisResultDetailsMapped } from "@/packages/features/accounts/src/types/analysis-results";
|
||||
import type { AnalysisResultDetailsMapped } from "@/packages/features/user-analyses/src/types/analysis-results";
|
||||
|
||||
type AnalysisTestResponse = Omit<AnalysisResultDetailsMapped, 'order' | 'orderedAnalysisElementIds' | 'summary' | 'elements'>;
|
||||
|
||||
@@ -139,7 +139,7 @@ const big1: AnalysisTestResponse = {
|
||||
"unit": null,
|
||||
"normLower": null,
|
||||
"normUpper": 2,
|
||||
"normStatus": 0,
|
||||
"normStatus": 2,
|
||||
"responseTime": "2024-02-29T10:13:01+00:00",
|
||||
"responseValue": null,
|
||||
"responseValueIsNegative": null,
|
||||
@@ -150,6 +150,26 @@ const big1: AnalysisTestResponse = {
|
||||
"analysisElementOriginalId": "59156-0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"analysisIdOriginal": "59156-0",
|
||||
"isWaitingForResults": false,
|
||||
"analysisName": "Glükoos",
|
||||
"results": {
|
||||
"nestedElements": [],
|
||||
"unit": null,
|
||||
"normLower": null,
|
||||
"normUpper": 2,
|
||||
"normStatus": 0,
|
||||
"responseTime": "2024-02-29T10:13:01+00:00",
|
||||
"responseValue": null,
|
||||
"responseValueIsNegative": null,
|
||||
"responseValueIsWithinNorm": true,
|
||||
"normLowerIncluded": false,
|
||||
"normUpperIncluded": false,
|
||||
"status": "4",
|
||||
"analysisElementOriginalId": "59156-0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"analysisIdOriginal": "13955-0",
|
||||
"isWaitingForResults": false,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { cache } from 'react';
|
||||
|
||||
import { AnalysisResultDetailsMapped } from '@kit/accounts/types/analysis-results';
|
||||
import type { AnalysisResultDetailsMapped } from '@/packages/features/user-analyses/src/types/analysis-results';
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
import { createUserAnalysesApi } from '@/packages/features/user-analyses/src/server/api';
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -4,6 +4,9 @@ const noop = (event: string) => {
|
||||
// do nothing - this is to prevent errors when the analytics service is not initialized
|
||||
|
||||
return async (...args: unknown[]) => {
|
||||
if (typeof window !== 'undefined') {
|
||||
return;
|
||||
}
|
||||
console.debug(
|
||||
`Noop analytics service called with event: ${event}`,
|
||||
...args.filter(Boolean),
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
import { Tables } from '@kit/supabase/database';
|
||||
|
||||
export type AnalysisOrder = Tables<{ schema: 'medreport' }, 'analysis_orders'>;
|
||||
@@ -1,139 +0,0 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
export type UserAnalysisElement =
|
||||
Database['medreport']['Tables']['analysis_response_elements']['Row'];
|
||||
export type UserAnalysisResponse =
|
||||
Database['medreport']['Tables']['analysis_responses']['Row'] & {
|
||||
elements: UserAnalysisElement[];
|
||||
};
|
||||
export type UserAnalysis = UserAnalysisResponse[];
|
||||
|
||||
const ElementSchema = z.object({
|
||||
unit: z.string(),
|
||||
norm_lower: z.number(),
|
||||
norm_upper: z.number(),
|
||||
norm_status: z.number(),
|
||||
analysis_name: z.string(),
|
||||
response_time: z.string(),
|
||||
response_value: z.number(),
|
||||
response_value_is_negative: z.boolean(),
|
||||
norm_lower_included: z.boolean(),
|
||||
norm_upper_included: z.boolean(),
|
||||
status: z.string(),
|
||||
analysis_element_original_id: z.string(),
|
||||
original_response_element: z.object({
|
||||
|
||||
}),
|
||||
});
|
||||
|
||||
const OrderSchema = z.object({
|
||||
status: z.string(),
|
||||
medusa_order_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
});
|
||||
|
||||
const DoctorAnalysisFeedbackSchema = z.object({
|
||||
id: z.number(),
|
||||
status: z.string(),
|
||||
user_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
created_by: z.string(),
|
||||
});
|
||||
|
||||
const SummarySchema = z.object({
|
||||
id: z.number(),
|
||||
value: z.string(),
|
||||
status: z.string(),
|
||||
user_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
created_by: z.string(),
|
||||
updated_at: z.coerce.date().nullable(),
|
||||
updated_by: z.string(),
|
||||
doctor_user_id: z.string().nullable(),
|
||||
analysis_order_id: z.number(),
|
||||
doctor_analysis_feedback: z.array(DoctorAnalysisFeedbackSchema),
|
||||
});
|
||||
|
||||
export const AnalysisResultDetailsSchema = z.object({
|
||||
id: z.number(),
|
||||
analysis_order_id: z.number(),
|
||||
order_number: z.string(),
|
||||
order_status: z.string(),
|
||||
user_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
updated_at: z.coerce.date().nullable(),
|
||||
elements: z.array(ElementSchema),
|
||||
order: OrderSchema,
|
||||
summary: SummarySchema.nullable(),
|
||||
});
|
||||
export type AnalysisResultDetails = z.infer<typeof AnalysisResultDetailsSchema>;
|
||||
|
||||
export type AnalysisResultDetailsElementResults = {
|
||||
unit: string | null;
|
||||
normLower: number | null;
|
||||
normUpper: number | null;
|
||||
normStatus: number | null;
|
||||
responseTime: string | null;
|
||||
responseValue: number | null;
|
||||
responseValueIsNegative: boolean | null;
|
||||
responseValueIsWithinNorm: boolean | null;
|
||||
normLowerIncluded: boolean;
|
||||
normUpperIncluded: boolean;
|
||||
status: string;
|
||||
analysisElementOriginalId: string;
|
||||
nestedElements: {
|
||||
analysisElementOriginalId: string;
|
||||
normLower?: number | null;
|
||||
normLowerIncluded: boolean;
|
||||
normStatus: number;
|
||||
normUpper?: number | null;
|
||||
normUpperIncluded: boolean;
|
||||
responseTime: string;
|
||||
responseValue: number;
|
||||
status: number;
|
||||
unit: string;
|
||||
}[];
|
||||
labComment?: string | null;
|
||||
};
|
||||
|
||||
export type AnalysisResultDetailsElement = {
|
||||
analysisIdOriginal: string;
|
||||
isWaitingForResults: boolean;
|
||||
analysisName: string;
|
||||
results: AnalysisResultDetailsElementResults;
|
||||
};
|
||||
|
||||
export type AnalysisResultDetailsMapped = {
|
||||
id: number;
|
||||
order: {
|
||||
status: string;
|
||||
medusaOrderId: string;
|
||||
createdAt: Date | string;
|
||||
};
|
||||
elements: {
|
||||
id: string;
|
||||
unit: string;
|
||||
norm_lower: number;
|
||||
norm_upper: number;
|
||||
norm_status: number;
|
||||
analysis_name: string;
|
||||
response_time: string;
|
||||
response_value: number;
|
||||
norm_lower_included: boolean;
|
||||
norm_upper_included: boolean;
|
||||
status: string;
|
||||
analysis_element_original_id: string;
|
||||
}[];
|
||||
orderedAnalysisElementIds: number[];
|
||||
orderedAnalysisElements: AnalysisResultDetailsElement[];
|
||||
summary: {
|
||||
id: number;
|
||||
status: string;
|
||||
user_id: string;
|
||||
created_at: Date;
|
||||
created_by: string;
|
||||
value?: string;
|
||||
} | null;
|
||||
};
|
||||
@@ -1,9 +1,10 @@
|
||||
import { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
import type { UuringElement, UuringuVastus } from '@kit/shared/types/medipost-analysis';
|
||||
import { toArray } from '@kit/shared/utils';
|
||||
import type { UuringuVastus } from '@kit/shared/types/medipost-analysis';
|
||||
|
||||
import type { AnalysisResultDetails, AnalysisResultDetailsMapped, UserAnalysis } from '../types/analysis-results';
|
||||
import type { AnalysisResultsQuery, AnalysisResultDetailsElement, AnalysisResultDetailsMapped, AnalysisResultLevel, AnalysisResultsDetailsElementNested, AnalysisStatus, UserAnalysis } from '../types/analysis-results';
|
||||
import type { AnalysisOrder } from '../types/analysis-orders';
|
||||
|
||||
/**
|
||||
@@ -43,31 +44,13 @@ class UserAnalysesApi {
|
||||
async getUserAnalysis(
|
||||
analysisOrderId: number,
|
||||
): Promise<AnalysisResultDetailsMapped | null> {
|
||||
const authUser = await this.client.auth.getUser();
|
||||
const { data, error: userError } = authUser;
|
||||
|
||||
if (userError) {
|
||||
console.error('Failed to get user', userError);
|
||||
throw userError;
|
||||
}
|
||||
|
||||
const { user } = data;
|
||||
|
||||
const analysisOrder = await this.getAnalysisOrder({ analysisOrderId });
|
||||
const orderedAnalysisElementIds = analysisOrder.analysis_element_ids ?? [];
|
||||
if (orderedAnalysisElementIds.length === 0) {
|
||||
console.error('No ordered analysis element ids found for analysis order id=', analysisOrderId);
|
||||
return null;
|
||||
}
|
||||
const { data: orderedAnalysisElements, error: orderedAnalysisElementsError } = await this.client
|
||||
.schema('medreport')
|
||||
.from('analysis_elements')
|
||||
.select('analysis_id_original,analysis_name_lab')
|
||||
.in('id', orderedAnalysisElementIds);
|
||||
if (orderedAnalysisElementsError) {
|
||||
console.error('Failed to get ordered analysis elements for analysis order id=', analysisOrderId, orderedAnalysisElementsError);
|
||||
throw orderedAnalysisElementsError;
|
||||
}
|
||||
const orderedAnalysisElements = await this.getOrderedAnalysisElements({ analysisOrderId, orderedAnalysisElementIds });
|
||||
|
||||
const orderedAnalysisElementOriginalIds = orderedAnalysisElements.map(({ analysis_id_original }) => analysis_id_original);
|
||||
if (orderedAnalysisElementOriginalIds.length === 0) {
|
||||
@@ -75,6 +58,43 @@ class UserAnalysesApi {
|
||||
return null;
|
||||
}
|
||||
|
||||
const responseWithElements = await this.getAnalysisResponseWithElements({ analysisOrderId });
|
||||
if (!responseWithElements) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const mappedOrderedAnalysisElements = await this.getMappedOrderedAnalysisElements({
|
||||
analysisResponseElements: responseWithElements.elements,
|
||||
orderedAnalysisElements,
|
||||
});
|
||||
|
||||
const feedback = responseWithElements.summary?.doctor_analysis_feedback?.[0];
|
||||
return {
|
||||
id: analysisOrderId,
|
||||
order: {
|
||||
status: analysisOrder.status,
|
||||
medusaOrderId: analysisOrder.medusa_order_id,
|
||||
createdAt: analysisOrder.created_at,
|
||||
},
|
||||
orderedAnalysisElements: mappedOrderedAnalysisElements,
|
||||
summary:
|
||||
feedback?.status === 'COMPLETED'
|
||||
? (responseWithElements.summary?.doctor_analysis_feedback?.[0] ?? null)
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
async getAnalysisResponseWithElements({
|
||||
analysisOrderId,
|
||||
}: {
|
||||
analysisOrderId: number;
|
||||
}) {
|
||||
const { data, error: userError } = await this.client.auth.getUser();
|
||||
if (userError) {
|
||||
throw userError;
|
||||
}
|
||||
const { user } = data;
|
||||
|
||||
const { data: analysisResponse } = await this.client
|
||||
.schema('medreport')
|
||||
.from('analysis_responses')
|
||||
@@ -87,29 +107,52 @@ class UserAnalysesApi {
|
||||
.eq('analysis_order_id', analysisOrderId)
|
||||
.throwOnError();
|
||||
|
||||
const responseWithElements = analysisResponse?.[0] as AnalysisResultDetails | null;
|
||||
if (!responseWithElements) {
|
||||
return null;
|
||||
return analysisResponse?.[0] as AnalysisResultsQuery | null;
|
||||
}
|
||||
|
||||
async getOrderedAnalysisElements({
|
||||
analysisOrderId,
|
||||
orderedAnalysisElementIds,
|
||||
}: {
|
||||
analysisOrderId: number;
|
||||
orderedAnalysisElementIds: number[];
|
||||
}) {
|
||||
const { data: orderedAnalysisElements, error: orderedAnalysisElementsError } = await this.client
|
||||
.schema('medreport')
|
||||
.from('analysis_elements')
|
||||
.select('analysis_id_original,analysis_name_lab')
|
||||
.in('id', orderedAnalysisElementIds);
|
||||
if (orderedAnalysisElementsError) {
|
||||
console.error(`Failed to get ordered analysis elements for analysis order id=${analysisOrderId}`, orderedAnalysisElementsError);
|
||||
throw orderedAnalysisElementsError;
|
||||
}
|
||||
return orderedAnalysisElements;
|
||||
}
|
||||
|
||||
const analysisResponseElements = responseWithElements.elements;
|
||||
|
||||
const feedback = responseWithElements.summary?.doctor_analysis_feedback?.[0];
|
||||
|
||||
async getMappedOrderedAnalysisElements({
|
||||
analysisResponseElements,
|
||||
orderedAnalysisElements,
|
||||
}: {
|
||||
analysisResponseElements: AnalysisResultsQuery['elements'];
|
||||
orderedAnalysisElements: { analysis_id_original: string; analysis_name_lab: string }[];
|
||||
}): Promise<AnalysisResultDetailsElement[]> {
|
||||
const mappedOrderedAnalysisElements = orderedAnalysisElements.map(({ analysis_id_original, analysis_name_lab }) => {
|
||||
return this.getOrderedAnalysisElements({
|
||||
return this.getOrderedAnalysisElement({
|
||||
analysisIdOriginal: analysis_id_original,
|
||||
analysisNameLab: analysis_name_lab,
|
||||
analysisResponseElements,
|
||||
});
|
||||
}).sort((a, b) => a.analysisName.localeCompare(b.analysisName));
|
||||
const nestedAnalysisElementIds = mappedOrderedAnalysisElements.map(({ results }) => results?.nestedElements.map(({ analysisElementOriginalId }) => analysisElementOriginalId)).flat().filter(Boolean);
|
||||
|
||||
const nestedAnalysisElementIds = mappedOrderedAnalysisElements
|
||||
.map(({ results }) => results?.nestedElements.map(({ analysisElementOriginalId }) => analysisElementOriginalId))
|
||||
.flat().filter(Boolean);
|
||||
if (nestedAnalysisElementIds.length > 0) {
|
||||
const { data: nestedAnalysisElements, error: nestedAnalysisElementsError } = await this.client
|
||||
.schema('medreport')
|
||||
.from('analysis_elements')
|
||||
.select('*')
|
||||
.in('id', nestedAnalysisElementIds);
|
||||
.in('analysis_id_original', nestedAnalysisElementIds);
|
||||
if (!nestedAnalysisElementsError && nestedAnalysisElements) {
|
||||
for (const mappedOrderedAnalysisElement of mappedOrderedAnalysisElements) {
|
||||
const { results } = mappedOrderedAnalysisElement;
|
||||
@@ -118,58 +161,33 @@ class UserAnalysesApi {
|
||||
}
|
||||
for (const nestedElement of results.nestedElements) {
|
||||
const { analysisElementOriginalId } = nestedElement;
|
||||
const nestedAnalysisElement = nestedAnalysisElements.find(({ id }) => id === analysisElementOriginalId);
|
||||
const nestedAnalysisElement = nestedAnalysisElements.find(({ analysis_id_original }) => analysis_id_original === analysisElementOriginalId);
|
||||
if (!nestedAnalysisElement) {
|
||||
continue;
|
||||
}
|
||||
results.nestedElements.push({
|
||||
...nestedAnalysisElement,
|
||||
analysisElementOriginalId,
|
||||
analysisName: nestedAnalysisElement.analysis_name_lab,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
mappedOrderedAnalysisElements.forEach(({ results }) => {
|
||||
results?.nestedElements.forEach(({ analysisElementOriginalId }) => {
|
||||
const nestedAnalysisElement = nestedAnalysisElements.find(({ id }) => id === analysisElementOriginalId);
|
||||
if (nestedAnalysisElement) {
|
||||
results?.nestedElements.push({
|
||||
...nestedAnalysisElement,
|
||||
analysisElementOriginalId,
|
||||
analysisName: nestedAnalysisElement.analysis_name_lab,
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
nestedElement.analysisElementOriginalId = analysisElementOriginalId;
|
||||
nestedElement.analysisName = nestedAnalysisElement.analysis_name_lab as string | undefined;
|
||||
}
|
||||
results.nestedElements = results.nestedElements.sort((a, b) => a.analysisName?.localeCompare(b.analysisName ?? '') ?? 0);
|
||||
}
|
||||
} else {
|
||||
console.error('Failed to get nested analysis elements by ids=', nestedAnalysisElementIds, nestedAnalysisElementsError);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
id: analysisOrderId,
|
||||
order: {
|
||||
status: analysisOrder.status,
|
||||
medusaOrderId: analysisOrder.medusa_order_id,
|
||||
createdAt: new Date(analysisOrder.created_at),
|
||||
},
|
||||
orderedAnalysisElementIds,
|
||||
orderedAnalysisElements: mappedOrderedAnalysisElements,
|
||||
summary:
|
||||
feedback?.status === 'COMPLETED'
|
||||
? (responseWithElements.summary?.doctor_analysis_feedback?.[0] ?? null)
|
||||
: null,
|
||||
};
|
||||
return mappedOrderedAnalysisElements;
|
||||
}
|
||||
|
||||
getOrderedAnalysisElements({
|
||||
getOrderedAnalysisElement({
|
||||
analysisIdOriginal,
|
||||
analysisNameLab,
|
||||
analysisResponseElements,
|
||||
}: {
|
||||
analysisIdOriginal: string;
|
||||
analysisNameLab: string;
|
||||
analysisResponseElements: AnalysisResultDetails['elements'];
|
||||
}) {
|
||||
analysisResponseElements: AnalysisResultsQuery['elements'];
|
||||
}): AnalysisResultDetailsElement {
|
||||
const elementResponse = analysisResponseElements.find((element) => element.analysis_element_original_id === analysisIdOriginal);
|
||||
if (!elementResponse) {
|
||||
return {
|
||||
@@ -184,51 +202,74 @@ class UserAnalysesApi {
|
||||
isWaitingForResults: false,
|
||||
analysisName: analysisNameLab,
|
||||
results: {
|
||||
nestedElements: (() => {
|
||||
const nestedElements = elementResponse.original_response_element?.UuringuElement as UuringElement[] | undefined;
|
||||
if (!nestedElements) {
|
||||
return [];
|
||||
}
|
||||
return nestedElements.map((element) => {
|
||||
const elementVastus = element.UuringuVastus as UuringuVastus | undefined;
|
||||
const responseValue = elementVastus?.VastuseVaartus;
|
||||
const responseValueIsNumeric = !isNaN(Number(responseValue));
|
||||
const responseValueIsNegative = responseValue === 'Negatiivne';
|
||||
const responseValueIsWithinNorm = responseValue === 'Normi piires';
|
||||
nestedElements: ((): AnalysisResultsDetailsElementNested[] => {
|
||||
const nestedElements = toArray(elementResponse.original_response_element?.UuringuElement)
|
||||
return nestedElements.map<AnalysisResultsDetailsElementNested>((element) => {
|
||||
const mappedResponse = this.mapUuringVastus({
|
||||
uuringVastus: element.UuringuVastus as UuringuVastus | undefined,
|
||||
});
|
||||
return {
|
||||
status: element.UuringOlek,
|
||||
unit: element.Mootyhik,
|
||||
normLower: elementVastus?.NormAlum?.['#text'],
|
||||
normUpper: elementVastus?.NormYlem?.['#text'],
|
||||
normStatus: elementVastus?.NormiStaatus,
|
||||
responseTime: elementVastus?.VastuseAeg,
|
||||
response_value: responseValueIsNegative || !responseValueIsNumeric ? null : (responseValue ?? null),
|
||||
response_value_is_negative: responseValueIsNumeric ? null : responseValueIsNegative,
|
||||
response_value_is_within_norm: responseValueIsNumeric ? null : responseValueIsWithinNorm,
|
||||
normLowerIncluded: elementVastus?.NormAlum?.['@_kaasaarvatud'] === 'JAH',
|
||||
normUpperIncluded: elementVastus?.NormYlem?.['@_kaasaarvatud'] === 'JAH',
|
||||
unit: element.Mootyhik ?? null,
|
||||
normLower: mappedResponse.normLower,
|
||||
normUpper: mappedResponse.normUpper,
|
||||
normStatus: mappedResponse.normStatus,
|
||||
responseTime: mappedResponse.responseTime,
|
||||
responseValue: mappedResponse.responseValue,
|
||||
responseValueIsNegative: mappedResponse.responseValueIsNegative,
|
||||
responseValueIsWithinNorm: mappedResponse.responseValueIsWithinNorm,
|
||||
normLowerIncluded: mappedResponse.normLowerIncluded,
|
||||
normUpperIncluded: mappedResponse.normUpperIncluded,
|
||||
analysisElementOriginalId: element.UuringId,
|
||||
status: Number(elementResponse.status) as AnalysisStatus,
|
||||
analysisName: undefined,
|
||||
};
|
||||
});
|
||||
})(),
|
||||
labComment,
|
||||
//originalResponseElement: elementResponse.original_response_element ?? null,
|
||||
unit: elementResponse.unit,
|
||||
normLower: elementResponse.norm_lower,
|
||||
normUpper: elementResponse.norm_upper,
|
||||
normStatus: elementResponse.norm_status,
|
||||
responseTime: elementResponse.response_time,
|
||||
responseValue: elementResponse.response_value,
|
||||
responseValueIsNegative: elementResponse.response_value_is_negative === true,
|
||||
responseValueIsWithinNorm: elementResponse.response_value_is_within_norm === true,
|
||||
responseValueIsNegative: elementResponse.response_value_is_negative === null ? null : elementResponse.response_value_is_negative === true,
|
||||
responseValueIsWithinNorm: elementResponse.response_value_is_within_norm === null ? null : elementResponse.response_value_is_within_norm === true,
|
||||
normLowerIncluded: elementResponse.norm_lower_included,
|
||||
normUpperIncluded: elementResponse.norm_upper_included,
|
||||
status: elementResponse.status,
|
||||
status: Number(elementResponse.status) as AnalysisStatus,
|
||||
analysisElementOriginalId: elementResponse.analysis_element_original_id,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
mapUuringVastus({ uuringVastus }: { uuringVastus?: UuringuVastus }) {
|
||||
const vastuseVaartus = uuringVastus?.VastuseVaartus;
|
||||
const responseValue = (() => {
|
||||
const valueAsNumber = Number(vastuseVaartus);
|
||||
if (isNaN(valueAsNumber)) {
|
||||
return null;
|
||||
}
|
||||
return valueAsNumber;
|
||||
})();
|
||||
const responseValueNumber = Number(responseValue);
|
||||
const responseValueIsNumeric = !isNaN(responseValueNumber);
|
||||
const responseValueIsNegative = vastuseVaartus === 'Negatiivne';
|
||||
const responseValueIsWithinNorm = vastuseVaartus === 'Normi piires';
|
||||
return {
|
||||
normLower: uuringVastus?.NormAlum?.['#text'] ?? null,
|
||||
normUpper: uuringVastus?.NormYlem?.['#text'] ?? null,
|
||||
normStatus: (uuringVastus?.NormiStaatus ?? null) as AnalysisResultLevel | null,
|
||||
responseTime: uuringVastus?.VastuseAeg ?? null,
|
||||
responseValue: responseValueIsNegative || !responseValueIsNumeric ? null : (responseValueNumber ?? null),
|
||||
responseValueIsNegative: responseValueIsNumeric ? null : responseValueIsNegative,
|
||||
responseValueIsWithinNorm: responseValueIsNumeric ? null : responseValueIsWithinNorm,
|
||||
normLowerIncluded:
|
||||
uuringVastus?.NormAlum?.['@_kaasaarvatud'].toLowerCase() === 'jah',
|
||||
normUpperIncluded:
|
||||
uuringVastus?.NormYlem?.['@_kaasaarvatud'].toLowerCase() === 'jah',
|
||||
};
|
||||
}
|
||||
|
||||
// @TODO unused currently
|
||||
async getUserAnalyses(): Promise<UserAnalysis | null> {
|
||||
const authUser = await this.client.auth.getUser();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import * as z from 'zod';
|
||||
import type { Database } from '@kit/supabase/database';
|
||||
import type { AnalysisOrderStatus, NormStatus } from '@kit/shared/types/medipost-analysis';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
import type { AnalysisOrder } from './analysis-orders';
|
||||
|
||||
export type UserAnalysisElement =
|
||||
Database['medreport']['Tables']['analysis_response_elements']['Row'];
|
||||
@@ -10,92 +11,131 @@ export type UserAnalysisResponse =
|
||||
};
|
||||
export type UserAnalysis = UserAnalysisResponse[];
|
||||
|
||||
const ElementSchema = z.object({
|
||||
unit: z.string(),
|
||||
norm_lower: z.number(),
|
||||
norm_upper: z.number(),
|
||||
norm_status: z.number(),
|
||||
analysis_name: z.string(),
|
||||
response_time: z.string(),
|
||||
response_value: z.number(),
|
||||
response_value_is_negative: z.boolean(),
|
||||
response_value_is_within_norm: z.boolean(),
|
||||
norm_lower_included: z.boolean(),
|
||||
norm_upper_included: z.boolean(),
|
||||
status: z.string(),
|
||||
analysis_element_original_id: z.string(),
|
||||
original_response_element: z.object({
|
||||
export type AnalysisResultsQuery = {
|
||||
id: number,
|
||||
analysis_order_id: number,
|
||||
order_number: string,
|
||||
order_status: string,
|
||||
user_id: string,
|
||||
created_at: string,
|
||||
updated_at: string | null,
|
||||
elements: {
|
||||
unit: string,
|
||||
norm_lower: number,
|
||||
norm_upper: number,
|
||||
norm_status: number,
|
||||
analysis_name: string,
|
||||
response_time: string,
|
||||
response_value: number,
|
||||
response_value_is_negative: boolean,
|
||||
response_value_is_within_norm: boolean,
|
||||
norm_lower_included: boolean,
|
||||
norm_upper_included: boolean,
|
||||
status: string,
|
||||
analysis_element_original_id: string,
|
||||
original_response_element: {
|
||||
UuringuElement: {
|
||||
UuringIdOID: string,
|
||||
UuringId: string,
|
||||
TLyhend: string,
|
||||
KNimetus: string,
|
||||
UuringNimi: string,
|
||||
UuringuKommentaar: string | null,
|
||||
TellijaUuringId: number,
|
||||
TeostajaUuringId: string,
|
||||
UuringOlek: keyof typeof AnalysisOrderStatus,
|
||||
Mootyhik: string | null,
|
||||
Kood: {
|
||||
HkKood: number,
|
||||
HkKoodiKordaja: number,
|
||||
Koefitsient: number,
|
||||
Hind: number,
|
||||
},
|
||||
UuringuVastus: {
|
||||
VastuseVaartus: string,
|
||||
VastuseAeg: string,
|
||||
NormiStaatus: keyof typeof NormStatus,
|
||||
ProoviJarjenumber: number,
|
||||
},
|
||||
UuringuTaitjaAsutuseJnr: number,
|
||||
},
|
||||
UuringuKommentaar: string | null,
|
||||
},
|
||||
}[],
|
||||
order: {
|
||||
status: string,
|
||||
medusa_order_id: string,
|
||||
created_at: string,
|
||||
},
|
||||
summary: {
|
||||
id: number,
|
||||
value: string,
|
||||
status: string,
|
||||
user_id: string,
|
||||
created_at: string,
|
||||
created_by: string,
|
||||
updated_at: string | null,
|
||||
updated_by: string,
|
||||
doctor_user_id: string | null,
|
||||
analysis_order_id: number,
|
||||
doctor_analysis_feedback: {
|
||||
id: number,
|
||||
status: string,
|
||||
user_id: string,
|
||||
created_at: string,
|
||||
created_by: string,
|
||||
}[],
|
||||
} | null,
|
||||
};
|
||||
|
||||
}),
|
||||
});
|
||||
export type AnalysisResultsDetailsElementNested = {
|
||||
analysisElementOriginalId: string;
|
||||
analysisName?: string;
|
||||
} & Pick<
|
||||
AnalysisResultDetailsElementResults,
|
||||
'unit' |
|
||||
'normLower' |
|
||||
'normUpper' |
|
||||
'normStatus' |
|
||||
'responseTime' |
|
||||
'responseValue' |
|
||||
'responseValueIsNegative' |
|
||||
'responseValueIsWithinNorm' |
|
||||
'normLowerIncluded' |
|
||||
'normUpperIncluded' |
|
||||
'status' |
|
||||
'analysisElementOriginalId'
|
||||
>;
|
||||
|
||||
const OrderSchema = z.object({
|
||||
status: z.string(),
|
||||
medusa_order_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
});
|
||||
export enum AnalysisResultLevel {
|
||||
NORMAL = 0,
|
||||
WARNING = 1,
|
||||
CRITICAL = 2,
|
||||
}
|
||||
|
||||
const DoctorAnalysisFeedbackSchema = z.object({
|
||||
id: z.number(),
|
||||
status: z.string(),
|
||||
user_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
created_by: z.string(),
|
||||
});
|
||||
|
||||
const SummarySchema = z.object({
|
||||
id: z.number(),
|
||||
value: z.string(),
|
||||
status: z.string(),
|
||||
user_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
created_by: z.string(),
|
||||
updated_at: z.coerce.date().nullable(),
|
||||
updated_by: z.string(),
|
||||
doctor_user_id: z.string().nullable(),
|
||||
analysis_order_id: z.number(),
|
||||
doctor_analysis_feedback: z.array(DoctorAnalysisFeedbackSchema),
|
||||
});
|
||||
|
||||
export const AnalysisResultDetailsSchema = z.object({
|
||||
id: z.number(),
|
||||
analysis_order_id: z.number(),
|
||||
order_number: z.string(),
|
||||
order_status: z.string(),
|
||||
user_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
updated_at: z.coerce.date().nullable(),
|
||||
elements: z.array(ElementSchema),
|
||||
order: OrderSchema,
|
||||
summary: SummarySchema.nullable(),
|
||||
});
|
||||
export type AnalysisResultDetails = z.infer<typeof AnalysisResultDetailsSchema>;
|
||||
export enum AnalysisStatus {
|
||||
QUEUED = 1,
|
||||
PENDING = 2,
|
||||
ONGOING = 3,
|
||||
COMPLETED = 4,
|
||||
REFUSED = 5,
|
||||
CANCELLED = 6,
|
||||
}
|
||||
|
||||
export type AnalysisResultDetailsElementResults = {
|
||||
unit: string | null;
|
||||
normLower: number | null;
|
||||
normUpper: number | null;
|
||||
normStatus: number | null;
|
||||
normStatus: AnalysisResultLevel | null;
|
||||
responseTime: string | null;
|
||||
responseValue: number | null;
|
||||
responseValueIsNegative: boolean | null;
|
||||
responseValueIsWithinNorm: boolean | null;
|
||||
normLowerIncluded: boolean;
|
||||
normUpperIncluded: boolean;
|
||||
status: string;
|
||||
status: AnalysisStatus;
|
||||
analysisElementOriginalId: string;
|
||||
nestedElements: {
|
||||
analysisElementOriginalId: string;
|
||||
normLower?: number | null;
|
||||
normLowerIncluded: boolean;
|
||||
normStatus: number;
|
||||
normUpper?: number | null;
|
||||
normUpperIncluded: boolean;
|
||||
responseTime: string;
|
||||
responseValue: number;
|
||||
status: number;
|
||||
unit: string;
|
||||
}[];
|
||||
nestedElements: AnalysisResultsDetailsElementNested[];
|
||||
labComment?: string | null;
|
||||
};
|
||||
|
||||
@@ -103,37 +143,21 @@ export type AnalysisResultDetailsElement = {
|
||||
analysisIdOriginal: string;
|
||||
isWaitingForResults: boolean;
|
||||
analysisName: string;
|
||||
results: AnalysisResultDetailsElementResults;
|
||||
results?: AnalysisResultDetailsElementResults;
|
||||
};
|
||||
|
||||
export type AnalysisResultDetailsMapped = {
|
||||
id: number;
|
||||
order: {
|
||||
status: string;
|
||||
medusaOrderId: string;
|
||||
createdAt: Date | string;
|
||||
};
|
||||
elements: {
|
||||
id: string;
|
||||
unit: string;
|
||||
norm_lower: number;
|
||||
norm_upper: number;
|
||||
norm_status: number;
|
||||
analysis_name: string;
|
||||
response_time: string;
|
||||
response_value: number;
|
||||
norm_lower_included: boolean;
|
||||
norm_upper_included: boolean;
|
||||
status: string;
|
||||
analysis_element_original_id: string;
|
||||
}[];
|
||||
orderedAnalysisElementIds: number[];
|
||||
createdAt: string;
|
||||
} & Pick<AnalysisOrder, 'status'>;
|
||||
orderedAnalysisElements: AnalysisResultDetailsElement[];
|
||||
summary: {
|
||||
id: number;
|
||||
status: string;
|
||||
user_id: string;
|
||||
created_at: Date;
|
||||
created_at: string;
|
||||
created_by: string;
|
||||
value?: string;
|
||||
} | null;
|
||||
|
||||
@@ -2,6 +2,9 @@ import { MonitoringService } from '@kit/monitoring-core';
|
||||
|
||||
export class ConsoleMonitoringService implements MonitoringService {
|
||||
identifyUser(data: { id: string }) {
|
||||
if (typeof window !== 'undefined') {
|
||||
return;
|
||||
}
|
||||
console.log(`[Console Monitoring] Identified user`, data);
|
||||
}
|
||||
|
||||
@@ -12,6 +15,9 @@ export class ConsoleMonitoringService implements MonitoringService {
|
||||
}
|
||||
|
||||
captureEvent(event: string) {
|
||||
if (typeof window !== 'undefined') {
|
||||
return;
|
||||
}
|
||||
console.log(`[Console Monitoring] Captured event: ${event}`);
|
||||
}
|
||||
|
||||
|
||||
@@ -57,3 +57,8 @@ export const getPersonParameters = (personalCode: string) => {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
export function toArray<T>(input?: T | T[] | null): T[] {
|
||||
if (!input) return [];
|
||||
return Array.isArray(input) ? input : [input];
|
||||
}
|
||||
|
||||
@@ -1401,6 +1401,7 @@ export type Database = {
|
||||
medipost_private_message_id: string | null
|
||||
medusa_order_id: string | null
|
||||
response_xml: string | null
|
||||
updated_at: string | null
|
||||
xml: string | null
|
||||
}
|
||||
Insert: {
|
||||
@@ -1413,6 +1414,7 @@ export type Database = {
|
||||
medipost_private_message_id?: string | null
|
||||
medusa_order_id?: string | null
|
||||
response_xml?: string | null
|
||||
updated_at?: string | null
|
||||
xml?: string | null
|
||||
}
|
||||
Update: {
|
||||
@@ -1425,6 +1427,7 @@ export type Database = {
|
||||
medipost_private_message_id?: string | null
|
||||
medusa_order_id?: string | null
|
||||
response_xml?: string | null
|
||||
updated_at?: string | null
|
||||
xml?: string | null
|
||||
}
|
||||
Relationships: []
|
||||
|
||||
@@ -158,11 +158,11 @@ export function PageHeader({
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
'flex items-center justify-between py-5',
|
||||
'flex py-5 flex-col sm:flex-row items-start sm:items-center sm:justify-between',
|
||||
className,
|
||||
)}
|
||||
>
|
||||
<div className={'flex flex-col gap-y-2'}>
|
||||
<div className={'flex flex-col gap-y-4 sm:gap-y-2'}>
|
||||
<If condition={title}>
|
||||
<PageTitle>{title}</PageTitle>
|
||||
</If>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"pageTitle": "My analysis results",
|
||||
"description": "All analysis results will appear here within 1-3 business days after they have been done.",
|
||||
"description": "",
|
||||
"descriptionPartial": "All analysis results will appear here within 1-3 business days after they have been done.",
|
||||
"descriptionEmpty": "If you've already done your analysis, your results will appear here soon.",
|
||||
"orderNewAnalysis": "Order new analyses",
|
||||
"waitingForResults": "Waiting for results",
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"pageTitle": "Minu analüüside vastused",
|
||||
"description": "Kõikide analüüside tulemused ilmuvad 1-3 tööpäeva jooksul peale nende andmist.",
|
||||
"description": "",
|
||||
"descriptionPartial": "Kõikide analüüside tulemused ilmuvad 1-3 tööpäeva jooksul peale nende andmist.",
|
||||
"descriptionEmpty": "Kui oled juba käinud analüüse andmas, siis varsti jõuavad siia sinu analüüside vastused.",
|
||||
"orderNewAnalysis": "Telli uued analüüsid",
|
||||
"waitingForResults": "Tulemuse ootel",
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"pageTitle": "Мои результаты анализов",
|
||||
"description": "Все результаты анализов появляются в течение 1-3 рабочих дней после их сдачи.",
|
||||
"description": "",
|
||||
"descriptionPartial": "Все результаты анализов появляются в течение 1-3 рабочих дней после их сдачи.",
|
||||
"descriptionEmpty": "Если вы уже сдали анализы, то вскоре здесь появятся ваши результаты.",
|
||||
"orderNewAnalysis": "Заказать новые анализы",
|
||||
"waitingForResults": "Ожидание результатов",
|
||||
|
||||
@@ -75,11 +75,11 @@
|
||||
}
|
||||
|
||||
h5 {
|
||||
@apply text-base;
|
||||
@apply text-lg;
|
||||
}
|
||||
|
||||
h6 {
|
||||
@apply text-lg;
|
||||
@apply text-base;
|
||||
}
|
||||
|
||||
.lucide {
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
-- Add unique constraint on analysis_response_elements by analysis_response_id + analysis_element_original_id
|
||||
CREATE UNIQUE INDEX analysis_response_elements_unique_by_response_and_element
|
||||
ON "medreport"."analysis_response_elements"
|
||||
USING btree (analysis_response_id, analysis_element_original_id);
|
||||
|
||||
ALTER TABLE "medreport"."analysis_response_elements"
|
||||
ADD CONSTRAINT "analysis_response_elements_unique_by_response_and_element"
|
||||
UNIQUE USING INDEX "analysis_response_elements_unique_by_response_and_element";
|
||||
|
||||
-- Add updated_at column to medipost_actions table
|
||||
ALTER TABLE "medreport"."medipost_actions" ADD COLUMN "updated_at" TIMESTAMP WITH TIME ZONE DEFAULT now();
|
||||
|
||||
-- Add unique constraint on medipost_actions by medipost_private_message_id
|
||||
-- Using partial index to allow multiple NULL values but enforce uniqueness for non-NULL values
|
||||
CREATE UNIQUE INDEX medipost_actions_unique_by_private_message_id
|
||||
ON "medreport"."medipost_actions"
|
||||
USING btree (medipost_private_message_id)
|
||||
WHERE medipost_private_message_id IS NOT NULL;
|
||||
Reference in New Issue
Block a user