Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sadiqumar18/6aa4f33f3b0308c2a65c307929b9b842 to your computer and use it in GitHub Desktop.
Save sadiqumar18/6aa4f33f3b0308c2a65c307929b9b842 to your computer and use it in GitHub Desktop.
CloudIssuerUtilService
import {Global, HttpStatus, Injectable, Logger} from "@nestjs/common";
import {HttpService} from "@nestjs/axios";
import {ConfigService} from "@nestjs/config";
import * as https from "node:https";
import {lastValueFrom} from "rxjs";
export interface GenerateCardUniqueKeyRequest {
mkac: string;
accountNumber: string;
sequenceNumber: string;
}
interface GenerateCardUniqueKeyResponse {
statusCode: number;
message: string;
data?: {
keyCheckValue: string;
keyUnderKek: string;
errorCode: string;
};
}
@Injectable()
@Global()
export class CloudIssuerUtilService {
private readonly baseUrl: string;
private readonly apiKey: string;
private readonly httpsAgent: https.Agent;
constructor(
private readonly httpService: HttpService,
private readonly configService: ConfigService,
) {
this.baseUrl = this.configService.get<string>('CLOUD_ISSUER_UTIL_BASE_URL', 'https://3.10.140.104:8888');
this.apiKey = this.configService.get<string>('CLOUD_ISSUER_UTIL_API_KEY', '');
// Create HTTPS agent with client certificates
this.httpsAgent = new https.Agent({
ca: this.configService.get<string>('CLOUD_ISSUER_UTIL_CA_CERT'),
rejectUnauthorized: true,
});
}
async generateCardUniqueKey(
params: GenerateCardUniqueKeyRequest,
): Promise<GenerateCardUniqueKeyResponse> {
try {
const response = await lastValueFrom(
this.httpService.post<GenerateCardUniqueKeyResponse>(
`${this.baseUrl}/hsm/generate-card-unique-key`,
JSON.stringify(params),
{
httpsAgent: this.httpsAgent,
headers: {
'Content-Type': 'application/json',
'x-api-key': this.apiKey,
},
},
),
);
return response.data;
} catch (error) {
Logger.error(error);
const statusCode = error.response?.status;
const errorMessage = error.response?.data?.message || error.message;
// Log error details if needed
Logger.error(`HSM API Error: ${statusCode} - ${errorMessage}`);
// Return standardized error response
return {
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
message: `Failed to generate card unique key: ${errorMessage}`,
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment