forked from Shared/esg
initial commit
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsInt, IsNotEmpty, IsOptional, IsString, Length, Matches } from 'class-validator';
|
||||
|
||||
export class CivilRegistrationRequestDto {
|
||||
@ApiProperty({ example: '4311402422', description: 'Iranian national code (10 digits)' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Length(10, 10)
|
||||
@Matches(/^\d{10}$/, { message: 'nationalCode must be exactly 10 digits' })
|
||||
nationalCode!: string;
|
||||
|
||||
@ApiProperty({ example: '1370-01-01', description: 'Birth date as YYYY-MM-DD or YYYYMMDD' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Matches(/^\d{4}-?\d{2}-?\d{2}$/, {
|
||||
message: 'birthDate must be in YYYY-MM-DD or YYYYMMDD format',
|
||||
})
|
||||
birthDate!: string;
|
||||
|
||||
@ApiPropertyOptional({ default: 0 })
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
dateHasPostfix?: number;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
|
||||
export class CivilRegistrationResponseDto {
|
||||
@ApiPropertyOptional()
|
||||
NIN?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
BirthDate?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
FirstName?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
LastName?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
FatherName?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
SSN?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
Gender?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
DeathStatus?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
DeathDate?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
PostalCode?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
PostalCodeDescription?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
ExceptionMessage?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
Message?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
Error?: string;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { NormalizedErrorDto } from '../../common/dto/normalized-error.dto';
|
||||
|
||||
export class GenericInquiryResponseDto {
|
||||
@ApiProperty()
|
||||
success!: boolean;
|
||||
|
||||
@ApiProperty()
|
||||
provider!: string;
|
||||
|
||||
@ApiProperty()
|
||||
trackingCode!: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
message?: string;
|
||||
|
||||
@ApiPropertyOptional({ type: Object })
|
||||
data?: Record<string, unknown>;
|
||||
|
||||
@ApiPropertyOptional({ type: NormalizedErrorDto })
|
||||
error?: NormalizedErrorDto;
|
||||
|
||||
@ApiProperty()
|
||||
duration!: number;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
|
||||
export class PersonInquiryDataDto {
|
||||
@ApiProperty({ example: '0012345678' })
|
||||
nationalCode!: string;
|
||||
|
||||
@ApiProperty({ example: '1370-05-15' })
|
||||
birthDate!: string;
|
||||
|
||||
@ApiPropertyOptional({ example: 'علی محمدی' })
|
||||
fullName?: string;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsInt, IsNotEmpty, IsOptional, IsString, Length, Matches } from 'class-validator';
|
||||
|
||||
export class PersonInquiryRequestDto {
|
||||
@ApiProperty({ example: '0012345678', description: 'Iranian national code (10 digits)' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Length(10, 10)
|
||||
@Matches(/^\d{10}$/, { message: 'nationalCode must be exactly 10 digits' })
|
||||
nationalCode!: string;
|
||||
|
||||
@ApiProperty({ example: '1378-11-24', description: 'Jalali birth date (YYYY-MM-DD)' })
|
||||
@Matches(/^\d{4}-\d{2}-\d{2}$/, { message: 'birthDate must be in YYYY-MM-DD format' })
|
||||
@IsNotEmpty()
|
||||
birthDate!: string;
|
||||
|
||||
@ApiProperty({ example: 0, required: false, description: 'Civil registration date postfix flag' })
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
dateHasPostfix?: number;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { NormalizedErrorDto } from '../../common/dto/normalized-error.dto';
|
||||
import { PersonInquiryDataDto } from './person-inquiry-data.dto';
|
||||
|
||||
/** Swagger concrete type for person inquiry responses */
|
||||
export class PersonInquiryResponseDto {
|
||||
@ApiProperty()
|
||||
success!: boolean;
|
||||
|
||||
@ApiProperty()
|
||||
provider!: string;
|
||||
|
||||
@ApiProperty()
|
||||
trackingCode!: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
message?: string;
|
||||
|
||||
@ApiPropertyOptional({ type: PersonInquiryDataDto })
|
||||
data?: PersonInquiryDataDto;
|
||||
|
||||
@ApiPropertyOptional({ type: NormalizedErrorDto })
|
||||
error?: NormalizedErrorDto;
|
||||
|
||||
@ApiProperty()
|
||||
duration!: number;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString, Length, Matches } from 'class-validator';
|
||||
|
||||
export class PostalCodeRequestDto {
|
||||
@ApiProperty({ example: '1234567890', description: 'Postal code' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Length(10, 10)
|
||||
@Matches(/^\d{10}$/, { message: 'postalCode must be exactly 10 digits' })
|
||||
postalCode!: string;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
|
||||
export class PostalCodeResponseDto {
|
||||
@ApiPropertyOptional()
|
||||
Province?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
TownShip?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
Zone?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
Village?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
LocalityType?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
LocalityName?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
LocalityCode?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
SubLocality?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
Street?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
Street2?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
HouseNumber?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
Floor?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
SideFloor?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
BuildingName?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
Description?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
ErrorCode?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
ErrorMessage?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
TraceID?: string;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsIn, IsNotEmpty, IsOptional, IsString, Length, Matches } from 'class-validator';
|
||||
|
||||
export class SayahRequestDto {
|
||||
@ApiProperty({ example: '0', description: '0 for natural person' })
|
||||
@IsString()
|
||||
@IsIn(['0'])
|
||||
accountOwnerType!: string;
|
||||
|
||||
@ApiProperty({ example: '4311402422', description: 'Iranian national code (10 digits)' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Length(10, 10)
|
||||
@Matches(/^\d{10}$/, { message: 'nationalId must be exactly 10 digits' })
|
||||
nationalId!: string;
|
||||
|
||||
@ApiPropertyOptional({ nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
legalId?: string | null;
|
||||
|
||||
@ApiProperty({ example: 'IR000000000000000000000000', description: 'Iranian Sheba ID' })
|
||||
@IsString()
|
||||
@Length(26, 26)
|
||||
@Matches(/^IR\d{24}$/, { message: 'shebaId must start with IR and contain 24 digits' })
|
||||
shebaId!: string;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
|
||||
export class SayahResponseDto {
|
||||
@ApiPropertyOptional()
|
||||
ReturnValue?: boolean;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
HasError?: boolean;
|
||||
|
||||
@ApiPropertyOptional({ type: Object, nullable: true })
|
||||
Errors?: Record<string, string> | null;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString, Length, Matches } from 'class-validator';
|
||||
|
||||
export class ShahkarInquiryRequestDto {
|
||||
@ApiProperty({ example: '4311402422', description: 'Iranian national code (10 digits)' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Length(10, 10)
|
||||
@Matches(/^\d{10}$/, { message: 'nationalCode must be exactly 10 digits' })
|
||||
nationalCode!: string;
|
||||
|
||||
@ApiProperty({ example: '09226187419', description: 'Iranian mobile number' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Matches(/^09\d{9}$/, { message: 'mobileNo must be an Iranian mobile number' })
|
||||
mobileNo!: string;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString, Length, Matches } from 'class-validator';
|
||||
|
||||
export class ShahkarRequestDto {
|
||||
@ApiProperty({ example: '4311402422', description: 'Iranian national code (10 digits)' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Length(10, 10)
|
||||
@Matches(/^\d{10}$/, { message: 'nationalCode must be exactly 10 digits' })
|
||||
nationalCode!: string;
|
||||
|
||||
@ApiProperty({ example: '09226187419', description: 'Iranian mobile number' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Matches(/^09\d{9}$/, { message: 'mobileNo must be an Iranian mobile number' })
|
||||
mobileNo!: string;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
|
||||
export class ShahkarResponseDto {
|
||||
@ApiPropertyOptional()
|
||||
response?: number;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
requestId?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
id?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
result?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
comment?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
ErrorNams?: string;
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Post,
|
||||
Req,
|
||||
UseGuards,
|
||||
UseInterceptors,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
ApiBearerAuth,
|
||||
ApiOperation,
|
||||
ApiResponse,
|
||||
ApiTags,
|
||||
} from '@nestjs/swagger';
|
||||
import { Throttle } from '@nestjs/throttler';
|
||||
import { Request } from 'express';
|
||||
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
|
||||
import { InquiryAccessGuard } from '../auth/guards/inquiry-access.guard';
|
||||
import { InquiryAccess } from '../auth/decorators/inquiry-access.decorator';
|
||||
import { InquiryType } from '../common/enums/inquiry-type.enum';
|
||||
import { BaseInquiryResponseDto } from '../common/dto/base-inquiry-response.dto';
|
||||
import { ResponseTransformInterceptor } from '../common/interceptors/response-transform.interceptor';
|
||||
import { UserRateLimitGuard } from '../rate-limit/user-rate-limit.guard';
|
||||
import { PersonInquiryRequestDto } from './dto/person-inquiry-request.dto';
|
||||
import { PersonInquiryDataDto } from './dto/person-inquiry-data.dto';
|
||||
import { PersonInquiryResponseDto } from './dto/person-inquiry-response.dto';
|
||||
import { GenericInquiryResponseDto } from './dto/generic-inquiry-response.dto';
|
||||
import { PostalCodeRequestDto } from './dto/postal-code-request.dto';
|
||||
import { ShahkarRequestDto } from './dto/shahkar-request.dto';
|
||||
import { SayahRequestDto } from './dto/sayah-request.dto';
|
||||
import { InquiryService } from './inquiry.service';
|
||||
|
||||
/**
|
||||
* Unified inquiry endpoints — protected by JWT, inquiry RBAC, and rate limits.
|
||||
*/
|
||||
@ApiTags('Inquiries')
|
||||
@ApiBearerAuth()
|
||||
@Controller('inquiry')
|
||||
@UseGuards(JwtAuthGuard, InquiryAccessGuard, UserRateLimitGuard)
|
||||
@UseInterceptors(ResponseTransformInterceptor)
|
||||
export class InquiryController {
|
||||
constructor(private readonly inquiryService: InquiryService) {}
|
||||
|
||||
@Post('person')
|
||||
@InquiryAccess(InquiryType.PERSON)
|
||||
@Throttle({ default: { limit: 30, ttl: 60000 } })
|
||||
@ApiOperation({ summary: 'Person identity inquiry' })
|
||||
@ApiResponse({ status: 200, type: PersonInquiryResponseDto })
|
||||
@ApiResponse({ status: 401, description: 'Unauthorized' })
|
||||
@ApiResponse({ status: 403, description: 'Inquiry access denied' })
|
||||
@ApiResponse({ status: 429, description: 'Too many requests' })
|
||||
async inquirePerson(
|
||||
@Body() dto: PersonInquiryRequestDto,
|
||||
@Req() req: Request & { requestId?: string; trackingCode?: string },
|
||||
): Promise<BaseInquiryResponseDto<PersonInquiryDataDto>> {
|
||||
const requestId = req.requestId ?? 'unknown';
|
||||
const result = await this.inquiryService.inquirePerson(dto, requestId);
|
||||
req.trackingCode = result.trackingCode;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Post('realEstate')
|
||||
@InquiryAccess(InquiryType.REAL_ESTATE)
|
||||
@Throttle({ default: { limit: 30, ttl: 60000 } })
|
||||
@ApiOperation({ summary: 'Real estate inquiry' })
|
||||
@ApiResponse({ status: 200, type: GenericInquiryResponseDto })
|
||||
async inquireRealEstate(
|
||||
@Body() payload: Record<string, unknown>,
|
||||
@Req() req: Request & { requestId?: string; trackingCode?: string },
|
||||
): Promise<BaseInquiryResponseDto<Record<string, unknown>>> {
|
||||
return this.inquireGeneric(InquiryType.REAL_ESTATE, payload, req);
|
||||
}
|
||||
|
||||
@Post('sheba')
|
||||
@InquiryAccess(InquiryType.SHEBA)
|
||||
@Throttle({ default: { limit: 30, ttl: 60000 } })
|
||||
@ApiOperation({ summary: 'Sheba inquiry' })
|
||||
@ApiResponse({ status: 200, type: GenericInquiryResponseDto })
|
||||
async inquireSheba(
|
||||
@Body() payload: SayahRequestDto,
|
||||
@Req() req: Request & { requestId?: string; trackingCode?: string },
|
||||
): Promise<BaseInquiryResponseDto<Record<string, unknown>>> {
|
||||
return this.inquireGeneric(InquiryType.SHEBA, payload as unknown as Record<string, unknown>, req);
|
||||
}
|
||||
|
||||
@Post('shahkar')
|
||||
@InquiryAccess(InquiryType.SHAHKAR)
|
||||
@Throttle({ default: { limit: 30, ttl: 60000 } })
|
||||
@ApiOperation({ summary: 'Shahkar inquiry' })
|
||||
@ApiResponse({ status: 200, type: GenericInquiryResponseDto })
|
||||
async inquireShahkar(
|
||||
@Body() payload: ShahkarRequestDto,
|
||||
@Req() req: Request & { requestId?: string; trackingCode?: string },
|
||||
): Promise<BaseInquiryResponseDto<Record<string, unknown>>> {
|
||||
return this.inquireGeneric(
|
||||
InquiryType.SHAHKAR,
|
||||
payload as unknown as Record<string, unknown>,
|
||||
req,
|
||||
);
|
||||
}
|
||||
|
||||
@Post('postalCode')
|
||||
@InquiryAccess(InquiryType.POSTAL_CODE)
|
||||
@Throttle({ default: { limit: 30, ttl: 60000 } })
|
||||
@ApiOperation({ summary: 'Postal code inquiry' })
|
||||
@ApiResponse({ status: 200, type: GenericInquiryResponseDto })
|
||||
async inquirePostalCode(
|
||||
@Body() payload: PostalCodeRequestDto,
|
||||
@Req() req: Request & { requestId?: string; trackingCode?: string },
|
||||
): Promise<BaseInquiryResponseDto<Record<string, unknown>>> {
|
||||
return this.inquireGeneric(
|
||||
InquiryType.POSTAL_CODE,
|
||||
payload as unknown as Record<string, unknown>,
|
||||
req,
|
||||
);
|
||||
}
|
||||
|
||||
@Post('legalPerson')
|
||||
@InquiryAccess(InquiryType.LEGAL_PERSON)
|
||||
@Throttle({ default: { limit: 30, ttl: 60000 } })
|
||||
@ApiOperation({ summary: 'Legal person inquiry' })
|
||||
@ApiResponse({ status: 200, type: GenericInquiryResponseDto })
|
||||
async inquireLegalPerson(
|
||||
@Body() payload: Record<string, unknown>,
|
||||
@Req() req: Request & { requestId?: string; trackingCode?: string },
|
||||
): Promise<BaseInquiryResponseDto<Record<string, unknown>>> {
|
||||
return this.inquireGeneric(InquiryType.LEGAL_PERSON, payload, req);
|
||||
}
|
||||
|
||||
private async inquireGeneric(
|
||||
inquiryType: InquiryType,
|
||||
payload: Record<string, unknown>,
|
||||
req: Request & { requestId?: string; trackingCode?: string },
|
||||
): Promise<BaseInquiryResponseDto<Record<string, unknown>>> {
|
||||
const requestId = req.requestId ?? 'unknown';
|
||||
const result = await this.inquiryService.inquire(inquiryType, payload, requestId);
|
||||
req.trackingCode = result.trackingCode;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
import { LoggingModule } from '../logging/logging.module';
|
||||
import { ProvidersModule } from '../providers/providers.module';
|
||||
import { RateLimitModule } from '../rate-limit/rate-limit.module';
|
||||
import { InquiryController } from './inquiry.controller';
|
||||
import { InquiryService } from './inquiry.service';
|
||||
|
||||
@Module({
|
||||
imports: [ProvidersModule, LoggingModule, AuthModule, RateLimitModule],
|
||||
controllers: [InquiryController],
|
||||
providers: [InquiryService],
|
||||
})
|
||||
export class InquiryModule {}
|
||||
@@ -0,0 +1,245 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InquiryType } from '../common/enums/inquiry-type.enum';
|
||||
import { InquiryStatus } from '../common/enums/inquiry-status.enum';
|
||||
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 { InquiryException } from '../common/exceptions/inquiry.exception';
|
||||
import { InquiryLogService } from '../logging/inquiry-log.service';
|
||||
import {
|
||||
LegacyInquiryPayload,
|
||||
PersonInquiryPayload,
|
||||
PersonInquiryResult,
|
||||
} from '../providers/shared/legacy-api.provider.abstract';
|
||||
import { ProviderOrchestratorService } from '../providers/strategy/provider-orchestrator.service';
|
||||
import { PersonInquiryRequestDto } from './dto/person-inquiry-request.dto';
|
||||
import { PersonInquiryDataDto } from './dto/person-inquiry-data.dto';
|
||||
|
||||
@Injectable()
|
||||
export class InquiryService {
|
||||
constructor(
|
||||
private readonly orchestrator: ProviderOrchestratorService,
|
||||
private readonly inquiryLogService: InquiryLogService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Person inquiry — unified business flow with provider fallback and audit logging.
|
||||
*/
|
||||
async inquirePerson(
|
||||
dto: PersonInquiryRequestDto,
|
||||
requestId: string,
|
||||
): Promise<BaseInquiryResponseDto<PersonInquiryDataDto>> {
|
||||
const trackingCode = generateTrackingCode();
|
||||
const payload: PersonInquiryPayload = {
|
||||
nationalCode: dto.nationalCode,
|
||||
birthDate: dto.birthDate,
|
||||
dateHasPostfix: (dto as PersonInquiryRequestDto & { dateHasPostfix?: number }).dateHasPostfix,
|
||||
};
|
||||
|
||||
const start = Date.now();
|
||||
|
||||
try {
|
||||
const result = await this.executeInquiry<PersonInquiryPayload, PersonInquiryResult>(
|
||||
InquiryType.PERSON,
|
||||
payload,
|
||||
requestId,
|
||||
trackingCode,
|
||||
);
|
||||
|
||||
const response = this.buildSuccessResponse(
|
||||
result.data,
|
||||
result.provider,
|
||||
trackingCode,
|
||||
result.duration,
|
||||
'Person inquiry completed successfully',
|
||||
);
|
||||
|
||||
await this.persistLog({
|
||||
inquiryType: InquiryType.PERSON,
|
||||
provider: result.provider,
|
||||
requestPayload: payload as unknown as Record<string, unknown>,
|
||||
responsePayload: response.data as unknown as Record<string, unknown>,
|
||||
status: InquiryStatus.SUCCESS,
|
||||
duration: result.duration,
|
||||
requestId,
|
||||
trackingCode,
|
||||
});
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
const duration = Date.now() - start;
|
||||
const normalized = this.extractError(error);
|
||||
|
||||
const response: BaseInquiryResponseDto<PersonInquiryDataDto> = {
|
||||
success: false,
|
||||
provider: 'GATEWAY',
|
||||
trackingCode,
|
||||
message: normalized.message,
|
||||
error: normalized,
|
||||
duration,
|
||||
};
|
||||
|
||||
await this.persistLog({
|
||||
inquiryType: InquiryType.PERSON,
|
||||
provider: ProviderName.HAMTA,
|
||||
requestPayload: payload as unknown as Record<string, unknown>,
|
||||
status: InquiryStatus.FAILURE,
|
||||
duration,
|
||||
requestId,
|
||||
trackingCode,
|
||||
error: normalized as unknown as Record<string, unknown>,
|
||||
}).catch(() => undefined);
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
async inquire(
|
||||
inquiryType: InquiryType,
|
||||
payload: LegacyInquiryPayload,
|
||||
requestId: string,
|
||||
): Promise<BaseInquiryResponseDto<Record<string, unknown>>> {
|
||||
const trackingCode = generateTrackingCode();
|
||||
const start = Date.now();
|
||||
|
||||
try {
|
||||
const result = await this.executeInquiry<LegacyInquiryPayload, unknown>(
|
||||
inquiryType,
|
||||
payload,
|
||||
requestId,
|
||||
trackingCode,
|
||||
);
|
||||
|
||||
const response = this.buildGenericSuccessResponse(
|
||||
result.data,
|
||||
result.provider,
|
||||
trackingCode,
|
||||
result.duration,
|
||||
`${this.getInquiryLabel(inquiryType)} completed successfully`,
|
||||
);
|
||||
|
||||
await this.persistLog({
|
||||
inquiryType,
|
||||
provider: result.provider,
|
||||
requestPayload: payload,
|
||||
responsePayload: response.data,
|
||||
status: InquiryStatus.SUCCESS,
|
||||
duration: result.duration,
|
||||
requestId,
|
||||
trackingCode,
|
||||
});
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
const duration = Date.now() - start;
|
||||
const normalized = this.extractError(error);
|
||||
|
||||
const response: BaseInquiryResponseDto<Record<string, unknown>> = {
|
||||
success: false,
|
||||
provider: 'GATEWAY',
|
||||
trackingCode,
|
||||
message: normalized.message,
|
||||
error: normalized,
|
||||
duration,
|
||||
};
|
||||
|
||||
await this.persistLog({
|
||||
inquiryType,
|
||||
provider: ProviderName.HAMTA,
|
||||
requestPayload: payload,
|
||||
status: InquiryStatus.FAILURE,
|
||||
duration,
|
||||
requestId,
|
||||
trackingCode,
|
||||
error: normalized as unknown as Record<string, unknown>,
|
||||
}).catch(() => undefined);
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
private async executeInquiry<TRequest, TResponse>(
|
||||
inquiryType: InquiryType,
|
||||
payload: TRequest,
|
||||
requestId: string,
|
||||
trackingCode: string,
|
||||
) {
|
||||
return this.orchestrator.executeWithFallback<TRequest, TResponse>(inquiryType, payload, {
|
||||
requestId,
|
||||
trackingCode,
|
||||
});
|
||||
}
|
||||
|
||||
private buildSuccessResponse(
|
||||
result: PersonInquiryResult,
|
||||
provider: string,
|
||||
trackingCode: string,
|
||||
duration: number,
|
||||
message: string,
|
||||
): BaseInquiryResponseDto<PersonInquiryDataDto> {
|
||||
return {
|
||||
success: true,
|
||||
provider,
|
||||
trackingCode,
|
||||
message,
|
||||
duration,
|
||||
data: {
|
||||
nationalCode: result.nationalCode,
|
||||
birthDate: result.birthDate,
|
||||
fullName: result.fullName,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
private buildGenericSuccessResponse(
|
||||
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 (result && typeof result === 'object' && 'raw' in result) {
|
||||
const raw = (result as { raw: unknown }).raw;
|
||||
return raw && typeof raw === 'object' ? (raw as Record<string, unknown>) : { raw };
|
||||
}
|
||||
|
||||
return result && typeof result === 'object'
|
||||
? (result as Record<string, unknown>)
|
||||
: { raw: result };
|
||||
}
|
||||
|
||||
private getInquiryLabel(inquiryType: InquiryType): string {
|
||||
return inquiryType.replace(/_INQUIRY$/, '').toLowerCase().replace(/_/g, ' ');
|
||||
}
|
||||
|
||||
private extractError(error: unknown): NormalizedErrorDto {
|
||||
if (error instanceof InquiryException) {
|
||||
return error.normalizedError;
|
||||
}
|
||||
if (error && typeof error === 'object' && 'normalizedError' in error) {
|
||||
return (error as { normalizedError: NormalizedErrorDto }).normalizedError;
|
||||
}
|
||||
return {
|
||||
code: 'INQUIRY_FAILED',
|
||||
message: error instanceof Error ? error.message : 'Inquiry failed',
|
||||
};
|
||||
}
|
||||
|
||||
private async persistLog(
|
||||
input: Parameters<InquiryLogService['create']>[0],
|
||||
): Promise<void> {
|
||||
await this.inquiryLogService.create(input);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user