65 Commits

Author SHA1 Message Date
b211cd538a Fix warnings "You need to pass in an i18next instance using i18nextReactModule" 2025-11-13 13:23:30 +02:00
d643788919 MED-244: Hide CANCELLED jobs from user "in progress" jobs table 2025-11-13 13:12:03 +02:00
96fbb71c87 MED-244: Console.log the actual error that happened 2025-11-13 13:11:04 +02:00
bf5546c48b MED-244: Medipost can send more responses than ordered (hemogramm with sub-analyses) 2025-11-13 13:10:30 +02:00
44ca92ac91 MED-244: Fix loading doctor in progress jobs list 2025-11-13 13:10:00 +02:00
1b17dd845a Add user analysis view bars + nested element logic to doctor view also 2025-11-12 12:21:53 +02:00
2c0634f444 Fix types and condition 2025-11-12 11:50:01 +02:00
5757c44e12 Fix "comment" will never include IDs "doctors that received email: ", improve logging 2025-11-12 11:49:39 +02:00
487d604e19 Update analysis level bars in doctor view based on user view 2025-11-12 11:48:58 +02:00
f7fbbd2352 Fix /doctor/analysis/[id] expects id to be analysisResponse.id, not analysisOrder.id 2025-11-12 09:51:46 +02:00
a77e2a7f70 Make error logs clearer about what is missing 2025-11-12 09:50:55 +02:00
b216f7b211 Fix type errors 2025-11-12 09:50:37 +02:00
5ef7f58f5d Fix "ups" error for 1sec after logout 2025-11-12 08:53:21 +02:00
2cb6a0343a Make it clearer which date is shown inside tooltip 2025-11-12 08:53:05 +02:00
8f32fdf08d Move medipostPrivateMessage.service to separate classes, improve logging 2025-11-12 08:51:48 +02:00
Karli
0878b5d1bd Merge branch 'main' into develop 2025-11-10 16:56:54 +02:00
Karli
6ee2e65938 MED-238: Improve logging 2025-11-10 16:54:01 +02:00
Karli
633b6db1af MED-238: Fix "client.auth.getUser" cannot be used in job context 2025-11-07 10:24:33 +02:00
Karli
452440e8db MED-238: Fix "client.auth.getUser" cannot be used in job context 2025-11-07 10:24:10 +02:00
641ee91c90 Merge pull request #147 from MR-medreport/hotfix/error-logs-1107
hotfix: fix errors from aws logs
2025-11-07 10:02:12 +02:00
Karli
2aa2ce9ce1 MED-238: Update query limit, default=5 2025-11-07 10:01:29 +02:00
Karli
48381b2c27 MED-240: Fix error "[PostgrestError]: "failed to parse filter (not.is.COMPLETED)" (line 1, column 8)" 2025-11-07 10:01:16 +02:00
Karli
2972988211 MED-240: Fix error "Error syncing analysis results Error [AuthSessionMissingError]: Auth session missing!" 2025-11-07 10:01:10 +02:00
ac80460d93 Merge pull request #146 from MR-medreport/feature/MED-238-240
MED-238,240: Fix errors from logs
2025-11-07 09:27:05 +02:00
Karli
58958c8ace MED-238: Update query limit, default=5 2025-11-06 23:28:26 +02:00
Karli
b76bdf7622 MED-240: Use retries for medusa query, "ECONNRESET" 2025-11-06 23:16:51 +02:00
Karli
c002eeb74b MED-240: Fix error "[PostgrestError]: "failed to parse filter (not.is.COMPLETED)" (line 1, column 8)" 2025-11-06 20:17:11 +02:00
Karli
5c171fb930 MED-240: Fix error "Error syncing analysis results Error [AuthSessionMissingError]: Auth session missing!" 2025-11-06 20:10:11 +02:00
danelkungla
99f20cce39 Merge pull request #145 from MR-medreport/develop
fix wrapping
2025-10-29 11:02:28 +02:00
danelkungla
f37d3e19fe Merge pull request #144 from MR-medreport/develop
main <- develop
2025-10-28 16:52:32 +02:00
danelkungla
a43624e559 Merge pull request #142 from MR-medreport/develop
fix analysis order page
2025-10-23 12:32:22 +03:00
danelkungla
3ad7afe2be main <- develop
main <- develop
2025-10-23 12:23:01 +03:00
danelkungla
4919c4fc12 Merge pull request #139 from MR-medreport/develop
main <- develop
2025-10-21 12:10:54 +03:00
Danel Kungla
b94e633742 Update CONNECTED_ONLINE url 2025-10-21 10:05:13 +03:00
danelkungla
766e44e5c3 Merge pull request #138 from MR-medreport/develop
main <- develop
2025-10-10 18:11:20 +03:00
danelkungla
0f962ef59a Merge pull request #136 from MR-medreport/develop
main <- develop
2025-10-09 19:01:09 +03:00
danelkungla
a5ddd790f6 MAIN <- develop
MAIN <- develop
2025-10-08 18:40:03 +03:00
danelkungla
c07b97fb3a main <- develop
main <- develop
2025-10-07 18:44:59 +03:00
danelkungla
0e14428518 main <- develop
main <- develop
2025-10-07 09:42:41 +03:00
danelkungla
839882f616 main <- develop
main <- develop
2025-10-06 19:45:45 +03:00
danelkungla
c66f71b01c main <- develop
main <- develop
2025-10-06 19:15:14 +03:00
danelkungla
6f01f31f22 Merge pull request #124 from MR-medreport/develop
minor fixes
2025-10-06 07:43:13 +03:00
danelkungla
975ee20254 Merge pull request #123 from MR-medreport/develop
fix mfa login after keycloak
2025-10-04 17:42:38 +03:00
danelkungla
6c9ab76439 Merge pull request #122 from MR-medreport/develop
main <- develop
2025-10-03 16:42:53 +03:00
danelkungla
eeea6b0d6f Merge pull request #121 from MR-medreport/develop
develop -> main
2025-10-03 12:44:25 +03:00
Danel Kungla
ca88387071 restart prod 2025-10-02 11:29:08 +03:00
Danel Kungla
45f9283e55 restart prod 2025-10-01 16:21:52 +03:00
danelkungla
bac2fba473 Merge pull request #118 from MR-medreport/develop
revert migration
2025-10-01 13:33:14 +03:00
Danel Kungla
96d3880229 Restart prod 2025-10-01 12:58:11 +03:00
danelkungla
8b41653bb5 Merge pull request #117 from MR-medreport/develop
develop -> main
2025-10-01 11:48:46 +03:00
Danel Kungla
c0c4f5e3db Restart prod 2025-09-29 11:39:10 +03:00
danelkungla
45b77f6291 Merge pull request #113 from MR-medreport/develop
include credentials
2025-09-29 11:12:10 +03:00
danelkungla
5d4dc97d19 develop -> main
develop -> main
2025-09-29 11:11:03 +03:00
Danel Kungla
cc8c4093ff updated openai Prompt for live 2025-09-25 16:02:09 +03:00
d37a99d7cd Merge pull request #107 from MR-medreport/develop
develop -> main
2025-09-24 17:02:45 +03:00
d941d82373 rerun pipeline for updated aws parameters 2025-09-24 15:27:59 +03:00
857044793d Merge pull request #103 from MR-medreport/develop
develop -> main
2025-09-22 15:56:20 +03:00
ccbaba291e Merge pull request #99 from MR-medreport/develop
develop -> main
2025-09-19 11:38:30 +03:00
danelkungla
03ec6eb670 Merge pull request #95 from MR-medreport/develop
add user consent url
2025-09-12 18:37:19 +03:00
32ce3807e8 Merge pull request #94 from MR-medreport/develop
develop -> main
2025-09-12 11:26:24 +00:00
90aa6f6fd6 Merge pull request #90 from MR-medreport/develop
develop -> main
2025-09-10 07:13:24 +00:00
be1ba7ef16 Merge pull request #88 from MR-medreport/develop
develop -> main
2025-09-10 04:31:11 +00:00
d3d937dbb2 Merge pull request #85 from MR-medreport/develop
develop -> main
2025-09-09 12:50:30 +00:00
7baa1d43a2 Merge pull request #81 from MR-medreport/develop
develop -> main; keycloak, fixes etc
2025-09-09 07:10:01 +00:00
7a479787c8 Merge pull request #77 from MR-medreport/develop
develop -> main
2025-09-06 19:59:39 +00:00
42 changed files with 1392 additions and 612 deletions

View File

