added offline inquiry in system setting also fix some bugs

This commit is contained in:
2026-08-03 11:49:24 +03:30
parent ab4f667c8d
commit 7f672541ae
19 changed files with 965 additions and 26 deletions

View File

@@ -1,6 +1,9 @@
import { Injectable, Logger } from "@nestjs/common";
import { FanavaranClientConfigService } from "src/fanavaran/fanavaran-client-config.service";
import {
OfflineInquirySettingsViewDto,
SystemSettingsResponseDto,
UpdateOfflineInquirySettingsDto,
UpdateSystemSettingsDto,
} from "./dto/system-settings.dto";
import { SystemSettingsDbService } from "./entities/db-service/system-settings.db.service";
@@ -12,7 +15,10 @@ export class SystemSettingsService {
private cacheAt = 0;
private readonly cacheTtlMs = 15_000;
constructor(private readonly db: SystemSettingsDbService) {}
constructor(
private readonly db: SystemSettingsDbService,
private readonly fanavaranClientConfigService: FanavaranClientConfigService,
) {}
private async loadGlobal(): Promise<Record<string, unknown>> {
const now = Date.now();
@@ -22,10 +28,13 @@ export class SystemSettingsService {
let doc = await this.db.findGlobal();
if (!doc) {
doc = await this.db.upsertGlobal({
$set: { externalApis: { sandHubUseLiveApi: false } },
$set: {
externalApis: { sandHubUseLiveApi: false },
offlineInquiry: { enabled: true },
},
});
this.logger.log(
"Created default system_settings document (sandHubUseLiveApi=false)",
"Created default system_settings document (sandHubUseLiveApi=false, offlineInquiry.enabled=true)",
);
}
this.cache = doc;
@@ -38,6 +47,14 @@ export class SystemSettingsService {
this.cacheAt = 0;
}
private readOfflineInquiryEnabled(doc: Record<string, unknown>): boolean {
const offlineInquiry = doc.offlineInquiry as
| { enabled?: boolean }
| undefined;
// Unset → enabled (preserve prior always-on seed behavior).
return offlineInquiry?.enabled !== false;
}
/**
* Whether SandHub/Tejarat live HTTP should run (`system_settings` collection).
*/
@@ -49,6 +66,18 @@ export class SystemSettingsService {
return externalApis?.sandHubUseLiveApi === true;
}
/**
* Whether seeded offline plate inquiries may short-circuit live/mock inquiry.
*/
async isOfflineInquiryEnabled(): Promise<boolean> {
const doc = await this.loadGlobal();
return this.readOfflineInquiryEnabled(doc);
}
async getOfflineInquirySettings(): Promise<OfflineInquirySettingsViewDto> {
return { enabled: await this.isOfflineInquiryEnabled() };
}
async getSettingsView(): Promise<SystemSettingsResponseDto> {
const doc = await this.loadGlobal();
const externalApis = doc.externalApis as
@@ -58,6 +87,7 @@ export class SystemSettingsService {
return {
key: String(doc.key ?? "global"),
externalApis: { sandHubUseLiveApi },
offlineInquiry: { enabled: this.readOfflineInquiryEnabled(doc) },
sandHubMode: sandHubUseLiveApi ? "live" : "mock",
};
}
@@ -69,6 +99,9 @@ export class SystemSettingsService {
if (body.externalApis?.sandHubUseLiveApi !== undefined) {
$set["externalApis.sandHubUseLiveApi"] = body.externalApis.sandHubUseLiveApi;
}
if (body.offlineInquiry?.enabled !== undefined) {
$set["offlineInquiry.enabled"] = body.offlineInquiry.enabled;
}
if (Object.keys($set).length > 0) {
await this.db.upsertGlobal({ $set });
this.invalidateCache();
@@ -76,4 +109,30 @@ export class SystemSettingsService {
}
return this.getSettingsView();
}
async updateOfflineInquirySettings(
body: UpdateOfflineInquirySettingsDto,
): Promise<OfflineInquirySettingsViewDto> {
await this.db.upsertGlobal({
$set: { "offlineInquiry.enabled": body.enabled },
});
this.invalidateCache();
this.logger.log(
`Offline inquiry master switch set to enabled=${body.enabled}`,
);
return { enabled: body.enabled };
}
/**
* Re-read `fanavaranClientConfigs` into the in-memory profile cache.
* Auth fingerprint changes invalidate cached Fanavaran tokens.
*/
async reloadFanavaranConfigCache(): Promise<{ ok: true; message: string }> {
await this.fanavaranClientConfigService.reloadCache();
this.logger.log("Fanavaran client config cache reloaded via system-settings API");
return {
ok: true,
message: "Fanavaran client config cache reloaded",
};
}
}