forked from Yara724/api
Merge pull request 'lookups update automatically' (#362) from s.hajizadeh/yara724api:main into main
Reviewed-on: Yara724/api#362
This commit is contained in:
@@ -63,6 +63,7 @@ type TejaratAccidentLabelRow = {
|
||||
export class LookupsService implements OnModuleInit, OnModuleDestroy {
|
||||
private readonly logger = new Logger(LookupsService.name);
|
||||
private vehicleKindsSyncPromise: Promise<void> | null = null;
|
||||
private remoteLookupsSyncPromise: Promise<void> | null = null;
|
||||
private vehicleKindsRefreshTimer: ReturnType<typeof setInterval> | 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<void> {
|
||||
if (this.remoteLookupsSyncPromise) return this.remoteLookupsSyncPromise;
|
||||
this.remoteLookupsSyncPromise = this.syncMissingRemoteLookups().finally(
|
||||
() => {
|
||||
this.remoteLookupsSyncPromise = null;
|
||||
},
|
||||
);
|
||||
return this.remoteLookupsSyncPromise;
|
||||
}
|
||||
|
||||
private async syncMissingRemoteLookups(): Promise<void> {
|
||||
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<void> {
|
||||
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<any> {
|
||||
|
||||
Reference in New Issue
Block a user