@@ -14,9 +14,9 @@ export default async function sendOpenJobsEmails() {
const doctorAccounts = await getDoctorAccounts();
const doctorEmails = doctorAccounts
.map(({ email }) => email)
.filter((email) => !!email);
.filter((email): email is string => !!email);
if (doctorEmails !== null) {
if (doctorEmails.length === 0) {
return [];
}

View File

@@ -1,7 +1,4 @@
import { createUserAnalysesApi } from '@/packages/features/user-analyses/src/server/api';
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
import { readPrivateMessageResponse } from '~/lib/services/medipost/medipostPrivateMessage.service';
import MedipostPrivateMessageSync from '~/lib/services/medipost/medipostPrivateMessageSync.service';
type ProcessedMessage = {
messageId: string;
@@ -19,30 +16,22 @@ type GroupedResults = {
export default async function syncAnalysisResults() {
console.info('Syncing analysis results');
const supabase = getSupabaseServerClient();
const api = createUserAnalysesApi(supabase);
const sync = new MedipostPrivateMessageSync();
const processedMessages: ProcessedMessage[] = [];
const excludedMessageIds: string[] = [];
while (true) {
const result = await readPrivateMessageResponse({ excludedMessageIds });
if (result.messageId) {
processedMessages.push(result as ProcessedMessage);
}
const result = await sync.handleNextPrivateMessage({ excludedMessageIds });
await api.sendAnalysisResultsNotification({
hasFullAnalysisResponse: result.hasFullAnalysisResponse,
hasPartialAnalysisResponse: result.hasAnalysisResponse,
analysisOrderId: result.analysisOrderId,
});
if (!result.messageId) {
const { messageId } = result;
if (!messageId) {
console.info('No more messages to process');
break;
}
if (!excludedMessageIds.includes(result.messageId)) {
excludedMessageIds.push(result.messageId);
processedMessages.push(result as ProcessedMessage);
if (!excludedMessageIds.includes(messageId)) {
excludedMessageIds.push(messageId);
} else {
break;
}

View File

@@ -20,13 +20,14 @@ export const POST = async (request: NextRequest) => {
try {
const doctors = await sendOpenJobsEmails();
const doctorIds = doctors?.join(', ') ?? '-';
console.info(
'Successfully sent out open job notification emails to doctors',
`Successfully sent out open job notification emails to doctorIds: ${doctorIds}`,
);
await createNotificationLog({
action: NotificationAction.DOCTOR_NEW_JOBS,
status: 'SUCCESS',
comment: `doctors that received email: ${doctors}`,
comment: `doctors that received email: ${doctorIds}`,
});
return NextResponse.json(
{
@@ -35,7 +36,7 @@ export const POST = async (request: NextRequest) => {
},
{ status: 200 },
);
} catch (e: any) {
} catch (e) {
console.error(
'Error sending out open job notification emails to doctors.',
e,
@@ -43,7 +44,7 @@ export const POST = async (request: NextRequest) => {
await createNotificationLog({
action: NotificationAction.DOCTOR_NEW_JOBS,
status: 'FAIL',
comment: e?.message,
comment: e instanceof Error ? e.message : 'Unknown error',
});
return NextResponse.json(
{

View File

@@ -54,6 +54,7 @@ export async function POST(request: Request) {
action: 'send_fake_analysis_results_to_medipost',
xml: messageXml,
medusaOrderId,
medipostPrivateMessageId: `fake-response-${Date.now()}`,
});
await sendPrivateMessageTestResponse({ messageXml });
} catch (error) {

View File

@@ -2,7 +2,6 @@
import React, { ReactElement, ReactNode, useMemo, useState } from 'react';
import { UserAnalysisElement } from '@/packages/features/accounts/src/types/accounts';
import { format } from 'date-fns';
import { Info } from 'lucide-react';
@@ -10,29 +9,24 @@ import { Trans } from '@kit/ui/trans';
import { cn } from '@kit/ui/utils';
import { AnalysisElement } from '~/lib/services/analysis-element.service';
import { NestedAnalysisElement } from '@/packages/features/doctor/src/lib/server/schema/doctor-analysis-detail-view.schema';
import AnalysisLevelBar, {
AnalysisLevelBarSkeleton,
AnalysisResultLevel,
} from './analysis-level-bar';
export type AnalysisResultForDisplay = Pick<
UserAnalysisElement,
| 'norm_status'
| 'response_value'
| 'unit'
| 'norm_lower_included'
| 'norm_upper_included'
| 'norm_lower'
| 'norm_upper'
| 'response_time'
>;
export enum AnalysisStatus {
NORMAL = 0,
MEDIUM = 1,
HIGH = 2,
}
export type AnalysisResultForDisplay = {
norm_status?: number | null;
response_value?: number | null;
unit?: string | null;
norm_lower_included?: boolean | null;
norm_upper_included?: boolean | null;
norm_lower?: number | null;
norm_upper?: number | null;
response_time?: string | null;
nestedElements?: NestedAnalysisElement[];
};
const AnalysisDoctor = ({
analysisElement,
@@ -48,38 +42,41 @@ const AnalysisDoctor = ({
endIcon?: ReactNode | null;
}) => {
const name = analysisElement.analysis_name_lab || '';
const status = results?.norm_status || AnalysisStatus.NORMAL;
const status = results?.norm_status;
const value = results?.response_value || 0;
const unit = results?.unit || '';
const normLowerIncluded = results?.norm_lower_included || false;
const normUpperIncluded = results?.norm_upper_included || false;
const normLower = results?.norm_lower || 0;
const normUpper = results?.norm_upper || 0;
const normLower = results?.norm_lower ?? null;
const normUpper = results?.norm_upper ?? null;
const [showTooltip, setShowTooltip] = useState(false);
const analysisResultLevel = useMemo(() => {
if (!results) {
if (!results || status === null || status === undefined) {
return null;
}
const isUnderNorm = value < normLower;
if (isUnderNorm) {
switch (status) {
case AnalysisStatus.MEDIUM:
return AnalysisResultLevel.LOW;
default:
return AnalysisResultLevel.VERY_LOW;
}
}
switch (status) {
case AnalysisStatus.MEDIUM:
return AnalysisResultLevel.HIGH;
case AnalysisStatus.HIGH:
return AnalysisResultLevel.VERY_HIGH;
case 1:
return AnalysisResultLevel.WARNING;
case 2:
return AnalysisResultLevel.CRITICAL;
case 0:
default:
return AnalysisResultLevel.NORMAL;
}
}, [results, value, normLower]);
}, [results, status]);
const normRangeText = useMemo(() => {
if (normLower === null && normUpper === null) {
return null;
}
return `${normLower ?? '...'} - ${normUpper ?? '...'}`;
}, [normLower, normUpper]);
const nestedElements = results?.nestedElements ?? null;
const hasNestedElements =
Array.isArray(nestedElements) && nestedElements.length > 0;
const isAnalysisLevelBarHidden = isCancelled || !results || hasNestedElements;
return (
<div className="border-border rounded-lg border px-5">
@@ -110,37 +107,52 @@ const AnalysisDoctor = ({
</div>
)}
</div>
{results ? (
{isAnalysisLevelBarHidden ? null : (
<>
<div className="flex items-center gap-3 sm:ml-auto">
<div className="font-semibold">{value}</div>
<div className="text-muted-foreground text-sm">{unit}</div>
</div>
<div className="text-muted-foreground mx-8 flex flex-col-reverse gap-2 text-center text-sm sm:block sm:gap-0">
{normLower} - {normUpper}
{normRangeText}
<div>
<Trans i18nKey="analysis-results:results.range.normal" />
</div>
</div>
<AnalysisLevelBar
results={results}
normLowerIncluded={normLowerIncluded}
normUpperIncluded={normUpperIncluded}
level={analysisResultLevel!}
level={analysisResultLevel}
normRangeText={normRangeText}
/>
{endIcon || <div className="mx-2 w-4" />}
</>
) : isCancelled ? null : (
<>
<div className="flex items-center gap-3 sm:ml-auto">
<div className="font-semibold">
<Trans i18nKey="analysis-results:waitingForResults" />
</div>
</div>
<div className="mx-8 w-[60px]"></div>
<AnalysisLevelBarSkeleton />
</>
)}
{(() => {
// If parent has nested elements, don't show anything
if (hasNestedElements) {
return null;
}
// If we're showing the level bar, don't show waiting
if (!isAnalysisLevelBarHidden) {
return null;
}
// If cancelled, don't show waiting
if (isCancelled) {
return null;
}
// Otherwise, show waiting for results
return (
<>
<div className="flex items-center gap-3 sm:ml-auto">
<div className="font-semibold">
<Trans i18nKey="analysis-results:waitingForResults" />
</div>
</div>
<div className="mx-8 w-[60px]"></div>
<AnalysisLevelBarSkeleton />
</>
);
})()}
</div>
</div>
);

View File

@@ -7,11 +7,9 @@ import { cn } from '@kit/ui/utils';
import { AnalysisResultForDisplay } from './analysis-doctor';
export enum AnalysisResultLevel {
VERY_LOW = 0,
LOW = 1,
NORMAL = 2,
HIGH = 3,
VERY_HIGH = 4,
NORMAL = 'NORMAL',
WARNING = 'WARNING',
CRITICAL = 'CRITICAL',
}
const Level = ({
@@ -20,17 +18,19 @@ const Level = ({
isFirst = false,
isLast = false,
arrowLocation,
normRangeText,
}: {
isActive?: boolean;
color: 'destructive' | 'success' | 'warning' | 'gray-200';
isFirst?: boolean;
isLast?: boolean;
arrowLocation?: number;
normRangeText?: string | null;
}) => {
return (
<div
className={cn(`bg-${color} relative h-3 flex-1`, {
'opacity-20': !isActive,
'opacity-60': !isActive,
'rounded-l-lg': isFirst,
'rounded-r-lg': isLast,
})}
@@ -38,11 +38,32 @@ const Level = ({
{isActive && (
<div
className="absolute top-[-14px] left-1/2 -translate-x-1/2 rounded-[10px] bg-white p-[2px]"
style={{ left: `${arrowLocation}%` }}
{...(arrowLocation
? {
style: {
left: `${arrowLocation}%`,
...(arrowLocation > 92.5 && { left: '92.5%' }),
...(arrowLocation < 7.5 && { left: '7.5%' }),
},
}
: {})}
>
<ArrowDown strokeWidth={2} />
</div>
)}
{color === 'success' && typeof normRangeText === 'string' && (
<p
className={cn(
'text-muted-foreground absolute bottom-[-18px] left-3/8 text-xs font-bold whitespace-nowrap',
{
'opacity-60': isActive,
},
)}
>
{normRangeText}
</p>
)}
</div>
);
};
@@ -50,81 +71,148 @@ const Level = ({
export const AnalysisLevelBarSkeleton = () => {
return (
<div className="mt-4 flex h-3 w-[60%] max-w-[360px] gap-1 sm:mt-0 sm:w-[35%]">
<Level color="gray-200" />
<Level color="gray-200" isFirst isLast />
</div>
);
};
const AnalysisLevelBar = ({
normLowerIncluded = true,
normUpperIncluded = true,
level,
results,
normRangeText,
}: {
normLowerIncluded?: boolean;
normUpperIncluded?: boolean;
level: AnalysisResultLevel;
level: AnalysisResultLevel | null;
results: AnalysisResultForDisplay;
normRangeText: string | null;
}) => {
const {
norm_lower: lower,
norm_upper: upper,
response_value: value,
} = results;
const arrowLocation = useMemo(() => {
if (value < lower!) {
return 0;
}
if (normLowerIncluded || normUpperIncluded) {
// Calculate arrow position based on value within normal range
const arrowLocation = useMemo(() => {
// If no response value, center the arrow
if (value === null || value === undefined) {
return 50;
}
const calculated = ((value - lower!) / (upper! - lower!)) * 100;
if (calculated > 100) {
return 100;
// If no normal ranges defined, center the arrow
if (lower === null && upper === null) {
return 50;
}
return calculated;
// If only upper bound exists
if (lower === null && upper !== null) {
if (value <= upper!) {
return Math.min(75, (value / upper!) * 75); // Show in left 75% of normal range
}
return 100; // Beyond upper bound
}
// If only lower bound exists
if (upper === null && lower !== null) {
if (value >= lower!) {
// Value is in normal range (above lower bound)
// Position proportionally in the normal range section
const normalizedPosition = Math.min(
(value - lower!) / (lower! * 0.5),
1,
); // Use 50% of lower as scale
return normalizedPosition * 100;
}
// Value is below lower bound - position in the "below normal" section
const belowPosition = Math.max(0, Math.min(1, value / lower!));
return belowPosition * 100;
}
// Both bounds exist
if (lower !== null && upper !== null) {
if (value < lower!) {
return 0; // Below normal range
}
if (value > upper!) {
return 100; // Above normal range
}
// Within normal range
return ((value - lower!) / (upper! - lower!)) * 100;
}
return 50; // Fallback
}, [value, upper, lower]);
const [isVeryLow, isLow, isHigh, isVeryHigh] = useMemo(
() => [
level === AnalysisResultLevel.VERY_LOW,
level === AnalysisResultLevel.LOW,
level === AnalysisResultLevel.HIGH,
level === AnalysisResultLevel.VERY_HIGH,
],
[level, value, upper, lower],
);
// Determine level states based on normStatus
const isNormal = level === AnalysisResultLevel.NORMAL;
const isWarning = level === AnalysisResultLevel.WARNING;
const isCritical = level === AnalysisResultLevel.CRITICAL;
const isPending = level === null;
const hasAbnormalLevel = isVeryLow || isLow || isHigh || isVeryHigh;
// Show appropriate levels based on available norm bounds
const hasLowerBound = lower !== null;
// Calculate level configuration (must be called before any returns)
const [first, second, third] = useMemo(() => {
const [warning, normal, critical] = [
{
isActive: isWarning,
color: 'warning',
...(isWarning ? { arrowLocation } : {}),
},
{
isActive: isNormal,
color: 'success',
normRangeText,
...(isNormal ? { arrowLocation } : {}),
},
{
isActive: isCritical,
color: 'destructive',
isLast: true,
...(isCritical ? { arrowLocation } : {}),
},
] as const;
if (!hasLowerBound) {
return [{ ...normal, isFirst: true }, warning, critical] as const;
}
return [
{ ...warning, isFirst: true },
normal,
{ ...critical, isLast: true },
] as const;
}, [
arrowLocation,
normRangeText,
isNormal,
isWarning,
isCritical,
hasLowerBound,
]);
// If pending results, show gray bar
if (isPending) {
return (
<div className="w-60% mt-4 flex h-3 max-w-[360px] gap-1 sm:mt-0 sm:w-[35%]">
<Level color="gray-200" isFirst isLast />
</div>
);
}
return (
<div className="mt-4 flex h-3 w-[60%] max-w-[360px] gap-1 sm:mt-0 sm:w-[35%]">
{normLowerIncluded && (
<>
<Level isActive={isVeryLow} color="destructive" isFirst />
<Level isActive={isLow} color="warning" />
</>
)}
<Level
isFirst={!normLowerIncluded}
isLast={!normUpperIncluded}
{...(hasAbnormalLevel
? { color: 'warning', isActive: false }
: { color: 'success', isActive: true })}
arrowLocation={arrowLocation}
/>
{normUpperIncluded && (
<>
<Level isActive={isHigh} color="warning" />
<Level isActive={isVeryHigh} color="destructive" isLast />
</>
<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} />
</div>
);
};

View File

@@ -1,5 +1,7 @@
'use client';
import React from 'react';
import { CaretDownIcon, QuestionMarkCircledIcon } from '@radix-ui/react-icons';
import { useTranslation } from 'react-i18next';
@@ -23,55 +25,25 @@ export default function DoctorAnalysisWrapper({
const { t } = useTranslation();
return (
<Collapsible className="w-full" key={analysisData.id}>
<CollapsibleTrigger
disabled={!analysisData.latestPreviousAnalysis}
asChild
>
<div className="[&[data-state=open]_.caret-icon]:rotate-180">
<AnalysisDoctor
startIcon={
analysisData.latestPreviousAnalysis && (
<CaretDownIcon className="caret-icon transition-transform duration-200" />
)
}
endIcon={
analysisData.comment && (
<>
<div className="xs:flex hidden">
<InfoTooltip
content={analysisData.comment}
icon={
<QuestionMarkCircledIcon className="mx-2 text-blue-800" />
}
/>
</div>
<p className="xs:hidden">
<strong>
<Trans i18nKey="doctor:labComment" />:
</strong>{' '}
{analysisData.comment}
</p>
</>
)
}
analysisElement={{
analysis_name_lab: analysisData.analysis_name,
}}
results={analysisData}
/>
</div>
</CollapsibleTrigger>
{analysisData.latestPreviousAnalysis && (
<CollapsibleContent>
<div className="my-1 flex flex-col">
<>
<Collapsible className="w-full" key={analysisData.id}>
<CollapsibleTrigger
disabled={!analysisData.latestPreviousAnalysis}
asChild
>
<div className="[&[data-state=open]_.caret-icon]:rotate-180">
<AnalysisDoctor
startIcon={
analysisData.latestPreviousAnalysis && (
<CaretDownIcon className="caret-icon transition-transform duration-200" />
)
}
endIcon={
analysisData.latestPreviousAnalysis.comment && (
analysisData.comment && (
<>
<div className="xs:flex hidden">
<InfoTooltip
content={analysisData.latestPreviousAnalysis.comment}
content={analysisData.comment}
icon={
<QuestionMarkCircledIcon className="mx-2 text-blue-800" />
}
@@ -79,25 +51,103 @@ export default function DoctorAnalysisWrapper({
</div>
<p className="xs:hidden">
<strong>
<Trans i18nKey="doctor:labComment" />:{' '}
</strong>
{analysisData.latestPreviousAnalysis.comment}
<Trans i18nKey="doctor:labComment" />:
</strong>{' '}
{analysisData.comment}
</p>
</>
)
}
analysisElement={{
analysis_name_lab: t('doctor:previousResults', {
date: formatDate(
analysisData.latestPreviousAnalysis.response_time,
),
}),
analysis_name_lab: analysisData.analysis_name,
}}
results={analysisData.latestPreviousAnalysis}
results={analysisData}
/>
</div>
</CollapsibleContent>
)}
</Collapsible>
</CollapsibleTrigger>
{analysisData.latestPreviousAnalysis && (
<CollapsibleContent>
<div className="my-1 flex flex-col gap-2">
<AnalysisDoctor
endIcon={
analysisData.latestPreviousAnalysis.comment && (
<>
<div className="xs:flex hidden">
<InfoTooltip
content={analysisData.latestPreviousAnalysis.comment}
icon={
<QuestionMarkCircledIcon className="mx-2 text-blue-800" />
}
/>
</div>
<p className="xs:hidden">
<strong>
<Trans i18nKey="doctor:labComment" />:{' '}
</strong>
{analysisData.latestPreviousAnalysis.comment}
</p>
</>
)
}
analysisElement={{
analysis_name_lab: t('doctor:previousResults', {
date: formatDate(
analysisData.latestPreviousAnalysis.response_time!,
),
}),
}}
results={analysisData.latestPreviousAnalysis}
/>
{analysisData.latestPreviousAnalysis.nestedElements?.map(
(nestedElement, nestedIndex) => (
<div
key={`prev-nested-${nestedElement.analysisElementOriginalId}-${nestedIndex}`}
className="ml-8"
>
<AnalysisDoctor
analysisElement={{
analysis_name_lab: nestedElement.analysisNameLab ?? '',
}}
results={{
norm_status: nestedElement.normStatus,
response_value: nestedElement.responseValue,
unit: nestedElement.unit,
norm_lower: nestedElement.normLower,
norm_upper: nestedElement.normUpper,
norm_lower_included: nestedElement.normLowerIncluded,
norm_upper_included: nestedElement.normUpperIncluded,
response_time: nestedElement.responseTime,
}}
/>
</div>
),
)}
</div>
</CollapsibleContent>
)}
</Collapsible>
{analysisData.nestedElements?.map((nestedElement, nestedIndex) => (
<div
key={`nested-${nestedElement.analysisElementOriginalId}-${nestedIndex}`}
className="ml-8"
>
<AnalysisDoctor
analysisElement={{
analysis_name_lab: nestedElement.analysisNameLab ?? '',
}}
results={{
norm_status: nestedElement.normStatus,
response_value: nestedElement.responseValue,
unit: nestedElement.unit,
norm_lower: nestedElement.normLower,
norm_upper: nestedElement.normUpper,
norm_lower_included: nestedElement.normLowerIncluded,
norm_upper_included: nestedElement.normUpperIncluded,
response_time: nestedElement.responseTime,
}}
/>
</div>
))}
</>
);
}

View File

@@ -178,7 +178,7 @@ export default function ResultsTable({
<TableCell>
<Trans
i18nKey={
resultsReceived === elementsInOrder
resultsReceived >= elementsInOrder
? 'doctor:resultsTable.responsesReceived'
: 'doctor:resultsTable.waitingForNr'
}

View File

@@ -1,5 +1,6 @@
'use client';
import { useEffect } from 'react';
import Link from 'next/link';
import { ArrowLeft, MessageCircle } from 'lucide-react';
@@ -20,6 +21,22 @@ const ErrorPage = ({
}) => {
useCaptureException(error);
// Ignore next.js internal transient navigation errors that occur during auth state changes
const isTransientNavigationError =
error?.message?.includes('Error in input stream') ||
error?.message?.includes('AbortError') ||
(error?.name === 'ChunkLoadError');
useEffect(() => {
if (isTransientNavigationError && typeof window !== 'undefined') {
window.location.href = '/';
}
}, [isTransientNavigationError]);
if (isTransientNavigationError) {
return <div />;
}
return (
<div className={'flex h-screen flex-1 flex-col'}>
<SiteHeader />

View File

@@ -1,5 +1,6 @@
'use client';
import { useEffect } from 'react';
import Link from 'next/link';
import { ArrowLeft, MessageCircle } from 'lucide-react';
@@ -21,6 +22,22 @@ const GlobalErrorPage = ({
}) => {
useCaptureException(error);
// Ignore next.js internal transient navigation errors that occur during auth state changes
const isTransientNavigationError =
error?.message?.includes('Error in input stream') ||
error?.message?.includes('AbortError') ||
(error?.name === 'ChunkLoadError');
useEffect(() => {
if (isTransientNavigationError && typeof window !== 'undefined') {
window.location.href = '/';
}
}, [isTransientNavigationError]);
if (isTransientNavigationError) {
return <div />;
}
return (
<html>
<body>

View File

@@ -11,7 +11,9 @@ import { pathsConfig } from '@kit/shared/config';
import { Button } from '@kit/ui/button';
import { PageBody, PageHeader } from '@kit/ui/page';
import { Trans } from '@kit/ui/trans';
import { formatDateAndTime } from '@kit/shared/utils';
import { withI18n } from '~/lib/i18n/with-i18n';
import { loadCurrentUserAccount } from '~/home/(user)/_lib/server/load-user-account';
import { loadUserAnalysis } from '~/home/(user)/_lib/server/load-user-analysis';
import {
@@ -21,7 +23,7 @@ import {
import Analysis from '../_components/analysis';
export default async function AnalysisResultsPage({
async function AnalysisResultsPage({
params,
}: {
params: Promise<{
@@ -103,7 +105,14 @@ export default async function AnalysisResultsPage({
<h6>
<Trans i18nKey={`orders:status.${analysisResponse.order.status}`} />
<ButtonTooltip
content={`${analysisResponse.order.createdAt ? new Date(analysisResponse?.order?.createdAt).toLocaleString() : ''}`}
content={
<Trans
i18nKey="analysis-results:orderCreatedAt"
values={{
createdAt: formatDateAndTime(analysisResponse.order.createdAt)
}}
/>
}
className="ml-6"
/>
</h6>
@@ -142,3 +151,5 @@ export default async function AnalysisResultsPage({
</>
);
}
export default withI18n(AnalysisResultsPage);

View File

@@ -1,6 +1,7 @@
import Link from 'next/link';
import { createI18nServerInstance } from '@/lib/i18n/i18n.server';
import { withI18n } from '~/lib/i18n/with-i18n';
import { PageBody, PageHeader } from '@/packages/ui/src/makerkit/page';
import { toArray } from '@kit/shared/utils';
@@ -17,7 +18,7 @@ export async function generateMetadata() {
};
}
export default async function MontonioCheckoutCallbackErrorPage({
async function MontonioCheckoutCallbackErrorPage({
searchParams,
}: {
searchParams: Promise<{ reasonFailed: string }>;
@@ -61,3 +62,5 @@ export default async function MontonioCheckoutCallbackErrorPage({
</div>
);
}
export default withI18n(MontonioCheckoutCallbackErrorPage);

View File

@@ -7,6 +7,7 @@ import { ServiceCategory } from '../../_components/service-categories';
async function ttoServicesLoader() {
const response = await getProductCategories({
fields: '*products, is_active, metadata',
limit: 100,
});
const heroCategories = response.product_categories?.filter(

View File

@@ -1,8 +1,10 @@
import { withI18n } from '~/lib/i18n/with-i18n';
import { loadCurrentUserAccount } from '../../_lib/server/load-user-account';
import AccountPreferencesForm from '../_components/account-preferences-form';
import SettingsSectionHeader from '../_components/settings-section-header';
export default async function PreferencesPage() {
async function PreferencesPage() {
const { account } = await loadCurrentUserAccount();
return (
@@ -17,3 +19,5 @@ export default async function PreferencesPage() {
</div>
);
}
export default withI18n(PreferencesPage);

View File

@@ -1,8 +1,9 @@
import { MultiFactorAuthFactorsList } from '@kit/accounts/components';
import { withI18n } from '~/lib/i18n/with-i18n';
import SettingsSectionHeader from '../_components/settings-section-header';
export default function SecuritySettingsPage() {
async function SecuritySettingsPage() {
return (
<div className="mx-auto w-full bg-white p-6">
<div className="space-y-6">
@@ -15,3 +16,5 @@ export default function SecuritySettingsPage() {
</div>
);
}
export default withI18n(SecuritySettingsPage);

View File

@@ -0,0 +1,12 @@
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
export async function getAnalysisResponseAdmin(analysisOrderId: number) {
const { data } = await getSupabaseServerAdminClient()
.schema('medreport')
.from('analysis_responses')
.select('*')
.eq('analysis_order_id', analysisOrderId)
.single()
.throwOnError();
return data;
}

View File

@@ -7,7 +7,7 @@ async function getAssignedOrderIds() {
.schema('medreport')
.from('doctor_analysis_feedback')
.select('analysis_order_id')
.not('status', 'is', 'COMPLETED')
.not('status', 'eq', 'COMPLETED')
.not('doctor_user_id', 'is', null)
.throwOnError();

View File

@@ -0,0 +1,142 @@
import type { PostgrestError } from "@supabase/supabase-js";
import { toArray } from '@kit/shared/utils';
import type { AnalysisOrder } from "~/lib/types/order";
import type { AnalysisResponseElement } from '~/lib/types/analysis-response-element';
import { AnalysisOrderStatus } from '@/packages/shared/src/types/medipost-analysis';
import type {
MedipostAnalysisResult,
ResponseUuringuGrupp,
} from '@/packages/shared/src/types/medipost-analysis';
import { getAnalysisResponseElementsForGroup } from "./medipostPrivateMessage.service";
import {
getExistingAnalysisResponseElements,
upsertAnalysisResponse,
upsertAnalysisResponseElement,
} from "../analysis-order.service";
import type { Logger } from './types';
type AnalysisResponseElementMapped = Omit<
AnalysisResponseElement,
'created_at' | 'updated_at' | 'id' | 'analysis_response_id'
>;
export type SyncResult =
| {
isCompleted: boolean;
isPartial?: undefined;
}
| {
isPartial: boolean;
isCompleted?: undefined;
};
export default class MedipostAnalysisResultService {
public async storeAnalysisResult({
messageResponse: {
TellimuseNumber: orderNumber,
TellimuseOlek,
UuringuGrupp,
},
analysisOrder,
log,
}: {
messageResponse: Pick<
NonNullable<MedipostAnalysisResult>,
'TellimuseNumber' | 'TellimuseOlek' | 'UuringuGrupp'
>;
analysisOrder: AnalysisOrder;
log: Logger;
}): Promise<SyncResult> {
const orderStatus = AnalysisOrderStatus[TellimuseOlek];
const { analysisResponseId } = await upsertAnalysisResponse({
analysisOrderId: analysisOrder.id,
orderNumber,
orderStatus,
userId: analysisOrder.user_id,
});
const existingElements = await getExistingAnalysisResponseElements({
analysisResponseId,
});
const analysisGroups = toArray(UuringuGrupp);
log(`Order has results for ${analysisGroups.length} analysis groups`);
const newElements = await this.getNewAnalysisResponseElements({
analysisGroups,
existingElements,
log,
});
for (const element of newElements) {
try {
await upsertAnalysisResponseElement({
element: {
...element,
analysis_response_id: analysisResponseId,
},
});
} catch (e) {
log(
`Failed to create order response element for response id ${analysisResponseId}, element id '${element.analysis_element_original_id}'`,
"error",
e as PostgrestError,
);
}
}
const hasAllResults = await this.hasAllAnalysisResponseElements({
analysisResponseId,
analysisOrder,
});
log(`Order has ${hasAllResults ? 'all' : 'some'} results, status is ${orderStatus}`);
return hasAllResults
? { isCompleted: orderStatus === 'COMPLETED' }
: { isPartial: true };
}
private async getNewAnalysisResponseElements({
analysisGroups,
existingElements,
log,
}: {
analysisGroups: ResponseUuringuGrupp[];
existingElements: AnalysisResponseElement[];
log: Logger;
}): Promise<AnalysisResponseElementMapped[]> {
const newElements: AnalysisResponseElementMapped[] = [];
for (const analysisGroup of analysisGroups) {
log(
`[${analysisGroups.indexOf(analysisGroup) + 1}/${analysisGroups.length}] Syncing analysis group '${analysisGroup.UuringuGruppNimi}'`,
);
const elements = await getAnalysisResponseElementsForGroup({
analysisGroup,
existingElements,
log,
});
newElements.push(...elements);
}
return newElements;
}
private async hasAllAnalysisResponseElements({
analysisResponseId,
analysisOrder,
}: {
analysisResponseId: number;
analysisOrder: Pick<AnalysisOrder, 'analysis_element_ids'>;
}): Promise<boolean> {
const allOrderResponseElements = await getExistingAnalysisResponseElements({
analysisResponseId,
});
const expectedOrderResponseElements = analysisOrder.analysis_element_ids?.length ?? 0;
return allOrderResponseElements.length >= expectedOrderResponseElements;
}
}

View File

@@ -29,6 +29,19 @@ export async function getLatestMessage({
);
}
export async function getMedipostActionLog({
medipostPrivateMessageId,
}: {
medipostPrivateMessageId: string;
}) {
const { data: existingRecord } = await getSupabaseServerAdminClient()
.schema('medreport').from('medipost_actions')
.select('id')
.eq('medipost_private_message_id', medipostPrivateMessageId)
.single();
return existingRecord;
}
export async function upsertMedipostActionLog({
action,
xml,
@@ -51,6 +64,10 @@ export async function upsertMedipostActionLog({
medipostExternalOrderId?: string | null;
medipostPrivateMessageId?: string | null;
}) {
if (typeof medipostPrivateMessageId !== 'string') {
throw new Error('medipostPrivateMessageId is required');
}
const recordData = {
action,
xml,
@@ -62,18 +79,19 @@ export async function upsertMedipostActionLog({
medipost_private_message_id: medipostPrivateMessageId,
};
const query = getSupabaseServerAdminClient()
const existingActionLog = await getMedipostActionLog({ medipostPrivateMessageId });
if (existingActionLog) {
console.info(`Medipost action log already exists for private message id: ${medipostPrivateMessageId}`);
return { medipostActionId: existingActionLog.id };
}
console.info(`Inserting medipost action log for private message id: ${medipostPrivateMessageId}`);
const { data } = await 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();
.from('medipost_actions')
.insert(recordData)
.select('id')
.throwOnError();
const medipostActionId = data?.[0]?.id;
if (!medipostActionId) {
@@ -84,3 +102,46 @@ export async function upsertMedipostActionLog({
return { medipostActionId };
}
export async function createMedipostActionLogForError({
privateMessageXml,
medipostPrivateMessageId,
medusaOrderId,
medipostExternalOrderId,
}: {
privateMessageXml: string;
medipostPrivateMessageId: string;
medusaOrderId?: string;
medipostExternalOrderId: string;
}) {
await upsertMedipostActionLog({
action: 'sync_analysis_results_from_medipost',
xml: privateMessageXml,
hasAnalysisResults: false,
medipostPrivateMessageId,
medusaOrderId,
medipostExternalOrderId,
hasError: true,
});
}
export async function createMedipostActionLogForSuccess({
privateMessageXml,
medipostPrivateMessageId,
medusaOrderId,
medipostExternalOrderId,
}: {
privateMessageXml: string;
medipostPrivateMessageId: string;
medusaOrderId: string;
medipostExternalOrderId: string;
}) {
await upsertMedipostActionLog({
action: 'sync_analysis_results_from_medipost',
xml: privateMessageXml,
hasAnalysisResults: true,
medipostPrivateMessageId: medipostPrivateMessageId,
medusaOrderId,
medipostExternalOrderId,
});
}

View File

@@ -0,0 +1,87 @@
import axios from 'axios';
import type { GetMessageListResponse } from '~/lib/types/medipost';
import { MedipostAction } from '~/lib/types/medipost';
import type { MedipostOrderResponse } from '@/packages/shared/src/types/medipost-analysis';
import { validateMedipostResponse } from './medipostValidate.service';
import { parseXML } from '../util/xml.service';
import { getLatestMessage } from './medipostMessageBase.service';
const BASE_URL = process.env.MEDIPOST_URL!;
const USER = process.env.MEDIPOST_USER!;
const PASSWORD = process.env.MEDIPOST_PASSWORD!;
const IS_ENABLED_DELETE_PRIVATE_MESSAGE =
process.env.MEDIPOST_ENABLE_DELETE_RESPONSE_PRIVATE_MESSAGE_ON_READ ===
'true';
export default class MedipostMessageClient {
public async getLatestPrivateMessageListItem({
excludedMessageIds,
}: {
excludedMessageIds: string[];
}) {
const { data } = await axios.get<GetMessageListResponse>(BASE_URL, {
params: {
Action: MedipostAction.GetPrivateMessageList,
User: USER,
Password: PASSWORD,
},
});
if (data.code && data.code !== 0) {
throw new Error('Failed to get private message list');
}
return await getLatestMessage({
messages: data?.messages,
excludedMessageIds,
});
}
public async getPrivateMessage(messageId: string) {
const { data } = await axios.get(BASE_URL, {
params: {
Action: MedipostAction.GetPrivateMessage,
User: USER,
Password: PASSWORD,
MessageId: messageId,
},
headers: {
Accept: 'application/xml',
},
});
await validateMedipostResponse(data, { canHaveEmptyCode: true });
return {
message: (await parseXML(data)) as MedipostOrderResponse,
xml: data as string,
};
}
public async deletePrivateMessage({
medipostPrivateMessageId,
}: {
medipostPrivateMessageId: string;
}) {
if (!IS_ENABLED_DELETE_PRIVATE_MESSAGE) {
console.info(`Skipping delete private message id=${medipostPrivateMessageId} because deleting is not enabled`);
return;
}
const { data } = await axios.get(BASE_URL, {
params: {
Action: MedipostAction.DeletePrivateMessage,
User: USER,
Password: PASSWORD,
MessageId: medipostPrivateMessageId,
},
});
if (data.code && data.code !== 0) {
throw new Error(`Failed to delete private message (id: ${medipostPrivateMessageId})`);
}
}
}

View File

@@ -0,0 +1,106 @@
import type { MedipostOrderResponse, MedipostAnalysisResult } from '@/packages/shared/src/types/medipost-analysis';
interface ParsedMessageData {
analysisResult: NonNullable<MedipostAnalysisResult>;
orderNumber: string;
medipostExternalOrderId: number;
medipostExternalOrderIdRaw: string | number;
patientPersonalCode: string;
}
type ParseMessageResult =
| {
success: true;
data: ParsedMessageData;
}
| {
success: false;
reason: 'no_analysis_result' | 'invalid_order_id' | 'invalid_patient_code';
medipostExternalOrderIdRaw?: string | number;
medipostExternalOrderId?: number;
};
export default class MedipostMessageParser {
public extractAnalysisResult(
message: MedipostOrderResponse,
): ParsedMessageData['analysisResult'] | null {
return message?.Saadetis?.Vastus ?? null;
}
public extractOrderId(
message: MedipostOrderResponse,
analysisResult: ParsedMessageData['analysisResult'],
): { orderId: number; rawOrderId: string | number } | null {
const rawOrderId =
message.Saadetis?.Tellimus?.ValisTellimuseId ||
analysisResult.ValisTellimuseId;
if (!rawOrderId) {
return null;
}
const orderId = Number(rawOrderId);
if (isNaN(orderId)) {
return null;
}
return { orderId, rawOrderId };
}
public extractOrderNumber(
analysisResult: ParsedMessageData['analysisResult'],
): string {
return analysisResult.TellimuseNumber;
}
public extractPatientPersonalCode(
analysisResult: ParsedMessageData['analysisResult'],
): string | null {
return analysisResult.Patsient.Isikukood?.toString() ?? null;
}
public parseMessage(message: MedipostOrderResponse): ParseMessageResult {
const analysisResult = this.extractAnalysisResult(message);
if (!analysisResult) {
return {
success: false,
reason: 'no_analysis_result',
};
}
const orderIdResult = this.extractOrderId(message, analysisResult);
if (!orderIdResult) {
return {
success: false,
reason: 'invalid_order_id',
medipostExternalOrderIdRaw:
message.Saadetis?.Tellimus?.ValisTellimuseId ||
analysisResult.ValisTellimuseId,
};
}
const patientPersonalCode = this.extractPatientPersonalCode(analysisResult);
if (!patientPersonalCode) {
return {
success: false,
reason: 'invalid_patient_code',
medipostExternalOrderIdRaw: orderIdResult.rawOrderId,
medipostExternalOrderId: orderIdResult.orderId,
};
}
const orderNumber = this.extractOrderNumber(analysisResult);
return {
success: true,
data: {
analysisResult,
orderNumber,
medipostExternalOrderId: orderIdResult.orderId,
medipostExternalOrderIdRaw: orderIdResult.rawOrderId,
patientPersonalCode,
},
};
}
}

View File

@@ -1,12 +1,8 @@
'use server';
import type { PostgrestError } from '@supabase/supabase-js';
import { GetMessageListResponse, MedipostAction } from '@/lib/types/medipost';
import { MedipostAction } from '@/lib/types/medipost';
import { createUserAnalysesApi } from '@/packages/features/user-analyses/src/server/api';
import { AnalysisOrderStatus } from '@/packages/shared/src/types/medipost-analysis';
import type {
MedipostOrderResponse,
ResponseUuringuGrupp,
UuringElement,
} from '@/packages/shared/src/types/medipost-analysis';
@@ -14,77 +10,27 @@ import { getSupabaseServerAdminClient } from '@/packages/supabase/src/clients/se
import axios from 'axios';
import { toArray } from '@kit/shared/utils';
import { Tables } from '@kit/supabase/database';
import type { AnalysisResponseElement } from '~/lib/types/analysis-response-element';
import type { AnalysisOrder } from '~/lib/types/order';
import { getAccountAdmin } from '../account.service';
import { getAnalyses } from '../analyses.service';
import { getAnalysisElementsAdmin } from '../analysis-element.service';
import {
getExistingAnalysisResponseElements,
upsertAnalysisResponse,
upsertAnalysisResponseElement,
} from '../analysis-order.service';
import { logMedipostDispatch } from '../audit.service';
import { getAnalysisOrder } from '../order.service';
import { parseXML } from '../util/xml.service';
import { MedipostValidationError } from './MedipostValidationError';
import {
getLatestMessage,
upsertMedipostActionLog,
} from './medipostMessageBase.service';
import { validateMedipostResponse } from './medipostValidate.service';
import { OrderedAnalysisElement, composeOrderXML } from './medipostXML.service';
import type { Logger } from './types';
const BASE_URL = process.env.MEDIPOST_URL!;
const USER = process.env.MEDIPOST_USER!;
const PASSWORD = process.env.MEDIPOST_PASSWORD!;
const RECIPIENT = process.env.MEDIPOST_RECIPIENT!;
const IS_ENABLED_DELETE_PRIVATE_MESSAGE =
process.env.MEDIPOST_ENABLE_DELETE_RESPONSE_PRIVATE_MESSAGE_ON_READ ===
'true';
export async function getLatestPrivateMessageListItem({
excludedMessageIds,
}: {
excludedMessageIds: string[];
}) {
const { data } = await axios.get<GetMessageListResponse>(BASE_URL, {
params: {
Action: MedipostAction.GetPrivateMessageList,
User: USER,
Password: PASSWORD,
},
});
if (data.code && data.code !== 0) {
throw new Error('Failed to get private message list');
}
return await getLatestMessage({
messages: data?.messages,
excludedMessageIds,
});
}
const logger =
(
analysisOrder: AnalysisOrder,
externalId: string,
analysisResponseId: string,
) =>
(message: string, error?: PostgrestError | null) => {
const messageFormatted = `[${analysisOrder.id}] [${externalId}] [${analysisResponseId}] ${message}`;
if (error) {
console.info(messageFormatted, error);
} else {
console.info(messageFormatted);
}
};
export async function canCreateAnalysisResponseElement({
existingElements,
groupUuring: {
@@ -101,7 +47,7 @@ export async function canCreateAnalysisResponseElement({
UuringuElement: Pick<UuringElement, 'UuringOlek' | 'UuringId'>;
};
responseValue: number | null;
log: ReturnType<typeof logger>;
log: Logger;
}) {
const existingAnalysisResponseElement = existingElements.find(
({ analysis_element_original_id }) =>
@@ -138,7 +84,7 @@ export async function getAnalysisResponseElementsForGroup({
AnalysisResponseElement,
'analysis_element_original_id' | 'status' | 'response_value'
>[];
log: ReturnType<typeof logger>;
log: Logger;
}) {
const groupUuringItems = toArray(
analysisGroup.Uuring as ResponseUuringuGrupp['Uuring'],
@@ -211,284 +157,6 @@ export async function getAnalysisResponseElementsForGroup({
return results;
}
async function getNewAnalysisResponseElements({
analysisGroups,
existingElements,
log,
}: {
analysisGroups: ResponseUuringuGrupp[];
existingElements: AnalysisResponseElement[];
log: ReturnType<typeof logger>;
}) {
const newElements: Omit<
AnalysisResponseElement,
'created_at' | 'updated_at' | 'id' | 'analysis_response_id'
>[] = [];
for (const analysisGroup of analysisGroups) {
log(
`[${analysisGroups.indexOf(analysisGroup) + 1}/${analysisGroups.length}] Syncing analysis group '${analysisGroup.UuringuGruppNimi}'`,
);
const elements = await getAnalysisResponseElementsForGroup({
analysisGroup,
existingElements,
log,
});
newElements.push(...elements);
}
return newElements;
}
async function hasAllAnalysisResponseElements({
analysisResponseId,
order,
}: {
analysisResponseId: number;
order: Pick<AnalysisOrder, 'analysis_element_ids'>;
}) {
const allOrderResponseElements = await getExistingAnalysisResponseElements({
analysisResponseId,
});
const expectedOrderResponseElements = order.analysis_element_ids?.length ?? 0;
return allOrderResponseElements.length >= expectedOrderResponseElements;
}
export async function syncPrivateMessage({
messageResponse: {
ValisTellimuseId: externalId,
TellimuseNumber: orderNumber,
TellimuseOlek,
UuringuGrupp,
},
order,
}: {
messageResponse: Pick<
NonNullable<MedipostOrderResponse['Saadetis']['Vastus']>,
'ValisTellimuseId' | 'TellimuseNumber' | 'TellimuseOlek' | 'UuringuGrupp'
>;
order: Tables<{ schema: 'medreport' }, 'analysis_orders'>;
}) {
const supabase = getSupabaseServerAdminClient();
const orderStatus = AnalysisOrderStatus[TellimuseOlek];
const log = logger(order, externalId, orderNumber);
const { data: analysisOrder } = await supabase
.schema('medreport')
.from('analysis_orders')
.select('id, user_id')
.eq('id', order.id)
.single()
.throwOnError();
const { analysisResponseId } = await upsertAnalysisResponse({
analysisOrderId: order.id,
orderNumber,
orderStatus,
userId: analysisOrder.user_id,
});
const existingElements = await getExistingAnalysisResponseElements({
analysisResponseId,
});
const analysisGroups = toArray(UuringuGrupp);
log(`Order has results for ${analysisGroups.length} analysis groups`);
const newElements = await getNewAnalysisResponseElements({
analysisGroups,
existingElements,
log,
});
for (const element of newElements) {
try {
await upsertAnalysisResponseElement({
element: {
...element,
analysis_response_id: analysisResponseId,
},
});
} catch (e) {
log(
`Failed to create order response element for response id ${analysisResponseId}, element id '${element.analysis_element_original_id}' (order id: ${order.id})`,
e as PostgrestError,
);
}
}
return (await hasAllAnalysisResponseElements({ analysisResponseId, order }))
? { isCompleted: orderStatus === 'COMPLETED' }
: { isPartial: true };
}
export async function readPrivateMessageResponse({
excludedMessageIds,
}: {
excludedMessageIds: string[];
}): Promise<{
messageId: string | null;
hasAnalysisResponse: boolean;
hasPartialAnalysisResponse: boolean;
hasFullAnalysisResponse: boolean;
medusaOrderId: string | undefined;
analysisOrderId: number | undefined;
}> {
let messageId: string | null = null;
let hasAnalysisResponse = false;
let hasPartialAnalysisResponse = false;
let hasFullAnalysisResponse = false;
let medusaOrderId: string | undefined = undefined;
let analysisOrderId: number | undefined = undefined;
try {
const privateMessage = await getLatestPrivateMessageListItem({
excludedMessageIds,
});
messageId = privateMessage?.messageId ?? null;
if (!privateMessage || !messageId) {
return {
messageId: null,
hasAnalysisResponse: false,
hasPartialAnalysisResponse: false,
hasFullAnalysisResponse: false,
medusaOrderId: undefined,
analysisOrderId: undefined,
};
}
const { messageId: privateMessageId } = privateMessage;
const { message: privateMessageContent, xml: privateMessageXml } =
await getPrivateMessage(privateMessageId);
const messageResponse = privateMessageContent?.Saadetis?.Vastus;
const medipostExternalOrderId =
privateMessageContent?.Saadetis?.Tellimus?.ValisTellimuseId ||
messageResponse?.ValisTellimuseId;
const patientPersonalCode = messageResponse?.Patsient.Isikukood?.toString();
analysisOrderId = Number(medipostExternalOrderId);
const hasInvalidOrderId = isNaN(analysisOrderId);
if (hasInvalidOrderId || !messageResponse || !patientPersonalCode) {
console.log({
privateMessageContent,
saadetis: privateMessageContent?.Saadetis,
messageResponse,
});
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,
hasAnalysisResults: false,
medipostPrivateMessageId: privateMessageId,
medusaOrderId,
medipostExternalOrderId,
hasError: true,
});
return {
messageId,
hasAnalysisResponse: false,
hasPartialAnalysisResponse: false,
hasFullAnalysisResponse: false,
medusaOrderId: hasInvalidOrderId ? undefined : medusaOrderId,
analysisOrderId: hasInvalidOrderId ? undefined : analysisOrderId,
};
}
let analysisOrder: AnalysisOrder;
try {
analysisOrder = await getAnalysisOrder({ analysisOrderId });
medusaOrderId = analysisOrder.medusa_order_id;
} catch {
if (IS_ENABLED_DELETE_PRIVATE_MESSAGE) {
await deletePrivateMessage(privateMessageId);
}
throw new Error(
`No analysis order found for Medipost message ValisTellimuseId=${medipostExternalOrderId}`,
);
}
const orderPerson = await getAccountAdmin({
primaryOwnerUserId: analysisOrder.user_id,
});
if (orderPerson.personal_code !== patientPersonalCode) {
throw new Error(
`Order person personal code does not match Medipost message Patsient.Isikukood=${patientPersonalCode}, orderPerson.personal_code=${orderPerson.personal_code}`,
);
}
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,
hasAnalysisResults: true,
medipostPrivateMessageId: privateMessageId,
medusaOrderId,
medipostExternalOrderId,
});
if (status.isPartial) {
await createUserAnalysesApi(
getSupabaseServerAdminClient(),
).updateAnalysisOrderStatus({
medusaOrderId,
orderStatus: 'PARTIAL_ANALYSIS_RESPONSE',
});
hasAnalysisResponse = true;
hasPartialAnalysisResponse = true;
} else if (status.isCompleted) {
await createUserAnalysesApi(
getSupabaseServerAdminClient(),
).updateAnalysisOrderStatus({
medusaOrderId,
orderStatus: 'FULL_ANALYSIS_RESPONSE',
});
if (IS_ENABLED_DELETE_PRIVATE_MESSAGE) {
await deletePrivateMessage(privateMessageId);
}
hasAnalysisResponse = true;
hasFullAnalysisResponse = true;
}
} catch (e) {
console.warn(
`Failed to process private message id=${messageId}, message=${(e as Error).message}`,
);
}
return {
messageId,
hasAnalysisResponse,
hasPartialAnalysisResponse,
hasFullAnalysisResponse,
medusaOrderId,
analysisOrderId,
};
}
export async function deletePrivateMessage(messageId: string) {
const { data } = await axios.get(BASE_URL, {
params: {
Action: MedipostAction.DeletePrivateMessage,
User: USER,
Password: PASSWORD,
MessageId: messageId,
},
});
if (data.code && data.code !== 0) {
throw new Error(`Failed to delete private message (id: ${messageId})`);
}
}
export async function sendPrivateMessage(messageXml: string) {
const body = new FormData();
body.append('Action', MedipostAction.SendPrivateMessage);
@@ -508,27 +176,6 @@ export async function sendPrivateMessage(messageXml: string) {
await validateMedipostResponse(data);
}
export async function getPrivateMessage(messageId: string) {
const { data } = await axios.get(BASE_URL, {
params: {
Action: MedipostAction.GetPrivateMessage,
User: USER,
Password: PASSWORD,
MessageId: messageId,
},
headers: {
Accept: 'application/xml',
},
});
await validateMedipostResponse(data, { canHaveEmptyCode: true });
return {
message: (await parseXML(data)) as MedipostOrderResponse,
xml: data as string,
};
}
export async function sendOrderToMedipost({
medusaOrderId,
orderedAnalysisElements,
@@ -597,6 +244,7 @@ export async function sendOrderToMedipost({
medusaOrderId,
responseXml: e.response,
hasError: true,
medipostPrivateMessageId: `send-order-to-medipost-${Date.now()}`,
});
} else {
console.error(
@@ -613,6 +261,7 @@ export async function sendOrderToMedipost({
hasAnalysisResults: false,
medusaOrderId,
hasError: true,
medipostPrivateMessageId: `send-order-to-medipost-${Date.now()}`,
});
}
@@ -631,6 +280,7 @@ export async function sendOrderToMedipost({
xml: orderXml,
hasAnalysisResults: false,
medusaOrderId,
medipostPrivateMessageId: `send-order-to-medipost-${Date.now()}`,
});
await createUserAnalysesApi(
getSupabaseServerAdminClient(),

View File

@@ -0,0 +1,223 @@
import type { SupabaseClient } from "@supabase/supabase-js";
import type { Database } from '@kit/supabase/database';
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
import type { AnalysisOrder } from "~/lib/types/order";
import { createUserAnalysesApi } from "@/packages/features/user-analyses/src/server/api";
import { getAnalysisOrder } from "../order.service";
import { createMedipostActionLogForError, createMedipostActionLogForSuccess, getMedipostActionLog } from "./medipostMessageBase.service";
import type { Logger } from './types';
import MedipostMessageClient from './medipostMessageClient.service';
import MedipostMessageParser from './medipostMessageParser.service';
import MedipostAnalysisResultService from './medipostAnalysisResult.service';
import { validateOrderPerson } from "./medipostValidate.service";
interface IPrivateMessageSyncResult {
messageId: string | null;
hasAnalysisResponse: boolean;
hasPartialAnalysisResponse: boolean;
hasFullAnalysisResponse: boolean;
medusaOrderId: string | undefined;
analysisOrderId: number | undefined;
}
const NO_RESULT: IPrivateMessageSyncResult = {
messageId: null,
hasAnalysisResponse: false,
hasPartialAnalysisResponse: false,
hasFullAnalysisResponse: false,
medusaOrderId: undefined,
analysisOrderId: undefined,
};
export default class MedipostPrivateMessageSync {
private readonly client: SupabaseClient<Database>;
private readonly userAnalysesApi: ReturnType<typeof createUserAnalysesApi>;
private readonly messageClient: MedipostMessageClient;
private readonly messageParser: MedipostMessageParser;
private readonly analysisResultService: MedipostAnalysisResultService;
private loggerContext: {
analysisOrderId?: number;
orderNumber?: string;
medipostPrivateMessageId?: string;
} = {};
constructor() {
this.client = getSupabaseServerAdminClient();
this.userAnalysesApi = createUserAnalysesApi(this.client);
this.messageClient = new MedipostMessageClient();
this.messageParser = new MedipostMessageParser();
this.analysisResultService = new MedipostAnalysisResultService();
}
public async handleNextPrivateMessage({
excludedMessageIds,
}: {
excludedMessageIds: string[];
}): Promise<IPrivateMessageSyncResult> {
let medipostPrivateMessageId: string | null = null;
let hasAnalysisResponse = false;
let hasPartialAnalysisResponse = false;
let hasFullAnalysisResponse = false;
let medusaOrderId: string | undefined = undefined;
let medipostExternalOrderId: number | undefined = undefined;
try {
const privateMessage = await this.messageClient.getLatestPrivateMessageListItem({
excludedMessageIds,
});
medipostPrivateMessageId = privateMessage?.messageId ?? null;
if (!medipostPrivateMessageId) {
return NO_RESULT;
}
this.loggerContext.medipostPrivateMessageId = medipostPrivateMessageId;
if (await getMedipostActionLog({ medipostPrivateMessageId })) {
this.logger()(`Medipost action log already exists for private message`);
return { ...NO_RESULT, messageId: medipostPrivateMessageId };
}
const { message: privateMessageContent, xml: privateMessageXml } =
await this.messageClient.getPrivateMessage(medipostPrivateMessageId);
const parseResult = this.messageParser.parseMessage(privateMessageContent);
if (!parseResult.success) {
const createErrorLog = async () => createMedipostActionLogForError({
privateMessageXml,
medipostPrivateMessageId: medipostPrivateMessageId!,
medipostExternalOrderId: parseResult.medipostExternalOrderIdRaw?.toString() ?? '',
});
switch (parseResult.reason) {
case 'no_analysis_result':
console.info(`Missing results in private message, id=${medipostPrivateMessageId}`);
break;
case 'invalid_order_id':
console.error(`Invalid order id in private message, id=${medipostPrivateMessageId}`);
await createErrorLog();
break;
case 'invalid_patient_code':
console.error(`Invalid patient personal code in private message, id=${medipostPrivateMessageId}`);
await createErrorLog();
break;
}
return {
...NO_RESULT,
messageId: medipostPrivateMessageId,
analysisOrderId: parseResult.medipostExternalOrderId,
};
}
const {
analysisResult: analysisResultResponse,
orderNumber,
medipostExternalOrderIdRaw,
patientPersonalCode,
} = parseResult.data;
this.loggerContext.orderNumber = orderNumber;
medipostExternalOrderId = parseResult.data.medipostExternalOrderId;
this.loggerContext.analysisOrderId = medipostExternalOrderId;
let analysisOrder: AnalysisOrder;
try {
this.logger()(`Getting analysis order for message`);
analysisOrder = await getAnalysisOrder({ analysisOrderId: medipostExternalOrderId });
medusaOrderId = analysisOrder.medusa_order_id;
} catch (e) {
this.logger()("Get analysis order error", "error", e as Error);
await this.messageClient.deletePrivateMessage({ medipostPrivateMessageId });
throw new Error(
`No analysis order found for Medipost message ValisTellimuseId=${medipostExternalOrderIdRaw}`,
);
}
await validateOrderPerson({ analysisOrder, patientPersonalCode });
this.logger()('Storing analysis results');
const result = await this.analysisResultService.storeAnalysisResult({
messageResponse: analysisResultResponse,
analysisOrder,
log: this.logger(),
});
this.logger()('Creating medipost action log for success');
await createMedipostActionLogForSuccess({
privateMessageXml,
medipostPrivateMessageId,
medusaOrderId,
medipostExternalOrderId: medipostExternalOrderIdRaw.toString(),
});
if (result.isPartial) {
this.logger()('Updating analysis order status to PARTIAL_ANALYSIS_RESPONSE');
await this.userAnalysesApi.updateAnalysisOrderStatus({
medusaOrderId,
orderStatus: 'PARTIAL_ANALYSIS_RESPONSE',
});
hasAnalysisResponse = true;
hasPartialAnalysisResponse = true;
} else if (result.isCompleted) {
this.logger()('Updating analysis order status to FULL_ANALYSIS_RESPONSE');
await this.userAnalysesApi.updateAnalysisOrderStatus({
medusaOrderId,
orderStatus: 'FULL_ANALYSIS_RESPONSE',
});
await this.messageClient.deletePrivateMessage({ medipostPrivateMessageId });
hasAnalysisResponse = true;
hasFullAnalysisResponse = true;
}
this.logger()('Sending analysis results notification');
await this.userAnalysesApi.sendAnalysisResultsNotification({
hasFullAnalysisResponse,
hasPartialAnalysisResponse,
analysisOrderId: medipostExternalOrderId,
});
this.logger()('Successfully synced analysis results');
} catch (e) {
console.warn(
`Failed to process private message id=${medipostPrivateMessageId}, message=${(e as Error).message}`,
);
} finally {
this.clearLoggerContext();
}
return {
messageId: medipostPrivateMessageId,
hasAnalysisResponse,
hasPartialAnalysisResponse,
hasFullAnalysisResponse,
medusaOrderId,
analysisOrderId: medipostExternalOrderId,
};
}
private logger(): Logger {
const { analysisOrderId, orderNumber, medipostPrivateMessageId } = this.loggerContext;
return (message, level = 'info', error) => {
const messageFormatted = `[${analysisOrderId ?? ''}] [${orderNumber ?? '-'}] [${medipostPrivateMessageId ?? '-'}] ${message}`;
const logFn = console[level];
if (error) {
logFn(messageFormatted, error);
} else {
logFn(messageFormatted);
}
};
}
private clearLoggerContext(): void {
this.loggerContext = {};
}
}

View File

@@ -1,9 +1,11 @@
'use server';
import type { IMedipostResponseXMLBase } from '@/packages/shared/src/types/medipost-analysis';
import type { AnalysisOrder } from '~/lib/types/order';
import { parseXML } from '../util/xml.service';
import { MedipostValidationError } from './MedipostValidationError';
import { getAccountAdmin } from '../account.service';
export async function validateMedipostResponse(
response: string,
@@ -24,3 +26,20 @@ export async function validateMedipostResponse(
throw new MedipostValidationError(response);
}
}
export async function validateOrderPerson({
analysisOrder,
patientPersonalCode,
}: {
analysisOrder: AnalysisOrder;
patientPersonalCode: string;
}) {
const orderPerson = await getAccountAdmin({
primaryOwnerUserId: analysisOrder.user_id,
});
if (orderPerson.personal_code !== patientPersonalCode) {
throw new Error(
`Order person personal code does not match Medipost message Patsient.Isikukood=${patientPersonalCode}, orderPerson.personal_code=${orderPerson.personal_code}`,
);
}
}

View File

@@ -0,0 +1 @@
export type Logger = (message: string, level?: 'info' | 'error' | 'warn', error?: Error | null) => void;

View File

@@ -1,6 +1,5 @@
import type { StoreOrder } from '@medusajs/types';
import type { Tables } from '@kit/supabase/database';
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
import { getSupabaseServerClient } from '@kit/supabase/server-client';
@@ -72,6 +71,7 @@ export async function getAnalysisOrder({
const { data: order, error } = await query.single();
if (error) {
console.error("Get analysis order error", error);
throw new Error(
`Failed to get order by medusaOrderId=${medusaOrderId} or analysisOrderId=${analysisOrderId}, message=${error.message}, data=${JSON.stringify(order)}`,
);
@@ -82,7 +82,7 @@ export async function getAnalysisOrder({
export async function getAnalysisOrders({
orderStatus,
}: {
orderStatus?: Tables<{ schema: 'medreport' }, 'analysis_orders'>['status'];
orderStatus?: AnalysisOrder['status'];
} = {}) {
const client = getSupabaseServerClient();
@@ -111,7 +111,7 @@ export async function getAnalysisOrdersAdmin({
orderStatus,
medusaOrderId,
}: {
orderStatus?: Tables<{ schema: 'medreport' }, 'analysis_orders'>['status'];
orderStatus?: AnalysisOrder['status'];
medusaOrderId?: string | null;
} = {}) {
const query = getSupabaseServerAdminClient()

View File

@@ -12,7 +12,7 @@ import { createBillingEventHandlerService } from './billing-event-handler.servic
type ClientProvider = () => SupabaseClient<Database>;
// the billing provider from the database
type BillingProvider = Enums<'billing_provider'>;
type BillingProvider = Enums<{ schema: 'medreport' }, 'billing_provider'>;
/**
* @name getBillingEventHandlerService

View File

@@ -19,10 +19,10 @@ import { initializeEmailI18n } from '../lib/i18n';
export async function renderAllResultsReceivedEmail({
language,
analysisOrderId,
analysisResponseId,
}: {
language: string;
analysisOrderId: number;
analysisResponseId: number;
}) {
const namespace = 'all-results-received-email';
@@ -57,13 +57,13 @@ export async function renderAllResultsReceivedEmail({
{t(`${namespace}:openOrdersHeading`)}
</Text>
<EmailButton
href={`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisOrderId}`}
href={`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisResponseId}`}
>
{t(`${namespace}:linkText`)}
</EmailButton>
<Text>
{t(`${namespace}:ifLinksDisabled`)}{' '}
{`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisOrderId}`}
{`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisResponseId}`}
</Text>
<CommonFooter t={t} />
</EmailContent>

View File

@@ -19,10 +19,10 @@ import { initializeEmailI18n } from '../lib/i18n';
export async function renderFirstResultsReceivedEmail({
language,
analysisOrderId,
analysisResponseId,
}: {
language: string;
analysisOrderId: number;
analysisResponseId: number;
}) {
const namespace = 'first-results-received-email';
@@ -61,14 +61,14 @@ export async function renderFirstResultsReceivedEmail({
</Text>
<EmailButton
href={`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisOrderId}`}
href={`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisResponseId}`}
>
{t(`${namespace}:linkText`)}
</EmailButton>
<Text>
{t(`${namespace}:ifLinksDisabled`)}{' '}
{`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisOrderId}`}
{`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisResponseId}`}
</Text>
<CommonFooter t={t} />
</EmailContent>

View File

@@ -52,6 +52,23 @@ export const AnalysisResponsesSchema = z.object({
});
export type AnalysisResponses = z.infer<typeof AnalysisResponsesSchema>;
// Nested element schema (used recursively)
export const NestedAnalysisElementSchema = z.object({
analysisElementOriginalId: z.string(),
analysisName: z.string().optional().nullable(),
unit: z.string().nullable(),
normLower: z.number().nullable(),
normUpper: z.number().nullable(),
normStatus: z.number().nullable(),
responseTime: z.string().nullable(),
responseValue: z.number().nullable(),
normLowerIncluded: z.boolean(),
normUpperIncluded: z.boolean(),
status: z.number(),
analysisNameLab: z.string().optional().nullable(),
});
export type NestedAnalysisElement = z.infer<typeof NestedAnalysisElementSchema>;
export const AnalysisResponseSchema = z.object({
id: z.number(),
analysis_response_id: z.number(),
@@ -69,6 +86,7 @@ export const AnalysisResponseSchema = z.object({
analysis_name: z.string().nullable(),
analysis_responses: AnalysisResponsesSchema,
comment: z.string().nullable(),
nestedElements: z.array(NestedAnalysisElementSchema).optional(),
latestPreviousAnalysis: z
.object({
id: z.number(),
@@ -86,6 +104,7 @@ export const AnalysisResponseSchema = z.object({
updated_at: z.string().nullable(),
analysis_name: z.string().nullable(),
comment: z.string().nullable(),
nestedElements: z.array(NestedAnalysisElementSchema).optional(),
})
.optional()
.nullable(),

View File

@@ -5,12 +5,16 @@ import { isBefore } from 'date-fns';
import { renderDoctorSummaryReceivedEmail } from '@kit/email-templates';
import { getLogger } from '@kit/shared/logger';
import { getFullName } from '@kit/shared/utils';
import type { UuringuVastus, ResponseUuring } from '@kit/shared/types/medipost-analysis';
import { getFullName, toArray } from '@kit/shared/utils';
import { getSupabaseServerClient } from '@kit/supabase/server-client';
import { createUserAnalysesApi } from '@kit/user-analyses/api';
import { sendEmailFromTemplate } from '../../../../../../../lib/services/mailer.service';
import { AnalysisResultDetails } from '../schema/doctor-analysis-detail-view.schema';
import {
AnalysisResultDetails,
NestedAnalysisElement,
} from '../schema/doctor-analysis-detail-view.schema';
import {
AnalysisResponseBase,
DoctorAnalysisFeedbackTable,
@@ -20,6 +24,63 @@ import {
} from '../schema/doctor-analysis.schema';
import { ErrorReason } from '../schema/error.type';
function 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);
return {
normLower: uuringVastus?.NormAlum?.['#text'] ?? null,
normUpper: uuringVastus?.NormYlem?.['#text'] ?? null,
normStatus: (uuringVastus?.NormiStaatus ?? null) as number | null,
responseTime: uuringVastus?.VastuseAeg ?? null,
responseValue:
responseValueIsNumeric ? (responseValueNumber ?? null) : null,
normLowerIncluded:
uuringVastus?.NormAlum?.['@_kaasaarvatud']?.toLowerCase() === 'jah',
normUpperIncluded:
uuringVastus?.NormYlem?.['@_kaasaarvatud']?.toLowerCase() === 'jah',
};
}
function parseNestedElements(
originalResponseElement: ResponseUuring | null | undefined,
status: number,
): NestedAnalysisElement[] {
if (!originalResponseElement?.UuringuElement) {
return [];
}
const nestedElements = toArray(originalResponseElement.UuringuElement);
return nestedElements.map<NestedAnalysisElement>((element) => {
const mappedResponse = mapUuringVastus({
uuringVastus: element.UuringuVastus as UuringuVastus | undefined,
});
return {
analysisElementOriginalId: element.UuringId,
analysisName: undefined, // Will be populated later from analysis_elements table
unit: element.Mootyhik ?? null,
normLower: mappedResponse.normLower,
normUpper: mappedResponse.normUpper,
normStatus: mappedResponse.normStatus,
responseTime: mappedResponse.responseTime,
responseValue: mappedResponse.responseValue,
normLowerIncluded: mappedResponse.normLowerIncluded,
normUpperIncluded: mappedResponse.normUpperIncluded,
status,
analysisNameLab: element.UuringNimi,
};
});
}
async function enrichAnalysisData(analysisResponses?: AnalysisResponseBase[]) {
const supabase = getSupabaseServerClient();
@@ -186,12 +247,13 @@ export async function getUserInProgressResponses({
`,
{ count: 'exact' },
)
.neq('status', 'ON_HOLD')
.eq('order_status', 'COMPLETED')
.in('analysis_order_id', analysisOrderIds)
.range(offset, offset + pageSize - 1)
.order('created_at', { ascending: false });
if (error) {
console.error("Failed to get analysis responses", error);
throw new Error('Something went wrong');
}
@@ -388,7 +450,8 @@ export async function getAnalysisResultsForDoctor(
.from(`analysis_response_elements`)
.select(
`*,
analysis_responses(user_id, analysis_order:analysis_order_id(id,medusa_order_id, analysis_element_ids))`,
analysis_responses(user_id, analysis_order:analysis_order_id(id,medusa_order_id, analysis_element_ids)),
original_response_element`,
)
.eq('analysis_response_id', analysisResponseId);
@@ -404,8 +467,14 @@ export async function getAnalysisResultsForDoctor(
const medusaOrderId =
firstAnalysisResponse?.analysis_responses?.analysis_order?.medusa_order_id;
if (!analysisResponsesData?.length || !userId || !medusaOrderId) {
throw new Error('Failed to retrieve full analysis data.');
if (!analysisResponsesData?.length) {
throw new Error('No analysis responses data found.');
}
if (!userId) {
throw new Error('No user id found.');
}
if (!medusaOrderId) {
throw new Error('No medusa order id found.');
}
const responseElementAnalysisElementOriginalIds = analysisResponsesData.map(
@@ -446,7 +515,8 @@ export async function getAnalysisResultsForDoctor(
*,
analysis_responses!inner(
user_id
)
),
original_response_element
`,
)
.in(
@@ -485,8 +555,14 @@ export async function getAnalysisResultsForDoctor(
preferred_locale,
} = accountWithParams[0];
// Parse nested elements for current and previous analyses
const analysisResponseElementsWithPreviousData = [];
for (const analysisResponseElement of analysisResponsesData) {
const nestedElements = parseNestedElements(
analysisResponseElement.original_response_element as ResponseUuring,
Number(analysisResponseElement.status),
);
const latestPreviousAnalysis = previousAnalyses.find(
({ analysis_element_original_id, response_time }) => {
if (response_time && analysisResponseElement.response_time) {
@@ -501,12 +577,95 @@ export async function getAnalysisResultsForDoctor(
}
},
);
// Parse nested elements for previous analysis if it exists
const latestPreviousAnalysisWithNested = latestPreviousAnalysis
? {
...latestPreviousAnalysis,
nestedElements: parseNestedElements(
latestPreviousAnalysis.original_response_element as ResponseUuring,
Number(latestPreviousAnalysis.status),
),
}
: undefined;
analysisResponseElementsWithPreviousData.push({
...analysisResponseElement,
latestPreviousAnalysis,
nestedElements,
latestPreviousAnalysis: latestPreviousAnalysisWithNested,
});
}
// Collect all nested element IDs to fetch their names
const nestedElementIds = analysisResponseElementsWithPreviousData
.flatMap((element) => [
...(element.nestedElements?.map((ne) => ne.analysisElementOriginalId) ??
[]),
...(element.latestPreviousAnalysis?.nestedElements?.map(
(ne) => ne.analysisElementOriginalId,
) ?? []),
])
.filter(Boolean);
// Fetch analysis names for nested elements
if (nestedElementIds.length > 0) {
const { data: nestedAnalysisElements, error: nestedAnalysisElementsError } =
await supabase
.schema('medreport')
.from('analysis_elements')
.select('*')
.in('analysis_id_original', nestedElementIds);
if (!nestedAnalysisElementsError && nestedAnalysisElements) {
// Populate analysis names for current nested elements
for (const element of analysisResponseElementsWithPreviousData) {
if (element.nestedElements) {
for (const nestedElement of element.nestedElements) {
const analysisElement = nestedAnalysisElements.find(
(ae) =>
ae.analysis_id_original ===
nestedElement.analysisElementOriginalId,
);
if (analysisElement) {
nestedElement.analysisName =
analysisElement.analysis_name_lab as string | undefined;
}
}
// Sort nested elements by name
element.nestedElements.sort(
(a, b) => a.analysisName?.localeCompare(b.analysisName ?? '') ?? 0,
);
}
// Populate analysis names for previous nested elements
if (element.latestPreviousAnalysis?.nestedElements) {
for (const nestedElement of element.latestPreviousAnalysis
.nestedElements) {
const analysisElement = nestedAnalysisElements.find(
(ae) =>
ae.analysis_id_original ===
nestedElement.analysisElementOriginalId,
);
if (analysisElement) {
nestedElement.analysisName =
analysisElement.analysis_name_lab as string | undefined;
}
}
// Sort nested elements by name
element.latestPreviousAnalysis.nestedElements.sort(
(a, b) => a.analysisName?.localeCompare(b.analysisName ?? '') ?? 0,
);
}
}
} else {
console.error(
'Failed to get nested analysis elements by ids=',
nestedElementIds,
nestedAnalysisElementsError,
);
}
}
return {
analysisResponse: analysisResponseElementsWithPreviousData,
order: {

View File

@@ -15,6 +15,12 @@ import {
removeCartId,
setAuthToken,
} from './cookies';
import { withRetries } from '../util/with-retries';
const MedusaApiRetriesDefaultConfig = {
attempts: 3,
baseDelayMs: 1000,
};
export const retrieveCustomer =
async (): Promise<HttpTypes.StoreCustomer | null> => {
@@ -268,11 +274,21 @@ export const updateCustomerAddress = async (
};
async function medusaLogin(email: string, password: string) {
const token = await sdk.auth.login('customer', 'emailpass', {
email,
password,
const token = await withRetries(async () => {
const loginToken = await sdk.auth.login('customer', 'emailpass', {
email,
password,
});
if (typeof loginToken !== 'string') {
throw new Error('Failed to login Medusa account');
}
return loginToken;
}, {
...MedusaApiRetriesDefaultConfig,
label: 'medusa.auth.login',
});
await setAuthToken(token as string);
await setAuthToken(token);
try {
await transferCart();
@@ -303,20 +319,38 @@ async function medusaRegister({
`Creating new Medusa account for Keycloak user with email=${email}`,
);
const registerToken = await sdk.auth.register('customer', 'emailpass', {
email,
password,
});
const registerToken = await withRetries(
async () => {
const token = await sdk.auth.register('customer', 'emailpass', {
email,
password,
});
if (typeof token !== 'string') {
throw new Error('Failed to register Medusa account');
}
return token;
},
{
...MedusaApiRetriesDefaultConfig,
label: 'medusa.auth.register',
},
);
await setAuthToken(registerToken);
console.info(
`Creating new Medusa customer profile for Keycloak user with email=${email} and name=${name} and lastName=${lastName}`,
);
await sdk.store.customer.create(
{ email, first_name: name, last_name: lastName },
{},
await withRetries(
async () => {
await sdk.store.customer.create(
{ email, first_name: name, last_name: lastName },
{},
{ ...(await getAuthHeaders()) },
);
},
{
...(await getAuthHeaders()),
...MedusaApiRetriesDefaultConfig,
label: 'medusa.customer.create',
},
);
}

View File

@@ -8,3 +8,4 @@ export * from './money';
export * from './product';
export * from './repeat';
export * from './sort-products';
export * from './with-retries';

View File

@@ -0,0 +1,45 @@
type RetryOptions = {
attempts?: number;
baseDelayMs?: number;
label?: string;
};
function sleep(delay: number) {
return new Promise((resolve) => setTimeout(resolve, delay));
}
export async function withRetries<T>(
operation: () => Promise<T>,
{ attempts = 3, baseDelayMs = 500, label }: RetryOptions = {}
): Promise<T> {
let lastError: unknown;
for (let attempt = 1; attempt <= attempts; attempt++) {
try {
return await operation();
} catch (error) {
lastError = error;
if (attempt === attempts) {
break;
}
const delay = baseDelayMs * 2 ** (attempt - 1);
if (label) {
console.warn(
`Retrying ${label}, attempt ${attempt + 1}/${attempts} in ${delay}ms`,
error
);
}
if (delay > 0) {
await sleep(delay);
}
}
}
throw lastError instanceof Error
? lastError
: new Error("Operation failed after retries");
}

View File

@@ -16,6 +16,7 @@ import {
getDoctorAccounts,
getUserContactAdmin,
} from '~/lib/services/account.service';
import { getAnalysisResponseAdmin } from '~/lib/services/analysis-response.service';
import {
NotificationAction,
createNotificationLog,
@@ -74,14 +75,14 @@ class OrderWebhooksService {
}
logger.info(ctx, 'Status change processed. No notifications to send.');
} catch (e: any) {
} catch (e) {
if (actions.length)
await Promise.all(
actions.map((action) =>
createNotificationLog({
action,
status: 'FAIL',
comment: e?.message,
comment: e instanceof Error ? e.message : 'Unknown error',
relatedRecordId: analysisOrder.id,
}),
),
@@ -201,11 +202,11 @@ class OrderWebhooksService {
status: 'SUCCESS',
relatedRecordId: orderCart.order_id,
});
} catch (e: any) {
} catch (e) {
createNotificationLog({
action: NotificationAction.TTO_ORDER_CONFIRMATION,
status: 'FAIL',
comment: e?.message,
comment: e instanceof Error ? e.message : 'Unknown error',
relatedRecordId: ttoReservation.id,
});
logger.error(
@@ -345,10 +346,12 @@ class OrderWebhooksService {
.map(({ email }) => email)
.filter((email): email is string => !!email);
const analysisResponse = await getAnalysisResponseAdmin(analysisOrder.id);
await sendEmailFromTemplate(
renderFirstResultsReceivedEmail,
{
analysisOrderId: analysisOrder.id,
analysisResponseId: analysisResponse.id,
language: 'et',
},
doctorEmails,
@@ -380,10 +383,12 @@ class OrderWebhooksService {
return;
}
const analysisResponse = await getAnalysisResponseAdmin(analysisOrder.id);
await sendEmailFromTemplate(
renderAllResultsReceivedEmail,
{
analysisOrderId: analysisOrder.id,
analysisResponseId: analysisResponse.id,
language: 'et',
},
assignedDoctorEmail,

View File

@@ -507,11 +507,24 @@ class UserAnalysesApi {
if (!analysisOrderId) {
return;
}
const { data, error: userError } = await this.client.auth.getUser();
if (userError) {
throw userError;
const analysisOrder = await this.getAnalysisOrder({ analysisOrderId });
const userId = analysisOrder.user_id;
const { data: account } = await this.client
.schema('medreport')
.from('accounts')
.select('id')
.eq('primary_owner_user_id', userId)
.maybeSingle()
.throwOnError();
const accountId = account?.id;
if (!accountId) {
console.warn(
`Order ${analysisOrderId} got new responses but no account found for user_id=${userId}. Skipping notification. AnalysisOrder=${JSON.stringify(analysisOrder)}`,
);
return;
}
const { user } = data;
const notificationsApi = createNotificationsApi(this.client);
const { t } = await createI18nServerInstance();
@@ -521,7 +534,7 @@ class UserAnalysesApi {
if (hasFullAnalysisResponse || hasPartialAnalysisResponse) {
await notificationsApi.createNotification({
account_id: user.id,
account_id: accountId,
body: t('analysis-results:notification.body'),
link: `${pathsConfig.app.analysisResults}/${analysisOrderId}`,
});

View File

@@ -12,7 +12,7 @@ export function ButtonTooltip({
content,
className,
}: {
content?: string;
content?: string | React.ReactNode;
className?: string;
}) {
if (!content) return null;

View File

@@ -130,3 +130,4 @@ export type MedipostOrderResponse = IMedipostResponseXMLBase & {
};
};
};
export type MedipostAnalysisResult = MedipostOrderResponse['Saadetis']['Vastus'];

View File

@@ -65,7 +65,9 @@ export function useAuthChangeListener({
return;
}
window.location.reload();
// Redirect to home instead of reloading to avoid state mismatch errors
// during the transition
window.location.assign('/');
}
});

View File

@@ -24,5 +24,6 @@
"view": "View results",
"notification": {
"body": "You have new analysis results"
}
},
"orderCreatedAt": "Order created at: {{createdAt}}"
}

View File

@@ -24,5 +24,6 @@
"view": "Vaata tulemusi",
"notification": {
"body": "Teil on valmis uued analüüsi tulemused"
}
},
"orderCreatedAt": "Tellimus loodud: {{createdAt}}"
}

View File

@@ -23,5 +23,6 @@
"orderTitle": "Заказ {{orderNumber}}",
"notification": {
"body": "Teil on valmis uued analüüsi tulemused"
}
},
"orderCreatedAt": "Заказ создан: {{createdAt}}"
}