feat(MED-161): move medipost xml validate to separate service
This commit is contained in:
@@ -9,7 +9,6 @@ import { SyncStatus } from '@/lib/types/audit';
|
||||
import {
|
||||
AnalysisOrderStatus,
|
||||
GetMessageListResponse,
|
||||
IMedipostResponseXMLBase,
|
||||
MedipostAction,
|
||||
MedipostOrderResponse,
|
||||
MedipostPublicMessageResponse,
|
||||
@@ -19,7 +18,6 @@ import {
|
||||
} from '@/lib/types/medipost';
|
||||
import { toArray } from '@/lib/utils';
|
||||
import axios from 'axios';
|
||||
import { XMLParser } from 'fast-xml-parser';
|
||||
|
||||
import { Tables } from '@kit/supabase/database';
|
||||
import { createAnalysisGroup } from './analysis-group.service';
|
||||
@@ -35,6 +33,8 @@ import { getAnalysisElementMedusaProductIds } from '@/utils/medusa-product';
|
||||
import { MedipostValidationError } from './medipost/MedipostValidationError';
|
||||
import { logMedipostDispatch } from './audit.service';
|
||||
import { composeOrderXML, OrderedAnalysisElement } from './medipostXML.service';
|
||||
import { validateMedipostResponse } from './medipost/medipostValidate.service';
|
||||
import { parseXML } from './util/xml.service';
|
||||
|
||||
const BASE_URL = process.env.MEDIPOST_URL!;
|
||||
const USER = process.env.MEDIPOST_USER!;
|
||||
@@ -43,28 +43,6 @@ const RECIPIENT = process.env.MEDIPOST_RECIPIENT!;
|
||||
|
||||
const ANALYSIS_PACKAGE_HANDLE_PREFIX = 'analysis-package-';
|
||||
|
||||
function parseXML(xml: string) {
|
||||
const parser = new XMLParser({ ignoreAttributes: false });
|
||||
return parser.parse(xml);
|
||||
}
|
||||
|
||||
export async function validateMedipostResponse(response: string, { canHaveEmptyCode = false }: { canHaveEmptyCode?: boolean } = {}) {
|
||||
const parsed: IMedipostResponseXMLBase = parseXML(response);
|
||||
const code = parsed.ANSWER?.CODE;
|
||||
if (canHaveEmptyCode) {
|
||||
if (code && code !== 0) {
|
||||
console.error("Bad response", response);
|
||||
throw new MedipostValidationError(response);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof code !== 'number' || (code !== 0 && !canHaveEmptyCode)) {
|
||||
console.error("Bad response", response);
|
||||
throw new MedipostValidationError(response);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getMessages() {
|
||||
try {
|
||||
const publicMessage = await getLatestPublicMessageListItem();
|
||||
|
||||
25
lib/services/medipost/medipostValidate.service.ts
Normal file
25
lib/services/medipost/medipostValidate.service.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
'use server';
|
||||
|
||||
import {
|
||||
IMedipostResponseXMLBase,
|
||||
} from '@/lib/types/medipost';
|
||||
|
||||
import { MedipostValidationError } from './MedipostValidationError';
|
||||
import { parseXML } from '../util/xml.service';
|
||||
|
||||
export async function validateMedipostResponse(response: string, { canHaveEmptyCode = false }: { canHaveEmptyCode?: boolean } = {}) {
|
||||
const parsed: IMedipostResponseXMLBase = parseXML(response);
|
||||
const code = parsed.ANSWER?.CODE;
|
||||
if (canHaveEmptyCode) {
|
||||
if (code && code !== 0) {
|
||||
console.error("Bad response", response);
|
||||
throw new MedipostValidationError(response);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof code !== 'number' || (code !== 0 && !canHaveEmptyCode)) {
|
||||
console.error("Bad response", response);
|
||||
throw new MedipostValidationError(response);
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ import { Tables } from '@kit/supabase/database';
|
||||
import { formatDate } from 'date-fns';
|
||||
import { getAnalyses } from './analyses.service';
|
||||
import { getAnalysisElementsAdmin } from './analysis-element.service';
|
||||
import { validateMedipostResponse } from './medipost.service';
|
||||
import { validateMedipostResponse } from './medipost/medipostValidate.service';
|
||||
|
||||
const BASE_URL = process.env.MEDIPOST_URL!;
|
||||
const USER = process.env.MEDIPOST_USER!;
|
||||
|
||||
8
lib/services/util/xml.service.ts
Normal file
8
lib/services/util/xml.service.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
'use server';
|
||||
|
||||
import { XMLParser } from 'fast-xml-parser';
|
||||
|
||||
export function parseXML(xml: string) {
|
||||
const parser = new XMLParser({ ignoreAttributes: false });
|
||||
return parser.parse(xml);
|
||||
}
|
||||
Reference in New Issue
Block a user