Toggle External API

This commit is contained in:
SepehrYahyaee
2026-05-16 16:23:01 +03:30
parent f2848a6179
commit 094816ce8f
11 changed files with 358 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
import { HttpModule } from "@nestjs/axios";
import { Module } from "@nestjs/common";
import { MongooseModule } from "@nestjs/mongoose";
import { SystemSettingsModule } from "src/system-settings/system-settings.module";
import { SandHubDbService } from "src/sand-hub/entity/db-service/sand-hub.db.service";
import { SandHubModel, SandHubSchema } from "./entity/schema/sand-hub.schema";
import { SandHubService } from "./sand-hub.service";
@@ -8,6 +9,7 @@ import { SandHubService } from "./sand-hub.service";
@Module({
imports: [
HttpModule,
SystemSettingsModule,
MongooseModule.forFeature([
{ name: SandHubModel.name, schema: SandHubSchema },
]),

View File

@@ -12,6 +12,7 @@ import {
import axios from "axios";
import { SandHubDbService } from "src/sand-hub/entity/db-service/sand-hub.db.service";
import { SandHubModel } from "src/sand-hub/entity/schema/sand-hub.schema";
import { SystemSettingsService } from "src/system-settings/system-settings.service";
import { SandHubDetailDto } from "./dto/sand-hub.dto";
import { jalaliToGregorianDate } from "src/helpers/date-jalali";
@@ -28,14 +29,15 @@ export class SandHubService {
constructor(
private readonly httpService: HttpService,
private readonly sandHubDbService: SandHubDbService,
private readonly systemSettingsService: SystemSettingsService,
) {}
/**
* When false (default), no outbound HTTP to SandHub/Tejarat gateways — all inquiries return mocks.
* Set `SANDHUB_USE_REAL_API=true` to restore live calls.
* When false, no outbound HTTP to SandHub/Tejarat — inquiries use permissive mocks.
* Controlled by `system_settings.externalApis.sandHubUseLiveApi` (PATCH /system-settings).
*/
private useLiveSandHubApis(): boolean {
return process.env.SANDHUB_USE_REAL_API === "true";
private async useLiveSandHubApis(): Promise<boolean> {
return this.systemSettingsService.isSandHubLiveEnabled();
}
/** Fixed plate/insurance inquiry payload used everywhere we mock block-inquiry style APIs. */
@@ -117,9 +119,12 @@ export class SandHubService {
/**
* Bodies returned from `makeSandHubRequest` in mock mode (same shape callers expect from real API).
*/
private buildMockSandHubResponseForUrl(url: string): any {
private buildMockSandHubResponseForUrl(url: string, payload?: unknown): any {
const u = (url || "").toLowerCase();
if (u.includes("personal-inquiry")) {
const p = payload as { nationalCode?: string } | undefined;
const nin =
typeof p?.nationalCode === "string" ? p.nationalCode : "-";
return {
message: "success",
data: {
@@ -127,7 +132,7 @@ export class SandHubService {
lastName: "خانوادگی",
fatherName: "-",
birthCertificateNumber: "-",
nin: "-",
nin,
},
};
}
@@ -165,7 +170,7 @@ export class SandHubService {
}
private async getAccessToken(): Promise<string> {
if (!this.useLiveSandHubApis()) {
if (!(await this.useLiveSandHubApis())) {
return "mock-sandhub-access-token";
}
if (this.loginToken && this.tokenExpiry && this.tokenExpiry > new Date()) {
@@ -205,7 +210,7 @@ export class SandHubService {
}
private async getTejaratAccessToken(): Promise<string> {
if (!this.useLiveSandHubApis()) {
if (!(await this.useLiveSandHubApis())) {
return "mock-tejarat-access-token";
}
if (
@@ -263,7 +268,7 @@ export class SandHubService {
}
private async makeTejaratRequest(url: string, payload: any, maxRetries = 2) {
if (!this.useLiveSandHubApis()) {
if (!(await this.useLiveSandHubApis())) {
this.logger.log(`[MOCK] Tejarat POST skipped: ${url}`);
return this.getDefaultMockPlateInquiryRaw();
}
@@ -328,10 +333,11 @@ export class SandHubService {
};
const requestUrl = `${baseUrl}/block-inquiry-tejarat`;
const raw = this.useLiveSandHubApis()
const live = await this.useLiveSandHubApis();
const raw = live
? await this.makeTejaratRequest(requestUrl, requestPayload)
: this.getDefaultMockPlateInquiryRaw();
if (!this.useLiveSandHubApis()) {
if (!live) {
this.logger.debug(
`[MOCK] getTejaratBlockInquiry plate=${JSON.stringify(requestPayload)}`,
);
@@ -345,9 +351,9 @@ export class SandHubService {
}
private async makeSandHubRequest(url: string, payload: any, maxRetries = 3) {
if (!this.useLiveSandHubApis()) {
if (!(await this.useLiveSandHubApis())) {
this.logger.log(`[MOCK] SandHub POST skipped: ${url}`);
return this.buildMockSandHubResponseForUrl(url);
return this.buildMockSandHubResponseForUrl(url, payload);
}
const token = await this.getAccessToken();
const INITIAL_DELAY = 1000;
@@ -453,7 +459,7 @@ export class SandHubService {
const requestUrl = `${base}/block-inquiry-tejarat`;
let response: any;
if (this.useLiveSandHubApis()) {
if (await this.useLiveSandHubApis()) {
response = await this.makeSandHubRequest(requestUrl, requestPayload);
} else {
this.logger.debug(