forked from Yara724/api
blame and claim refactored
This commit is contained in:
@@ -20,6 +20,10 @@ export class SandHubService {
|
||||
private loginToken: string | null = null;
|
||||
private tokenExpiry: Date | null = null;
|
||||
|
||||
// Tejarat inquiry auth (82.99.202.245:3027)
|
||||
private tejaratAccessToken: string | null = null;
|
||||
private tejaratTokenExpiry: Date | null = null;
|
||||
|
||||
constructor(
|
||||
private readonly httpService: HttpService,
|
||||
private readonly sandHubDbService: SandHubDbService,
|
||||
@@ -62,6 +66,128 @@ export class SandHubService {
|
||||
}
|
||||
}
|
||||
|
||||
private async getTejaratAccessToken(): Promise<string> {
|
||||
if (
|
||||
this.tejaratAccessToken &&
|
||||
this.tejaratTokenExpiry &&
|
||||
this.tejaratTokenExpiry > new Date()
|
||||
) {
|
||||
return this.tejaratAccessToken;
|
||||
}
|
||||
|
||||
const baseUrl =
|
||||
process.env.TEJARAT_INQUIRY_BASE_URL ?? "http://82.99.202.245:3027";
|
||||
const email = process.env.TEJARAT_INQUIRY_EMAIL;
|
||||
const password = process.env.TEJARAT_INQUIRY_PASSWORD;
|
||||
|
||||
if (!email || !password) {
|
||||
throw new UnauthorizedException(
|
||||
"Tejarat inquiry credentials are not configured (TEJARAT_INQUIRY_EMAIL/TEJARAT_INQUIRY_PASSWORD)",
|
||||
);
|
||||
}
|
||||
|
||||
this.logger.log(
|
||||
"Tejarat inquiry token is missing or expired. Fetching a new one...",
|
||||
);
|
||||
|
||||
try {
|
||||
const response = await this.httpService.axiosRef.post(
|
||||
`${baseUrl}/user/login`,
|
||||
{ email, password },
|
||||
{
|
||||
headers: {
|
||||
Accept: "*/*",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
timeout: 30000,
|
||||
},
|
||||
);
|
||||
|
||||
const token = response.data?.accessToken;
|
||||
if (!token) {
|
||||
throw new Error("No access token returned from Tejarat login");
|
||||
}
|
||||
|
||||
this.tejaratAccessToken = token;
|
||||
// No explicit expiry in response; keep a conservative cache window.
|
||||
this.tejaratTokenExpiry = new Date(Date.now() + 55 * 60 * 1000);
|
||||
|
||||
return this.tejaratAccessToken;
|
||||
} catch (er) {
|
||||
this.logger.error("Failed to login to Tejarat inquiry:", er?.message || er);
|
||||
this.tejaratAccessToken = null;
|
||||
this.tejaratTokenExpiry = null;
|
||||
throw new UnauthorizedException("Tejarat inquiry authentication failed");
|
||||
}
|
||||
}
|
||||
|
||||
private async makeTejaratRequest(url: string, payload: any, maxRetries = 2) {
|
||||
const INITIAL_DELAY = 500;
|
||||
const BACKOFF_FACTOR = 2;
|
||||
|
||||
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
||||
const token = await this.getTejaratAccessToken();
|
||||
try {
|
||||
const response = await axios.post(url, payload, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
},
|
||||
timeout: 30000,
|
||||
});
|
||||
if (!response?.data) throw new Error("EMPTY_RESPONSE");
|
||||
return response.data;
|
||||
} catch (err: any) {
|
||||
const status = err?.response?.status;
|
||||
const data = err?.response?.data;
|
||||
|
||||
this.logger.error(
|
||||
`Tejarat request failed (attempt ${attempt + 1}/${maxRetries}) to ${url} with status ${
|
||||
status ?? "NO_STATUS"
|
||||
}`,
|
||||
data ? JSON.stringify(data) : err?.message || err,
|
||||
);
|
||||
|
||||
// If unauthorized, clear token and retry once
|
||||
if (status === 401) {
|
||||
this.tejaratAccessToken = null;
|
||||
this.tejaratTokenExpiry = null;
|
||||
}
|
||||
|
||||
const delay = INITIAL_DELAY * Math.pow(BACKOFF_FACTOR, attempt);
|
||||
await new Promise((resolve) => setTimeout(resolve, delay));
|
||||
if (attempt === maxRetries - 1) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tejarat block inquiry (replaces SandHub call for V2 flows).
|
||||
* Returns both raw + mapped (old-format) response.
|
||||
*/
|
||||
async getTejaratBlockInquiry(userDetail: SandHubDetailDto): Promise<{
|
||||
raw: any;
|
||||
mapped: any;
|
||||
}> {
|
||||
const baseUrl =
|
||||
process.env.TEJARAT_INQUIRY_BASE_URL ?? "http://82.99.202.245:3027";
|
||||
const requestPayload = {
|
||||
leftTwoDigits: String(userDetail.plate.leftDigits),
|
||||
serialLetter: String(userDetail.plate.centerAlphabet),
|
||||
threeDigits: String(userDetail.plate.centerDigits),
|
||||
rightTwoDigits: String(userDetail.plate.ir),
|
||||
nationalCode: userDetail.nationalCodeOfInsurer,
|
||||
};
|
||||
|
||||
const requestUrl = `${baseUrl}/block-inquiry-tejarat`;
|
||||
const raw = await this.makeTejaratRequest(requestUrl, requestPayload);
|
||||
const mapped = this.mapNewApiResponseToOldFormat(raw);
|
||||
return { raw, mapped };
|
||||
}
|
||||
|
||||
async getSandHubDataFromId(sandHubId) {
|
||||
return await this.sandHubDbService.findOneBySandHubId(sandHubId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user