forked from Yara724/api
update the 2 new apis for getting expertise and files of the request
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Controller,
|
||||
Get,
|
||||
Param,
|
||||
Query,
|
||||
UseGuards,
|
||||
} from "@nestjs/common";
|
||||
import {
|
||||
ApiBearerAuth,
|
||||
ApiOperation,
|
||||
ApiParam,
|
||||
ApiQuery,
|
||||
ApiTags,
|
||||
} from "@nestjs/swagger";
|
||||
import { LocalActorAuthGuard } from "src/auth/guards/actor-local.guard";
|
||||
import { CurrentUser } from "src/decorators/user.decorator";
|
||||
import {
|
||||
FANAVARAN_CLIENT_SWAGGER_ENUM,
|
||||
isFanavaranClientKey,
|
||||
normalizeFanavaranClientKey,
|
||||
} from "src/core/config/fanavaran-client.config";
|
||||
import { FanavaranHullReadService } from "./fanavaran-hull-read.service";
|
||||
|
||||
@ApiTags("fanavaran")
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(LocalActorAuthGuard)
|
||||
@ApiParam({ name: "client", enum: FANAVARAN_CLIENT_SWAGGER_ENUM })
|
||||
@ApiParam({
|
||||
name: "claimCaseId",
|
||||
description:
|
||||
"Local claim case MongoDB ObjectId; the saved Fanavaran claimId is used upstream",
|
||||
})
|
||||
@Controller("v2/fanavaran/:client/claim-cases/:claimCaseId/hull")
|
||||
export class FanavaranHullReadController {
|
||||
constructor(private readonly hull: FanavaranHullReadService) {}
|
||||
|
||||
@Get("files")
|
||||
@ApiOperation({
|
||||
summary:
|
||||
"Retrieve submitted hull attachment metadata from Fanavaran (GEN.05)",
|
||||
description:
|
||||
"Live file list, including Id, ClaimId, FileName, FileTypeId and Status. The list does not include Base64 Data. Does not depend on locally recorded file IDs.",
|
||||
})
|
||||
files(
|
||||
@Param("client") client: string,
|
||||
@Param("claimCaseId") claimCaseId: string,
|
||||
@CurrentUser() actor: any,
|
||||
) {
|
||||
return this.hull.files(this.clientKey(client), claimCaseId, actor);
|
||||
}
|
||||
|
||||
@Get("files/:fileId")
|
||||
@ApiParam({
|
||||
name: "fileId",
|
||||
type: Number,
|
||||
description: "Fanavaran file Id returned by the remote file list",
|
||||
})
|
||||
@ApiOperation({
|
||||
summary: "Retrieve a submitted hull file including Base64 Data (GEN.05)",
|
||||
})
|
||||
file(
|
||||
@Param("client") client: string,
|
||||
@Param("claimCaseId") claimCaseId: string,
|
||||
@Param("fileId") fileId: string,
|
||||
@CurrentUser() actor: any,
|
||||
) {
|
||||
return this.hull.file(this.clientKey(client), claimCaseId, fileId, actor);
|
||||
}
|
||||
|
||||
@Get("expertise/dmg-sections")
|
||||
@ApiQuery({
|
||||
name: "expertiseId",
|
||||
required: false,
|
||||
type: Number,
|
||||
description:
|
||||
"Optional historical Fanavaran expertise Id; defaults to the saved expertiseId",
|
||||
})
|
||||
@ApiOperation({
|
||||
summary:
|
||||
"Retrieve submitted hull expertise damage sections and costs (GEN.12)",
|
||||
description:
|
||||
"Reads live damage section IDs, costs, amounts, CINumber and CITrackingCode when supplied by Fanavaran. Does not resubmit expertise.",
|
||||
})
|
||||
damageSections(
|
||||
@Param("client") client: string,
|
||||
@Param("claimCaseId") claimCaseId: string,
|
||||
@CurrentUser() actor: any,
|
||||
@Query("expertiseId") expertiseId?: string,
|
||||
) {
|
||||
return this.hull.damageSections(
|
||||
this.clientKey(client),
|
||||
claimCaseId,
|
||||
actor,
|
||||
expertiseId,
|
||||
);
|
||||
}
|
||||
|
||||
private clientKey(client: string) {
|
||||
if (!isFanavaranClientKey(client))
|
||||
throw new BadRequestException("Invalid Fanavaran client");
|
||||
return normalizeFanavaranClientKey(client);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,221 @@
|
||||
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");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,131 @@
|
||||
import { HttpService } from "@nestjs/axios";
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import { firstValueFrom } from "rxjs";
|
||||
import { ClaimRequestManagementService } from "src/claim-request-management/claim-request-management.service";
|
||||
import {
|
||||
getFanavaranLookupBaseUrl,
|
||||
type FanavaranClientKey,
|
||||
} from "src/core/config/fanavaran-client.config";
|
||||
import { FanavaranAuthService } from "./fanavaran-auth.service";
|
||||
import { toFanavaranHttpException } from "./fanavaran-http-exception";
|
||||
|
||||
type ReadActor = {
|
||||
sub: string;
|
||||
role?: string;
|
||||
clientKey?: string;
|
||||
username?: string;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class FanavaranHullReadService {
|
||||
constructor(
|
||||
private readonly management: ClaimRequestManagementService,
|
||||
private readonly http: HttpService,
|
||||
private readonly auth: FanavaranAuthService,
|
||||
) {}
|
||||
|
||||
/** GEN.05 list contains file metadata/Status, not Base64 Data. */
|
||||
async files(
|
||||
clientKey: FanavaranClientKey,
|
||||
claimCaseId: string,
|
||||
actor: ReadActor,
|
||||
) {
|
||||
const context = await this.management.getFanavaranHullReadContext(
|
||||
claimCaseId,
|
||||
clientKey,
|
||||
actor,
|
||||
);
|
||||
const url = `${getFanavaranLookupBaseUrl(clientKey)}/car/vehicle-hull-claims/${context.claimId}/files`;
|
||||
return {
|
||||
clientKey,
|
||||
claimCaseId,
|
||||
claimId: context.claimId,
|
||||
data: await this.get(clientKey, url, context.headers),
|
||||
};
|
||||
}
|
||||
|
||||
/** GEN.05 single-file response includes Data as Base64. */
|
||||
async file(
|
||||
clientKey: FanavaranClientKey,
|
||||
claimCaseId: string,
|
||||
fileId: string,
|
||||
actor: ReadActor,
|
||||
) {
|
||||
const id = this.positiveId(fileId, "fileId");
|
||||
const context = await this.management.getFanavaranHullReadContext(
|
||||
claimCaseId,
|
||||
clientKey,
|
||||
actor,
|
||||
);
|
||||
const url = `${getFanavaranLookupBaseUrl(clientKey)}/car/vehicle-hull-claims/${context.claimId}/files/${id}`;
|
||||
return {
|
||||
clientKey,
|
||||
claimCaseId,
|
||||
claimId: context.claimId,
|
||||
fileId: id,
|
||||
data: await this.get(clientKey, url, context.headers),
|
||||
};
|
||||
}
|
||||
|
||||
/** GEN.12: read the stored expertise, or an explicitly selected historical one. */
|
||||
async damageSections(
|
||||
clientKey: FanavaranClientKey,
|
||||
claimCaseId: string,
|
||||
actor: ReadActor,
|
||||
expertiseId?: string,
|
||||
) {
|
||||
const selectedId =
|
||||
expertiseId === undefined
|
||||
? undefined
|
||||
: this.positiveId(expertiseId, "expertiseId");
|
||||
const context = await this.management.getFanavaranHullReadContext(
|
||||
claimCaseId,
|
||||
clientKey,
|
||||
actor,
|
||||
);
|
||||
const id = selectedId ?? context.expertiseId;
|
||||
if (id == null)
|
||||
throw new BadRequestException(
|
||||
"No submitted expertiseId is stored; provide expertiseId for a previously submitted expertise",
|
||||
);
|
||||
const url = `${getFanavaranLookupBaseUrl(clientKey)}/car/vehicle-hull-claims/${context.claimId}/expertise/${id}/dmg-sections`;
|
||||
return {
|
||||
clientKey,
|
||||
claimCaseId,
|
||||
claimId: context.claimId,
|
||||
expertiseId: id,
|
||||
data: await this.get(clientKey, url, context.headers),
|
||||
};
|
||||
}
|
||||
|
||||
private positiveId(value: string, field: string): number {
|
||||
const id = Number(value);
|
||||
if (!/^\d+$/.test(value) || !Number.isSafeInteger(id) || id <= 0) {
|
||||
throw new BadRequestException(`${field} must be a positive integer`);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
private async get(
|
||||
clientKey: FanavaranClientKey,
|
||||
url: string,
|
||||
headers: Record<string, string>,
|
||||
): Promise<unknown> {
|
||||
this.auth.assertNotInBackoff(clientKey);
|
||||
try {
|
||||
const response = await firstValueFrom(
|
||||
this.http.get(url, {
|
||||
headers: { ...headers, Accept: "application/json" },
|
||||
timeout: 20000,
|
||||
}),
|
||||
);
|
||||
this.auth.clearBackoff(clientKey);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
this.auth.registerFailure(clientKey, error);
|
||||
throw toFanavaranHttpException(error, {
|
||||
fallback: "Failed to retrieve submitted hull data from Fanavaran",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -205,7 +205,7 @@ export class FanavaranController {
|
||||
@ApiOperation({
|
||||
summary: "Preview Fanavaran attachment upload plan",
|
||||
description:
|
||||
"Lists local required-document and captured damage images that would be sent to GEN.07. Shows which images are already recorded as uploaded to Fanavaran.",
|
||||
"Lists local documents and captured images for third-party GEN.07 or hull GEN.05. Returns external file IDs, saved upload status/errors, local file availability, and pending image counts.",
|
||||
})
|
||||
@ApiParam({
|
||||
name: "client",
|
||||
@@ -231,7 +231,7 @@ export class FanavaranController {
|
||||
@ApiOperation({
|
||||
summary: "Submit missing Fanavaran attachments",
|
||||
description:
|
||||
"Uploads every local image that does not already have a recorded successful GEN.07 Fanavaran file upload. Uses one multipart request per image.",
|
||||
"Uploads pending local images to third-party GEN.07 or hull GEN.05 and persists each returned fileId. Reports failures and skipped/missing local files. Uses one multipart request per image.",
|
||||
})
|
||||
@ApiParam({
|
||||
name: "client",
|
||||
|
||||
@@ -1,10 +1,26 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { HttpModule } from "@nestjs/axios";
|
||||
import { ConfigModule, ConfigService } from "@nestjs/config";
|
||||
import { createHttpModuleOptions } from "src/core/config/http-proxy.factory";
|
||||
import { ClaimRequestManagementModule } from "src/claim-request-management/claim-request-management.module";
|
||||
import { FanavaranAuditModule } from "./fanavaran-audit.module";
|
||||
import { FanavaranController } from "./fanavaran.controller";
|
||||
import { FanavaranLookupModule } from "./fanavaran-lookup.module";
|
||||
import { FanavaranHullReadService } from "./fanavaran-hull-read.service";
|
||||
import { FanavaranHullReadController } from "./fanavaran-hull-read.controller";
|
||||
|
||||
@Module({
|
||||
imports: [ClaimRequestManagementModule, FanavaranAuditModule],
|
||||
controllers: [FanavaranController],
|
||||
imports: [
|
||||
ClaimRequestManagementModule,
|
||||
FanavaranAuditModule,
|
||||
FanavaranLookupModule,
|
||||
HttpModule.registerAsync({
|
||||
imports: [ConfigModule],
|
||||
inject: [ConfigService],
|
||||
useFactory: createHttpModuleOptions,
|
||||
}),
|
||||
],
|
||||
controllers: [FanavaranController, FanavaranHullReadController],
|
||||
providers: [FanavaranHullReadService],
|
||||
})
|
||||
export class FanavaranModule {}
|
||||
|
||||
Reference in New Issue
Block a user