forked from Yara724/api
urls dynamic integration
This commit is contained in:
@@ -11,6 +11,7 @@ import { isAxiosError } from "axios";
|
||||
import { Model } from "mongoose";
|
||||
import { firstValueFrom } from "rxjs";
|
||||
import {
|
||||
getFanavaranApiRoot,
|
||||
getFanavaranClientProfile,
|
||||
type FanavaranAuthConfig,
|
||||
type FanavaranClientKey,
|
||||
@@ -44,10 +45,13 @@ interface TenantBackoffState {
|
||||
export class FanavaranAuthService {
|
||||
private readonly logger = new Logger(FanavaranAuthService.name);
|
||||
|
||||
private readonly getAppTokenUrl =
|
||||
"https://apimanager.iraneit.com/BimeApiManager/api/EITAuthentication/GetAppToken";
|
||||
private readonly loginUrl =
|
||||
"https://apimanager.iraneit.com/BimeApiManager/api/EITAuthentication/Login";
|
||||
private getAppTokenUrl(clientKey: FanavaranClientKey): string {
|
||||
return `${getFanavaranApiRoot(clientKey)}/EITAuthentication/GetAppToken`;
|
||||
}
|
||||
|
||||
private loginUrl(clientKey: FanavaranClientKey): string {
|
||||
return `${getFanavaranApiRoot(clientKey)}/EITAuthentication/Login`;
|
||||
}
|
||||
|
||||
/** When Fanavaran says "try again later", pause all tenant calls. */
|
||||
static readonly TRANSIENT_BACKOFF_MS = 5 * 60 * 1000;
|
||||
@@ -181,8 +185,12 @@ export class FanavaranAuthService {
|
||||
* Stable fingerprint of tenant auth. Any change (location, credentials, …)
|
||||
* forces a fresh GetAppToken+Login even before Tehran midnight.
|
||||
*/
|
||||
static authFingerprint(auth: FanavaranAuthConfig): string {
|
||||
static authFingerprint(
|
||||
clientKey: FanavaranClientKey,
|
||||
auth: FanavaranAuthConfig,
|
||||
): string {
|
||||
return [
|
||||
getFanavaranApiRoot(clientKey),
|
||||
auth.appName,
|
||||
auth.secret,
|
||||
auth.username,
|
||||
@@ -211,6 +219,7 @@ export class FanavaranAuthService {
|
||||
): Promise<string> {
|
||||
this.assertNotInBackoff(clientKey);
|
||||
const fingerprint = FanavaranAuthService.authFingerprint(
|
||||
clientKey,
|
||||
getFanavaranClientProfile(clientKey).auth,
|
||||
);
|
||||
|
||||
@@ -277,9 +286,17 @@ export class FanavaranAuthService {
|
||||
auditSession?: FanavaranAuditSession,
|
||||
): Promise<string> {
|
||||
const profile = getFanavaranClientProfile(clientKey);
|
||||
const fingerprint = FanavaranAuthService.authFingerprint(profile.auth);
|
||||
const appToken = await this.fetchAppToken(profile.auth, auditSession);
|
||||
const fingerprint = FanavaranAuthService.authFingerprint(
|
||||
clientKey,
|
||||
profile.auth,
|
||||
);
|
||||
const appToken = await this.fetchAppToken(
|
||||
clientKey,
|
||||
profile.auth,
|
||||
auditSession,
|
||||
);
|
||||
const authenticationToken = await this.fetchLoginToken(
|
||||
clientKey,
|
||||
appToken,
|
||||
profile.auth,
|
||||
auditSession,
|
||||
@@ -413,9 +430,11 @@ export class FanavaranAuthService {
|
||||
}
|
||||
|
||||
private async fetchAppToken(
|
||||
clientKey: FanavaranClientKey,
|
||||
config: Pick<FanavaranAuthConfig, "appName" | "secret">,
|
||||
auditSession?: FanavaranAuditSession,
|
||||
): Promise<string> {
|
||||
const url = this.getAppTokenUrl(clientKey);
|
||||
const startedAt = Date.now();
|
||||
const requestHeaders = {
|
||||
appname: config.appName,
|
||||
@@ -427,7 +446,7 @@ export class FanavaranAuthService {
|
||||
session: auditSession,
|
||||
step: FanavaranAuditStep.GET_APP_TOKEN,
|
||||
status: FanavaranAuditStatus.STARTED,
|
||||
requestUrl: this.getAppTokenUrl,
|
||||
requestUrl: url,
|
||||
requestMethod: "POST",
|
||||
requestHeaders,
|
||||
requestBody: "",
|
||||
@@ -437,7 +456,7 @@ export class FanavaranAuthService {
|
||||
|
||||
try {
|
||||
const response = await firstValueFrom(
|
||||
this.httpService.post(this.getAppTokenUrl, "", {
|
||||
this.httpService.post(url, "", {
|
||||
headers: requestHeaders,
|
||||
transformRequest: this.emptyBodyTransformRequest(),
|
||||
}),
|
||||
@@ -462,7 +481,7 @@ export class FanavaranAuthService {
|
||||
session: auditSession,
|
||||
step: FanavaranAuditStep.GET_APP_TOKEN,
|
||||
status: FanavaranAuditStatus.SUCCESS,
|
||||
requestUrl: this.getAppTokenUrl,
|
||||
requestUrl: url,
|
||||
requestMethod: "POST",
|
||||
httpStatus: response.status,
|
||||
requestHeaders: exchange.requestHeaders,
|
||||
@@ -485,7 +504,7 @@ export class FanavaranAuthService {
|
||||
session: auditSession,
|
||||
step: FanavaranAuditStep.GET_APP_TOKEN,
|
||||
status: FanavaranAuditStatus.FAILURE,
|
||||
requestUrl: this.getAppTokenUrl,
|
||||
requestUrl: url,
|
||||
requestMethod: "POST",
|
||||
httpStatus: exchange.httpStatus,
|
||||
requestHeaders: exchange.requestHeaders,
|
||||
@@ -506,10 +525,12 @@ export class FanavaranAuthService {
|
||||
}
|
||||
|
||||
private async fetchLoginToken(
|
||||
clientKey: FanavaranClientKey,
|
||||
appToken: string,
|
||||
config: Pick<FanavaranAuthConfig, "username" | "password">,
|
||||
auditSession?: FanavaranAuditSession,
|
||||
): Promise<string> {
|
||||
const url = this.loginUrl(clientKey);
|
||||
const startedAt = Date.now();
|
||||
const requestHeaders = {
|
||||
appToken,
|
||||
@@ -522,7 +543,7 @@ export class FanavaranAuthService {
|
||||
session: auditSession,
|
||||
step: FanavaranAuditStep.LOGIN,
|
||||
status: FanavaranAuditStatus.STARTED,
|
||||
requestUrl: this.loginUrl,
|
||||
requestUrl: url,
|
||||
requestMethod: "POST",
|
||||
requestHeaders,
|
||||
requestBody: "",
|
||||
@@ -532,7 +553,7 @@ export class FanavaranAuthService {
|
||||
|
||||
try {
|
||||
const response = await firstValueFrom(
|
||||
this.httpService.post(this.loginUrl, "", {
|
||||
this.httpService.post(url, "", {
|
||||
headers: requestHeaders,
|
||||
transformRequest: this.emptyBodyTransformRequest(),
|
||||
}),
|
||||
@@ -562,7 +583,7 @@ export class FanavaranAuthService {
|
||||
session: auditSession,
|
||||
step: FanavaranAuditStep.LOGIN,
|
||||
status: FanavaranAuditStatus.SUCCESS,
|
||||
requestUrl: this.loginUrl,
|
||||
requestUrl: url,
|
||||
requestMethod: "POST",
|
||||
httpStatus: response.status,
|
||||
requestHeaders: exchange.requestHeaders,
|
||||
@@ -588,7 +609,7 @@ export class FanavaranAuthService {
|
||||
session: auditSession,
|
||||
step: FanavaranAuditStep.LOGIN,
|
||||
status: FanavaranAuditStatus.FAILURE,
|
||||
requestUrl: this.loginUrl,
|
||||
requestUrl: url,
|
||||
requestMethod: "POST",
|
||||
httpStatus: exchange.httpStatus,
|
||||
requestHeaders: exchange.requestHeaders,
|
||||
|
||||
@@ -71,6 +71,7 @@ export class FanavaranClientConfigService implements OnModuleInit {
|
||||
previousFingerprints.set(
|
||||
key,
|
||||
FanavaranAuthService.authFingerprint(
|
||||
key,
|
||||
getFanavaranClientProfile(key).auth,
|
||||
),
|
||||
);
|
||||
@@ -110,7 +111,10 @@ export class FanavaranClientConfigService implements OnModuleInit {
|
||||
setFanavaranClientProfilesCache(cache);
|
||||
|
||||
for (const key of FANAVARAN_CLIENT_KEYS) {
|
||||
const next = FanavaranAuthService.authFingerprint(cache[key]!.auth);
|
||||
const next = FanavaranAuthService.authFingerprint(
|
||||
key,
|
||||
cache[key]!.auth,
|
||||
);
|
||||
const prev = previousFingerprints.get(key);
|
||||
if (prev && prev !== next) {
|
||||
this.logger.warn(
|
||||
|
||||
@@ -11,7 +11,10 @@ import { InjectModel } from "@nestjs/mongoose";
|
||||
import { Model } from "mongoose";
|
||||
import { firstValueFrom } from "rxjs";
|
||||
import { isAxiosError } from "axios";
|
||||
import { type FanavaranClientKey } from "src/core/config/fanavaran-client.config";
|
||||
import {
|
||||
getFanavaranLookupBaseUrl,
|
||||
type FanavaranClientKey,
|
||||
} from "src/core/config/fanavaran-client.config";
|
||||
import {
|
||||
LookupModel,
|
||||
type LookupDocument,
|
||||
@@ -184,7 +187,11 @@ export class FanavaranLookupService {
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await this.fetchFromFanavaran(clientKey, url);
|
||||
const tenantUrl = url.replace(
|
||||
FANAVARAN_LOOKUP_BASE_URL,
|
||||
getFanavaranLookupBaseUrl(clientKey),
|
||||
);
|
||||
const data = await this.fetchFromFanavaran(clientKey, tenantUrl);
|
||||
await this.writeCacheFile(clientKey, cacheFile, data);
|
||||
return data;
|
||||
} catch (error) {
|
||||
@@ -249,7 +256,7 @@ export class FanavaranLookupService {
|
||||
vin: string,
|
||||
options?: { contractIdOverride?: string },
|
||||
): Promise<unknown> {
|
||||
const url = `${FANAVARAN_LOOKUP_BASE_URL}/car/vehicles/inquiry-by-vin?vin=${encodeURIComponent(vin)}`;
|
||||
const url = `${getFanavaranLookupBaseUrl(clientKey)}/car/vehicles/inquiry-by-vin?vin=${encodeURIComponent(vin)}`;
|
||||
return this.fetchFromFanavaran(clientKey, url, options);
|
||||
}
|
||||
|
||||
@@ -260,7 +267,7 @@ export class FanavaranLookupService {
|
||||
options?: { contractIdOverride?: string },
|
||||
): Promise<unknown> {
|
||||
const url =
|
||||
`${FANAVARAN_LOOKUP_BASE_URL}/common/Policies/inquiry-my-policies` +
|
||||
`${getFanavaranLookupBaseUrl(clientKey)}/common/Policies/inquiry-my-policies` +
|
||||
`?InsuranceLineId=${insuranceLineId}` +
|
||||
`&NationalCode=${encodeURIComponent(nationalCode)}`;
|
||||
return this.fetchFromFanavaran(clientKey, url, options);
|
||||
@@ -271,7 +278,7 @@ export class FanavaranLookupService {
|
||||
policyId: number,
|
||||
options?: { contractIdOverride?: string },
|
||||
): Promise<unknown> {
|
||||
const url = `${FANAVARAN_LOOKUP_BASE_URL}/car/third-party-car-policies/${policyId}`;
|
||||
const url = `${getFanavaranLookupBaseUrl(clientKey)}/car/third-party-car-policies/${policyId}`;
|
||||
return this.fetchFromFanavaran(clientKey, url, options);
|
||||
}
|
||||
|
||||
@@ -280,7 +287,7 @@ export class FanavaranLookupService {
|
||||
policyId: number,
|
||||
options?: { contractIdOverride?: string },
|
||||
): Promise<unknown> {
|
||||
const url = `${FANAVARAN_LOOKUP_BASE_URL}/car/vehicle-hull-policies/${policyId}`;
|
||||
const url = `${getFanavaranLookupBaseUrl(clientKey)}/car/vehicle-hull-policies/${policyId}`;
|
||||
return this.fetchFromFanavaran(clientKey, url, options);
|
||||
}
|
||||
|
||||
@@ -290,7 +297,7 @@ export class FanavaranLookupService {
|
||||
policyId: number,
|
||||
options?: { contractIdOverride?: string },
|
||||
): Promise<unknown> {
|
||||
const url = `${FANAVARAN_LOOKUP_BASE_URL}/car/vehicle-hull-policies/${policyId}/dmg-accessories`;
|
||||
const url = `${getFanavaranLookupBaseUrl(clientKey)}/car/vehicle-hull-policies/${policyId}/dmg-accessories`;
|
||||
return this.fetchFromFanavaran(clientKey, url, options);
|
||||
}
|
||||
|
||||
@@ -301,7 +308,7 @@ export class FanavaranLookupService {
|
||||
options?: { contractIdOverride?: string },
|
||||
): Promise<unknown> {
|
||||
const query = versionNo == null ? "" : `?versionno=${versionNo}`;
|
||||
const url = `${FANAVARAN_LOOKUP_BASE_URL}/car/vehicles/${vehicleId}${query}`;
|
||||
const url = `${getFanavaranLookupBaseUrl(clientKey)}/car/vehicles/${vehicleId}${query}`;
|
||||
return this.fetchFromFanavaran(clientKey, url, options);
|
||||
}
|
||||
|
||||
@@ -310,7 +317,7 @@ export class FanavaranLookupService {
|
||||
customerId: number,
|
||||
options?: { contractIdOverride?: string },
|
||||
): Promise<unknown> {
|
||||
const url = `${FANAVARAN_LOOKUP_BASE_URL}/common/customers/${customerId}`;
|
||||
const url = `${getFanavaranLookupBaseUrl(clientKey)}/common/customers/${customerId}`;
|
||||
return this.fetchFromFanavaran(clientKey, url, options);
|
||||
}
|
||||
|
||||
@@ -324,7 +331,7 @@ export class FanavaranLookupService {
|
||||
},
|
||||
): Promise<unknown> {
|
||||
const url =
|
||||
`${FANAVARAN_LOOKUP_BASE_URL}/common/parties/inquiry-by-unique-identifier` +
|
||||
`${getFanavaranLookupBaseUrl(clientKey)}/common/parties/inquiry-by-unique-identifier` +
|
||||
`?NationalCode=${encodeURIComponent(params.nationalCode)}` +
|
||||
`&BirthYear=${params.birthYear}` +
|
||||
`&BirthMonth=${params.birthMonth}` +
|
||||
@@ -336,7 +343,7 @@ export class FanavaranLookupService {
|
||||
clientKey: FanavaranClientKey,
|
||||
personId: number,
|
||||
): Promise<unknown> {
|
||||
const url = `${FANAVARAN_LOOKUP_BASE_URL}/common/other-people/${personId}`;
|
||||
const url = `${getFanavaranLookupBaseUrl(clientKey)}/common/other-people/${personId}`;
|
||||
return this.fetchFromFanavaran(clientKey, url);
|
||||
}
|
||||
|
||||
@@ -344,7 +351,7 @@ export class FanavaranLookupService {
|
||||
clientKey: FanavaranClientKey,
|
||||
payload: Record<string, unknown>,
|
||||
): Promise<unknown> {
|
||||
const url = `${FANAVARAN_LOOKUP_BASE_URL}/common/other-people`;
|
||||
const url = `${getFanavaranLookupBaseUrl(clientKey)}/common/other-people`;
|
||||
return this.postToFanavaran(clientKey, url, payload);
|
||||
}
|
||||
|
||||
@@ -407,7 +414,7 @@ export class FanavaranLookupService {
|
||||
|
||||
// Try fetching the full list directly from Fanavaran
|
||||
let companies: unknown;
|
||||
const url = `${FANAVARAN_LOOKUP_BASE_URL}/common/code-list/insurance-corp`;
|
||||
const url = `${getFanavaranLookupBaseUrl(clientKey)}/common/code-list/insurance-corp`;
|
||||
try {
|
||||
companies = await this.fetchFromFanavaran(clientKey, url);
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user