forked from Yara724/api
lookups query and api call fixed
This commit is contained in:
@@ -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`,
|
||||
|
||||
@@ -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<unknown | null | undefined>;
|
||||
/** When true: try DB after file miss, before Fanavaran. */
|
||||
/** When true: try DB before the cache file and Fanavaran. */
|
||||
preferDbBeforeRemote?: boolean;
|
||||
},
|
||||
): Promise<unknown> {
|
||||
@@ -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,
|
||||
|
||||
@@ -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();
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user