External API for badane

This commit is contained in:
SepehrYahyaee
2026-05-30 17:15:21 +03:30
parent 3b0db0d250
commit 84b752c6cc
2 changed files with 66 additions and 14 deletions

View File

@@ -355,6 +355,58 @@ export class SandHubService {
return await this.sandHubDbService.findOneBySandHubId(sandHubId);
}
/**
* Tejarat block inquiry for Car Body Insurance (Badane).
* Returns both raw API response and mapped old-format response structure.
*/
async getTejaratCarBodyInquiry(userDetail: SandHubDetailDto): Promise<{
raw: any;
}> {
const baseUrl =
process.env.TEJARAT_INQUIRY_BASE_URL ?? "http://82.99.202.245:3027";
// 1. Build payload following the strict data requirements of the external API
const requestPayload = {
part1: Number(userDetail.plate.leftDigits),
part2: String(userDetail.plate.centerAlphabet),
part3: Number(userDetail.plate.centerDigits),
part4: Number(userDetail.plate.ir),
nationalCode: String(userDetail.nationalCodeOfInsurer),
};
const requestUrl = `${baseUrl}/block-inquiry-tejarat/badane`;
const live = await this.useLiveSandHubApis();
let raw: any;
if (live) {
// 2. Safely obtain a fresh or cached access token using your helper
const token = await this.getTejaratAccessToken();
try {
// 3. Make the live call using NestJS HttpService + firstValueFrom pattern
const response = await firstValueFrom(
this.httpService.post(requestUrl, requestPayload, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
timeout: 30000,
}),
);
raw = response.data;
} catch (error) {
this.logger.error(
`[TEJARAT BADANE ERROR] Request failed for ${requestUrl}: ${error?.message || error}`,
error?.stack,
);
throw error;
}
}
return { raw };
}
private async makeSandHubRequest(url: string, payload: any, maxRetries = 3) {
if (!(await this.useLiveSandHubApis())) {
this.logger.log(`[MOCK] SandHub POST skipped: ${url}`);