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

@@ -4,10 +4,12 @@ import { Model } from "mongoose";
import {
FANAVARAN_CLIENT_KEYS,
SEED_FANAVARAN_CLIENT_PROFILES,
getFanavaranClientProfile,
setFanavaranClientProfilesCache,
type FanavaranClientKey,
type FanavaranClientProfile,
} from "src/core/config/fanavaran-client.config";
import { FanavaranAuthService } from "./fanavaran-auth.service";
import {
FanavaranClientConfig,
FanavaranClientConfigDocument,
@@ -27,6 +29,7 @@ export class FanavaranClientConfigService implements OnModuleInit {
constructor(
@InjectModel(FanavaranClientConfig.name)
private readonly configModel: Model<FanavaranClientConfigDocument>,
private readonly fanavaranAuthService: FanavaranAuthService,
) {}
async onModuleInit(): Promise<void> {
@@ -56,7 +59,26 @@ export class FanavaranClientConfigService implements OnModuleInit {
}
}
/**
* Re-read Mongo into the runtime profile cache. If any tenant's auth block
* changed (location, credentials, …), drop cached Fanavaran tokens so the
* next call re-logins instead of waiting for Tehran midnight.
*/
async reloadCache(): Promise<void> {
const previousFingerprints = new Map<FanavaranClientKey, string>();
for (const key of FANAVARAN_CLIENT_KEYS) {
try {
previousFingerprints.set(
key,
FanavaranAuthService.authFingerprint(
getFanavaranClientProfile(key).auth,
),
);
} catch {
// First boot / empty cache — nothing to compare.
}
}
const docs = await this.configModel.find().lean().exec();
const cache: Partial<Record<FanavaranClientKey, FanavaranClientProfile>> =
{};
@@ -83,6 +105,18 @@ export class FanavaranClientConfigService implements OnModuleInit {
}
setFanavaranClientProfilesCache(cache);
for (const key of FANAVARAN_CLIENT_KEYS) {
const next = FanavaranAuthService.authFingerprint(cache[key]!.auth);
const prev = previousFingerprints.get(key);
if (prev && prev !== next) {
this.logger.warn(
`[${key}] Fanavaran auth config changed — invalidating cached authenticationToken`,
);
this.fanavaranAuthService.invalidateToken(key);
}
}
this.logger.log(
`Loaded ${docs.length} Fanavaran client config(s) from Mongo into runtime cache`,
);