forked from Yara724/api
Added complete validation for expert claim submit state
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
import { REPAIR_LINE_AMOUNT_TOMAN } from "src/constants/repair-amount-limits";
|
||||
import { DaghiOption } from "src/Types&Enums/claim-request-management/daghi-option.enum";
|
||||
import { TypeOfDamage } from "src/Types&Enums/claim-request-management/type-of-damage.enum";
|
||||
import { parseMoneyAmountToman } from "src/utils/unicode-digits";
|
||||
|
||||
type ExpertReplyPricingPart = {
|
||||
partId?: unknown;
|
||||
typeOfDamage?: unknown;
|
||||
price?: unknown;
|
||||
salary?: unknown;
|
||||
totalPayment?: unknown;
|
||||
daghi?: { option?: unknown; price?: unknown };
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -28,13 +32,30 @@ export function getExpertReplyPricingValidationError(
|
||||
? `Part ${String(part.partId)}`
|
||||
: `Damaged part ${index + 1}`;
|
||||
|
||||
if (!part || typeof part !== "object") {
|
||||
return `${label} must include price, salary, and totalPayment.`;
|
||||
if (!part || typeof part !== "object" || part.partId == null) {
|
||||
return `${label} must include a damaged-part partId.`;
|
||||
}
|
||||
|
||||
if (
|
||||
!Object.values(TypeOfDamage).includes(part.typeOfDamage as TypeOfDamage)
|
||||
) {
|
||||
return `${label} requires typeOfDamage to be either '${TypeOfDamage.Repair}' or '${TypeOfDamage.Change}'.`;
|
||||
}
|
||||
|
||||
if (
|
||||
!part.daghi ||
|
||||
!Object.values(DaghiOption).includes(part.daghi.option as DaghiOption)
|
||||
) {
|
||||
return `${label} requires a valid daghi option.`;
|
||||
}
|
||||
|
||||
const price = parseMoneyAmountToman(part.price);
|
||||
const salary = parseMoneyAmountToman(part.salary);
|
||||
const totalPayment = parseMoneyAmountToman(part.totalPayment);
|
||||
const daghiPrice = parseMoneyAmountToman(part.daghi.price);
|
||||
const hasPrice =
|
||||
part.price != null &&
|
||||
(typeof part.price !== "string" || part.price.trim() !== "");
|
||||
|
||||
const splitAmountIsValid = (amount: number | null) =>
|
||||
amount !== null &&
|
||||
@@ -43,15 +64,28 @@ export function getExpertReplyPricingValidationError(
|
||||
amount <= REPAIR_LINE_AMOUNT_TOMAN.MAX));
|
||||
const totalIsValid =
|
||||
totalPayment !== null &&
|
||||
totalPayment >= REPAIR_LINE_AMOUNT_TOMAN.MIN &&
|
||||
totalPayment >= 0 &&
|
||||
totalPayment <= REPAIR_LINE_AMOUNT_TOMAN.MAX;
|
||||
const priceIsRequired = part.typeOfDamage === TypeOfDamage.Repair;
|
||||
const priceIsValid =
|
||||
!hasPrice || (price !== null && splitAmountIsValid(price));
|
||||
|
||||
if (
|
||||
!splitAmountIsValid(price) ||
|
||||
!splitAmountIsValid(salary) ||
|
||||
!totalIsValid
|
||||
!totalIsValid ||
|
||||
(priceIsRequired && !splitAmountIsValid(price)) ||
|
||||
!priceIsValid
|
||||
) {
|
||||
return `${label} requires valid price, salary, and totalPayment. Price and salary may be 0; totalPayment must be between ${REPAIR_LINE_AMOUNT_TOMAN.MIN.toLocaleString("en-US")} and ${REPAIR_LINE_AMOUNT_TOMAN.MAX.toLocaleString("en-US")} Toman.`;
|
||||
return `${label} requires valid salary and totalPayment; price is also required for '${TypeOfDamage.Repair}' damage. Price and salary may be 0; totalPayment may be 0.`;
|
||||
}
|
||||
|
||||
if (
|
||||
part.daghi.option === DaghiOption.RECYCLED_PARTS_VALUE &&
|
||||
(daghiPrice === null ||
|
||||
daghiPrice < REPAIR_LINE_AMOUNT_TOMAN.MIN ||
|
||||
daghiPrice > REPAIR_LINE_AMOUNT_TOMAN.MAX)
|
||||
) {
|
||||
return `${label} requires a valid daghi price when option is '${DaghiOption.RECYCLED_PARTS_VALUE}'.`;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user