Added extra fanavaran fields

This commit is contained in:
SepehrYahyaee
2026-10-04 12:01:45 +03:30
parent 91074ca1e5
commit 268258a23c
13 changed files with 1216 additions and 38 deletions
+219 -17
View File
@@ -184,6 +184,11 @@ import {
import { claimPriceCapAppliesToBlame } from "src/helpers/claim-price-cap";
import { LookupsService } from "src/lookups/lookups.service";
import { ApproveFanavaranVehicleReviewDto } from "./dto/fanavaran-vehicle-review.dto";
import { UpdateFanavaranStageFieldsDto } from "./dto/fanavaran-stage-fields.dto";
import {
fanavaranFieldStageSent, type FanavaranFieldReview,
type FanavaranFieldValues,
} from "src/claim-request-management/fanavaran-stage-fields";
import {
buildFanavaranVehicleSuggestion,
collectFanavaranVehicleInquiryRecords,
@@ -5677,6 +5682,18 @@ export class ExpertClaimService {
"Vehicle classification cannot be changed after the claim was sent to Fanavaran.",
);
}
const fields =
body.stageFields !== undefined
? await this.prepareFanavaranStageFieldReview(
claim,
{
values: body.stageFields,
expectedVersion: body.stageFieldsVersion,
confirmed: body.confirmed,
},
actor,
)
: undefined;
const [groupsRaw, useTypesRaw, systemsRaw, vehicleKind] = await Promise.all(
[
this.lookupsService.getClientRemoteLookup("vehicle-groups"),
@@ -5735,30 +5752,47 @@ export class ExpertClaimService {
approvedByRole: String(actor.role),
version: Number((claim as any).fanavaranVehicleReview?.version ?? 0) + 1,
};
const updated = await this.claimCaseDbService.findByIdAndUpdate(
claimRequestId,
const updated = await this.claimCaseDbService.findOneAndUpdate(
{
$set: { fanavaranVehicleReview: review },
_id: claimRequestId,
claimId: null,
claimNo: null,
...(fields?.filter ?? {}),
},
{
$set: {
fanavaranVehicleReview: review,
...(fields ? { fanavaranFieldReview: fields.review } : {}),
},
$push: {
history: {
type: "FANAVARAN_VEHICLE_REVIEW_APPROVED",
actor: {
actorId: new Types.ObjectId(String(actor.sub)),
actorName: actor.fullName,
actorType: actor.role,
},
timestamp: review.approvedAt,
metadata: {
version: review.version,
vehicleGroupId: body.vehicleGroupId,
vehicleUseTypeId: body.vehicleUseTypeId,
vehicleSystemId: body.vehicleSystemId,
vehicleKindId: body.vehicleKindId,
},
$each: [
{
type: "FANAVARAN_VEHICLE_REVIEW_APPROVED",
actor: {
actorId: new Types.ObjectId(String(actor.sub)),
actorName: actor.fullName,
actorType: actor.role,
},
timestamp: review.approvedAt,
metadata: {
version: review.version,
vehicleGroupId: body.vehicleGroupId,
vehicleUseTypeId: body.vehicleUseTypeId,
vehicleSystemId: body.vehicleSystemId,
vehicleKindId: body.vehicleKindId,
},
},
...(fields ? [fields.history] : []),
],
},
},
},
);
if (!updated)
throw new ConflictException(
"The case changed during review. Refresh before saving.",
);
const baseClaim =
await this.claimRequestManagementService.autoSubmitToFanavaranV2OnClaimCreated(
claimRequestId,
@@ -5776,6 +5810,174 @@ export class ExpertClaimService {
};
}
async getFanavaranStageFieldsV2(claimRequestId: string, actor: any) {
await this.getClaimDetailV2(claimRequestId, actor);
const claim = await this.claimCaseDbService.findById(claimRequestId);
if (!claim) throw new NotFoundException("Claim request not found");
const blame = await this.loadBlameForClaim(claim);
return this.claimRequestManagementService.getFanavaranStageFieldsContext(
claim,
blame,
);
}
private async prepareFanavaranStageFieldReview(
claim: any,
body: UpdateFanavaranStageFieldsDto,
actor: any,
) {
if (
body.confirmed !== true ||
!body.values ||
typeof body.values !== "object" ||
Array.isArray(body.values)
) {
throw new BadRequestException(
"Explicit confirmation and field values are required.",
);
}
const version = claim.fanavaranFieldReview?.version ?? 0;
if (
!Number.isSafeInteger(body.expectedVersion) ||
body.expectedVersion !== version
) {
throw new ConflictException(
"The Fanavaran fields changed. Refresh before saving.",
);
}
const context =
await this.claimRequestManagementService.getFanavaranStageFieldsContext(
claim,
await this.loadBlameForClaim(claim),
);
const values: FanavaranFieldValues = {};
const captions: FanavaranFieldReview["captions"] = {};
const stages = new Set<string>();
const before: FanavaranFieldValues = {};
for (const [key, raw] of Object.entries(body.values)) {
const field = context.fields.find((field) => field.key === key);
if (!field)
throw new BadRequestException(
`Field ${key} does not apply to this case type.`,
);
if (fanavaranFieldStageSent(claim, field.stage)) {
throw new ConflictException(
`Field ${key} has already been sent to Fanavaran and cannot be changed.`,
);
}
let value: string | number = raw;
if (field.type === "text") {
if (
typeof raw !== "string" ||
!raw.trim() ||
raw.trim().length > 1000
) {
throw new BadRequestException(
`${key} must be a non-empty address of at most 1000 characters.`,
);
}
value = raw.trim();
} else {
if (
typeof raw !== "number" ||
!Number.isSafeInteger(raw) ||
raw < (field.min ?? 1) ||
(field.max != null && raw > field.max)
) {
throw new BadRequestException(
`Field ${key} is out of range or invalid.`,
);
}
if (field.type === "lookup") {
const row = field.options?.find((row) => row.Id === raw);
if (!row)
throw new BadRequestException(
`Field ${key} must use an active Fanavaran lookup value. Reload the options and try again.`,
);
captions[field.key] = row.Caption;
}
}
values[field.key] = value;
before[field.key] = field.value;
stages.add(field.stage);
}
if (!Object.keys(values).length)
throw new BadRequestException("No Fanavaran fields were supplied.");
const review: FanavaranFieldReview = {
values: { ...claim.fanavaranFieldReview?.values, ...values },
captions: { ...claim.fanavaranFieldReview?.captions, ...captions },
version: version + 1,
reviewedAt: new Date(),
reviewedByActorId: String(actor.sub),
reviewedByActorName: actor.fullName,
reviewedByRole: String(actor.role),
};
const conditions: Record<string, unknown>[] = [
version === 0
? {
$or: [
{ "fanavaranFieldReview.version": { $exists: false } },
{ "fanavaranFieldReview.version": 0 },
],
}
: { "fanavaranFieldReview.version": version },
];
for (const stage of stages) {
const id = {
baseClaim: "claimId",
damageCase: "dmgCaseId",
expertise: "expertiseId",
}[stage];
conditions.push({
[id]: null,
[`fanavaranSync.${stage}.${id}`]: null,
[`fanavaranSync.${stage}.status`]: { $ne: "success" },
});
if (stage === "baseClaim") conditions.push({ claimNo: null });
}
return {
review,
filter: { $and: conditions },
history: {
type: "FANAVARAN_FIELDS_REVIEWED",
actor: {
actorId: new Types.ObjectId(String(actor.sub)),
actorName: actor.fullName,
actorType: actor.role,
},
timestamp: review.reviewedAt,
metadata: { version: review.version, before, after: values, captions },
},
};
}
async updateFanavaranStageFieldsV2(
claimRequestId: string,
body: UpdateFanavaranStageFieldsDto,
actor: any,
) {
await this.getClaimDetailV2(claimRequestId, actor);
const claim = await this.claimCaseDbService.findById(claimRequestId);
if (!claim) throw new NotFoundException("Claim request not found");
const fields = await this.prepareFanavaranStageFieldReview(
claim,
body,
actor,
);
const updated = await this.claimCaseDbService.findOneAndUpdate(
{ _id: claimRequestId, ...fields.filter },
{
$set: { fanavaranFieldReview: fields.review },
$push: { history: fields.history },
},
);
if (!updated)
throw new ConflictException(
"The case changed or its stage was sent. Refresh before saving.",
);
return { version: fields.review.version };
}
/** V2 price-drop: claim locked by this expert during damage assessment. */
private async assertExpertCanEditClaimDuringReviewV2(
claim: any,