forked from Yara724/api
222 lines
7.0 KiB
TypeScript
222 lines
7.0 KiB
TypeScript
import { ForbiddenException } from "@nestjs/common";
|
|
import { of, throwError } from "rxjs";
|
|
import { FanavaranHullReadService } from "./fanavaran-hull-read.service";
|
|
import { ClaimRequestManagementService } from "src/claim-request-management/claim-request-management.service";
|
|
|
|
const actor = { sub: "staff-1", role: "damage_expert", clientKey: "tenant-1" };
|
|
const localId = "6af4594a32d485128a55742d";
|
|
const headers = {
|
|
authenticationToken: "test-token",
|
|
CorpId: "543",
|
|
ContractId: "hull-contract",
|
|
Location: "110011",
|
|
};
|
|
const base =
|
|
"https://apimanager.iraneit.com/BimeApiManager/api/BimeApi/v2.0/car/vehicle-hull-claims/5190845";
|
|
|
|
function setup(data: unknown = []) {
|
|
const management = {
|
|
getFanavaranHullReadContext: jest
|
|
.fn()
|
|
.mockResolvedValue({ claimId: 5190845, expertiseId: 44237, headers }),
|
|
};
|
|
const http = { get: jest.fn().mockReturnValue(of({ data })) };
|
|
const auth = {
|
|
assertNotInBackoff: jest.fn(),
|
|
registerFailure: jest.fn(),
|
|
clearBackoff: jest.fn(),
|
|
};
|
|
return {
|
|
service: new FanavaranHullReadService(
|
|
management as any,
|
|
http as any,
|
|
auth as any,
|
|
),
|
|
management,
|
|
http,
|
|
auth,
|
|
};
|
|
}
|
|
|
|
describe("Fanavaran hull retrieval", () => {
|
|
it("reads the remote file list even without any locally saved file IDs", async () => {
|
|
const data = [
|
|
{
|
|
Id: 2008737,
|
|
ClaimId: 5190845,
|
|
FileName: "1.jpg",
|
|
FileTypeId: 70,
|
|
Status: 2003,
|
|
},
|
|
];
|
|
const { service, management, http } = setup(data);
|
|
expect(await service.files("parsian", localId, actor)).toEqual({
|
|
clientKey: "parsian",
|
|
claimCaseId: localId,
|
|
claimId: 5190845,
|
|
data,
|
|
});
|
|
expect(management.getFanavaranHullReadContext).toHaveBeenCalledWith(
|
|
localId,
|
|
"parsian",
|
|
actor,
|
|
);
|
|
expect(http.get).toHaveBeenCalledWith(`${base}/files`, {
|
|
headers: { ...headers, Accept: "application/json" },
|
|
timeout: 20000,
|
|
});
|
|
});
|
|
|
|
it("preserves the single-file Base64 Data and provider Status", async () => {
|
|
const data = { Id: 2008737, Data: "aW1hZ2U=", Status: 2003 };
|
|
const { service, http } = setup(data);
|
|
expect(
|
|
await service.file("parsian", localId, "2008737", actor),
|
|
).toMatchObject({ fileId: 2008737, data });
|
|
expect(http.get.mock.calls[0][0]).toBe(`${base}/files/2008737`);
|
|
});
|
|
|
|
it("retrieves damage sections using stored expertiseId and retains cost IDs/tracking codes", async () => {
|
|
const data = [
|
|
{
|
|
Id: 9,
|
|
ExpertiseId: 44237,
|
|
CINumber: "86",
|
|
CITrackingCode: "tracking",
|
|
DmgSectionCosts: [{ Id: 10, DmgSectionId: 9, Amount: 120000000 }],
|
|
},
|
|
];
|
|
const { service, http } = setup(data);
|
|
expect(
|
|
await service.damageSections("parsian", localId, actor),
|
|
).toMatchObject({ expertiseId: 44237, data });
|
|
expect(http.get.mock.calls[0][0]).toBe(
|
|
`${base}/expertise/44237/dmg-sections`,
|
|
);
|
|
await service.damageSections("parsian", localId, actor, "44238");
|
|
expect(http.get.mock.calls[1][0]).toBe(
|
|
`${base}/expertise/44238/dmg-sections`,
|
|
);
|
|
});
|
|
|
|
it("requires a saved or explicitly selected expertise before calling Fanavaran", async () => {
|
|
const { service, management, http } = setup();
|
|
management.getFanavaranHullReadContext.mockResolvedValue({
|
|
claimId: 5190845,
|
|
expertiseId: null,
|
|
headers,
|
|
});
|
|
await expect(
|
|
service.damageSections("parsian", localId, actor),
|
|
).rejects.toThrow("expertiseId");
|
|
expect(http.get).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it.each(["0", "-1", "1.2", "../files", "9007199254740992"])(
|
|
"rejects invalid remote IDs: %s",
|
|
async (id) => {
|
|
const { service, http } = setup();
|
|
await expect(
|
|
service.file("parsian", localId, id, actor),
|
|
).rejects.toThrow();
|
|
await expect(
|
|
service.damageSections("parsian", localId, actor, id),
|
|
).rejects.toThrow();
|
|
expect(http.get).not.toHaveBeenCalled();
|
|
},
|
|
);
|
|
|
|
it("does not make a remote request when claim access is denied", async () => {
|
|
const { service, management, http } = setup();
|
|
management.getFanavaranHullReadContext.mockRejectedValue(
|
|
new ForbiddenException(),
|
|
);
|
|
await expect(service.files("parsian", localId, actor)).rejects.toThrow(
|
|
ForbiddenException,
|
|
);
|
|
expect(http.get).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("preserves vendor errors and registers them with shared backoff", async () => {
|
|
const { service, http, auth } = setup();
|
|
const error = {
|
|
isAxiosError: true,
|
|
response: {
|
|
status: 404,
|
|
data: { Message: "پرونده یافت نشد", RuleName: "HullClaimRule" },
|
|
},
|
|
};
|
|
http.get.mockReturnValue(throwError(() => error));
|
|
await expect(
|
|
service.files("parsian", localId, actor),
|
|
).rejects.toMatchObject({
|
|
response: {
|
|
statusCode: 404,
|
|
Message: "پرونده یافت نشد",
|
|
RuleName: "HullClaimRule",
|
|
},
|
|
});
|
|
expect(auth.registerFailure).toHaveBeenCalledWith("parsian", error);
|
|
});
|
|
});
|
|
|
|
describe("hull read context", () => {
|
|
function context(claim: any = {}) {
|
|
const service = Object.create(
|
|
ClaimRequestManagementService.prototype,
|
|
) as any;
|
|
service.claimCaseDbService = {
|
|
findById: jest
|
|
.fn()
|
|
.mockResolvedValue({
|
|
claimId: 5190845,
|
|
claimNo: 123,
|
|
expertiseId: 44237,
|
|
owner: { clientId: "tenant-1" },
|
|
...claim,
|
|
}),
|
|
};
|
|
service.resolveFanavaranClaimProduct = jest
|
|
.fn()
|
|
.mockResolvedValue("car-body");
|
|
service.getFanavaranAuthHeaders = jest.fn().mockResolvedValue(headers);
|
|
return service;
|
|
}
|
|
|
|
it("uses the saved claimId rather than claimNo and shares submission headers", async () => {
|
|
const service = context();
|
|
expect(
|
|
await service.getFanavaranHullReadContext(localId, "parsian", actor),
|
|
).toEqual({ claimId: 5190845, expertiseId: 44237, headers });
|
|
expect(service.getFanavaranAuthHeaders).toHaveBeenCalledWith(
|
|
"parsian",
|
|
undefined,
|
|
localId,
|
|
);
|
|
});
|
|
|
|
it("rejects another organization's claim before retrieving credentials", async () => {
|
|
const service = context({ owner: { clientId: "other-tenant" } });
|
|
await expect(
|
|
service.getFanavaranHullReadContext(localId, "parsian", actor),
|
|
).rejects.toThrow(ForbiddenException);
|
|
expect(service.getFanavaranAuthHeaders).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("rejects third-party claims and claims that only have a claimNo", async () => {
|
|
const service = context();
|
|
service.resolveFanavaranClaimProduct.mockResolvedValue("third-party");
|
|
await expect(
|
|
service.getFanavaranHullReadContext(localId, "parsian", actor),
|
|
).rejects.toThrow("vehicle-hull");
|
|
service.resolveFanavaranClaimProduct.mockResolvedValue("car-body");
|
|
service.claimCaseDbService.findById.mockResolvedValue({
|
|
claimNo: 123,
|
|
owner: { clientId: "tenant-1" },
|
|
});
|
|
await expect(
|
|
service.getFanavaranHullReadContext(localId, "parsian", actor),
|
|
).rejects.toThrow("claimId");
|
|
});
|
|
});
|