forked from Yara724/api
lookups update per client
This commit is contained in:
@@ -1,15 +1,47 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { Injectable, Logger, NotFoundException } from "@nestjs/common";
|
||||
import { resolveFanavaranClientKey } from "src/core/config/fanavaran-client.config";
|
||||
import {
|
||||
FANAVARAN_REMOTE_LOOKUPS,
|
||||
TEJARAT_STATIC_ACCIDENT_FILES,
|
||||
} from "src/fanavaran/fanavaran-lookup.config";
|
||||
import { FanavaranLookupService } from "src/fanavaran/fanavaran-lookup.service";
|
||||
import { LookupDbService } from "./entities/db-service/lookup.db.service";
|
||||
|
||||
type TejaratAccidentReasonRow = {
|
||||
id: number;
|
||||
persianLabel: string;
|
||||
fanavaranID: number;
|
||||
};
|
||||
|
||||
type TejaratAccidentLabelRow = {
|
||||
id: number;
|
||||
persianLabel: string;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class LookupsService {
|
||||
private readonly logger = new Logger(LookupsService.name);
|
||||
|
||||
constructor(private readonly lookupDbService: LookupDbService) {}
|
||||
constructor(
|
||||
private readonly lookupDbService: LookupDbService,
|
||||
private readonly fanavaranLookupService: FanavaranLookupService,
|
||||
) {}
|
||||
|
||||
private activeClientKey() {
|
||||
return resolveFanavaranClientKey();
|
||||
}
|
||||
|
||||
private findRemoteLookup(name: string) {
|
||||
const lookup = FANAVARAN_REMOTE_LOOKUPS.find((item) => item.name === name);
|
||||
if (!lookup) {
|
||||
throw new Error(`Unknown Fanavaran remote lookup: ${name}`);
|
||||
}
|
||||
return lookup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns stored `response` for a lookup document by `name` in the lookups collection.
|
||||
* Legacy Tejarat seed data — used as fallback when live fetch is unavailable.
|
||||
*/
|
||||
async getLookup(lookupName: string): Promise<any> {
|
||||
const doc = await this.lookupDbService.findOne({ name: lookupName });
|
||||
@@ -20,42 +52,114 @@ export class LookupsService {
|
||||
return doc.response;
|
||||
}
|
||||
|
||||
private async getClientRemoteLookup(lookupName: string): Promise<unknown> {
|
||||
const clientKey = this.activeClientKey();
|
||||
const definition = this.findRemoteLookup(lookupName);
|
||||
|
||||
return this.fanavaranLookupService.getRemoteLookup(
|
||||
clientKey,
|
||||
definition.url,
|
||||
definition.cacheFile,
|
||||
clientKey === "tejaratno"
|
||||
? async () => this.getLookup(lookupName)
|
||||
: undefined,
|
||||
);
|
||||
}
|
||||
|
||||
async getAccidentCauses(): Promise<any> {
|
||||
return await this.getLookup("accident-causes");
|
||||
return await this.getClientRemoteLookup("accident-causes");
|
||||
}
|
||||
|
||||
async getAccidentReportType(): Promise<any> {
|
||||
return await this.getLookup("accident-report-type");
|
||||
return await this.getClientRemoteLookup("accident-report-type");
|
||||
}
|
||||
|
||||
async getVehicleUseTypes(): Promise<any> {
|
||||
return await this.getLookup("vehicle-use-types");
|
||||
return await this.getClientRemoteLookup("vehicle-use-types");
|
||||
}
|
||||
|
||||
async getDmgPayMethod(): Promise<any> {
|
||||
return await this.getLookup("dmg-pay-method");
|
||||
return await this.getClientRemoteLookup("dmg-pay-method");
|
||||
}
|
||||
|
||||
async getDrivingLicenceTypes(): Promise<any> {
|
||||
return await this.getLookup("driving-licence-types");
|
||||
return await this.getClientRemoteLookup("driving-licence-types");
|
||||
}
|
||||
|
||||
async getAccidentCulpritType(): Promise<any> {
|
||||
return await this.getLookup("accident-culprit-type");
|
||||
return await this.getClientRemoteLookup("accident-culprit-type");
|
||||
}
|
||||
|
||||
async getAccidentWay(): Promise<{ id: number; label: string }[]> {
|
||||
const raw: { id: number; persianLabel: string }[] = JSON.parse(
|
||||
await readFile("src/static/ACCIDENT_WAY.json", "utf-8"),
|
||||
);
|
||||
const clientKey = this.activeClientKey();
|
||||
const fileName = TEJARAT_STATIC_ACCIDENT_FILES.accidentWay;
|
||||
|
||||
const cached = await this.fanavaranLookupService.readCacheFile<
|
||||
TejaratAccidentLabelRow[]
|
||||
>(clientKey, fileName);
|
||||
const raw =
|
||||
cached ??
|
||||
(clientKey === "tejaratno"
|
||||
? await this.fanavaranLookupService.readTejaratStaticAccidentFile<
|
||||
TejaratAccidentLabelRow[]
|
||||
>(fileName)
|
||||
: null);
|
||||
|
||||
if (!raw) {
|
||||
this.logger.warn(
|
||||
`No accident-way lookup for client ${clientKey}; falling back to tejarat static file`,
|
||||
);
|
||||
const fallback =
|
||||
await this.fanavaranLookupService.readTejaratStaticAccidentFile<
|
||||
TejaratAccidentLabelRow[]
|
||||
>(fileName);
|
||||
return fallback.map((item) => ({
|
||||
id: item.id,
|
||||
label: item.persianLabel,
|
||||
}));
|
||||
}
|
||||
|
||||
return raw.map((item) => ({ id: item.id, label: item.persianLabel }));
|
||||
}
|
||||
|
||||
async getAccidentReason(): Promise<
|
||||
{ id: number; label: string; fanavaran: number }[]
|
||||
> {
|
||||
const raw: { id: number; persianLabel: string; fanavaranID: number }[] =
|
||||
JSON.parse(await readFile("src/static/ACCIDENT_REASON.json", "utf-8"));
|
||||
const clientKey = this.activeClientKey();
|
||||
|
||||
if (clientKey === "parsian") {
|
||||
const cacheFile = "accident-reason-options.json";
|
||||
const cached =
|
||||
await this.fanavaranLookupService.readCacheFile<
|
||||
{ id: number; label: string; fanavaran: number }[]
|
||||
>(clientKey, cacheFile);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
const causes = await this.getAccidentCauses();
|
||||
const mapped =
|
||||
this.fanavaranLookupService.mapFanavaranAccidentCausesToReasonOptions(
|
||||
causes,
|
||||
);
|
||||
await this.fanavaranLookupService.writeCacheFile(
|
||||
clientKey,
|
||||
cacheFile,
|
||||
mapped,
|
||||
);
|
||||
return mapped;
|
||||
}
|
||||
|
||||
const fileName = TEJARAT_STATIC_ACCIDENT_FILES.accidentReason;
|
||||
const cached = await this.fanavaranLookupService.readCacheFile<
|
||||
TejaratAccidentReasonRow[]
|
||||
>(clientKey, fileName);
|
||||
const raw =
|
||||
cached ??
|
||||
(await this.fanavaranLookupService.readTejaratStaticAccidentFile<
|
||||
TejaratAccidentReasonRow[]
|
||||
>(fileName));
|
||||
|
||||
return raw.map((item) => ({
|
||||
id: item.id,
|
||||
label: item.persianLabel,
|
||||
@@ -64,9 +168,34 @@ export class LookupsService {
|
||||
}
|
||||
|
||||
async getAccidentType(): Promise<{ id: number; label: string }[]> {
|
||||
const raw: { id: number; persianLabel: string }[] = JSON.parse(
|
||||
await readFile("src/static/ACCIDENT_TYPE.json", "utf-8"),
|
||||
);
|
||||
const clientKey = this.activeClientKey();
|
||||
const fileName = TEJARAT_STATIC_ACCIDENT_FILES.accidentType;
|
||||
|
||||
const cached = await this.fanavaranLookupService.readCacheFile<
|
||||
TejaratAccidentLabelRow[]
|
||||
>(clientKey, fileName);
|
||||
const raw =
|
||||
cached ??
|
||||
(clientKey === "tejaratno"
|
||||
? await this.fanavaranLookupService.readTejaratStaticAccidentFile<
|
||||
TejaratAccidentLabelRow[]
|
||||
>(fileName)
|
||||
: null);
|
||||
|
||||
if (!raw) {
|
||||
this.logger.warn(
|
||||
`No accident-type lookup for client ${clientKey}; falling back to tejarat static file`,
|
||||
);
|
||||
const fallback =
|
||||
await this.fanavaranLookupService.readTejaratStaticAccidentFile<
|
||||
TejaratAccidentLabelRow[]
|
||||
>(fileName);
|
||||
return fallback.map((item) => ({
|
||||
id: item.id,
|
||||
label: item.persianLabel,
|
||||
}));
|
||||
}
|
||||
|
||||
return raw.map((item) => ({ id: item.id, label: item.persianLabel }));
|
||||
}
|
||||
|
||||
@@ -74,12 +203,18 @@ export class LookupsService {
|
||||
accidentWay: { id: number; label: string }[];
|
||||
accidentReason: { id: number; label: string; fanavaran: number }[];
|
||||
accidentType: { id: number; label: string }[];
|
||||
client: string;
|
||||
}> {
|
||||
const [accidentWay, accidentReason, accidentType] = await Promise.all([
|
||||
this.getAccidentWay(),
|
||||
this.getAccidentReason(),
|
||||
this.getAccidentType(),
|
||||
]);
|
||||
return { accidentWay, accidentReason, accidentType };
|
||||
return {
|
||||
client: this.activeClientKey(),
|
||||
accidentWay,
|
||||
accidentReason,
|
||||
accidentType,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user