forked from Yara724/api
feat(depreciation): apply Fanavaran tables and age eligibility
Use the supplied part, severity and age codes for depreciation. Exclude vehicles older than ten from calculated and manual amounts, preserve historical assessments, and send zero depreciation to Fanavaran for ineligible vehicles.
This commit is contained in:
@@ -55,9 +55,18 @@ import {
|
||||
buildDamagedPartsPriceDropLines,
|
||||
buildPriceDropCatalogForApi,
|
||||
calculateClaimPriceDrop,
|
||||
calculatePriceDropForVehicleAge,
|
||||
parsePriceDropNumber,
|
||||
PRICE_DROP_SEVERITY_OPTIONS,
|
||||
resolveVehicleModelYearFromBlame,
|
||||
resolvePriceDropAccidentYear,
|
||||
resolvePriceDropAccidentDate,
|
||||
normalizePriceDropSelectedParts,
|
||||
resolveClaimPriceDropAge,
|
||||
buildAgeIneligiblePriceDrop,
|
||||
PRICE_DROP_AGE_INELIGIBLE_MESSAGE,
|
||||
} from "src/helpers/claim-price-drop";
|
||||
import { FANAVARAN_PRICE_DROP_AGES, FANAVARAN_PRICE_DROP_VERSION, FANAVARAN_PRICE_DROP_AGE_EFFECTIVE_FROM } from "src/static/fanavaran-price-drop";
|
||||
import {
|
||||
claimCaseStatusAfterExpertReplyV2,
|
||||
claimIsAwaitingExpertFactorValidationV2,
|
||||
@@ -4472,19 +4481,21 @@ export class ExpertClaimService {
|
||||
await this.assertExpertCanEditClaimDuringReviewV2(claim, actor);
|
||||
|
||||
const carType = claim.vehicle?.carType as ClaimVehicleTypeV2 | undefined;
|
||||
const selectedNorm = normalizeDamageSelectedParts(
|
||||
const selectedNorm = normalizePriceDropSelectedParts(
|
||||
claim.damage?.selectedParts,
|
||||
carType,
|
||||
(claim.damage as any)?.selectedOuterParts,
|
||||
);
|
||||
|
||||
let suggestedCarModelYear: number | null = null;
|
||||
let accidentYear: number | null = null;
|
||||
let calculationUnavailableReason: string | null = null;
|
||||
if (claim.blameRequestId) {
|
||||
const blameRows = await this.blameRequestDbService.find(
|
||||
{ _id: new Types.ObjectId(claim.blameRequestId.toString()) },
|
||||
{
|
||||
lean: true,
|
||||
select: "parties.role parties.person.userId parties.vehicle.inquiry",
|
||||
select: "type expert.decision.guiltyPartyId parties.role parties.person.userId parties.vehicle parties.statement.accidentDate",
|
||||
},
|
||||
);
|
||||
const blame = blameRows[0] as Record<string, unknown> | undefined;
|
||||
@@ -4492,9 +4503,17 @@ export class ExpertClaimService {
|
||||
claim,
|
||||
blame as Parameters<typeof resolveVehicleModelYearFromBlame>[1],
|
||||
);
|
||||
accidentYear = resolvePriceDropAccidentYear(blame);
|
||||
const accidentDate = resolvePriceDropAccidentDate(blame);
|
||||
if (accidentDate && accidentDate < FANAVARAN_PRICE_DROP_AGE_EFFECTIVE_FROM) {
|
||||
calculationUnavailableReason = "جدول سن ارسالی فناوران از ۱۴۰۴/۰۵/۲۸ معتبر است. برای حادثه پیش از این تاریخ مبلغ افت قیمت را دستی ثبت کنید.";
|
||||
}
|
||||
}
|
||||
|
||||
const existing = (claim as any).evaluation?.priceDrop;
|
||||
const knownAges = [existing?.vehicleAge, suggestedCarModelYear != null && accidentYear != null ? accidentYear - suggestedCarModelYear : null]
|
||||
.filter(age => Number.isInteger(age) && age >= 0);
|
||||
const vehicleAge = knownAges.some(age => age > 10) ? Math.max(...knownAges) : existing?.vehicleAge ?? knownAges[0] ?? null;
|
||||
|
||||
return {
|
||||
claimRequestId: String(claim._id),
|
||||
@@ -4504,6 +4523,17 @@ export class ExpertClaimService {
|
||||
carType: claim.vehicle?.carType,
|
||||
},
|
||||
suggestedCarModelYear,
|
||||
accidentYear,
|
||||
suggestedVehicleAge: vehicleAge,
|
||||
inquiryVehicleAge: suggestedCarModelYear != null && accidentYear != null ? accidentYear - suggestedCarModelYear : null,
|
||||
depreciationEligible: vehicleAge == null ? null : vehicleAge <= 10,
|
||||
ineligibilityReason: vehicleAge != null && vehicleAge > 10 ? PRICE_DROP_AGE_INELIGIBLE_MESSAGE : null,
|
||||
calculationVersion: FANAVARAN_PRICE_DROP_VERSION,
|
||||
ageTableEffectiveFrom: FANAVARAN_PRICE_DROP_AGE_EFFECTIVE_FROM,
|
||||
calculationUnavailableReason,
|
||||
ageCatalog: FANAVARAN_PRICE_DROP_AGES.map(row => ({
|
||||
vehicleAge: row.age, fanavaranAgeId: row.id, coefficient: row.coefficient,
|
||||
})),
|
||||
priceDrop: existing ?? null,
|
||||
severityOptions: PRICE_DROP_SEVERITY_OPTIONS,
|
||||
priceDropCatalog: buildPriceDropCatalogForApi(),
|
||||
@@ -4533,18 +4563,71 @@ export class ExpertClaimService {
|
||||
vehicleSet["vehicle.carModel"] = String(body.carModel).trim();
|
||||
}
|
||||
|
||||
let carModelYear = body.carModelYear;
|
||||
if (carModelYear != null && (!Number.isInteger(carModelYear) || carModelYear < 1300 || carModelYear >= 1500)) {
|
||||
throw new BadRequestException("سال ساخت باید یک سال شمسی معتبر باشد؛ برای سال میلادی، سن خودرو در زمان حادثه را وارد کنید.");
|
||||
}
|
||||
let accidentYear: number | null = null;
|
||||
let blame: any = null;
|
||||
let accidentDate: string | null = null;
|
||||
if (claim.blameRequestId) {
|
||||
const blameRows = await this.blameRequestDbService.find(
|
||||
{ _id: new Types.ObjectId(claim.blameRequestId.toString()) },
|
||||
{
|
||||
lean: true,
|
||||
select: "type expert.decision.guiltyPartyId parties.role parties.person.userId parties.vehicle parties.statement.accidentDate",
|
||||
},
|
||||
);
|
||||
carModelYear ??=
|
||||
resolveVehicleModelYearFromBlame(
|
||||
claim,
|
||||
blameRows[0] as Parameters<
|
||||
typeof resolveVehicleModelYearFromBlame
|
||||
>[1],
|
||||
) ?? undefined;
|
||||
blame = blameRows[0];
|
||||
accidentYear = resolvePriceDropAccidentYear(blame);
|
||||
accidentDate = resolvePriceDropAccidentDate(blame);
|
||||
}
|
||||
if (body.vehicleAge != null && (!Number.isInteger(body.vehicleAge) || body.vehicleAge < 0)) {
|
||||
throw new BadRequestException("سن خودرو در زمان حادثه باید عددی صحیح و نامنفی باشد.");
|
||||
}
|
||||
const enteredAge = body.vehicleAge ?? (carModelYear != null && accidentYear != null ? accidentYear - carModelYear : undefined);
|
||||
const vehicleAge = resolveClaimPriceDropAge(claim, blame, enteredAge);
|
||||
if (vehicleAge != null && vehicleAge > 10) {
|
||||
if (body.manualPriceDrop != null && body.manualPriceDrop !== 0) {
|
||||
throw new BadRequestException(PRICE_DROP_AGE_INELIGIBLE_MESSAGE);
|
||||
}
|
||||
const priceDrop = {
|
||||
...buildAgeIneligiblePriceDrop(vehicleAge, body.carPrice ?? (claim as any).evaluation?.priceDrop?.carPrice),
|
||||
manualOverride: false,
|
||||
partLines: [],
|
||||
...(accidentYear != null ? { accidentYear } : {}),
|
||||
...(carModelYear != null ? { carModel: carModelYear } : {}),
|
||||
};
|
||||
await this.claimCaseDbService.findByIdAndUpdate(claimRequestId, {
|
||||
$set: { ...vehicleSet, "evaluation.priceDrop": priceDrop },
|
||||
});
|
||||
return { claimRequestId, vehicle: claim.vehicle, priceDrop, partLines: [] };
|
||||
}
|
||||
|
||||
// Manual override path — expert knows the number, skip calculation entirely
|
||||
if (body.manualPriceDrop != null) {
|
||||
if (!Number.isFinite(body.manualPriceDrop) || body.manualPriceDrop < 0) {
|
||||
if (!Number.isSafeInteger(body.manualPriceDrop) || body.manualPriceDrop < 0) {
|
||||
throw new BadRequestException(
|
||||
"manualPriceDrop must be a non-negative finite number.",
|
||||
"مبلغ افت قیمت باید عددی صحیح و نامنفی به ریال باشد.",
|
||||
);
|
||||
}
|
||||
|
||||
// Manual path — change totalPriceDrop → total to match calculated path
|
||||
if (body.manualPriceDrop > 0 && vehicleAge == null) {
|
||||
throw new BadRequestException("برای ثبت افت قیمت، سن خودرو در زمان حادثه را مشخص کنید.");
|
||||
}
|
||||
const manualPayload = {
|
||||
...(vehicleAge != null ? { vehicleAge, eligible: true } : {}),
|
||||
manualOverride: true,
|
||||
total: body.manualPriceDrop, // ← was totalPriceDrop
|
||||
total: parsePriceDropNumber(body.manualPriceDrop),
|
||||
carPrice: body.carPrice != null ? parsePriceDropNumber(body.carPrice)
|
||||
: (claim as any).evaluation?.priceDrop?.carPrice,
|
||||
partLines: [],
|
||||
};
|
||||
|
||||
@@ -4568,8 +4651,11 @@ export class ExpertClaimService {
|
||||
};
|
||||
}
|
||||
|
||||
// Calculated path — existing logic unchanged
|
||||
// At the top of the calculated path, after the manualPriceDrop early return:
|
||||
if (accidentDate && accidentDate < FANAVARAN_PRICE_DROP_AGE_EFFECTIVE_FROM) {
|
||||
throw new BadRequestException("جدول سن ارسالی فناوران از ۱۴۰۴/۰۵/۲۸ معتبر است. برای حادثه پیش از این تاریخ مبلغ افت قیمت را دستی ثبت کنید.");
|
||||
}
|
||||
|
||||
// An explicit part list is required when replacing a manual correction.
|
||||
const existingPriceDrop = (claim as any).evaluation?.priceDrop;
|
||||
if (
|
||||
existingPriceDrop?.manualOverride === true &&
|
||||
@@ -4582,38 +4668,24 @@ export class ExpertClaimService {
|
||||
}
|
||||
|
||||
const carType = claim.vehicle?.carType as ClaimVehicleTypeV2 | undefined;
|
||||
const selectedNorm = normalizeDamageSelectedParts(
|
||||
const selectedNorm = normalizePriceDropSelectedParts(
|
||||
claim.damage?.selectedParts,
|
||||
carType,
|
||||
(claim.damage as any)?.selectedOuterParts,
|
||||
);
|
||||
|
||||
if (!body.partSeverities?.length) {
|
||||
if (!Array.isArray(body.partSeverities) || !body.partSeverities.length) {
|
||||
throw new BadRequestException(
|
||||
"Provide at least one partSeverities line, or use manualPriceDrop for a direct override.",
|
||||
"شدت آسیب حداقل یک قطعه را انتخاب کنید یا مبلغ نهایی افت قیمت را دستی ثبت کنید.",
|
||||
);
|
||||
}
|
||||
if (body.partSeverities.some(p => !p || typeof p !== "object")) {
|
||||
throw new BadRequestException("فهرست قطعات افت قیمت معتبر نیست. گروه قطعات و شدت آسیب را دوباره انتخاب کنید.");
|
||||
}
|
||||
|
||||
let carModelYear = body.carModelYear;
|
||||
if (carModelYear == null && claim.blameRequestId) {
|
||||
const blameRows = await this.blameRequestDbService.find(
|
||||
{ _id: new Types.ObjectId(claim.blameRequestId.toString()) },
|
||||
{
|
||||
lean: true,
|
||||
select: "parties.role parties.person.userId parties.vehicle.inquiry",
|
||||
},
|
||||
);
|
||||
carModelYear =
|
||||
resolveVehicleModelYearFromBlame(
|
||||
claim,
|
||||
blameRows[0] as Parameters<
|
||||
typeof resolveVehicleModelYearFromBlame
|
||||
>[1],
|
||||
) ?? undefined;
|
||||
}
|
||||
if (carModelYear == null || !Number.isFinite(carModelYear)) {
|
||||
if (body.vehicleAge == null && (carModelYear == null || accidentYear == null)) {
|
||||
throw new BadRequestException(
|
||||
"carModelYear is required when it cannot be resolved from the linked blame plate inquiry.",
|
||||
"سن خودرو در زمان حادثه را وارد کنید؛ سال ساخت یا تاریخ حادثه برای محاسبه سن در دسترس نیست.",
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4622,6 +4694,7 @@ export class ExpertClaimService {
|
||||
body.partSeverities.map((p) => ({
|
||||
partId: p.partId,
|
||||
severity: p.severity,
|
||||
fanavaranPartId: p.fanavaranPartId,
|
||||
})),
|
||||
carType,
|
||||
);
|
||||
@@ -4634,14 +4707,17 @@ export class ExpertClaimService {
|
||||
);
|
||||
}
|
||||
|
||||
const calculated = calculateClaimPriceDrop(
|
||||
body.carPrice,
|
||||
carModelYear,
|
||||
coefficients,
|
||||
);
|
||||
const calculated = body.vehicleAge != null
|
||||
? {
|
||||
...calculatePriceDropForVehicleAge(body.carPrice, body.vehicleAge, coefficients),
|
||||
...(carModelYear != null ? { carModel: carModelYear } : {}),
|
||||
...(accidentYear != null ? { accidentYear } : {}),
|
||||
}
|
||||
: calculateClaimPriceDrop(body.carPrice, carModelYear!, coefficients, accidentYear!);
|
||||
|
||||
const priceDropPayload = {
|
||||
...calculated,
|
||||
manualOverride: false,
|
||||
partLines: lines,
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user