forked from Yara724/api
fanavaran rate bugs fixed + sms of claimId and claimNo added
This commit is contained in:
@@ -9,26 +9,22 @@ import {
|
||||
} from "@nestjs/common";
|
||||
import { firstValueFrom } from "rxjs";
|
||||
import { isAxiosError } from "axios";
|
||||
import {
|
||||
getFanavaranClientProfile,
|
||||
type FanavaranClientKey,
|
||||
} from "src/core/config/fanavaran-client.config";
|
||||
import { type FanavaranClientKey } from "src/core/config/fanavaran-client.config";
|
||||
import {
|
||||
FANAVARAN_LOOKUP_BASE_URL,
|
||||
fanavaranLookupCacheDir,
|
||||
tejaratStaticAccidentFilePath,
|
||||
} from "./fanavaran-lookup.config";
|
||||
import { FanavaranAuthService } from "./fanavaran-auth.service";
|
||||
|
||||
@Injectable()
|
||||
export class FanavaranLookupService {
|
||||
private readonly logger = new Logger(FanavaranLookupService.name);
|
||||
|
||||
private readonly getAppTokenUrl =
|
||||
"https://apimanager.iraneit.com/BimeApiManager/api/EITAuthentication/GetAppToken";
|
||||
private readonly loginUrl =
|
||||
"https://apimanager.iraneit.com/BimeApiManager/api/EITAuthentication/Login";
|
||||
|
||||
constructor(private readonly httpService: HttpService) {}
|
||||
constructor(
|
||||
private readonly httpService: HttpService,
|
||||
private readonly fanavaranAuthService: FanavaranAuthService,
|
||||
) {}
|
||||
|
||||
private cacheFilePath(clientKey: FanavaranClientKey, fileName: string): string {
|
||||
return join(fanavaranLookupCacheDir(clientKey), fileName);
|
||||
@@ -71,91 +67,12 @@ export class FanavaranLookupService {
|
||||
return JSON.parse(cached) as T;
|
||||
}
|
||||
|
||||
private async getAppToken(config: {
|
||||
appName: string;
|
||||
secret: string;
|
||||
}): Promise<string> {
|
||||
const response = await firstValueFrom(
|
||||
this.httpService.post(this.getAppTokenUrl, "", {
|
||||
headers: {
|
||||
appname: config.appName,
|
||||
secret: config.secret,
|
||||
"Content-Length": "0",
|
||||
},
|
||||
transformRequest: [
|
||||
(_data, headers) => {
|
||||
if (headers) {
|
||||
delete headers["Content-Type"];
|
||||
delete headers["content-type"];
|
||||
}
|
||||
return _data;
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
|
||||
const appToken =
|
||||
response.headers.apptoken ||
|
||||
response.headers.appToken ||
|
||||
response.headers["apptoken"] ||
|
||||
response.headers["appToken"];
|
||||
|
||||
if (!appToken) {
|
||||
throw new BadGatewayException("Failed to get Fanavaran appToken");
|
||||
}
|
||||
|
||||
return appToken;
|
||||
}
|
||||
|
||||
private async login(
|
||||
appToken: string,
|
||||
config: { username: string; password: string },
|
||||
): Promise<string> {
|
||||
const response = await firstValueFrom(
|
||||
this.httpService.post(this.loginUrl, "", {
|
||||
headers: {
|
||||
appToken,
|
||||
userName: config.username,
|
||||
password: config.password,
|
||||
"Content-Length": "0",
|
||||
},
|
||||
transformRequest: [
|
||||
(_data, headers) => {
|
||||
if (headers) {
|
||||
delete headers["Content-Type"];
|
||||
delete headers["content-type"];
|
||||
}
|
||||
return _data;
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
|
||||
const authenticationToken =
|
||||
response.headers.authenticationtoken ||
|
||||
response.headers.authenticationToken ||
|
||||
response.headers["authenticationtoken"] ||
|
||||
response.headers["authenticationToken"] ||
|
||||
response.data?.authenticationtoken ||
|
||||
response.data?.authenticationToken ||
|
||||
response.data?.authentication_token;
|
||||
|
||||
if (!authenticationToken) {
|
||||
throw new BadGatewayException("Failed to get Fanavaran authenticationToken");
|
||||
}
|
||||
|
||||
return authenticationToken;
|
||||
}
|
||||
|
||||
async fetchFromFanavaran(
|
||||
clientKey: FanavaranClientKey,
|
||||
url: string,
|
||||
): Promise<unknown> {
|
||||
const profile = getFanavaranClientProfile(clientKey);
|
||||
|
||||
try {
|
||||
const appToken = await this.getAppToken(profile.auth);
|
||||
const authenticationToken = await this.login(appToken, profile.auth);
|
||||
const headers = await this.fanavaranAuthService.getRequestHeaders(clientKey);
|
||||
|
||||
this.logger.log(
|
||||
`[${clientKey}] Calling Fanavaran lookup API: ${url}`,
|
||||
@@ -164,10 +81,7 @@ export class FanavaranLookupService {
|
||||
const response = await firstValueFrom(
|
||||
this.httpService.get(url, {
|
||||
headers: {
|
||||
authenticationToken,
|
||||
CorpId: profile.auth.corpId,
|
||||
ContractId: profile.auth.contractId,
|
||||
Location: profile.auth.location,
|
||||
...headers,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
timeout: 20000,
|
||||
@@ -184,8 +98,10 @@ export class FanavaranLookupService {
|
||||
`[${clientKey}] Fanavaran lookup response status=${response.status} dataCount=${dataCount}`,
|
||||
);
|
||||
|
||||
this.fanavaranAuthService.clearBackoff(clientKey);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
this.fanavaranAuthService.registerFailure(clientKey, error);
|
||||
const message = isAxiosError(error)
|
||||
? error.response?.data?.Message ||
|
||||
error.response?.data?.message ||
|
||||
|
||||
Reference in New Issue
Block a user