feat(MED-161): move medipost xml validate to separate service
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user