diff --git a/src/core/config/fanavaran-client.config.ts b/src/core/config/fanavaran-client.config.ts index 5f154d2..9b700b5 100644 --- a/src/core/config/fanavaran-client.config.ts +++ b/src/core/config/fanavaran-client.config.ts @@ -177,9 +177,11 @@ export const SEED_FANAVARAN_CLIENT_PROFILES: Record< contractId: "304", location: "1", }, - // Expert role ids not confirmed for Moallem yet — start from shared shape. defaults: { ...SHARED_FANAVARAN_DEFAULTS, + // GEN.03 financial case owner / GEN.08 third-party financial assessor. + ClaimExpertId: 5347, + ExpertiseClaimExpertId: 5350, }, }, }; diff --git a/src/fanavaran/fanavaran-lookup.config.ts b/src/fanavaran/fanavaran-lookup.config.ts index 28822ce..a4772bd 100644 --- a/src/fanavaran/fanavaran-lookup.config.ts +++ b/src/fanavaran/fanavaran-lookup.config.ts @@ -96,6 +96,16 @@ export const FANAVARAN_REMOTE_LOOKUPS: FanavaranRemoteLookupDefinition[] = [ url: `${FANAVARAN_LOOKUP_BASE_URL}/car/base-info/vehicle-kinds`, 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", url: `${FANAVARAN_LOOKUP_BASE_URL}/common/code-list/person-role`, diff --git a/src/fanavaran/fanavaran-lookup.service.ts b/src/fanavaran/fanavaran-lookup.service.ts index bccc8ca..fd9383f 100644 --- a/src/fanavaran/fanavaran-lookup.service.ts +++ b/src/fanavaran/fanavaran-lookup.service.ts @@ -142,7 +142,7 @@ export class FanavaranLookupService { /** * 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 * Others: file → Fanavaran */ @@ -155,7 +155,7 @@ export class FanavaranLookupService { | { /** Soft/hard DB read — return null/undefined to miss. */ dbSource?: () => Promise; - /** When true: try DB after file miss, before Fanavaran. */ + /** When true: try DB before the cache file and Fanavaran. */ preferDbBeforeRemote?: boolean; }, ): Promise { @@ -173,11 +173,6 @@ export class FanavaranLookupService { ? () => this.readLookupsCollectionByCacheFile(cacheFile) : undefined); - const cached = await this.readCacheFile(clientKey, cacheFile); - if (cached !== null) { - return cached; - } - if (preferDbBeforeRemote && dbSource) { const fromDb = await this.tryDbSource(clientKey, cacheFile, dbSource); if (fromDb !== null) { @@ -186,6 +181,11 @@ export class FanavaranLookupService { } } + const cached = await this.readCacheFile(clientKey, cacheFile); + if (cached !== null) { + return cached; + } + try { const tenantUrl = url.replace( FANAVARAN_LOOKUP_BASE_URL, diff --git a/src/fanavaran/tejarat-notifier.spec.ts b/src/fanavaran/tejarat-notifier.spec.ts index 0d32b3b..d77be78 100644 --- a/src/fanavaran/tejarat-notifier.spec.ts +++ b/src/fanavaran/tejarat-notifier.spec.ts @@ -1,4 +1,3 @@ -import { BadRequestException } from "@nestjs/common"; import { LookupsService } from "src/lookups/lookups.service"; import { toFanavaranHullBaseClaimPayload } from "src/claim-request-management/fanavaran-hull-base-claim"; import { @@ -8,34 +7,61 @@ import { describe("TejaratNo notifier contract", () => { 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) => { - const stored = tejaratNotifierLookup(name); + const remote = tejaratNotifierLookup(name); const service = Object.create(LookupsService.prototype) as any; service.activeClientKey = jest.fn().mockReturnValue("tejaratno"); service.fanavaranLookupService = { - getRemoteLookup: jest.fn(), - readStoredLookup: jest.fn().mockResolvedValue(stored), + getRemoteLookup: jest + .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); - expect(result).toEqual(stored); + expect(result).toEqual(remote); expect(service.fanavaranLookupService.readStoredLookup).toHaveBeenCalledWith( name, ); expect( 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"])( - "rejects the tenant-specific lookups for %s", + "also requests %s notifier lookups from Fanavaran on a DB miss", async (clientKey) => { const service = Object.create(LookupsService.prototype) as any; service.activeClientKey = jest.fn().mockReturnValue(clientKey); - await expect( - service.getClientRemoteLookup("notifier-type"), - ).rejects.toBeInstanceOf(BadRequestException); + service.fanavaranLookupService = { + getRemoteLookup: jest + .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(); }, ); diff --git a/src/lookups/lookups.controller.ts b/src/lookups/lookups.controller.ts index 0195f0b..bcf3919 100644 --- a/src/lookups/lookups.controller.ts +++ b/src/lookups/lookups.controller.ts @@ -671,7 +671,7 @@ export class LookupsController { description: "Generic cached Fanavaran lookup fetcher. Configured names use their catalogue URL; " + "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({ name: "lookupName", diff --git a/src/lookups/lookups.service.ts b/src/lookups/lookups.service.ts index 162a0ab..18cf2fe 100644 --- a/src/lookups/lookups.service.ts +++ b/src/lookups/lookups.service.ts @@ -136,30 +136,20 @@ export class LookupsService implements OnModuleInit, OnModuleDestroy { async getClientRemoteLookup(lookupName: string): Promise { 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); - // Parsian: file → lookups collection → Fanavaran (default inside FanavaranLookupService) - // Tejaratno: file → Fanavaran → DB on API failure - // readStoredLookup returns null when the row is missing, so a Fanavaran - // failure is not replaced by "Lookup not found". + // Notifier lookups check the shared DB seed before Fanavaran for every + // tenant. A DB miss proceeds to Fanavaran and the response is cached below. + // Other lookups retain their configured tenant-specific fallback order. const data = await this.fanavaranLookupService.getRemoteLookup( clientKey, definition.url, definition.cacheFile, - clientKey === "tejaratno" + isTejaratNotifierLookup(lookupName) ? { dbSource: () => this.fanavaranLookupService.readStoredLookup(lookupName), + preferDbBeforeRemote: true, } : undefined, );