Add TejaratNo notifier review and submission validation

This commit is contained in:
SepehrYahyaee
2026-10-10 12:51:11 +03:30
parent a410655faa
commit 8f568f379c
14 changed files with 588 additions and 34 deletions
@@ -2,6 +2,7 @@ import { Type } from "class-transformer";
import {
Equals,
IsInt,
IsIn,
IsOptional,
IsString,
Max,
@@ -12,6 +13,26 @@ import {
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
export class FanavaranStageFieldValuesDto {
@ApiPropertyOptional({
enum: [5516, 5517],
description:
"TejaratNo only: notifier type; required before initial submission",
})
@IsOptional()
@IsInt()
@IsIn([5516, 5517])
NotifierTypeId?: number;
@ApiPropertyOptional({
enum: [5519, 5520, 5521, 5522, 5523, 5524, 5525, 5526],
description:
"TejaratNo only: notifier relation; required before initial submission",
})
@IsOptional()
@IsInt()
@IsIn([5519, 5520, 5521, 5522, 5523, 5524, 5525, 5526])
NotifierRelationId?: number;
@ApiPropertyOptional()
@IsOptional()
@IsString()
+13
View File
@@ -1,5 +1,8 @@
"use strict";
import { hasReviewedTejaratNotifier } from "src/fanavaran/tejarat-notifier";
import { resolveFanavaranClientKey } from "src/core/config/fanavaran-client.config";
import {
BadRequestException,
ConflictException,
@@ -4104,6 +4107,16 @@ export class ExpertClaimService {
actor,
)
: undefined;
if (
!hasReviewedTejaratNotifier(
fields?.review.values ?? claim.fanavaranFieldReview?.values,
resolveFanavaranClientKey(),
)
) {
throw new BadRequestException(
"نوع و نسبت اعلام‌کننده را پیش از تایید و ارسال پرونده انتخاب کنید.",
);
}
const [groupsRaw, useTypesRaw, systemsRaw, vehicleKind] = await Promise.all(
[
this.lookupsService.getClientRemoteLookup("vehicle-groups"),
+298 -24
View File
@@ -1,3 +1,4 @@
import { tejaratNotifierLookup } from "src/fanavaran/tejarat-notifier";
import {
BadRequestException,
ConflictException,
@@ -38,6 +39,8 @@ function setup(product = "third-party", extra: any = {}) {
management.getFanavaranLookupRows = jest
.fn()
.mockImplementation(async (_client, name) => {
const notifier = tejaratNotifierLookup(name);
if (notifier) return notifier;
const ids = {
cities: 702,
"accident-report-type": 156,
@@ -65,11 +68,9 @@ function setup(product = "third-party", extra: any = {}) {
const service = Object.create(ExpertClaimService.prototype) as any;
service.getClaimDetailV2 = jest.fn().mockResolvedValue({});
service.blameRequestDbService = {
findById: jest
.fn()
.mockResolvedValue({
type: product === "car-body" ? "CAR_BODY" : "THIRD_PARTY",
}),
findById: jest.fn().mockResolvedValue({
type: product === "car-body" ? "CAR_BODY" : "THIRD_PARTY",
}),
};
service.claimCaseDbService = {
findById: jest.fn().mockResolvedValue(claim),
@@ -87,22 +88,33 @@ function setup(product = "third-party", extra: any = {}) {
return [{ Id: 1, Caption: "شخصی", VehicleGroupId: 1 }];
return [{ Id: 14, Caption: "پراید", VehicleGroupId: 1 }];
}),
getActiveVehicleKindById: jest
.fn()
.mockResolvedValue({
Id: 7456,
Caption: "پراید",
VehicleGroupId: 1,
VehicleSystemId: 14,
}),
getActiveVehicleKindById: jest.fn().mockResolvedValue({
Id: 7456,
Caption: "پراید",
VehicleGroupId: 1,
VehicleSystemId: 14,
}),
};
return { service, management, claim };
}
describe("Expert Fanavaran stage fields", () => {
it.each(["third-party", "car-body"])(
"puts reviewed base fields into the actual %s preview after derived/default enrichment",
async (product) => {
const originalClient = process.env.FANAVARAN_CLIENT;
beforeEach(() => {
process.env.FANAVARAN_CLIENT = "parsian";
});
afterEach(() => {
if (originalClient === undefined) delete process.env.FANAVARAN_CLIENT;
else process.env.FANAVARAN_CLIENT = originalClient;
});
it.each([
["third-party", "parsian"],
["car-body", "parsian"],
["third-party", "tejaratno"],
["car-body", "tejaratno"],
] as const)(
"puts reviewed base fields into the actual %s/%s preview after derived/default enrichment",
async (product, clientKey) => {
const { management, claim } = setup(product, {
fanavaranFieldReview: {
values: {
@@ -113,6 +125,8 @@ describe("Expert Fanavaran stage fields", () => {
CulpritTypeId: 301,
AuthorityCulpritId: 8,
CustomerFaultPercent: 0,
NotifierTypeId: 5517,
NotifierRelationId: 5523,
},
},
fanavaranVehicleReview: { vehicleUseType: { id: 1 } },
@@ -127,13 +141,11 @@ describe("Expert Fanavaran stage fields", () => {
findByIdAndUpdate: jest.fn(),
};
management.blameRequestDbService = {
findById: jest
.fn()
.mockResolvedValue({
type: product === "car-body" ? "CAR_BODY" : "THIRD_PARTY",
parties: [{ role: "FIRST" }],
createdAt: new Date(),
}),
findById: jest.fn().mockResolvedValue({
type: product === "car-body" ? "CAR_BODY" : "THIRD_PARTY",
parties: [{ role: "FIRST" }],
createdAt: new Date(),
}),
};
management.resolveAccidentVehicleUsedFromApis = jest
.fn()
@@ -147,7 +159,7 @@ describe("Expert Fanavaran stage fields", () => {
payload.CustomerFaultPercent = 100;
payload.AuthorityCulpritId = 1;
});
const payload = await management.previewFanavaranSubmitV2(id, "parsian", {
const payload = await management.previewFanavaranSubmitV2(id, clientKey, {
persistPayload: false,
forceRefreshPolicy: false,
});
@@ -156,6 +168,15 @@ describe("Expert Fanavaran stage fields", () => {
AccidentCityId: 702,
AccidentReportTypeId: 156,
});
if (clientKey === "tejaratno")
expect(payload).toMatchObject({
NotifierTypeId: 5517,
NotifierRelationId: 5523,
});
else {
expect(payload).not.toHaveProperty("NotifierTypeId");
expect(payload).not.toHaveProperty("NotifierRelationId");
}
if (product === "car-body") {
expect(payload).toMatchObject({
AccidentTypeId: 4,
@@ -491,4 +512,257 @@ describe("Expert Fanavaran stage fields", () => {
});
expect(fanavaranStageFields("third-party")).toHaveLength(5);
});
it.each(["third-party", "car-body"])(
"shows only the supplied TejaratNo notifier options for %s",
async (product) => {
process.env.FANAVARAN_CLIENT = "TejaratNo";
const { service } = setup(product);
const context = await service.getFanavaranStageFieldsV2(id, actor);
expect(context.clientKey).toBe("tejaratno");
const fields = context.fields.filter((field) =>
field.key.startsWith("Notifier"),
);
expect(fields.map((field) => field.key)).toEqual([
"NotifierTypeId",
"NotifierRelationId",
]);
expect(
fields.every(
(field) =>
field.value === null &&
field.editable &&
field.requiredBeforeSubmit,
),
).toBe(true);
expect(fields[0].options.map((row) => row.Id).sort()).toEqual([
5516, 5517,
]);
expect(fields[1].options.map((row) => row.Id).sort()).toEqual([
5519, 5520, 5521, 5522, 5523, 5524, 5525, 5526,
]);
},
);
it.each(["parsian", "moallem"])(
"hides and rejects notifier edits for %s",
async (tenant) => {
process.env.FANAVARAN_CLIENT = tenant;
const { service } = setup();
const context = await service.getFanavaranStageFieldsV2(id, actor);
expect(
context.fields.some((field) => field.key.startsWith("Notifier")),
).toBe(false);
await expect(
service.updateFanavaranStageFieldsV2(
id,
{
values: { NotifierTypeId: 5516 },
expectedVersion: 0,
confirmed: true,
},
actor,
),
).rejects.toBeInstanceOf(BadRequestException);
expect(
service.claimCaseDbService.findOneAndUpdate,
).not.toHaveBeenCalled();
},
);
it.each([
{ NotifierTypeId: 5518 },
{ NotifierTypeId: 5519 },
{ NotifierRelationId: 5516 },
{ NotifierRelationId: 5527 },
{ NotifierTypeId: "5516" },
{ NotifierRelationId: null },
])("rejects excluded or invalid notifier values: %j", async (values) => {
process.env.FANAVARAN_CLIENT = "tejaratno";
const { service } = setup();
await expect(
service.updateFanavaranStageFieldsV2(
id,
{ values, expectedVersion: 0, confirmed: true },
actor,
),
).rejects.toBeInstanceOf(BadRequestException);
expect(service.claimCaseDbService.findOneAndUpdate).not.toHaveBeenCalled();
});
it("persists notifier IDs, captions and audit history with the existing version guard", async () => {
process.env.FANAVARAN_CLIENT = "tejaratno";
const { service, claim } = setup();
await service.updateFanavaranStageFieldsV2(
id,
{
values: { NotifierTypeId: 5516, NotifierRelationId: 5521 },
expectedVersion: 0,
confirmed: true,
},
actor,
);
expect(claim.fanavaranFieldReview).toMatchObject({
values: { NotifierTypeId: 5516, NotifierRelationId: 5521 },
captions: { NotifierTypeId: "آسيب ديده", NotifierRelationId: "مالک" },
version: 1,
});
const [filter, update] =
service.claimCaseDbService.findOneAndUpdate.mock.calls[0];
expect(filter.$and).toContainEqual({
claimId: null,
"fanavaranSync.baseClaim.claimId": null,
"fanavaranSync.baseClaim.status": { $ne: "success" },
});
expect(update.$push.history.metadata.after).toMatchObject({
NotifierTypeId: 5516,
NotifierRelationId: 5521,
});
});
it("saves both notifier choices atomically before combined approval starts submission", async () => {
process.env.FANAVARAN_CLIENT = "tejaratno";
const { service, claim, management } = setup();
await service.approveFanavaranVehicleReviewV2(
id,
{
...vehicleBody,
stageFieldsVersion: 0,
stageFields: { NotifierTypeId: 5517, NotifierRelationId: 5526 },
},
actor,
);
expect(claim.fanavaranFieldReview.values).toMatchObject({
NotifierTypeId: 5517,
NotifierRelationId: 5526,
});
expect(
management.autoSubmitToFanavaranV2OnClaimCreated,
).toHaveBeenCalledTimes(1);
});
it.each([undefined, { NotifierTypeId: 5516 }, { NotifierRelationId: 5523 }])(
"does not approve a TejaratNo vehicle with incomplete notifier selection: %j",
async (stageFields) => {
process.env.FANAVARAN_CLIENT = "tejaratno";
const { service, management } = setup();
await expect(
service.approveFanavaranVehicleReviewV2(
id,
{ ...vehicleBody, stageFieldsVersion: 0, stageFields },
actor,
),
).rejects.toBeInstanceOf(BadRequestException);
expect(
service.claimCaseDbService.findOneAndUpdate,
).not.toHaveBeenCalled();
expect(
management.autoSubmitToFanavaranV2OnClaimCreated,
).not.toHaveBeenCalled();
},
);
it("locks notifier fields after initial submission and shows the submitted selection", async () => {
process.env.FANAVARAN_CLIENT = "tejaratno";
const { service } = setup("third-party", {
claimId: 10,
fanavaranSync: {
baseClaim: {
lastPayload: { NotifierTypeId: 5517, NotifierRelationId: 5523 },
},
},
});
const context = await service.getFanavaranStageFieldsV2(id, actor);
expect(
context.fields.find((field) => field.key === "NotifierTypeId"),
).toMatchObject({
editable: false,
value: 5517,
source: "submitted",
caption: "مقصر حادثه",
});
await expect(
service.updateFanavaranStageFieldsV2(
id,
{
values: { NotifierTypeId: 5516 },
expectedVersion: 0,
confirmed: true,
},
actor,
),
).rejects.toBeInstanceOf(ConflictException);
});
it("blocks manual initial submission without notifier review before remote work", async () => {
const { management, claim } = setup("third-party", {
fanavaranVehicleReview: {
approved: true,
vehicleGroup: { id: 1 },
vehicleUseType: { id: 1 },
vehicleSystem: { id: 14 },
vehicleKind: { id: 7456 },
},
});
management.logger = { log: jest.fn() };
management.claimCaseDbService = {
findById: jest.fn().mockResolvedValue(claim),
};
management.previewFanavaranSubmitV2 = jest.fn();
await expect(
management.executeFanavaranV2Submit(id, "tejaratno"),
).rejects.toMatchObject({
response: { code: "FANAVARAN_NOTIFIER_REVIEW_REQUIRED" },
});
expect(management.previewFanavaranSubmitV2).not.toHaveBeenCalled();
});
it("soft-skips automatic TejaratNo submission until both choices are saved", async () => {
process.env.FANAVARAN_CLIENT = "tejaratno";
const { management, claim } = setup("third-party", {
fanavaranVehicleReview: { approved: true },
});
management.blameRequestDbService = {
findById: jest.fn().mockResolvedValue({ type: "THIRD_PARTY" }),
};
expect(await management.getFanavaranAutoSubmitSkipReason(claim)).toContain(
"اعلام‌کننده",
);
claim.fanavaranFieldReview = {
values: { NotifierTypeId: 5516, NotifierRelationId: 5519 },
};
expect(await management.getFanavaranAutoSubmitSkipReason(claim)).toBeNull();
delete claim.fanavaranFieldReview;
claim.claimId = 10;
expect(await management.getFanavaranAutoSubmitSkipReason(claim)).toBe(
"Already submitted to Fanavaran",
);
});
it("reuses notifier selections from a submitted base payload and preserves legacy records without them", () => {
const payload = {};
applyFanavaranStageFieldReview(
payload,
{
claimId: 10,
fanavaranSync: {
baseClaim: {
lastPayload: { NotifierTypeId: 5517, NotifierRelationId: 5523 },
},
},
},
"car-body",
"baseClaim",
"tejaratno",
);
expect(payload).toEqual({ NotifierTypeId: 5517, NotifierRelationId: 5523 });
const legacy = {};
applyFanavaranStageFieldReview(
legacy,
{ claimId: 10 },
"car-body",
"baseClaim",
"tejaratno",
);
expect(legacy).toEqual({});
});
});
@@ -57,6 +57,14 @@ function makeService(overrides?: { kind?: typeof kind }) {
}
describe("Fanavaran vehicle review", () => {
const originalClient = process.env.FANAVARAN_CLIENT;
beforeEach(() => {
process.env.FANAVARAN_CLIENT = "parsian";
});
afterEach(() => {
if (originalClient === undefined) delete process.env.FANAVARAN_CLIENT;
else process.env.FANAVARAN_CLIENT = originalClient;
});
it("persists an authoritative snapshot and releases deferred submission", async () => {
const service = makeService();