From 47447d1153fbab595b772cdd6984eca2daaaf78f Mon Sep 17 00:00:00 2001 From: "s.hajizadeh" Date: Sun, 11 Oct 2026 13:52:38 +0330 Subject: [PATCH] lookups update automatically --- src/lookups/lookups.service.ts | 101 ++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 2 deletions(-) diff --git a/src/lookups/lookups.service.ts b/src/lookups/lookups.service.ts index fe67591..162a0ab 100644 --- a/src/lookups/lookups.service.ts +++ b/src/lookups/lookups.service.ts @@ -63,6 +63,7 @@ type TejaratAccidentLabelRow = { export class LookupsService implements OnModuleInit, OnModuleDestroy { private readonly logger = new Logger(LookupsService.name); private vehicleKindsSyncPromise: Promise | null = null; + private remoteLookupsSyncPromise: Promise | null = null; private vehicleKindsRefreshTimer: ReturnType | null = null; @@ -86,6 +87,15 @@ export class LookupsService implements OnModuleInit, OnModuleDestroy { setTimeout(() => { void this.ensureVehicleKindsSynced(false).catch(() => undefined); }, 15_000); + setTimeout(() => { + void this.ensureRemoteLookupsSynced().catch((error) => { + this.logger.warn( + `Startup lookup sync failed: ${ + error instanceof Error ? error.message : error + }`, + ); + }); + }, 5_000); } onModuleDestroy() { @@ -140,14 +150,101 @@ export class LookupsService implements OnModuleInit, OnModuleDestroy { // Parsian: file → lookups collection → Fanavaran (default inside FanavaranLookupService) // Tejaratno: file → Fanavaran → DB on API failure - return this.fanavaranLookupService.getRemoteLookup( + // readStoredLookup returns null when the row is missing, so a Fanavaran + // failure is not replaced by "Lookup not found". + const data = await this.fanavaranLookupService.getRemoteLookup( clientKey, definition.url, definition.cacheFile, clientKey === "tejaratno" - ? { dbSource: () => this.getLookup(lookupName) } + ? { + dbSource: () => + this.fanavaranLookupService.readStoredLookup(lookupName), + } : undefined, ); + await this.rememberRemoteLookup(definition, data).catch((error) => { + this.logger.warn( + `Could not store lookup ${lookupName}: ${ + error instanceof Error ? error.message : error + }`, + ); + }); + return data; + } + + /** + * First boot of an empty instance: fill cache files and the lookups + * collection from Fanavaran. Later calls reuse whichever copy already exists. + */ + async ensureRemoteLookupsSynced(): Promise { + if (this.remoteLookupsSyncPromise) return this.remoteLookupsSyncPromise; + this.remoteLookupsSyncPromise = this.syncMissingRemoteLookups().finally( + () => { + this.remoteLookupsSyncPromise = null; + }, + ); + return this.remoteLookupsSyncPromise; + } + + private async syncMissingRemoteLookups(): Promise { + const clientKey = this.activeClientKey(); + let fetched = 0; + for (const lookup of FANAVARAN_REMOTE_LOOKUPS) { + try { + const cached = await this.fanavaranLookupService.readCacheFile( + clientKey, + lookup.cacheFile, + ); + if (cached != null) { + await this.rememberRemoteLookup(lookup, cached); + continue; + } + const stored = await this.fanavaranLookupService.readStoredLookup( + lookup.name, + ); + if (stored != null) { + await this.fanavaranLookupService.writeCacheFile( + clientKey, + lookup.cacheFile, + stored, + ); + continue; + } + if (lookup.name === "vehicle-kinds") continue; + await this.getClientRemoteLookup(lookup.name); + fetched += 1; + } catch (error) { + this.logger.warn( + `Startup lookup sync failed for ${lookup.name}: ${ + error instanceof Error ? error.message : error + }`, + ); + } + } + this.logger.log( + `Startup lookup sync finished for ${clientKey} (${fetched} fetched from Fanavaran)`, + ); + } + + private async rememberRemoteLookup( + lookup: FanavaranRemoteLookupDefinition, + response: unknown, + ): Promise { + if (response == null) return; + const existing = await this.lookupDbService.findOne({ name: lookup.name }); + if (existing?.response != null) return; + await this.lookupDbService.findOneAndUpdate( + { name: lookup.name }, + { + $set: { + name: lookup.name, + url: lookup.url, + response, + authenticationToken: existing?.authenticationToken || "synced", + }, + }, + ); } async getAccidentCauses(): Promise {