feat(MED-161): create medipost common service
This commit is contained in:
@@ -3,7 +3,8 @@ import { getAnalysisOrder } from "~/lib/services/order.service";
|
|||||||
import { composeOrderTestResponseXML, sendPrivateMessageTestResponse } from "~/lib/services/medipostTest.service";
|
import { composeOrderTestResponseXML, sendPrivateMessageTestResponse } from "~/lib/services/medipostTest.service";
|
||||||
import { retrieveOrder } from "@lib/data";
|
import { retrieveOrder } from "@lib/data";
|
||||||
import { getAccountAdmin } from "~/lib/services/account.service";
|
import { getAccountAdmin } from "~/lib/services/account.service";
|
||||||
import { createMedipostActionLog, getOrderedAnalysisIds } from "~/lib/services/medipost.service";
|
import { createMedipostActionLog } from "~/lib/services/medipost/medipostMessageBase.service";
|
||||||
|
import { getOrderedAnalysisIds } from "~/lib/services/medipost.service";
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
// const isDev = process.env.NODE_ENV === 'development';
|
// const isDev = process.env.NODE_ENV === 'development';
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import {
|
|||||||
MedipostAction,
|
MedipostAction,
|
||||||
MedipostOrderResponse,
|
MedipostOrderResponse,
|
||||||
MedipostPublicMessageResponse,
|
MedipostPublicMessageResponse,
|
||||||
Message,
|
|
||||||
ResponseUuringuGrupp,
|
ResponseUuringuGrupp,
|
||||||
UuringuGrupp,
|
UuringuGrupp,
|
||||||
} from '@/lib/types/medipost';
|
} from '@/lib/types/medipost';
|
||||||
@@ -35,6 +34,7 @@ import { logMedipostDispatch } from './audit.service';
|
|||||||
import { composeOrderXML, OrderedAnalysisElement } from './medipostXML.service';
|
import { composeOrderXML, OrderedAnalysisElement } from './medipostXML.service';
|
||||||
import { validateMedipostResponse } from './medipost/medipostValidate.service';
|
import { validateMedipostResponse } from './medipost/medipostValidate.service';
|
||||||
import { parseXML } from './util/xml.service';
|
import { parseXML } from './util/xml.service';
|
||||||
|
import { createMedipostActionLog, getLatestMessage } from './medipost/medipostMessageBase.service';
|
||||||
|
|
||||||
const BASE_URL = process.env.MEDIPOST_URL!;
|
const BASE_URL = process.env.MEDIPOST_URL!;
|
||||||
const USER = process.env.MEDIPOST_USER!;
|
const USER = process.env.MEDIPOST_USER!;
|
||||||
@@ -422,29 +422,6 @@ export async function syncPublicMessage(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLatestMessage({
|
|
||||||
messages,
|
|
||||||
excludedMessageIds,
|
|
||||||
}: {
|
|
||||||
messages?: Message[];
|
|
||||||
excludedMessageIds?: string[];
|
|
||||||
}) {
|
|
||||||
if (!messages?.length) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const filtered = messages.filter(({ messageId }) => !excludedMessageIds?.includes(messageId));
|
|
||||||
|
|
||||||
if (!filtered.length) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return filtered.reduce((prev, current) =>
|
|
||||||
Number(prev.messageId) > Number(current.messageId) ? prev : current,
|
|
||||||
{ messageId: '' } as Message,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function syncPrivateMessage({
|
async function syncPrivateMessage({
|
||||||
messageResponse,
|
messageResponse,
|
||||||
order,
|
order,
|
||||||
@@ -733,37 +710,3 @@ export async function getOrderedAnalysisIds({
|
|||||||
|
|
||||||
return [...analysisPackageElements, ...orderedAnalysisElements, ...orderedAnalyses];
|
return [...analysisPackageElements, ...orderedAnalysisElements, ...orderedAnalyses];
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createMedipostActionLog({
|
|
||||||
action,
|
|
||||||
xml,
|
|
||||||
hasAnalysisResults = false,
|
|
||||||
medusaOrderId,
|
|
||||||
responseXml,
|
|
||||||
hasError = false,
|
|
||||||
}: {
|
|
||||||
action:
|
|
||||||
| '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;
|
|
||||||
responseXml?: string | null;
|
|
||||||
hasError?: boolean;
|
|
||||||
}) {
|
|
||||||
await getSupabaseServerAdminClient()
|
|
||||||
.schema('medreport')
|
|
||||||
.from('medipost_actions')
|
|
||||||
.insert({
|
|
||||||
action,
|
|
||||||
xml,
|
|
||||||
has_analysis_results: hasAnalysisResults,
|
|
||||||
medusa_order_id: medusaOrderId,
|
|
||||||
response_xml: responseXml,
|
|
||||||
has_error: hasError,
|
|
||||||
})
|
|
||||||
.select('id')
|
|
||||||
.throwOnError();
|
|
||||||
}
|
|
||||||
|
|||||||
63
lib/services/medipost/medipostMessageBase.service.ts
Normal file
63
lib/services/medipost/medipostMessageBase.service.ts
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
import type { Message } from '@/lib/types/medipost';
|
||||||
|
|
||||||
|
import { getSupabaseServerAdminClient } from '@/packages/supabase/src/clients/server-admin-client';
|
||||||
|
|
||||||
|
export function getLatestMessage({
|
||||||
|
messages,
|
||||||
|
excludedMessageIds,
|
||||||
|
}: {
|
||||||
|
messages?: Message[];
|
||||||
|
excludedMessageIds?: string[];
|
||||||
|
}) {
|
||||||
|
if (!messages?.length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const filtered = messages.filter(({ messageId }) => !excludedMessageIds?.includes(messageId));
|
||||||
|
|
||||||
|
if (!filtered.length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return filtered.reduce((prev, current) =>
|
||||||
|
Number(prev.messageId) > Number(current.messageId) ? prev : current,
|
||||||
|
{ messageId: '' } as Message,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createMedipostActionLog({
|
||||||
|
action,
|
||||||
|
xml,
|
||||||
|
hasAnalysisResults = false,
|
||||||
|
medusaOrderId,
|
||||||
|
responseXml,
|
||||||
|
hasError = false,
|
||||||
|
}: {
|
||||||
|
action:
|
||||||
|
| '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;
|
||||||
|
responseXml?: string | null;
|
||||||
|
hasError?: boolean;
|
||||||
|
}) {
|
||||||
|
await getSupabaseServerAdminClient()
|
||||||
|
.schema('medreport')
|
||||||
|
.from('medipost_actions')
|
||||||
|
.insert({
|
||||||
|
action,
|
||||||
|
xml,
|
||||||
|
has_analysis_results: hasAnalysisResults,
|
||||||
|
medusa_order_id: medusaOrderId,
|
||||||
|
response_xml: responseXml,
|
||||||
|
has_error: hasError,
|
||||||
|
})
|
||||||
|
.select('id')
|
||||||
|
.throwOnError();
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user