B2B-85: add order xml generation (#8)
* B2B-85: add order xml generation * some fixes after merge --------- Co-authored-by: Helena <helena@Helenas-MacBook-Pro.local>
This commit is contained in:
@@ -1,16 +1,34 @@
|
||||
import {
|
||||
SupabaseClient,
|
||||
createClient as createCustomClient,
|
||||
} from '@supabase/supabase-js';
|
||||
|
||||
import {
|
||||
getAnalysisGroup,
|
||||
getClientInstitution,
|
||||
getClientPerson,
|
||||
getConfidentiality,
|
||||
getOrderEnteredByPerson,
|
||||
getPais,
|
||||
getPatient,
|
||||
getProviderInstitution,
|
||||
getSpecimen,
|
||||
} from '@/lib/templates/medipost-order';
|
||||
import { SyncStatus } from '@/lib/types/audit';
|
||||
import {
|
||||
AnalysisOrderStatus,
|
||||
GetMessageListResponse,
|
||||
MaterjalideGrupp,
|
||||
MedipostAction,
|
||||
MedipostPublicMessageResponse,
|
||||
Message,
|
||||
UuringuGrupp,
|
||||
} from "@/lib/types/medipost";
|
||||
import { Tables } from "@/supabase/database.types";
|
||||
import { createClient, SupabaseClient } from "@supabase/supabase-js";
|
||||
import axios from "axios";
|
||||
import { XMLParser } from "fast-xml-parser";
|
||||
import { SyncStatus } from "@/lib/types/audit";
|
||||
import { toArray } from "@/lib/utils";
|
||||
} from '@/lib/types/medipost';
|
||||
import { toArray } from '@/lib/utils';
|
||||
import { Tables } from '@/supabase/database.types';
|
||||
import axios from 'axios';
|
||||
import { XMLParser } from 'fast-xml-parser';
|
||||
import { uniqBy } from 'lodash';
|
||||
|
||||
const BASE_URL = process.env.MEDIPOST_URL!;
|
||||
const USER = process.env.MEDIPOST_USER!;
|
||||
@@ -37,14 +55,14 @@ export async function getLatestPublicMessageListItem() {
|
||||
Action: MedipostAction.GetPublicMessageList,
|
||||
User: USER,
|
||||
Password: PASSWORD,
|
||||
Sender: "syndev",
|
||||
Sender: 'syndev',
|
||||
// LastChecked (date+time) can be used here to get only messages since the last check - add when cron is created
|
||||
// MessageType check only for messages of certain type
|
||||
},
|
||||
});
|
||||
|
||||
if (data.code && data.code !== 0) {
|
||||
throw new Error("Failed to get public message list");
|
||||
throw new Error('Failed to get public message list');
|
||||
}
|
||||
|
||||
return getLatestMessage(data?.messages);
|
||||
@@ -59,7 +77,7 @@ export async function getPublicMessage(messageId: string) {
|
||||
MessageId: messageId,
|
||||
},
|
||||
headers: {
|
||||
Accept: "application/xml",
|
||||
Accept: 'application/xml',
|
||||
},
|
||||
});
|
||||
const parser = new XMLParser({ ignoreAttributes: false });
|
||||
@@ -74,16 +92,16 @@ export async function getPublicMessage(messageId: string) {
|
||||
|
||||
export async function sendPrivateMessage(messageXml: string, receiver: string) {
|
||||
const body = new FormData();
|
||||
body.append("Action", MedipostAction.SendPrivateMessage);
|
||||
body.append("User", USER);
|
||||
body.append("Password", PASSWORD);
|
||||
body.append("Receiver", receiver);
|
||||
body.append("MessageType", "Tellimus");
|
||||
body.append('Action', MedipostAction.SendPrivateMessage);
|
||||
body.append('User', USER);
|
||||
body.append('Password', PASSWORD);
|
||||
body.append('Receiver', receiver);
|
||||
body.append('MessageType', 'Tellimus');
|
||||
body.append(
|
||||
"Message",
|
||||
'Message',
|
||||
new Blob([messageXml], {
|
||||
type: "text/xml; charset=UTF-8",
|
||||
})
|
||||
type: 'text/xml; charset=UTF-8',
|
||||
}),
|
||||
);
|
||||
|
||||
const { data } = await axios.post(BASE_URL, body);
|
||||
@@ -103,7 +121,7 @@ export async function getLatestPrivateMessageListItem() {
|
||||
});
|
||||
|
||||
if (data.code && data.code !== 0) {
|
||||
throw new Error("Failed to get private message list");
|
||||
throw new Error('Failed to get private message list');
|
||||
}
|
||||
|
||||
return getLatestMessage(data?.messages);
|
||||
@@ -118,7 +136,7 @@ export async function getPrivateMessage(messageId: string) {
|
||||
MessageId: messageId,
|
||||
},
|
||||
headers: {
|
||||
Accept: "application/xml",
|
||||
Accept: 'application/xml',
|
||||
},
|
||||
});
|
||||
|
||||
@@ -154,7 +172,7 @@ export async function readPrivateMessageResponse() {
|
||||
}
|
||||
|
||||
const privateMessageContent = await getPrivateMessage(
|
||||
privateMessage.messageId
|
||||
privateMessage.messageId,
|
||||
);
|
||||
|
||||
if (privateMessageContent) {
|
||||
@@ -168,29 +186,29 @@ export async function readPrivateMessageResponse() {
|
||||
|
||||
async function saveAnalysisGroup(
|
||||
analysisGroup: UuringuGrupp,
|
||||
supabase: SupabaseClient
|
||||
supabase: SupabaseClient,
|
||||
) {
|
||||
const { data: insertedAnalysisGroup, error } = await supabase
|
||||
.from("analysis_groups")
|
||||
.from('analysis_groups')
|
||||
.upsert(
|
||||
{
|
||||
original_id: analysisGroup.UuringuGruppId,
|
||||
name: analysisGroup.UuringuGruppNimi,
|
||||
order: analysisGroup.UuringuGruppJarjekord,
|
||||
},
|
||||
{ onConflict: "original_id", ignoreDuplicates: false }
|
||||
{ onConflict: 'original_id', ignoreDuplicates: false },
|
||||
)
|
||||
.select("id");
|
||||
.select('id');
|
||||
|
||||
if (error || !insertedAnalysisGroup[0]?.id) {
|
||||
throw new Error(
|
||||
`Failed to insert analysis group (id: ${analysisGroup.UuringuGruppId}), error: ${error?.message}`
|
||||
`Failed to insert analysis group (id: ${analysisGroup.UuringuGruppId}), error: ${error?.message}`,
|
||||
);
|
||||
}
|
||||
const analysisGroupId = insertedAnalysisGroup[0].id;
|
||||
|
||||
const analysisGroupCodes = toArray(analysisGroup.Kood);
|
||||
const codes: Partial<Tables<"codes">>[] = analysisGroupCodes.map((kood) => ({
|
||||
const codes: Partial<Tables<'codes'>>[] = analysisGroupCodes.map((kood) => ({
|
||||
hk_code: kood.HkKood,
|
||||
hk_code_multiplier: kood.HkKoodiKordaja,
|
||||
coefficient: kood.Koefitsient,
|
||||
@@ -204,7 +222,7 @@ async function saveAnalysisGroup(
|
||||
const analysisElement = item.UuringuElement;
|
||||
|
||||
const { data: insertedAnalysisElement, error } = await supabase
|
||||
.from("analysis_elements")
|
||||
.from('analysis_elements')
|
||||
.upsert(
|
||||
{
|
||||
analysis_id_oid: analysisElement.UuringIdOID,
|
||||
@@ -216,13 +234,13 @@ async function saveAnalysisGroup(
|
||||
parent_analysis_group_id: analysisGroupId,
|
||||
material_groups: toArray(item.MaterjalideGrupp),
|
||||
},
|
||||
{ onConflict: "analysis_id_original", ignoreDuplicates: false }
|
||||
{ onConflict: 'analysis_id_original', ignoreDuplicates: false },
|
||||
)
|
||||
.select('id');
|
||||
|
||||
if (error || !insertedAnalysisElement[0]?.id) {
|
||||
throw new Error(
|
||||
`Failed to insert analysis element (id: ${analysisElement.UuringId}), error: ${error?.message}`
|
||||
`Failed to insert analysis element (id: ${analysisElement.UuringId}), error: ${error?.message}`,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -237,7 +255,7 @@ async function saveAnalysisGroup(
|
||||
coefficient: kood.Koefitsient,
|
||||
price: kood.Hind,
|
||||
analysis_element_id: insertedAnalysisElementId,
|
||||
}))
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -245,7 +263,7 @@ async function saveAnalysisGroup(
|
||||
if (analyses?.length) {
|
||||
for (const analysis of analyses) {
|
||||
const { data: insertedAnalysis, error } = await supabase
|
||||
.from("analyses")
|
||||
.from('analyses')
|
||||
.upsert(
|
||||
{
|
||||
analysis_id_oid: analysis.UuringIdOID,
|
||||
@@ -256,13 +274,13 @@ async function saveAnalysisGroup(
|
||||
order: analysis.Jarjekord,
|
||||
parent_analysis_element_id: insertedAnalysisElementId,
|
||||
},
|
||||
{ onConflict: "analysis_id_original", ignoreDuplicates: false }
|
||||
{ onConflict: 'analysis_id_original', ignoreDuplicates: false },
|
||||
)
|
||||
.select('id');
|
||||
|
||||
if (error || !insertedAnalysis[0]?.id) {
|
||||
throw new Error(
|
||||
`Failed to insert analysis (id: ${analysis.UuringId}) error: ${error?.message}`
|
||||
`Failed to insert analysis (id: ${analysis.UuringId}) error: ${error?.message}`,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -277,7 +295,7 @@ async function saveAnalysisGroup(
|
||||
coefficient: kood.Koefitsient,
|
||||
price: kood.Hind,
|
||||
analysis_id: insertedAnalysisId,
|
||||
}))
|
||||
})),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -285,20 +303,21 @@ async function saveAnalysisGroup(
|
||||
}
|
||||
|
||||
const { error: codesError } = await supabase
|
||||
.from("codes")
|
||||
.from('codes')
|
||||
.upsert(codes, { ignoreDuplicates: false });
|
||||
|
||||
if (codesError?.code) {
|
||||
console.error(codesError); // TODO remove this
|
||||
throw new Error(
|
||||
`Failed to insert codes (analysis group id: ${analysisGroup.UuringuGruppId})`
|
||||
`Failed to insert codes (analysis group id: ${analysisGroup.UuringuGruppId})`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function syncPublicMessage(
|
||||
message?: MedipostPublicMessageResponse | null
|
||||
message?: MedipostPublicMessageResponse | null,
|
||||
) {
|
||||
const supabase = createClient(
|
||||
const supabase = createCustomClient(
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
||||
process.env.NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY!,
|
||||
{
|
||||
@@ -307,20 +326,20 @@ export async function syncPublicMessage(
|
||||
autoRefreshToken: false,
|
||||
detectSessionInUrl: false,
|
||||
},
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
try {
|
||||
const providers = toArray(message?.Saadetis?.Teenused.Teostaja);
|
||||
const analysisGroups = providers.flatMap((provider) =>
|
||||
toArray(provider.UuringuGrupp)
|
||||
toArray(provider.UuringuGrupp),
|
||||
);
|
||||
if (!message || !analysisGroups.length) {
|
||||
return supabase.schema("audit").from("sync_entries").insert({
|
||||
operation: "ANALYSES_SYNC",
|
||||
comment: "No data received",
|
||||
return supabase.schema('audit').from('sync_entries').insert({
|
||||
operation: 'ANALYSES_SYNC',
|
||||
comment: 'No data received',
|
||||
status: SyncStatus.Fail,
|
||||
changed_by_role: "service_role",
|
||||
changed_by_role: 'service_role',
|
||||
});
|
||||
}
|
||||
|
||||
@@ -328,31 +347,170 @@ export async function syncPublicMessage(
|
||||
await saveAnalysisGroup(analysisGroup, supabase);
|
||||
}
|
||||
|
||||
await supabase.schema("audit").from("sync_entries").insert({
|
||||
operation: "ANALYSES_SYNC",
|
||||
await supabase.schema('audit').from('sync_entries').insert({
|
||||
operation: 'ANALYSES_SYNC',
|
||||
status: SyncStatus.Success,
|
||||
changed_by_role: "service_role",
|
||||
changed_by_role: 'service_role',
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
await supabase
|
||||
.schema("audit")
|
||||
.from("sync_entries")
|
||||
.schema('audit')
|
||||
.from('sync_entries')
|
||||
.insert({
|
||||
operation: "ANALYSES_SYNC",
|
||||
operation: 'ANALYSES_SYNC',
|
||||
status: SyncStatus.Fail,
|
||||
comment: JSON.stringify(e),
|
||||
changed_by_role: "service_role",
|
||||
changed_by_role: 'service_role',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// TODO use actual parameters
|
||||
export async function composeOrderXML(
|
||||
/* chosenAnalysisElements?: number[],
|
||||
chosenAnalyses?: number[], */
|
||||
comment?: string,
|
||||
) {
|
||||
const supabase = createCustomClient(
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
||||
process.env.NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY!,
|
||||
{
|
||||
auth: {
|
||||
persistSession: false,
|
||||
autoRefreshToken: false,
|
||||
detectSessionInUrl: false,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
// TODO remove dummy when actual implemetation is present
|
||||
const orderedElements = [1, 75];
|
||||
const orderedAnalyses = [10, 11, 100];
|
||||
|
||||
const createdAnalysisOrder = {
|
||||
id: 4,
|
||||
user_id: 'currentUser.user?.id',
|
||||
analysis_element_ids: orderedElements,
|
||||
analysis_ids: orderedAnalyses,
|
||||
status: AnalysisOrderStatus[1],
|
||||
created_at: new Date(),
|
||||
};
|
||||
|
||||
const { data: analysisElements } = (await supabase
|
||||
.from('analysis_elements')
|
||||
.select(`*, analysis_groups(*)`)
|
||||
.in('id', orderedElements)) as {
|
||||
data: ({
|
||||
analysis_groups: Tables<'analysis_groups'>;
|
||||
} & Tables<'analysis_elements'>)[];
|
||||
};
|
||||
const { data: analyses } = (await supabase
|
||||
.from('analyses')
|
||||
.select(`*, analysis_elements(*, analysis_groups(*))`)
|
||||
.in('id', orderedAnalyses)) as {
|
||||
data: ({
|
||||
analysis_elements: Tables<'analysis_elements'> & {
|
||||
analysis_groups: Tables<'analysis_groups'>;
|
||||
};
|
||||
} & Tables<'analyses'>)[];
|
||||
};
|
||||
|
||||
const analysisGroups: Tables<'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);
|
||||
}
|
||||
|
||||
// TODO get actual data when order creation is implemented
|
||||
return `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Saadetis xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="TellimusLOINC.xsd">
|
||||
${getPais(process.env.MEDIPOST_USER!, process.env.MEDIPOST_RECIPIENT!, createdAnalysisOrder.created_at, createdAnalysisOrder.id)}
|
||||
<Tellimus cito="EI">
|
||||
<ValisTellimuseId>${createdAnalysisOrder.id}</ValisTellimuseId>
|
||||
<!--<TellijaAsutus>-->
|
||||
${getClientInstitution()}
|
||||
<!--<TeostajaAsutus>-->
|
||||
${getProviderInstitution()}
|
||||
<!--<TellijaIsik>-->
|
||||
${getClientPerson()}
|
||||
<!--<SisestajaIsik>-->
|
||||
${getOrderEnteredByPerson()}
|
||||
<TellijaMarkused>${comment ?? ''}</TellijaMarkused>
|
||||
${getPatient(49610230861, 'Surname', 'First Name', '1996-10-23', 'N')}
|
||||
${getConfidentiality()}
|
||||
${specimenSection.join('')}
|
||||
${analysisSection?.join('')}
|
||||
</Tellimus>
|
||||
</Saadetis>`;
|
||||
}
|
||||
|
||||
function getLatestMessage(messages?: Message[]) {
|
||||
if (!messages?.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return messages.reduce((prev, current) =>
|
||||
Number(prev.messageId) > Number(current.messageId) ? prev : current
|
||||
Number(prev.messageId) > Number(current.messageId) ? prev : current,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user