forked from Yara724/api
Added complete validation for expert claim submit state
This commit is contained in:
@@ -1,19 +1,92 @@
|
||||
import { getExpertReplyPricingValidationError } from "./expert-reply-pricing";
|
||||
import { TypeOfDamage } from "src/Types&Enums/claim-request-management/type-of-damage.enum";
|
||||
import { DaghiOption } from "src/Types&Enums/claim-request-management/daghi-option.enum";
|
||||
|
||||
describe("getExpertReplyPricingValidationError", () => {
|
||||
it("rejects a blank pricing line", () => {
|
||||
expect(
|
||||
getExpertReplyPricingValidationError([
|
||||
{ partId: 201, price: "", salary: "", totalPayment: "" },
|
||||
{
|
||||
partId: 201,
|
||||
typeOfDamage: TypeOfDamage.Repair,
|
||||
price: "",
|
||||
salary: "",
|
||||
totalPayment: "",
|
||||
daghi: { option: DaghiOption.NO_VALUE },
|
||||
},
|
||||
]),
|
||||
).toMatch(/requires valid price, salary, and totalPayment/);
|
||||
).toMatch(/requires valid salary and totalPayment/);
|
||||
});
|
||||
|
||||
it("accepts a fully priced line with a zero-valued split", () => {
|
||||
expect(
|
||||
getExpertReplyPricingValidationError([
|
||||
{ partId: 201, price: "0", salary: "10000", totalPayment: "10000" },
|
||||
{
|
||||
partId: 201,
|
||||
typeOfDamage: TypeOfDamage.Repair,
|
||||
price: "0",
|
||||
salary: "10000",
|
||||
totalPayment: "10000",
|
||||
daghi: { option: DaghiOption.NO_VALUE },
|
||||
},
|
||||
]),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects a repair line without a price", () => {
|
||||
expect(
|
||||
getExpertReplyPricingValidationError([
|
||||
{
|
||||
partId: 201,
|
||||
typeOfDamage: TypeOfDamage.Repair,
|
||||
salary: "0",
|
||||
totalPayment: "0",
|
||||
daghi: { option: DaghiOption.NO_VALUE },
|
||||
},
|
||||
]),
|
||||
).toMatch(/price is also required/);
|
||||
});
|
||||
|
||||
it("rejects recycled-value daghi without its price", () => {
|
||||
expect(
|
||||
getExpertReplyPricingValidationError([
|
||||
{
|
||||
partId: 201,
|
||||
typeOfDamage: TypeOfDamage.Change,
|
||||
salary: "0",
|
||||
totalPayment: "0",
|
||||
daghi: { option: DaghiOption.RECYCLED_PARTS_VALUE },
|
||||
},
|
||||
]),
|
||||
).toMatch(/requires a valid daghi price/);
|
||||
});
|
||||
|
||||
it("accepts a replacement line without a part price", () => {
|
||||
expect(
|
||||
getExpertReplyPricingValidationError([
|
||||
{
|
||||
partId: 201,
|
||||
typeOfDamage: TypeOfDamage.Change,
|
||||
salary: "0",
|
||||
totalPayment: "0",
|
||||
daghi: { option: DaghiOption.NO_VALUE },
|
||||
},
|
||||
]),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects an invalid price when a replacement line supplies one", () => {
|
||||
expect(
|
||||
getExpertReplyPricingValidationError([
|
||||
{
|
||||
partId: 201,
|
||||
typeOfDamage: TypeOfDamage.Change,
|
||||
price: "not-a-number",
|
||||
salary: "0",
|
||||
totalPayment: "0",
|
||||
daghi: { option: DaghiOption.NO_VALUE },
|
||||
},
|
||||
]),
|
||||
).toMatch(/requires valid salary and totalPayment/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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