Fixed mock data

This commit is contained in:
SepehrYahyaee
2026-06-20 15:48:39 +03:30
parent 4fabed77e5
commit 59eddb8e0e
3 changed files with 197 additions and 51 deletions

View File

@@ -142,11 +142,44 @@ export class SandHubService {
};
}
/** Build {@link SandHubDetailDto} from Tejarat badane / block-inquiry request bodies. */
private tejaratPayloadToSandHubDetail(payload: {
nationalCode?: string | number;
part1?: string | number;
part2?: string | number;
part3?: string | number;
part4?: string | number;
leftTwoDigits?: string | number;
serialLetter?: string | number;
threeDigits?: string | number;
rightTwoDigits?: string | number;
}): SandHubDetailDto {
const nationalCode = String(payload.nationalCode ?? "");
return {
nationalCodeOfInsurer: nationalCode,
plate: {
leftDigits: Number(payload.part1 ?? payload.leftTwoDigits ?? 16),
centerAlphabet: String(
payload.part2 ?? payload.serialLetter ?? "12",
),
centerDigits: Number(payload.part3 ?? payload.threeDigits ?? 498),
ir: Number(payload.part4 ?? payload.rightTwoDigits ?? 60),
nationalCode,
},
};
}
private buildMockCarBodyInquiryRaw(
userDetail: SandHubDetailDto,
ctx: MockInquiryCompanyContext,
): Record<string, unknown> {
const companyId = Number(ctx.companyId);
const plate = userDetail?.plate;
const platePartOne = Number(plate?.leftDigits ?? 16);
const platePartThree = Number(plate?.centerDigits ?? 498);
const plateSerialNumber = Number(plate?.ir ?? 60);
const plateLetterid = Number(plate?.centerAlphabet ?? 12);
const nationalCode = String(userDetail?.nationalCodeOfInsurer ?? "");
return {
data: {
@@ -163,19 +196,19 @@ export class SandHubService {
vin: "LFP8C7PC3R1K12157",
plateTypeId: 9,
plateTypeTitle: "پلاک قدیمی",
platePartOne: 16,
plateSerialNumber: 60,
plateLetterid: 12,
platePartOne,
plateSerialNumber,
plateLetterid,
plateLetterTitle: "م ",
vehicleSystemTitle: "فائو ( FAW )",
vehicleGroupId: 2,
vehicleGroupTitle: "سواری چهار سیلندر",
insurerNationalCode: userDetail.nationalCodeOfInsurer,
insurerNationalCode: nationalCode,
insurerName: "هانيه کارخانهء",
ownerNationalCode: userDetail.nationalCodeOfInsurer,
ownerNationalCode: nationalCode,
previousId: 70006331822,
noLossYearsCount: 4,
platePartThree: 498,
platePartThree,
lossDocuments: [],
},
isSuccess: true,
@@ -184,6 +217,23 @@ export class SandHubService {
};
}
private buildMockInquiryRaw(
inquiryType: ExternalInquiryType,
ctx: MockInquiryCompanyContext,
payload?: unknown,
userDetail?: SandHubDetailDto,
): Record<string, unknown> {
if (inquiryType === "carBodyPlate") {
const detail =
userDetail ??
this.tejaratPayloadToSandHubDetail(
(payload ?? {}) as Record<string, unknown>,
);
return this.buildMockCarBodyInquiryRaw(detail, ctx);
}
return this.buildMockPlateInquiryRaw(ctx);
}
/**
* Bodies returned from `makeSandHubRequest` in mock mode (same shape callers expect from real API).
*/
@@ -229,12 +279,27 @@ export class SandHubService {
Message: "mock-sheba-ok",
};
}
if (u.includes("badane")) {
return (
plateMock ??
this.buildMockCarBodyInquiryRaw(
this.tejaratPayloadToSandHubDetail(
(payload ?? {}) as Record<string, unknown>,
),
{
companyId: process.env.CLIENT_ID ?? "15",
companyName: process.env.CLIENT_NAME ?? "بیمه سامان",
},
)
);
}
if (u.includes("block-inquiry") || u.includes("tejarat")) {
return this.mapNewApiResponseToOldFormat(
plateMock ?? this.buildMockPlateInquiryRaw({
companyId: process.env.CLIENT_ID ?? "15",
companyName: process.env.CLIENT_NAME ?? "بیمه سامان",
}),
plateMock ??
this.buildMockPlateInquiryRaw({
companyId: process.env.CLIENT_ID ?? "15",
companyName: process.env.CLIENT_NAME ?? "بیمه سامان",
}),
);
}
this.logger.warn(
@@ -402,9 +467,6 @@ export class SandHubService {
if (!(await this.isInquiryLive(inquiryType, options))) {
this.logger.log(`[MOCK] ESG POST skipped (${inquiryType}): ${url}`);
const ctx = await this.mockCompanyContext(options);
if (inquiryType === "thirdPartyPlate") {
return this.buildMockPlateInquiryRaw(ctx);
}
if (inquiryType === "personalIdentity") {
const nin =
typeof payload?.nationalCode === "string"
@@ -419,7 +481,7 @@ export class SandHubService {
Message: "mock-sheba-ok",
};
}
return this.buildMockPlateInquiryRaw(ctx);
return this.buildMockInquiryRaw(inquiryType, ctx, payload);
}
const INITIAL_DELAY = 500;
@@ -613,7 +675,7 @@ export class SandHubService {
if (!(await this.isInquiryLive(inquiryType, options))) {
this.logger.log(`[MOCK] Tejarat POST skipped (${inquiryType}): ${url}`);
const ctx = await this.mockCompanyContext(options);
return this.buildMockPlateInquiryRaw(ctx);
return this.buildMockInquiryRaw(inquiryType, ctx, payload);
}
const INITIAL_DELAY = 500;
const BACKOFF_FACTOR = 2;
@@ -753,38 +815,19 @@ export class SandHubService {
const requestUrl = `${baseUrl}/block-inquiry-tejarat/badane`;
const live = await this.isInquiryLive("carBodyPlate", options);
const ctx = await this.mockCompanyContext(options);
let raw: any;
if (live) {
const token = await this.getTejaratAccessToken();
try {
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;
}
} else {
if (!live) {
this.logger.debug(
`[MOCK] getTejaratCarBodyInquiry plate=${JSON.stringify(requestPayload)}`,
);
raw = this.buildMockCarBodyInquiryRaw(userDetail, ctx);
}
const raw = await this.makeTejaratRequest(
requestUrl,
requestPayload,
"carBodyPlate",
options,
);
const mapped = this.mapCarBodyInquiryResponse(raw);
return { raw, mapped };
}
@@ -863,8 +906,11 @@ export class SandHubService {
if (!(await this.isInquiryLive(inquiryType, options))) {
this.logger.log(`[MOCK] SandHub POST skipped (${inquiryType}): ${url}`);
const ctx = await this.mockCompanyContext(options);
if (inquiryType === "carBodyPlate") {
return this.buildMockInquiryRaw(inquiryType, ctx, payload);
}
const plateMock =
inquiryType === "thirdPartyPlate" || inquiryType === "carBodyPlate"
inquiryType === "thirdPartyPlate"
? this.buildMockPlateInquiryRaw(ctx)
: undefined;
return this.buildMockSandHubResponseForUrl(url, payload, plateMock);