forked from Shared/esg
final update on moallm client
This commit is contained in:
@@ -5,6 +5,7 @@ import { ProviderName } from '../common/enums/provider-name.enum';
|
||||
import { BaseInquiryResponseDto } from '../common/dto/base-inquiry-response.dto';
|
||||
import { NormalizedErrorDto } from '../common/dto/normalized-error.dto';
|
||||
import { generateTrackingCode } from '../common/helpers/tracking-code.helper';
|
||||
import { buildInquiryResponse } from '../common/helpers/inquiry-response.helper';
|
||||
import { InquiryException } from '../common/exceptions/inquiry.exception';
|
||||
import { InquiryLogService } from '../logging/inquiry-log.service';
|
||||
import {
|
||||
@@ -54,19 +55,20 @@ export class InquiryService {
|
||||
trackingCode,
|
||||
);
|
||||
|
||||
const response = this.buildSuccessResponse(
|
||||
result.data,
|
||||
result.provider,
|
||||
const response = buildInquiryResponse({
|
||||
success: true,
|
||||
provider: result.provider,
|
||||
trackingCode,
|
||||
result.duration,
|
||||
`${this.getInquiryLabel(inquiryType)} completed successfully`,
|
||||
);
|
||||
message: `${this.getInquiryLabel(inquiryType)} completed successfully`,
|
||||
duration: result.duration,
|
||||
data: this.toResponseData(result.data),
|
||||
});
|
||||
|
||||
await this.persistLog({
|
||||
inquiryType,
|
||||
provider: result.provider,
|
||||
requestPayload: payload,
|
||||
responsePayload: response.data,
|
||||
responsePayload: response.data ?? undefined,
|
||||
status: InquiryStatus.SUCCESS,
|
||||
duration: result.duration,
|
||||
requestId,
|
||||
@@ -76,20 +78,20 @@ export class InquiryService {
|
||||
return response;
|
||||
} catch (error) {
|
||||
const duration = Date.now() - start;
|
||||
const normalized = this.extractError(error);
|
||||
const { normalized, provider } = this.extractFailure(error);
|
||||
|
||||
const response: BaseInquiryResponseDto<Record<string, unknown>> = {
|
||||
const response = buildInquiryResponse({
|
||||
success: false,
|
||||
provider: 'GATEWAY',
|
||||
provider: provider ?? 'GATEWAY',
|
||||
trackingCode,
|
||||
message: normalized.message,
|
||||
error: normalized,
|
||||
duration,
|
||||
};
|
||||
error: normalized,
|
||||
});
|
||||
|
||||
await this.persistLog({
|
||||
inquiryType,
|
||||
provider: ProviderName.HAMTA,
|
||||
provider: (provider as ProviderName | undefined) ?? ProviderName.HAMTA,
|
||||
requestPayload: payload,
|
||||
status: InquiryStatus.FAILURE,
|
||||
duration,
|
||||
@@ -114,23 +116,6 @@ export class InquiryService {
|
||||
});
|
||||
}
|
||||
|
||||
private buildSuccessResponse(
|
||||
result: unknown,
|
||||
provider: string,
|
||||
trackingCode: string,
|
||||
duration: number,
|
||||
message: string,
|
||||
): BaseInquiryResponseDto<Record<string, unknown>> {
|
||||
return {
|
||||
success: true,
|
||||
provider,
|
||||
trackingCode,
|
||||
message,
|
||||
duration,
|
||||
data: this.toResponseData(result),
|
||||
};
|
||||
}
|
||||
|
||||
private toResponseData(result: unknown): Record<string, unknown> {
|
||||
if (this.isPersonInquiryResult(result)) {
|
||||
const raw =
|
||||
@@ -169,16 +154,23 @@ export class InquiryService {
|
||||
return inquiryType.replace(/_INQUIRY$/, '').toLowerCase().replace(/_/g, ' ');
|
||||
}
|
||||
|
||||
private extractError(error: unknown): NormalizedErrorDto {
|
||||
private extractFailure(error: unknown): {
|
||||
normalized: NormalizedErrorDto;
|
||||
provider?: string;
|
||||
} {
|
||||
if (error instanceof InquiryException) {
|
||||
return error.normalizedError;
|
||||
return { normalized: error.normalizedError, provider: error.provider };
|
||||
}
|
||||
if (error && typeof error === 'object' && 'normalizedError' in error) {
|
||||
return (error as { normalizedError: NormalizedErrorDto }).normalizedError;
|
||||
return {
|
||||
normalized: (error as { normalizedError: NormalizedErrorDto }).normalizedError,
|
||||
};
|
||||
}
|
||||
return {
|
||||
code: 'INQUIRY_FAILED',
|
||||
message: error instanceof Error ? error.message : 'Inquiry failed',
|
||||
normalized: {
|
||||
code: 'INQUIRY_FAILED',
|
||||
message: error instanceof Error ? error.message : 'Inquiry failed',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user