lookups query and api call fixed

This commit is contained in:
2026-10-24 15:10:55 +03:30
parent 8d73d4996c
commit 69a9fc351c
6 changed files with 63 additions and 35 deletions
+3 -1
View File
@@ -177,9 +177,11 @@ export const SEED_FANAVARAN_CLIENT_PROFILES: Record<
contractId: "304", contractId: "304",
location: "1", location: "1",
}, },
// Expert role ids not confirmed for Moallem yet — start from shared shape.
defaults: { defaults: {
...SHARED_FANAVARAN_DEFAULTS, ...SHARED_FANAVARAN_DEFAULTS,
// GEN.03 financial case owner / GEN.08 third-party financial assessor.
ClaimExpertId: 5347,
ExpertiseClaimExpertId: 5350,
}, },
}, },
}; };
+10
View File
@@ -96,6 +96,16 @@ export const FANAVARAN_REMOTE_LOOKUPS: FanavaranRemoteLookupDefinition[] = [
url: `${FANAVARAN_LOOKUP_BASE_URL}/car/base-info/vehicle-kinds`, url: `${FANAVARAN_LOOKUP_BASE_URL}/car/base-info/vehicle-kinds`,
cacheFile: "vehicle-kinds.json", cacheFile: "vehicle-kinds.json",
}, },
{
name: "notifier-type",
url: `${FANAVARAN_LOOKUP_BASE_URL}/car/code-list/notifier-type`,
cacheFile: "notifier-type.json",
},
{
name: "notifier-relation",
url: `${FANAVARAN_LOOKUP_BASE_URL}/car/code-list/notifier-relation`,
cacheFile: "notifier-relation.json",
},
{ {
name: "person-role", name: "person-role",
url: `${FANAVARAN_LOOKUP_BASE_URL}/common/code-list/person-role`, url: `${FANAVARAN_LOOKUP_BASE_URL}/common/code-list/person-role`,
+7 -7
View File
@@ -142,7 +142,7 @@ export class FanavaranLookupService {
/** /**
* Resolve a remote Fanavaran lookup. * Resolve a remote Fanavaran lookup.
* *
* Parsian (default): file → `lookups` collection → Fanavaran API * Parsian (default): `lookups` collection → file → Fanavaran API
* Tejaratno with dbSource: file → Fanavaran → DB on API failure * Tejaratno with dbSource: file → Fanavaran → DB on API failure
* Others: file → Fanavaran * Others: file → Fanavaran
*/ */
@@ -155,7 +155,7 @@ export class FanavaranLookupService {
| { | {
/** Soft/hard DB read — return null/undefined to miss. */ /** Soft/hard DB read — return null/undefined to miss. */
dbSource?: () => Promise<unknown | null | undefined>; dbSource?: () => Promise<unknown | null | undefined>;
/** When true: try DB after file miss, before Fanavaran. */ /** When true: try DB before the cache file and Fanavaran. */
preferDbBeforeRemote?: boolean; preferDbBeforeRemote?: boolean;
}, },
): Promise<unknown> { ): Promise<unknown> {
@@ -173,11 +173,6 @@ export class FanavaranLookupService {
? () => this.readLookupsCollectionByCacheFile(cacheFile) ? () => this.readLookupsCollectionByCacheFile(cacheFile)
: undefined); : undefined);
const cached = await this.readCacheFile(clientKey, cacheFile);
if (cached !== null) {
return cached;
}
if (preferDbBeforeRemote && dbSource) { if (preferDbBeforeRemote && dbSource) {
const fromDb = await this.tryDbSource(clientKey, cacheFile, dbSource); const fromDb = await this.tryDbSource(clientKey, cacheFile, dbSource);
if (fromDb !== null) { if (fromDb !== null) {
@@ -186,6 +181,11 @@ export class FanavaranLookupService {
} }
} }
const cached = await this.readCacheFile(clientKey, cacheFile);
if (cached !== null) {
return cached;
}
try { try {
const tenantUrl = url.replace( const tenantUrl = url.replace(
FANAVARAN_LOOKUP_BASE_URL, FANAVARAN_LOOKUP_BASE_URL,
+37 -11
View File
@@ -1,4 +1,3 @@
import { BadRequestException } from "@nestjs/common";
import { LookupsService } from "src/lookups/lookups.service"; import { LookupsService } from "src/lookups/lookups.service";
import { toFanavaranHullBaseClaimPayload } from "src/claim-request-management/fanavaran-hull-base-claim"; import { toFanavaranHullBaseClaimPayload } from "src/claim-request-management/fanavaran-hull-base-claim";
import { import {
@@ -8,34 +7,61 @@ import {
describe("TejaratNo notifier contract", () => { describe("TejaratNo notifier contract", () => {
it.each(["notifier-type", "notifier-relation"])( it.each(["notifier-type", "notifier-relation"])(
"reads %s from the lookups collection and does not call Fanavaran", "falls through to Fanavaran for %s when the lookups collection has no row",
async (name) => { async (name) => {
const stored = tejaratNotifierLookup(name); const remote = tejaratNotifierLookup(name);
const service = Object.create(LookupsService.prototype) as any; const service = Object.create(LookupsService.prototype) as any;
service.activeClientKey = jest.fn().mockReturnValue("tejaratno"); service.activeClientKey = jest.fn().mockReturnValue("tejaratno");
service.fanavaranLookupService = { service.fanavaranLookupService = {
getRemoteLookup: jest.fn(), getRemoteLookup: jest
readStoredLookup: jest.fn().mockResolvedValue(stored), .fn()
.mockImplementation(async (_client, _url, _cache, options) => {
const stored = await options.dbSource();
return stored ?? remote;
}),
readStoredLookup: jest.fn().mockResolvedValue(null),
};
service.lookupDbService = {
findOne: jest.fn().mockResolvedValue(null),
findOneAndUpdate: jest.fn().mockResolvedValue(undefined),
}; };
const result = await service.getClientRemoteLookup(name); const result = await service.getClientRemoteLookup(name);
expect(result).toEqual(stored); expect(result).toEqual(remote);
expect(service.fanavaranLookupService.readStoredLookup).toHaveBeenCalledWith( expect(service.fanavaranLookupService.readStoredLookup).toHaveBeenCalledWith(
name, name,
); );
expect( expect(
service.fanavaranLookupService.getRemoteLookup, service.fanavaranLookupService.getRemoteLookup,
).not.toHaveBeenCalled(); ).toHaveBeenCalledWith(
"tejaratno",
expect.stringContaining(`/car/code-list/${name}`),
`${name}.json`,
expect.objectContaining({ preferDbBeforeRemote: true }),
);
expect(service.lookupDbService.findOneAndUpdate).toHaveBeenCalled();
}, },
); );
it.each(["parsian", "moallem"])( it.each(["parsian", "moallem"])(
"rejects the tenant-specific lookups for %s", "also requests %s notifier lookups from Fanavaran on a DB miss",
async (clientKey) => { async (clientKey) => {
const service = Object.create(LookupsService.prototype) as any; const service = Object.create(LookupsService.prototype) as any;
service.activeClientKey = jest.fn().mockReturnValue(clientKey); service.activeClientKey = jest.fn().mockReturnValue(clientKey);
await expect( service.fanavaranLookupService = {
service.getClientRemoteLookup("notifier-type"), getRemoteLookup: jest
).rejects.toBeInstanceOf(BadRequestException); .fn()
.mockImplementation(async (_client, _url, _cache, options) => {
const stored = await options.dbSource();
return stored ?? [];
}),
readStoredLookup: jest.fn().mockResolvedValue(null),
};
service.lookupDbService = {
findOne: jest.fn().mockResolvedValue(null),
findOneAndUpdate: jest.fn().mockResolvedValue(undefined),
};
await service.getClientRemoteLookup("notifier-type");
expect(service.fanavaranLookupService.getRemoteLookup).toHaveBeenCalled();
}, },
); );
+1 -1
View File
@@ -671,7 +671,7 @@ export class LookupsController {
description: description:
"Generic cached Fanavaran lookup fetcher. Configured names use their catalogue URL; " + "Generic cached Fanavaran lookup fetcher. Configured names use their catalogue URL; " +
"unlisted slugs (e.g. vehicle-groups) fall back to car/base-info/{lookupName}. " + "unlisted slugs (e.g. vehicle-groups) fall back to car/base-info/{lookupName}. " +
"TejaratNo notifier-type and notifier-relation are read from the lookups collection and are not fetched from Fanavaran.", "Notifier lookups check the lookups collection first for every tenant, then fetch from Fanavaran on a miss.",
}) })
@ApiParam({ @ApiParam({
name: "lookupName", name: "lookupName",
+5 -15
View File
@@ -136,30 +136,20 @@ export class LookupsService implements OnModuleInit, OnModuleDestroy {
async getClientRemoteLookup(lookupName: string): Promise<unknown> { async getClientRemoteLookup(lookupName: string): Promise<unknown> {
const clientKey = this.activeClientKey(); const clientKey = this.activeClientKey();
if (isTejaratNotifierLookup(lookupName)) {
if (clientKey !== "tejaratno") {
throw new BadRequestException("Notifier lookups apply only to TejaratNo.");
}
const stored = await this.fanavaranLookupService.readStoredLookup(lookupName);
if (stored == null) {
throw new NotFoundException(`Lookup "${lookupName}" not found`);
}
return stored;
}
const definition = this.findRemoteLookup(lookupName); const definition = this.findRemoteLookup(lookupName);
// Parsian: file → lookups collection → Fanavaran (default inside FanavaranLookupService) // Notifier lookups check the shared DB seed before Fanavaran for every
// Tejaratno: file → Fanavaran → DB on API failure // tenant. A DB miss proceeds to Fanavaran and the response is cached below.
// readStoredLookup returns null when the row is missing, so a Fanavaran // Other lookups retain their configured tenant-specific fallback order.
// failure is not replaced by "Lookup not found".
const data = await this.fanavaranLookupService.getRemoteLookup( const data = await this.fanavaranLookupService.getRemoteLookup(
clientKey, clientKey,
definition.url, definition.url,
definition.cacheFile, definition.cacheFile,
clientKey === "tejaratno" isTejaratNotifierLookup(lookupName)
? { ? {
dbSource: () => dbSource: () =>
this.fanavaranLookupService.readStoredLookup(lookupName), this.fanavaranLookupService.readStoredLookup(lookupName),
preferDbBeforeRemote: true,
} }
: undefined, : undefined,
); );