forked from Shared/esg
requests fix
This commit is contained in:
@@ -117,15 +117,33 @@ export abstract class LegacyApiProvider extends BaseProvider<
|
||||
): Promise<{ raw: unknown }> {
|
||||
const inquiryConfig = this.getInquiryConfig(inquiryType);
|
||||
|
||||
// Transform payload for Real Estate inquiry to match CentInsur API format
|
||||
const requestPayload = this.transformPayloadForInquiryType(inquiryType, payload);
|
||||
|
||||
try {
|
||||
const response = await this.httpClient.post<LegacyProviderApiResponse>(
|
||||
inquiryConfig.url,
|
||||
payload,
|
||||
requestPayload,
|
||||
await this.buildRequestConfig(inquiryType, inquiryConfig),
|
||||
);
|
||||
|
||||
const body = response.data;
|
||||
|
||||
// Handle Sayah/Sheba API format (ReturnValue/HasError)
|
||||
if ('ReturnValue' in body || 'HasError' in body) {
|
||||
if (body.HasError) {
|
||||
// Format error message from Errors object
|
||||
const errorMessages = body.Errors
|
||||
? Object.entries(body.Errors).map(([code, field]) => `${field} (code: ${code})`).join(', ')
|
||||
: 'Request failed';
|
||||
throw this.formatProviderError(
|
||||
errorMessages,
|
||||
'SAYAH_ERROR',
|
||||
);
|
||||
}
|
||||
return { raw: body };
|
||||
}
|
||||
|
||||
// Handle CentInsur API format (IsSucceed/Result)
|
||||
if ('IsSucceed' in body) {
|
||||
if (!body.IsSucceed) {
|
||||
@@ -149,6 +167,17 @@ export abstract class LegacyApiProvider extends BaseProvider<
|
||||
if (error instanceof AxiosError) {
|
||||
const data = error.response?.data as LegacyProviderApiResponse | undefined;
|
||||
|
||||
// Check for Sayah/Sheba format error
|
||||
if (data && ('ReturnValue' in data || 'HasError' in data) && data.HasError) {
|
||||
const errorMessages = data.Errors
|
||||
? Object.entries(data.Errors).map(([code, field]) => `${field} (code: ${code})`).join(', ')
|
||||
: error.message;
|
||||
throw this.formatProviderError(
|
||||
errorMessages,
|
||||
String(error.response?.status ?? 'SAYAH_ERROR'),
|
||||
);
|
||||
}
|
||||
|
||||
// Check for CentInsur format error
|
||||
if (data && 'IsSucceed' in data && !data.IsSucceed) {
|
||||
throw this.formatProviderError(
|
||||
@@ -166,6 +195,25 @@ export abstract class LegacyApiProvider extends BaseProvider<
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform payload to match provider-specific API format
|
||||
* Real Estate inquiry uses NationalId/PostalCode instead of nationalCode/postalCode
|
||||
*/
|
||||
protected transformPayloadForInquiryType(
|
||||
inquiryType: InquiryType,
|
||||
payload: LegacyInquiryPayload,
|
||||
): LegacyInquiryPayload {
|
||||
if (inquiryType === InquiryType.REAL_ESTATE) {
|
||||
return {
|
||||
NationalId: payload.nationalCode ?? payload.nationalId ?? payload.NationalId,
|
||||
PostalCode: payload.postalCode ?? payload.PostalCode,
|
||||
...payload,
|
||||
};
|
||||
}
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
protected getInquiryConfig(inquiryType: InquiryType): InquiryConfig {
|
||||
const inquiryConfig = this.providerConfig.inquiries[inquiryType];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user