forked from Shared/esg
parsian done
This commit is contained in:
@@ -2,6 +2,11 @@ import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import axios, { AxiosError } from 'axios';
|
||||
import { mergeOutboundAxiosConfig } from '../../common/helpers/http-client.helper';
|
||||
import {
|
||||
buildCivilRegistrationFullName,
|
||||
getCivilRegistrationProviderError,
|
||||
parseCiiEstelamResult,
|
||||
} from '../../common/helpers/soap-civil-registration.helper';
|
||||
import {
|
||||
getSayahProviderError,
|
||||
SayahApiResponse,
|
||||
@@ -20,6 +25,7 @@ import {
|
||||
LegacyApiProvider,
|
||||
LegacyInquiryPayload,
|
||||
LegacyInquiryResult,
|
||||
PersonInquiryResult,
|
||||
} from '../shared/legacy-api.provider.abstract';
|
||||
|
||||
interface CivilRegistrationPayload extends LegacyInquiryPayload {
|
||||
@@ -107,6 +113,7 @@ export class HamtaProvider extends LegacyApiProvider {
|
||||
);
|
||||
|
||||
const inquiryConfig = this.getInquiryConfig(InquiryType.POSTAL_CODE);
|
||||
this.assertAmitisCredentials(InquiryType.POSTAL_CODE, inquiryConfig);
|
||||
const token = await this.amitisProvider.getAccessToken(
|
||||
ProviderName.HAMTA,
|
||||
InquiryType.POSTAL_CODE,
|
||||
@@ -138,7 +145,7 @@ export class HamtaProvider extends LegacyApiProvider {
|
||||
|
||||
private async inquireCivilRegistration(
|
||||
payload: CivilRegistrationPayload,
|
||||
): Promise<{ raw: unknown }> {
|
||||
): Promise<PersonInquiryResult> {
|
||||
const nationalCode = this.getRequiredString(
|
||||
payload.nationalCode ?? payload.NIN,
|
||||
'nationalCode',
|
||||
@@ -151,6 +158,7 @@ export class HamtaProvider extends LegacyApiProvider {
|
||||
const response = await axios.post<string>(
|
||||
inquiryConfig.url,
|
||||
this.buildCivilRegistrationEnvelope(
|
||||
payload,
|
||||
nationalCode,
|
||||
birthDate,
|
||||
inquiryConfig.username,
|
||||
@@ -166,22 +174,23 @@ export class HamtaProvider extends LegacyApiProvider {
|
||||
}),
|
||||
);
|
||||
|
||||
const result = this.extractSoapValue(response.data, 'SubmitInqDteStsWithPstCodResult');
|
||||
const civilRegistration = parseCiiEstelamResult(response.data);
|
||||
const providerError = getCivilRegistrationProviderError(civilRegistration);
|
||||
if (providerError) {
|
||||
throw this.formatProviderError(providerError.message, providerError.code);
|
||||
}
|
||||
|
||||
const fullName = buildCivilRegistrationFullName(civilRegistration);
|
||||
|
||||
return {
|
||||
raw: {
|
||||
nationalCode,
|
||||
birthDate,
|
||||
result,
|
||||
soap: response.data,
|
||||
},
|
||||
nationalCode,
|
||||
birthDate,
|
||||
fullName: fullName || undefined,
|
||||
raw: civilRegistration,
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof AxiosError) {
|
||||
throw this.formatProviderError(
|
||||
error.message,
|
||||
String(error.response?.status ?? 'NETWORK_ERROR'),
|
||||
);
|
||||
throw this.formatAxiosError(error);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
@@ -251,6 +260,7 @@ export class HamtaProvider extends LegacyApiProvider {
|
||||
const shebaId = this.getRequiredString(payload.sheba, 'sheba');
|
||||
|
||||
const inquiryConfig = this.getInquiryConfig(InquiryType.SHEBA);
|
||||
this.assertAmitisCredentials(InquiryType.SHEBA, inquiryConfig);
|
||||
const token = await this.amitisProvider.getAccessToken(
|
||||
ProviderName.HAMTA,
|
||||
InquiryType.SHEBA,
|
||||
@@ -294,19 +304,35 @@ export class HamtaProvider extends LegacyApiProvider {
|
||||
}
|
||||
|
||||
private buildCivilRegistrationEnvelope(
|
||||
payload: CivilRegistrationPayload,
|
||||
nationalCode: string,
|
||||
birthDate: string,
|
||||
username: string,
|
||||
password: string,
|
||||
): string {
|
||||
const birthDateCompact = birthDate.replace(/-/g, '');
|
||||
const dateHasPostfix = payload.dateHasPostfix ?? 0;
|
||||
|
||||
return `<?xml version="1.0" encoding="utf-8"?>
|
||||
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
||||
<soap:Body>
|
||||
<SubmitInqDteStsWithPstCod xmlns="http://tempuri.org/">
|
||||
<NIN>${this.escapeXml(nationalCode)}</NIN>
|
||||
<BirthDate>${this.escapeXml(birthDate)}</BirthDate>
|
||||
<req>
|
||||
<Nin>${this.escapeXml(nationalCode)}</Nin>
|
||||
<Shenasnameserial>0</Shenasnameserial>
|
||||
<ShenasnameNo>0</ShenasnameNo>
|
||||
<BirthDate>${this.escapeXml(birthDateCompact)}</BirthDate>
|
||||
<DateHasPostfix>${dateHasPostfix}</DateHasPostfix>
|
||||
<Gender>0</Gender>
|
||||
<OfficeCode>0</OfficeCode>
|
||||
<BookNo>0</BookNo>
|
||||
<NameHasPrefix>0</NameHasPrefix>
|
||||
<NameHasPostFix>0</NameHasPostFix>
|
||||
<FamilyHasPrefix>0</FamilyHasPrefix>
|
||||
<FamilyHasPostFix>0</FamilyHasPostFix>
|
||||
</req>
|
||||
<Username>${this.escapeXml(username)}</Username>
|
||||
<Password>${this.escapeXml(password)}</Password>
|
||||
</SubmitInqDteStsWithPstCod>
|
||||
@@ -335,9 +361,17 @@ export class HamtaProvider extends LegacyApiProvider {
|
||||
</soap:Envelope>`;
|
||||
}
|
||||
|
||||
private extractSoapValue(xml: string, tagName: string): string | null {
|
||||
const match = xml.match(new RegExp(`<(?:\\w+:)?${tagName}[^>]*>([\\s\\S]*?)</(?:\\w+:)?${tagName}>`));
|
||||
return match ? this.decodeXml(match[1].trim()) : null;
|
||||
private assertAmitisCredentials(
|
||||
inquiryType: InquiryType,
|
||||
inquiryConfig: { username: string; password: string },
|
||||
): void {
|
||||
const envPrefix = inquiryType.replace(/_INQUIRY$/, '');
|
||||
if (!inquiryConfig.username?.trim() || !inquiryConfig.password?.trim()) {
|
||||
throw this.formatProviderError(
|
||||
`${envPrefix} AMITIS credentials are missing. Set HAMTA_${envPrefix}_USERNAME and HAMTA_${envPrefix}_PASSWORD in .env`,
|
||||
'PROVIDER_NOT_CONFIGURED',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private getRequiredString(value: unknown, fieldName: string): string {
|
||||
@@ -359,15 +393,6 @@ export class HamtaProvider extends LegacyApiProvider {
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
private decodeXml(value: string): string {
|
||||
return value
|
||||
.replace(/'/g, "'")
|
||||
.replace(/"/g, '"')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/</g, '<')
|
||||
.replace(/&/g, '&');
|
||||
}
|
||||
}
|
||||
|
||||
// Made with Bob
|
||||
|
||||
Reference in New Issue
Block a user