feat(MED-131): improve xml response code validation

This commit is contained in:
2025-08-04 11:52:57 +03:00
parent dfce4047cd
commit 08950896e5

View File

@@ -19,6 +19,7 @@ import { SyncStatus } from '@/lib/types/audit';
import { import {
AnalysisOrderStatus, AnalysisOrderStatus,
GetMessageListResponse, GetMessageListResponse,
IMedipostResponseXMLBase,
MaterjalideGrupp, MaterjalideGrupp,
MedipostAction, MedipostAction,
MedipostOrderResponse, MedipostOrderResponse,
@@ -38,6 +39,20 @@ import { createAnalysisGroup } from './analysis-group.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!;
const PASSWORD = process.env.MEDIPOST_PASSWORD!; const PASSWORD = process.env.MEDIPOST_PASSWORD!;
const RECIPIENT = process.env.MEDIPOST_RECIPIENT!;
function parseXML(xml: string) {
const parser = new XMLParser({ ignoreAttributes: false });
return parser.parse(xml);
}
export async function validateMedipostResponse(response: string) {
const parsed: IMedipostResponseXMLBase = parseXML(response);
if (typeof parsed.ANSWER?.CODE !== 'number' || parsed.ANSWER?.CODE !== 0) {
console.error("Bad response", response);
throw new Error(`Medipost response is invalid`);
}
}
export async function getMessages() { export async function getMessages() {
try { try {
@@ -85,14 +100,8 @@ export async function getPublicMessage(messageId: string) {
Accept: 'application/xml', Accept: 'application/xml',
}, },
}); });
const parser = new XMLParser({ ignoreAttributes: false }); await validateMedipostResponse(data);
const parsed: MedipostPublicMessageResponse = parser.parse(data); return parseXML(data) as MedipostPublicMessageResponse;
if (parsed.ANSWER?.CODE && parsed.ANSWER?.CODE !== 0) {
throw new Error(`Failed to get public message (id: ${messageId})`);
}
return parsed;
} }
export async function sendPrivateMessage(messageXml: string, receiver: string) { export async function sendPrivateMessage(messageXml: string, receiver: string) {
@@ -111,9 +120,7 @@ export async function sendPrivateMessage(messageXml: string, receiver: string) {
const { data } = await axios.post(BASE_URL, body); const { data } = await axios.post(BASE_URL, body);
if (data.code && data.code !== 0) { await validateMedipostResponse(data);
throw new Error(`Failed to send private message`);
}
} }
export async function getLatestPrivateMessageListItem() { export async function getLatestPrivateMessageListItem() {
@@ -145,15 +152,9 @@ export async function getPrivateMessage(messageId: string) {
}, },
}); });
const parser = new XMLParser({ ignoreAttributes: false }); await validateMedipostResponse(data);
const parsed = parser.parse(data);
if (parsed.ANSWER?.CODE && parsed.ANSWER?.CODE !== 0) { return parseXML(data) as MedipostOrderResponse;
console.error("Bad response", data);
throw new Error(`Failed to get private message (id: ${messageId})`);
}
return parsed;
} }
export async function deletePrivateMessage(messageId: string) { export async function deletePrivateMessage(messageId: string) {