Merge pull request 'main' (#363) from s.hajizadeh/yara724api:main into main

Reviewed-on: Yara724/api#363
This commit is contained in:
2026-10-18 10:21:52 +03:30
6 changed files with 103 additions and 44 deletions
@@ -219,6 +219,7 @@ import { HttpService } from "@nestjs/axios";
import { firstValueFrom } from "rxjs";
import {
fanavaranSubmitPath,
getFanavaranLookupBaseUrl,
getFanavaranClientProfile,
resolveFanavaranClientKey,
resolveFanavaranProductContractId,
@@ -228,10 +229,7 @@ import { buildEnrichedDamagedParts } from "src/expert-claim/dto/claim-damaged-pa
import { FanavaranAuditService } from "src/fanavaran/fanavaran-audit.service";
import type { FanavaranAuditSession } from "src/fanavaran/fanavaran-audit.types";
import { FanavaranAuthService } from "src/fanavaran/fanavaran-auth.service";
import {
FANAVARAN_LOOKUP_BASE_URL,
FANAVARAN_REMOTE_LOOKUPS,
} from "src/fanavaran/fanavaran-lookup.config";
import { FANAVARAN_REMOTE_LOOKUPS } from "src/fanavaran/fanavaran-lookup.config";
import { FanavaranLookupService } from "src/fanavaran/fanavaran-lookup.service";
import {
FanavaranAuditSource,
@@ -3306,6 +3304,7 @@ export class ClaimRequestManagementService {
): Promise<string> {
return fanavaranClaimsBaseUrl(
await this.resolveFanavaranClaimProduct(claimCaseId, blameCase),
resolveFanavaranClientKey(),
);
}
@@ -3447,7 +3446,7 @@ export class ClaimRequestManagementService {
(await this.resolveFanavaranClaimProduct(options?.claimCaseId));
const insuranceLineId =
options?.insuranceLineId ?? fanavaranInsuranceLineIdForProduct(product);
const policyInquiryUrl = `https://apimanager.iraneit.com/BimeApiManager/api/BimeApi/v2.0/common/Policies/inquiry-my-policies?InsuranceLineId=${insuranceLineId}&NationalCode=${nationalCodeOfInsurer}`;
const policyInquiryUrl = `${getFanavaranLookupBaseUrl(clientKey)}/common/Policies/inquiry-my-policies?InsuranceLineId=${insuranceLineId}&NationalCode=${nationalCodeOfInsurer}`;
const startedAt = Date.now();
const requestMeta = {
@@ -3821,7 +3820,7 @@ export class ClaimRequestManagementService {
return null;
}
const url = `${FANAVARAN_LOOKUP_BASE_URL}/common/other-people`;
const url = `${getFanavaranLookupBaseUrl(clientKey)}/common/other-people`;
const startedAt = Date.now();
const auditSession: FanavaranAuditSession = {
trackingCode: this.fanavaranAuditService.generateTrackingCode(),
@@ -4533,7 +4532,12 @@ export class ClaimRequestManagementService {
claimId: claimCase.claimId ?? null,
dmgCaseId: claimCase.dmgCaseId ?? null,
submitUrl: claimCase.claimId
? fanavaranClaimStageUrl(product, "damage-case", claimCase.claimId)
? fanavaranClaimStageUrl(
product,
"damage-case",
claimCase.claimId,
clientKey,
)
: null,
warning: warnings.length ? warnings.join(" ") : undefined,
payload,
@@ -6111,7 +6115,7 @@ export class ClaimRequestManagementService {
: built,
clientKey,
);
const url = `${fanavaranClaimsBaseUrl("car-body")}/${nestedId}`;
const url = `${fanavaranClaimsBaseUrl("car-body", clientKey)}/${nestedId}`;
this.logger.log(
`[Fanavaran hull sync] PUT ${url} keys=${Object.keys(hull).join(",")}`,
);
@@ -6248,6 +6252,7 @@ export class ClaimRequestManagementService {
product,
"damage-case",
claimCase.claimId,
clientKey,
);
if (url == null) {
this.logger.log(
@@ -1,5 +1,10 @@
import { BlameRequestType } from "src/Types&Enums/blame-request-management/blameRequestType.enum";
import { PartyRole } from "src/request-management/entities/schema/partyRole.enum";
import {
getFanavaranLookupBaseUrl,
resolveFanavaranClientKey,
type FanavaranClientKey,
} from "src/core/config/fanavaran-client.config";
import {
FANAVARAN_CAR_BODY_LINE_ID,
FANAVARAN_THIRD_PARTY_LINE_ID,
@@ -12,9 +17,6 @@ import type { InquiryParticipantRoleAssignments } from "src/request-management/i
export type FanavaranClaimProduct = FanavaranCarPolicyProduct;
const FANAVARAN_CLAIMS_HOST =
"https://apimanager.iraneit.com/BimeApiManager/api/BimeApi/v2.0/car";
export function fanavaranClaimProductFromBlameType(
type?: string | null,
): FanavaranClaimProduct {
@@ -44,8 +46,9 @@ export function fanavaranClaimsResourcePath(
export function fanavaranClaimsBaseUrl(
product: FanavaranClaimProduct,
clientKey: FanavaranClientKey = resolveFanavaranClientKey(),
): string {
return `${FANAVARAN_CLAIMS_HOST}/${fanavaranClaimsResourcePath(product)}`;
return `${getFanavaranLookupBaseUrl(clientKey)}/car/${fanavaranClaimsResourcePath(product)}`;
}
export type FanavaranClaimStage =
@@ -66,14 +69,17 @@ export function fanavaranHasDamageCaseStage(
return product === "third-party";
}
export function fanavaranHullImportClaimsUrl(): string {
return `${FANAVARAN_CLAIMS_HOST}/${FANAVARAN_HULL_IMPORT_CLAIMS_PATH}`;
export function fanavaranHullImportClaimsUrl(
clientKey: FanavaranClientKey = resolveFanavaranClientKey(),
): string {
return `${getFanavaranLookupBaseUrl(clientKey)}/car/${FANAVARAN_HULL_IMPORT_CLAIMS_PATH}`;
}
export function fanavaranClaimStageUrl(
product: FanavaranClaimProduct,
stage: FanavaranClaimStage,
claimId: number | string,
clientKey: FanavaranClientKey = resolveFanavaranClientKey(),
): string | null {
if (stage === "damage-case" && !fanavaranHasDamageCaseStage(product)) {
return null;
@@ -89,7 +95,7 @@ export function fanavaranClaimStageUrl(
: stage === "culprits"
? "culprits"
: "expertise";
return `${fanavaranClaimsBaseUrl(product)}/${claimId}/${suffix}`;
return `${fanavaranClaimsBaseUrl(product, clientKey)}/${claimId}/${suffix}`;
}
export function fanavaranInsuranceLineIdForProduct(
@@ -1,5 +1,21 @@
export type FanavaranClientKey = "parsian" | "tejaratno" | "moallem";
const FANAVARAN_API_ROOTS: Record<FanavaranClientKey, string> = {
parsian: "https://apimanager.iraneit.com/BimeApiManager/api",
tejaratno: "https://apimanager.iraneit.com/BimeApiManager/api",
moallem: "https://api.inssw.ir/BimeApiManager/api",
};
export function getFanavaranApiRoot(clientKey: FanavaranClientKey): string {
return FANAVARAN_API_ROOTS[clientKey];
}
export function getFanavaranLookupBaseUrl(
clientKey: FanavaranClientKey,
): string {
return `${getFanavaranApiRoot(clientKey)}/BimeApi/v2.0`;
}
export const FANAVARAN_CLIENT_KEYS: readonly FanavaranClientKey[] = [
"parsian",
"tejaratno",
+36 -15
View File
@@ -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(
+20 -13
View File
@@ -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) {