mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-26 18:42:49 +00:00
143 lines
3.8 KiB
TypeScript
143 lines
3.8 KiB
TypeScript
import { Injectable, Logger, BadRequestException } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { PrismaService } from '../../common/prisma.service';
|
|
import axios, { AxiosInstance } from 'axios';
|
|
|
|
export interface VerifaydaPassengerData {
|
|
fullName: string;
|
|
dateOfBirth: Date;
|
|
gender?: string;
|
|
nationality?: string;
|
|
profileData?: Record<string, any>;
|
|
}
|
|
|
|
export interface VerifaydaVerificationResult {
|
|
verified: boolean;
|
|
passengerData?: VerifaydaPassengerData;
|
|
failureReason?: string;
|
|
}
|
|
|
|
@Injectable()
|
|
export class VerifaydaService {
|
|
private readonly logger = new Logger(VerifaydaService.name);
|
|
private readonly httpClient: AxiosInstance;
|
|
private readonly enabled: boolean;
|
|
private readonly apiUrl: string;
|
|
private readonly apiKey: string;
|
|
|
|
constructor(
|
|
private readonly config: ConfigService,
|
|
private readonly prisma: PrismaService,
|
|
) {
|
|
this.enabled = this.config.get<boolean>('VERIFAYDA_ENABLED', false);
|
|
this.apiUrl = this.config.get<string>('VERIFAYDA_API_URL', 'https://api.verifayda.gov.et/v2');
|
|
this.apiKey = this.config.get<string>('VERIFAYDA_API_KEY', '');
|
|
|
|
this.httpClient = axios.create({
|
|
baseURL: this.apiUrl,
|
|
timeout: 10000,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-API-Key': this.apiKey,
|
|
},
|
|
});
|
|
}
|
|
|
|
async verifyNationalId(
|
|
nationalId: string,
|
|
bookingId?: string,
|
|
): Promise<VerifaydaVerificationResult> {
|
|
if (!this.enabled) {
|
|
this.logger.warn('Verifayda is disabled - skipping verification');
|
|
return {
|
|
verified: false,
|
|
failureReason: 'Verifayda integration is disabled',
|
|
};
|
|
}
|
|
|
|
const requestPayload = {
|
|
nationalId,
|
|
requestedFields: ['fullName', 'dateOfBirth', 'gender', 'nationality'],
|
|
timestamp: new Date().toISOString(),
|
|
};
|
|
|
|
try {
|
|
this.logger.log(`Verifying national ID via Verifayda 2.0`);
|
|
|
|
const response = await this.httpClient.post('/verify', requestPayload);
|
|
|
|
const { data } = response;
|
|
|
|
if (data.status === 'verified' && data.citizen) {
|
|
const passengerData: VerifaydaPassengerData = {
|
|
fullName: data.citizen.fullName,
|
|
dateOfBirth: new Date(data.citizen.dateOfBirth),
|
|
gender: data.citizen.gender,
|
|
nationality: data.citizen.nationality || 'Ethiopian',
|
|
profileData: data.citizen,
|
|
};
|
|
|
|
await this.prisma.verifaydaVerification.create({
|
|
data: {
|
|
bookingId,
|
|
nationalId,
|
|
requestPayload,
|
|
responsePayload: data,
|
|
verified: true,
|
|
verifiedAt: new Date(),
|
|
},
|
|
});
|
|
|
|
this.logger.log('Verifayda verification successful');
|
|
|
|
return {
|
|
verified: true,
|
|
passengerData,
|
|
};
|
|
} else {
|
|
const failureReason = data.message || 'Verification failed';
|
|
|
|
await this.prisma.verifaydaVerification.create({
|
|
data: {
|
|
bookingId,
|
|
nationalId,
|
|
requestPayload,
|
|
responsePayload: data,
|
|
verified: false,
|
|
failureReason,
|
|
},
|
|
});
|
|
|
|
this.logger.warn(`Verifayda verification failed: ${failureReason}`);
|
|
|
|
return {
|
|
verified: false,
|
|
failureReason,
|
|
};
|
|
}
|
|
} catch (error: any) {
|
|
const errorMessage = error.response?.data?.message || error.message || 'Unknown error';
|
|
|
|
await this.prisma.verifaydaVerification.create({
|
|
data: {
|
|
bookingId,
|
|
nationalId,
|
|
requestPayload,
|
|
verified: false,
|
|
failureReason: errorMessage,
|
|
},
|
|
});
|
|
|
|
this.logger.error(`Verifayda API error: ${errorMessage}`);
|
|
|
|
throw new BadRequestException(
|
|
`National ID verification failed: ${errorMessage}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
isEnabled(): boolean {
|
|
return this.enabled;
|
|
}
|
|
}
|