forked from Shared/esg
parsian done
This commit is contained in:
85
src/common/helpers/soap-car-policy.helper.ts
Normal file
85
src/common/helpers/soap-car-policy.helper.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
function decodeXml(value: string): string {
|
||||
return value
|
||||
.replace(/'/g, "'")
|
||||
.replace(/"/g, '"')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/</g, '<')
|
||||
.replace(/&/g, '&');
|
||||
}
|
||||
|
||||
function normalizeSoapTextValue(value: string): string {
|
||||
if (/i:nil\s*=\s*["']true["']/i.test(value)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const nestedStrings = [
|
||||
...value.matchAll(/<(?:[\w]+:)?string[^>]*>([\s\S]*?)<\/(?:[\w]+:)?string>/gi),
|
||||
]
|
||||
.map((match) => decodeXml(match[1].trim()))
|
||||
.filter(Boolean);
|
||||
|
||||
if (nestedStrings.length > 0) {
|
||||
return nestedStrings.join(', ');
|
||||
}
|
||||
|
||||
return decodeXml(value.trim());
|
||||
}
|
||||
|
||||
function parseSoapFields(block: string): Record<string, string> {
|
||||
const fields: Record<string, string> = {};
|
||||
const tagRegex = /<(?:[\w]+:)?(\w+)(?:[^>]*)>([\s\S]*?)<\/(?:[\w]+:)?\1>/gi;
|
||||
let match: RegExpExecArray | null;
|
||||
|
||||
while ((match = tagRegex.exec(block)) !== null) {
|
||||
fields[match[1]] = normalizeSoapTextValue(match[2]);
|
||||
}
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
export function parseFirstCarPolicy(soapXml: string): Record<string, string> {
|
||||
const resultMatch = soapXml.match(
|
||||
/<(?:[\w]+:)?(?:CIIWSPolicyChassis|CIIWSPolicyVehicleMeli|CIIWSPolicyNationalId)Result[^>]*>([\s\S]*?)<\/(?:[\w]+:)?(?:CIIWSPolicyChassis|CIIWSPolicyVehicleMeli|CIIWSPolicyNationalId)Result>/i,
|
||||
);
|
||||
|
||||
if (!resultMatch) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const firstPolicyMatch = resultMatch[1].match(
|
||||
/<(?:[\w]+:)?Policy(?:\s[^>]*)?>\s*<(?:[\w]+:)?Policy(?:\s[^>]*)?>([\s\S]*?)<\/(?:[\w]+:)?Policy>/i,
|
||||
);
|
||||
|
||||
if (!firstPolicyMatch) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return parseSoapFields(firstPolicyMatch[1]);
|
||||
}
|
||||
|
||||
export function getCarPolicyProviderError(
|
||||
soapXml: string,
|
||||
policy: Record<string, string>,
|
||||
): { message: string; code: string } | null {
|
||||
const resultMatch = soapXml.match(
|
||||
/<(?:[\w]+:)?(?:CIIWSPolicyChassis|CIIWSPolicyVehicleMeli|CIIWSPolicyNationalId)Result[^>]*>([\s\S]*?)<\/(?:[\w]+:)?(?:CIIWSPolicyChassis|CIIWSPolicyVehicleMeli|CIIWSPolicyNationalId)Result>/i,
|
||||
);
|
||||
|
||||
if (resultMatch) {
|
||||
const errorMatch = resultMatch[1].match(
|
||||
/<(?:[\w]+:)?Error(?![^>]*i:nil\s*=\s*["']true["'])[^>]*>([\s\S]*?)<\/(?:[\w]+:)?Error>/i,
|
||||
);
|
||||
if (errorMatch) {
|
||||
const errorText = normalizeSoapTextValue(errorMatch[1]).trim();
|
||||
if (errorText) {
|
||||
return { message: errorText, code: 'PROVIDER_ERROR' };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Object.keys(policy).length === 0) {
|
||||
return { message: 'No policy record found', code: 'RECORD_NOT_FOUND' };
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user