Merge pull request #80 from MR-medreport/keycloak-improvements-0909
some small fixes/improvements for update-account-form, medipost-xml, analysis packages
This commit is contained in:
@@ -37,7 +37,7 @@ async function UpdateAccount() {
|
||||
}
|
||||
return account?.email ?? user?.email ?? '';
|
||||
})(),
|
||||
phone: account?.phone ?? '',
|
||||
phone: account?.phone ?? '+372',
|
||||
city: account?.city ?? '',
|
||||
weight: account?.accountParams?.weight ?? 0,
|
||||
height: account?.accountParams?.height ?? 0,
|
||||
|
||||
@@ -112,7 +112,6 @@ export async function processMontonioCallback(orderToken: string) {
|
||||
throw new Error("Cart not found");
|
||||
}
|
||||
|
||||
|
||||
const medusaOrder = await placeOrder(cartId, { revalidateCacheTags: false });
|
||||
const orderedAnalysisElements = await getOrderedAnalysisIds({ medusaOrder });
|
||||
const orderId = await createOrder({ medusaOrder, orderedAnalysisElements });
|
||||
|
||||
@@ -128,7 +128,7 @@ const ComparePackagesModal = async ({
|
||||
|
||||
return (
|
||||
<TableRow key={id}>
|
||||
<TableCell className="py-6">
|
||||
<TableCell className="py-6 sm:max-w-[30vw]">
|
||||
{title}{' '}
|
||||
{description && (<InfoTooltip content={description} icon={<QuestionMarkCircledIcon />} />)}
|
||||
</TableCell>
|
||||
|
||||
@@ -16,7 +16,6 @@ import {
|
||||
} from 'lucide-react';
|
||||
|
||||
import { pathsConfig } from '@kit/shared/config';
|
||||
import { getPersonParameters } from '@kit/shared/utils';
|
||||
import { Button } from '@kit/ui/button';
|
||||
import {
|
||||
Card,
|
||||
@@ -30,7 +29,7 @@ import { cn } from '@kit/ui/utils';
|
||||
|
||||
import { isNil } from 'lodash';
|
||||
import { BmiCategory } from '~/lib/types/bmi';
|
||||
import {
|
||||
import PersonalCode, {
|
||||
bmiFromMetric,
|
||||
getBmiBackgroundColor,
|
||||
getBmiStatus,
|
||||
@@ -60,7 +59,7 @@ const cards = ({
|
||||
}) => [
|
||||
{
|
||||
title: 'dashboard:gender',
|
||||
description: gender ?? 'dashboard:male',
|
||||
description: gender ?? '-',
|
||||
icon: <User />,
|
||||
iconBg: 'bg-success',
|
||||
},
|
||||
@@ -145,21 +144,19 @@ export default function Dashboard({
|
||||
'id'
|
||||
>[];
|
||||
}) {
|
||||
const params = getPersonParameters(account.personal_code!);
|
||||
const bmiStatus = getBmiStatus(bmiThresholds, {
|
||||
age: params?.age || 0,
|
||||
height: account.accountParams?.height || 0,
|
||||
weight: account.accountParams?.weight || 0,
|
||||
});
|
||||
const height = account.accountParams?.height || 0;
|
||||
const weight = account.accountParams?.weight || 0;
|
||||
const { age = 0, gender } = PersonalCode.parsePersonalCode(account.personal_code!);
|
||||
const bmiStatus = getBmiStatus(bmiThresholds, { age, height, weight });
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="xs:grid-cols-2 grid auto-rows-fr gap-3 sm:grid-cols-4 lg:grid-cols-5">
|
||||
{cards({
|
||||
gender: params?.gender,
|
||||
age: params?.age,
|
||||
height: account.accountParams?.height,
|
||||
weight: account.accountParams?.weight,
|
||||
gender: gender.label,
|
||||
age,
|
||||
height,
|
||||
weight,
|
||||
bmiStatus,
|
||||
smoking: account.accountParams?.isSmoker,
|
||||
}).map(
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { cache } from 'react';
|
||||
import Isikukood, { Gender } from 'isikukood';
|
||||
|
||||
import { listProductTypes, listProducts } from "@lib/data/products";
|
||||
import { listRegions } from '@lib/data/regions';
|
||||
@@ -8,6 +7,7 @@ import type { StoreProduct } from '@medusajs/types';
|
||||
import { loadCurrentUserAccount } from './load-user-account';
|
||||
import { AccountWithParams } from '@/packages/features/accounts/src/server/api';
|
||||
import { AnalysisPackageWithVariant } from '@kit/shared/components/select-analysis-package';
|
||||
import PersonalCode from '~/lib/utils';
|
||||
|
||||
async function countryCodesLoader() {
|
||||
const countryCodes = await listRegions().then((regions) =>
|
||||
@@ -32,27 +32,8 @@ function userSpecificVariantLoader({
|
||||
if (!personalCode) {
|
||||
throw new Error('Personal code not found');
|
||||
}
|
||||
const parsed = new Isikukood(personalCode);
|
||||
const ageRange = (() => {
|
||||
const age = parsed.getAge();
|
||||
if (age >= 18 && age <= 29) {
|
||||
return '18-29';
|
||||
}
|
||||
if (age >= 30 && age <= 39) {
|
||||
return '30-39';
|
||||
}
|
||||
if (age >= 40 && age <= 49) {
|
||||
return '40-49';
|
||||
}
|
||||
if (age >= 50 && age <= 59) {
|
||||
return '50-59';
|
||||
}
|
||||
if (age >= 60) {
|
||||
return '60';
|
||||
}
|
||||
throw new Error('Age range not supported');
|
||||
})();
|
||||
const gender = parsed.getGender() === Gender.MALE ? 'M' : 'F';
|
||||
|
||||
const { ageRange, gender: { value: gender } } = PersonalCode.parsePersonalCode(personalCode);
|
||||
|
||||
return ({
|
||||
product,
|
||||
@@ -89,6 +70,7 @@ async function analysisPackageElementsLoader({
|
||||
queryParams: {
|
||||
id: analysisElementMedusaProductIds,
|
||||
limit: 100,
|
||||
order: "title",
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -31,11 +31,11 @@ export const getAccountHealthDetailsFields = (
|
||||
>[],
|
||||
members: Database['medreport']['Functions']['get_account_members']['Returns'],
|
||||
): AccountHealthDetailsField[] => {
|
||||
const avarageWeight =
|
||||
const averageWeight =
|
||||
memberParams.reduce((sum, r) => sum + r.weight!, 0) / memberParams.length;
|
||||
const avarageHeight =
|
||||
const averageHeight =
|
||||
memberParams.reduce((sum, r) => sum + r.height!, 0) / memberParams.length;
|
||||
const avarageAge =
|
||||
const averageAge =
|
||||
members.reduce((sum, r) => {
|
||||
const person = new Isikukood(r.personal_code);
|
||||
return sum + person.getAge();
|
||||
@@ -48,11 +48,11 @@ export const getAccountHealthDetailsFields = (
|
||||
const person = new Isikukood(r.personal_code);
|
||||
return person.getGender() === 'female';
|
||||
}).length;
|
||||
const averageBMI = bmiFromMetric(avarageWeight, avarageHeight);
|
||||
const averageBMI = bmiFromMetric(averageWeight, averageHeight);
|
||||
const bmiStatus = getBmiStatus(bmiThresholds, {
|
||||
age: avarageAge,
|
||||
height: avarageHeight,
|
||||
weight: avarageWeight,
|
||||
age: averageAge,
|
||||
height: averageHeight,
|
||||
weight: averageWeight,
|
||||
});
|
||||
const malePercentage = members.length
|
||||
? (numberOfMaleMembers / members.length) * 100
|
||||
@@ -76,7 +76,7 @@ export const getAccountHealthDetailsFields = (
|
||||
},
|
||||
{
|
||||
title: 'teams:healthDetails.avgAge',
|
||||
value: avarageAge.toFixed(0),
|
||||
value: averageAge.toFixed(0),
|
||||
Icon: Clock,
|
||||
iconBg: 'bg-success',
|
||||
},
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
export const DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||
export const DATE_FORMAT = "yyyy-mm-dd";
|
||||
export const DATE_FORMAT = "yyyy-MM-dd";
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { Tables } from '@/packages/supabase/src/database.types';
|
||||
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||
import type { IUuringElement } from "./medipost.types";
|
||||
|
||||
type AnalysesWithGroupsAndElements = ({
|
||||
export type AnalysesWithGroupsAndElements = ({
|
||||
analysis_elements: Tables<{ schema: 'medreport' }, 'analysis_elements'> & {
|
||||
analysis_groups: Tables<{ schema: 'medreport' }, 'analysis_groups'>;
|
||||
};
|
||||
@@ -105,7 +105,13 @@ export const createMedusaSyncSuccessEntry = async () => {
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAnalyses({ ids, originalIds }: { ids?: number[], originalIds?: string[] }): Promise<AnalysesWithGroupsAndElements> {
|
||||
export async function getAnalyses({
|
||||
ids,
|
||||
originalIds,
|
||||
}: {
|
||||
ids?: number[];
|
||||
originalIds?: string[];
|
||||
}): Promise<AnalysesWithGroupsAndElements> {
|
||||
const query = getSupabaseServerAdminClient()
|
||||
.schema('medreport')
|
||||
.from('analyses')
|
||||
|
||||
@@ -5,23 +5,11 @@ import {
|
||||
createClient as createCustomClient,
|
||||
} from '@supabase/supabase-js';
|
||||
|
||||
import {
|
||||
getAnalysisGroup,
|
||||
getClientInstitution,
|
||||
getClientPerson,
|
||||
getConfidentiality,
|
||||
getOrderEnteredPerson,
|
||||
getPais,
|
||||
getPatient,
|
||||
getProviderInstitution,
|
||||
getSpecimen,
|
||||
} from '@/lib/templates/medipost-order';
|
||||
import { SyncStatus } from '@/lib/types/audit';
|
||||
import {
|
||||
AnalysisOrderStatus,
|
||||
GetMessageListResponse,
|
||||
IMedipostResponseXMLBase,
|
||||
MaterjalideGrupp,
|
||||
MedipostAction,
|
||||
MedipostOrderResponse,
|
||||
MedipostPublicMessageResponse,
|
||||
@@ -32,7 +20,6 @@ import {
|
||||
import { toArray } from '@/lib/utils';
|
||||
import axios from 'axios';
|
||||
import { XMLParser } from 'fast-xml-parser';
|
||||
import { uniqBy } from 'lodash';
|
||||
|
||||
import { Tables } from '@kit/supabase/database';
|
||||
import { createAnalysisGroup } from './analysis-group.service';
|
||||
@@ -47,6 +34,7 @@ import { listRegions } from '@lib/data/regions';
|
||||
import { getAnalysisElementMedusaProductIds } from '@/utils/medusa-product';
|
||||
import { MedipostValidationError } from './medipost/MedipostValidationError';
|
||||
import { logMedipostDispatch } from './audit.service';
|
||||
import { composeOrderXML, OrderedAnalysisElement } from './medipostXML.service';
|
||||
|
||||
const BASE_URL = process.env.MEDIPOST_URL!;
|
||||
const USER = process.env.MEDIPOST_USER!;
|
||||
@@ -451,122 +439,6 @@ export async function syncPublicMessage(
|
||||
}
|
||||
}
|
||||
|
||||
export async function composeOrderXML({
|
||||
person,
|
||||
orderedAnalysisElementsIds,
|
||||
orderedAnalysesIds,
|
||||
orderId,
|
||||
orderCreatedAt,
|
||||
comment,
|
||||
}: {
|
||||
person: {
|
||||
idCode: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
phone: string;
|
||||
};
|
||||
orderedAnalysisElementsIds: number[];
|
||||
orderedAnalysesIds: number[];
|
||||
orderId: string;
|
||||
orderCreatedAt: Date;
|
||||
comment?: string;
|
||||
}) {
|
||||
const analysisElements = await getAnalysisElementsAdmin({ ids: orderedAnalysisElementsIds });
|
||||
if (analysisElements.length !== orderedAnalysisElementsIds.length) {
|
||||
throw new Error(`Got ${analysisElements.length} analysis elements, expected ${orderedAnalysisElementsIds.length}`);
|
||||
}
|
||||
|
||||
const analyses = await getAnalyses({ ids: orderedAnalysesIds });
|
||||
if (analyses.length !== orderedAnalysesIds.length) {
|
||||
throw new Error(`Got ${analyses.length} analyses, expected ${orderedAnalysesIds.length}`);
|
||||
}
|
||||
|
||||
const analysisGroups: Tables<{ schema: 'medreport' }, 'analysis_groups'>[] =
|
||||
uniqBy(
|
||||
(
|
||||
analysisElements?.flatMap(({ analysis_groups }) => analysis_groups) ??
|
||||
[]
|
||||
).concat(
|
||||
analyses?.flatMap(
|
||||
({ analysis_elements }) => analysis_elements.analysis_groups,
|
||||
) ?? [],
|
||||
),
|
||||
'id',
|
||||
);
|
||||
|
||||
const specimenSection = [];
|
||||
const analysisSection = [];
|
||||
let order = 1;
|
||||
for (const currentGroup of analysisGroups) {
|
||||
let relatedAnalysisElement = analysisElements?.find(
|
||||
(element) => element.analysis_groups.id === currentGroup.id,
|
||||
);
|
||||
const relatedAnalyses = analyses?.filter((analysis) => {
|
||||
return analysis.analysis_elements.analysis_groups.id === currentGroup.id;
|
||||
});
|
||||
|
||||
if (!relatedAnalysisElement) {
|
||||
relatedAnalysisElement = relatedAnalyses?.find(
|
||||
(relatedAnalysis) =>
|
||||
relatedAnalysis.analysis_elements.analysis_groups.id ===
|
||||
currentGroup.id,
|
||||
)?.analysis_elements;
|
||||
}
|
||||
|
||||
if (!relatedAnalysisElement || !relatedAnalysisElement.material_groups) {
|
||||
throw new Error(
|
||||
`Failed to find related analysis element for group ${currentGroup.name} (id: ${currentGroup.id})`,
|
||||
);
|
||||
}
|
||||
|
||||
for (const group of relatedAnalysisElement?.material_groups as MaterjalideGrupp[]) {
|
||||
const materials = toArray(group.Materjal);
|
||||
const specimenXml = materials.flatMap(
|
||||
({ MaterjaliNimi, MaterjaliTyyp, MaterjaliTyypOID, Konteiner }) => {
|
||||
return toArray(Konteiner).map((container) =>
|
||||
getSpecimen(
|
||||
MaterjaliTyypOID,
|
||||
MaterjaliTyyp,
|
||||
MaterjaliNimi,
|
||||
order,
|
||||
container.ProovinouKoodOID,
|
||||
container.ProovinouKood,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
specimenSection.push(...specimenXml);
|
||||
}
|
||||
|
||||
const groupXml = getAnalysisGroup(
|
||||
currentGroup.original_id,
|
||||
currentGroup.name,
|
||||
order,
|
||||
relatedAnalysisElement,
|
||||
);
|
||||
order++;
|
||||
analysisSection.push(groupXml);
|
||||
}
|
||||
|
||||
return `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Saadetis xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="TellimusLOINC.xsd">
|
||||
${getPais(USER, RECIPIENT, orderCreatedAt, orderId)}
|
||||
<Tellimus cito="EI">
|
||||
<ValisTellimuseId>${orderId}</ValisTellimuseId>
|
||||
${getClientInstitution()}
|
||||
${getProviderInstitution()}
|
||||
${getClientPerson()}
|
||||
${getOrderEnteredPerson()}
|
||||
<TellijaMarkused>${comment ?? ''}</TellijaMarkused>
|
||||
${getPatient(person)}
|
||||
${getConfidentiality()}
|
||||
${specimenSection.join('')}
|
||||
${analysisSection?.join('')}
|
||||
</Tellimus>
|
||||
</Saadetis>`;
|
||||
}
|
||||
|
||||
function getLatestMessage({
|
||||
messages,
|
||||
excludedMessageIds,
|
||||
@@ -694,7 +566,7 @@ async function syncPrivateMessage({
|
||||
);
|
||||
}
|
||||
|
||||
const { data: allOrderResponseElements} = await supabase
|
||||
const { data: allOrderResponseElements } = await supabase
|
||||
.schema('medreport')
|
||||
.from('analysis_response_elements')
|
||||
.select('*')
|
||||
@@ -714,20 +586,36 @@ export async function sendOrderToMedipost({
|
||||
orderedAnalysisElements,
|
||||
}: {
|
||||
medusaOrderId: string;
|
||||
orderedAnalysisElements: { analysisElementId?: number; analysisId?: number }[];
|
||||
orderedAnalysisElements: OrderedAnalysisElement[];
|
||||
}) {
|
||||
const medreportOrder = await getOrder({ medusaOrderId });
|
||||
const account = await getAccountAdmin({ primaryOwnerUserId: medreportOrder.user_id });
|
||||
|
||||
const orderedAnalysesIds = orderedAnalysisElements
|
||||
.map(({ analysisId }) => analysisId)
|
||||
.filter(Boolean) as number[];
|
||||
const orderedAnalysisElementsIds = orderedAnalysisElements
|
||||
.map(({ analysisElementId }) => analysisElementId)
|
||||
.filter(Boolean) as number[];
|
||||
|
||||
const analyses = await getAnalyses({ ids: orderedAnalysesIds });
|
||||
if (analyses.length !== orderedAnalysesIds.length) {
|
||||
throw new Error(`Got ${analyses.length} analyses, expected ${orderedAnalysesIds.length}`);
|
||||
}
|
||||
const analysisElements = await getAnalysisElementsAdmin({ ids: orderedAnalysisElementsIds });
|
||||
if (analysisElements.length !== orderedAnalysisElementsIds.length) {
|
||||
throw new Error(`Got ${analysisElements.length} analysis elements, expected ${orderedAnalysisElementsIds.length}`);
|
||||
}
|
||||
|
||||
const orderXml = await composeOrderXML({
|
||||
analyses,
|
||||
analysisElements,
|
||||
person: {
|
||||
idCode: account.personal_code!,
|
||||
firstName: account.name ?? '',
|
||||
lastName: account.last_name ?? '',
|
||||
phone: account.phone ?? '',
|
||||
},
|
||||
orderedAnalysisElementsIds: orderedAnalysisElements.map(({ analysisElementId }) => analysisElementId).filter(Boolean) as number[],
|
||||
orderedAnalysesIds: orderedAnalysisElements.map(({ analysisId }) => analysisId).filter(Boolean) as number[],
|
||||
orderId: medusaOrderId,
|
||||
orderCreatedAt: new Date(medreportOrder.created_at),
|
||||
comment: '',
|
||||
@@ -826,7 +714,12 @@ export async function getOrderedAnalysisIds({
|
||||
throw new Error(`Got ${orderedPackagesProducts.length} ordered packages products, expected ${orderedPackageIds.length}`);
|
||||
}
|
||||
|
||||
const ids = getAnalysisElementMedusaProductIds(orderedPackagesProducts);
|
||||
const ids = getAnalysisElementMedusaProductIds(
|
||||
orderedPackagesProducts.map(({ id, metadata }) => ({
|
||||
metadata,
|
||||
variant: orderedPackages.find(({ product }) => product?.id === id)?.variant,
|
||||
})),
|
||||
);
|
||||
if (ids.length === 0) {
|
||||
return [];
|
||||
}
|
||||
@@ -867,10 +760,10 @@ export async function createMedipostActionLog({
|
||||
hasError = false,
|
||||
}: {
|
||||
action:
|
||||
| 'send_order_to_medipost'
|
||||
| 'sync_analysis_results_from_medipost'
|
||||
| 'send_fake_analysis_results_to_medipost'
|
||||
| 'send_analysis_results_to_medipost';
|
||||
| 'send_order_to_medipost'
|
||||
| 'sync_analysis_results_from_medipost'
|
||||
| 'send_fake_analysis_results_to_medipost'
|
||||
| 'send_analysis_results_to_medipost';
|
||||
xml: string;
|
||||
hasAnalysisResults?: boolean;
|
||||
medusaOrderId?: string | null;
|
||||
|
||||
201
lib/services/medipostXML.service.ts
Normal file
201
lib/services/medipostXML.service.ts
Normal file
@@ -0,0 +1,201 @@
|
||||
'use server';
|
||||
|
||||
import {
|
||||
getAnalysisGroup,
|
||||
getClientInstitution,
|
||||
getClientPerson,
|
||||
getConfidentiality,
|
||||
getOrderEnteredPerson,
|
||||
getPais,
|
||||
getPatient,
|
||||
getProviderInstitution,
|
||||
getSpecimen,
|
||||
} from '@/lib/templates/medipost-order';
|
||||
import {
|
||||
MaterjalideGrupp,
|
||||
} from '@/lib/types/medipost';
|
||||
import { toArray } from '@/lib/utils';
|
||||
import { uniqBy } from 'lodash';
|
||||
|
||||
import { Tables } from '@kit/supabase/database';
|
||||
import { AnalysisElement } from './analysis-element.service';
|
||||
import { AnalysesWithGroupsAndElements } from './analyses.service';
|
||||
|
||||
const USER = process.env.MEDIPOST_USER!;
|
||||
const RECIPIENT = process.env.MEDIPOST_RECIPIENT!;
|
||||
|
||||
export type OrderedAnalysisElement = {
|
||||
analysisElementId?: number;
|
||||
analysisId?: number;
|
||||
}
|
||||
|
||||
export async function composeOrderXML({
|
||||
analyses,
|
||||
analysisElements,
|
||||
person,
|
||||
orderId,
|
||||
orderCreatedAt,
|
||||
comment,
|
||||
}: {
|
||||
analyses: AnalysesWithGroupsAndElements;
|
||||
analysisElements: AnalysisElement[];
|
||||
person: {
|
||||
idCode: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
phone: string;
|
||||
};
|
||||
orderId: string;
|
||||
orderCreatedAt: Date;
|
||||
comment?: string;
|
||||
}) {
|
||||
const analysisGroups: Tables<{ schema: 'medreport' }, 'analysis_groups'>[] =
|
||||
uniqBy(
|
||||
(
|
||||
analysisElements?.flatMap(({ analysis_groups }) => analysis_groups) ??
|
||||
[]
|
||||
).concat(
|
||||
analyses?.flatMap(
|
||||
({ analysis_elements }) => analysis_elements.analysis_groups,
|
||||
) ?? [],
|
||||
),
|
||||
'id',
|
||||
);
|
||||
|
||||
// First, collect all unique materials across all analysis groups
|
||||
const uniqueMaterials = new Map<string, {
|
||||
MaterjaliTyypOID: string;
|
||||
MaterjaliTyyp: string;
|
||||
MaterjaliNimi: string;
|
||||
ProovinouKoodOID?: string;
|
||||
ProovinouKood?: string;
|
||||
order: number;
|
||||
}>();
|
||||
|
||||
let specimenOrder = 1;
|
||||
|
||||
// Collect all materials from all analysis groups
|
||||
for (const currentGroup of analysisGroups) {
|
||||
let relatedAnalysisElement = analysisElements?.find(
|
||||
(element) => element.analysis_groups.id === currentGroup.id,
|
||||
);
|
||||
const relatedAnalyses = analyses?.filter((analysis) => {
|
||||
return analysis.analysis_elements.analysis_groups.id === currentGroup.id;
|
||||
});
|
||||
|
||||
if (!relatedAnalysisElement) {
|
||||
relatedAnalysisElement = relatedAnalyses?.find(
|
||||
(relatedAnalysis) =>
|
||||
relatedAnalysis.analysis_elements.analysis_groups.id ===
|
||||
currentGroup.id,
|
||||
)?.analysis_elements;
|
||||
}
|
||||
|
||||
if (!relatedAnalysisElement || !relatedAnalysisElement.material_groups) {
|
||||
throw new Error(
|
||||
`Failed to find related analysis element for group ${currentGroup.name} (id: ${currentGroup.id})`,
|
||||
);
|
||||
}
|
||||
|
||||
for (const group of relatedAnalysisElement?.material_groups as MaterjalideGrupp[]) {
|
||||
const materials = toArray(group.Materjal);
|
||||
for (const material of materials) {
|
||||
const { MaterjaliNimi, MaterjaliTyyp, MaterjaliTyypOID, Konteiner } = material;
|
||||
const containers = toArray(Konteiner);
|
||||
|
||||
for (const container of containers) {
|
||||
// Use MaterialTyyp as the key for deduplication
|
||||
const materialKey = MaterjaliTyyp;
|
||||
|
||||
if (!uniqueMaterials.has(materialKey)) {
|
||||
uniqueMaterials.set(materialKey, {
|
||||
MaterjaliTyypOID,
|
||||
MaterjaliTyyp,
|
||||
MaterjaliNimi,
|
||||
ProovinouKoodOID: container.ProovinouKoodOID,
|
||||
ProovinouKood: container.ProovinouKood,
|
||||
order: specimenOrder++,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generate specimen section from unique materials
|
||||
const specimenSection = Array.from(uniqueMaterials.values()).map(material =>
|
||||
getSpecimen(
|
||||
material.MaterjaliTyypOID,
|
||||
material.MaterjaliTyyp,
|
||||
material.MaterjaliNimi,
|
||||
material.order,
|
||||
material.ProovinouKoodOID,
|
||||
material.ProovinouKood,
|
||||
)
|
||||
);
|
||||
|
||||
// Generate analysis section with correct specimen references
|
||||
const analysisSection = [];
|
||||
for (const currentGroup of analysisGroups) {
|
||||
let relatedAnalysisElement = analysisElements?.find(
|
||||
(element) => element.analysis_groups.id === currentGroup.id,
|
||||
);
|
||||
const relatedAnalyses = analyses?.filter((analysis) => {
|
||||
return analysis.analysis_elements.analysis_groups.id === currentGroup.id;
|
||||
});
|
||||
|
||||
if (!relatedAnalysisElement) {
|
||||
relatedAnalysisElement = relatedAnalyses?.find(
|
||||
(relatedAnalysis) =>
|
||||
relatedAnalysis.analysis_elements.analysis_groups.id ===
|
||||
currentGroup.id,
|
||||
)?.analysis_elements;
|
||||
}
|
||||
|
||||
if (!relatedAnalysisElement || !relatedAnalysisElement.material_groups) {
|
||||
throw new Error(
|
||||
`Failed to find related analysis element for group ${currentGroup.name} (id: ${currentGroup.id})`,
|
||||
);
|
||||
}
|
||||
|
||||
// Find the specimen order number for this analysis group
|
||||
let specimenOrderNumber = 1;
|
||||
for (const group of relatedAnalysisElement?.material_groups as MaterjalideGrupp[]) {
|
||||
const materials = toArray(group.Materjal);
|
||||
for (const material of materials) {
|
||||
const materialKey = material.MaterjaliTyyp;
|
||||
const uniqueMaterial = uniqueMaterials.get(materialKey);
|
||||
if (uniqueMaterial) {
|
||||
specimenOrderNumber = uniqueMaterial.order;
|
||||
break; // Use the first material's order number
|
||||
}
|
||||
}
|
||||
if (specimenOrderNumber > 1) break; // Found a specimen, use it
|
||||
}
|
||||
|
||||
const groupXml = getAnalysisGroup(
|
||||
currentGroup.original_id,
|
||||
currentGroup.name,
|
||||
specimenOrderNumber,
|
||||
relatedAnalysisElement,
|
||||
);
|
||||
analysisSection.push(groupXml);
|
||||
}
|
||||
|
||||
return `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Saadetis xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="TellimusLOINC.xsd">
|
||||
${getPais(USER, RECIPIENT, orderCreatedAt, orderId)}
|
||||
<Tellimus cito="EI">
|
||||
<ValisTellimuseId>${orderId}</ValisTellimuseId>
|
||||
${getClientInstitution()}
|
||||
${getProviderInstitution()}
|
||||
${getClientPerson()}
|
||||
${getOrderEnteredPerson()}
|
||||
<TellijaMarkused>${comment ?? ''}</TellijaMarkused>
|
||||
${getPatient(person)}
|
||||
${getConfidentiality()}
|
||||
${specimenSection.join('')}
|
||||
${analysisSection?.join('')}
|
||||
</Tellimus>
|
||||
</Saadetis>`;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { format } from 'date-fns';
|
||||
import Isikukood, { Gender } from 'isikukood';
|
||||
import { Tables } from '@/packages/supabase/src/database.types';
|
||||
import { DATE_FORMAT, DATE_TIME_FORMAT } from '@/lib/constants';
|
||||
import PersonalCode from '../utils';
|
||||
|
||||
const isProd = process.env.NODE_ENV === 'production';
|
||||
|
||||
@@ -73,15 +73,15 @@ export const getPatient = ({
|
||||
lastName: string,
|
||||
firstName: string,
|
||||
}) => {
|
||||
const isikukood = new Isikukood(idCode);
|
||||
const { dob, gender } = PersonalCode.parsePersonalCode(idCode);
|
||||
return `<Patsient>
|
||||
<IsikukoodiOID>1.3.6.1.4.1.28284.6.2.2.1</IsikukoodiOID>
|
||||
<Isikukood>${idCode}</Isikukood>
|
||||
<PerekonnaNimi>${lastName}</PerekonnaNimi>
|
||||
<EesNimi>${firstName}</EesNimi>
|
||||
<SynniAeg>${format(isikukood.getBirthday(), DATE_FORMAT)}</SynniAeg>
|
||||
<SynniAeg>${format(dob, DATE_FORMAT)}</SynniAeg>
|
||||
<SuguOID>1.3.6.1.4.1.28284.6.2.3.16.2</SuguOID>
|
||||
<Sugu>${isikukood.getGender() === Gender.MALE ? 'M' : 'N'}</Sugu>
|
||||
<Sugu>${gender.value === 'M' ? 'M' : 'N'}</Sugu>
|
||||
</Patsient>`;
|
||||
};
|
||||
|
||||
|
||||
62
lib/utils.ts
62
lib/utils.ts
@@ -90,9 +90,61 @@ export function getBmiBackgroundColor(bmiStatus: BmiCategory | null): string {
|
||||
}
|
||||
}
|
||||
|
||||
export function getGenderStringFromPersonalCode(personalCode: string) {
|
||||
const person = new Isikukood(personalCode);
|
||||
if (person.getGender() === Gender.FEMALE) return 'common:female';
|
||||
if (person.getGender() === Gender.MALE) return 'common:male';
|
||||
return 'common:unknown';
|
||||
type AgeRange = '18-29' | '30-39' | '40-49' | '50-59' | '60';
|
||||
export default class PersonalCode {
|
||||
static getPersonalCode(personalCode: string | null) {
|
||||
if (!personalCode) {
|
||||
return null;
|
||||
}
|
||||
if (personalCode.toLowerCase().startsWith('ee')) {
|
||||
return personalCode.substring(2);
|
||||
}
|
||||
return personalCode;
|
||||
}
|
||||
|
||||
static parsePersonalCode(personalCode: string): {
|
||||
ageRange: AgeRange;
|
||||
gender: { label: string; value: string };
|
||||
dob: Date;
|
||||
age: number;
|
||||
} {
|
||||
const parsed = new Isikukood(personalCode);
|
||||
const ageRange = (() => {
|
||||
const age = parsed.getAge();
|
||||
if (age >= 18 && age <= 29) {
|
||||
return '18-29';
|
||||
}
|
||||
if (age >= 30 && age <= 39) {
|
||||
return '30-39';
|
||||
}
|
||||
if (age >= 40 && age <= 49) {
|
||||
return '40-49';
|
||||
}
|
||||
if (age >= 50 && age <= 59) {
|
||||
return '50-59';
|
||||
}
|
||||
if (age >= 60) {
|
||||
return '60';
|
||||
}
|
||||
throw new Error('Age range not supported');
|
||||
})();
|
||||
const gender = (() => {
|
||||
const gender = parsed.getGender();
|
||||
switch (gender) {
|
||||
case Gender.FEMALE:
|
||||
return { label: 'common:female', value: 'F' };
|
||||
case Gender.MALE:
|
||||
return { label: 'common:male', value: 'M' };
|
||||
default:
|
||||
throw new Error('Gender not supported');
|
||||
}
|
||||
})();
|
||||
|
||||
return {
|
||||
ageRange,
|
||||
gender,
|
||||
dob: parsed.getBirthday(),
|
||||
age: parsed.getAge(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { SupabaseClient } from '@supabase/supabase-js';
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
import { AnalysisResultDetails, UserAnalysis } from '../types/accounts';
|
||||
import PersonalCode from '~/lib/utils';
|
||||
|
||||
export type AccountWithParams =
|
||||
Database['medreport']['Tables']['accounts']['Row'] & {
|
||||
@@ -71,15 +72,7 @@ class AccountsApi {
|
||||
const { personal_code, ...rest } = data;
|
||||
return {
|
||||
...rest,
|
||||
personal_code: (() => {
|
||||
if (!personal_code) {
|
||||
return null;
|
||||
}
|
||||
if (personal_code.toLowerCase().startsWith('ee')) {
|
||||
return personal_code.substring(2);
|
||||
}
|
||||
return personal_code;
|
||||
})(),
|
||||
personal_code: PersonalCode.getPersonalCode(personal_code),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -68,6 +68,7 @@ class AuthApi {
|
||||
p_name: data.firstName,
|
||||
p_last_name: data.lastName,
|
||||
p_personal_code: data.personalCode,
|
||||
p_email: data.email || '',
|
||||
p_phone: data.phone || '',
|
||||
p_city: data.city || '',
|
||||
p_has_consent_personal_data: data.userConsent,
|
||||
|
||||
@@ -14,7 +14,12 @@ export const listProducts = async ({
|
||||
regionId,
|
||||
}: {
|
||||
pageParam?: number
|
||||
queryParams?: HttpTypes.FindParams & HttpTypes.StoreProductParams & { "type_id[0]"?: string; id?: string[], category_id?: string }
|
||||
queryParams?: HttpTypes.FindParams & HttpTypes.StoreProductParams & {
|
||||
"type_id[0]"?: string;
|
||||
id?: string[],
|
||||
category_id?: string;
|
||||
order?: 'title';
|
||||
}
|
||||
countryCode?: string
|
||||
regionId?: string
|
||||
}): Promise<{
|
||||
|
||||
@@ -1257,6 +1257,26 @@ export type Database = {
|
||||
},
|
||||
]
|
||||
}
|
||||
medipost_actions: {
|
||||
Row: {
|
||||
created_at: string
|
||||
id: number
|
||||
action: string
|
||||
xml: string
|
||||
has_analysis_results: boolean
|
||||
medusa_order_id: string
|
||||
response_xml: string
|
||||
has_error: boolean
|
||||
}
|
||||
Insert: {
|
||||
action: string
|
||||
xml: string
|
||||
has_analysis_results: boolean
|
||||
medusa_order_id: string
|
||||
response_xml: string
|
||||
has_error: boolean
|
||||
}
|
||||
}
|
||||
medreport_product_groups: {
|
||||
Row: {
|
||||
created_at: string
|
||||
@@ -2053,6 +2073,7 @@ export type Database = {
|
||||
p_personal_code: string
|
||||
p_phone: string
|
||||
p_uid: string
|
||||
p_email: string
|
||||
}
|
||||
Returns: undefined
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
CREATE OR REPLACE FUNCTION medreport.update_account(p_name character varying, p_last_name text, p_personal_code text, p_phone text, p_city text, p_has_consent_personal_data boolean, p_uid uuid, p_email character varying)
|
||||
RETURNS void
|
||||
LANGUAGE plpgsql
|
||||
AS $function$begin
|
||||
update medreport.accounts
|
||||
set name = coalesce(p_name, name),
|
||||
last_name = coalesce(p_last_name, last_name),
|
||||
personal_code = coalesce(p_personal_code, personal_code),
|
||||
phone = coalesce(p_phone, phone),
|
||||
city = coalesce(p_city, city),
|
||||
has_consent_personal_data = coalesce(p_has_consent_personal_data,
|
||||
has_consent_personal_data),
|
||||
email = coalesce(p_email, email)
|
||||
where id = p_uid;
|
||||
end;$function$
|
||||
;
|
||||
|
||||
grant
|
||||
execute on function medreport.update_account(
|
||||
p_name character varying,
|
||||
p_last_name text,
|
||||
p_personal_code text,
|
||||
p_phone text,
|
||||
p_city text,
|
||||
p_has_consent_personal_data boolean,
|
||||
p_uid uuid,
|
||||
p_email character varying) to authenticated,
|
||||
service_role;
|
||||
@@ -1,17 +1,27 @@
|
||||
export const getAnalysisElementMedusaProductIds = (products: ({
|
||||
import { StoreProduct } from "@medusajs/types";
|
||||
|
||||
type Product = {
|
||||
metadata?: {
|
||||
analysisElementMedusaProductIds?: string;
|
||||
} | null;
|
||||
} | null)[]) => {
|
||||
variant?: {
|
||||
metadata?: {
|
||||
analysisElementMedusaProductIds?: string;
|
||||
} | null;
|
||||
} | null;
|
||||
} | null;
|
||||
|
||||
export const getAnalysisElementMedusaProductIds = (products: Pick<StoreProduct, 'metadata'>[]) => {
|
||||
if (!products) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const mapped = products
|
||||
.flatMap((product) => {
|
||||
const value = product?.metadata?.analysisElementMedusaProductIds?.replaceAll("'", '"');
|
||||
const value = (product as Product)?.metadata?.analysisElementMedusaProductIds?.replaceAll("'", '"');
|
||||
const value_variant = (product as Product)?.variant?.metadata?.analysisElementMedusaProductIds?.replaceAll("'", '"');
|
||||
try {
|
||||
return JSON.parse(value as string);
|
||||
return [...JSON.parse(value as string), ...JSON.parse(value_variant as string)];
|
||||
} catch (e) {
|
||||
console.error("Failed to parse analysisElementMedusaProductIds from analysis package, possibly invalid format", e);
|
||||
return [];
|
||||
|
||||
Reference in New Issue
Block a user