fix(claim-pricing): keep invoice and repair states consistent

This commit is contained in:
SepehrYahyaee
2026-11-23 12:03:02 +03:30
parent c74b29337e
commit 036d218ebd
7 changed files with 160 additions and 43 deletions
+4 -4
View File
@@ -50,7 +50,7 @@ export class PartPricingV2Dto {
@ApiProperty({
enum: TypeOfDamage,
description: "'change' requires price; 'repair' may omit it.",
description: "Replacement requires a part price unless factorNeeded=true. Repairs cannot request a part invoice.",
})
@IsEnum(TypeOfDamage)
typeOfDamage: TypeOfDamage;
@@ -58,11 +58,11 @@ export class PartPricingV2Dto {
@ApiPropertyOptional({
example: "5000000",
description:
"Required for change lines; omitted for repair lines. Every supplied amount must be between 1,000,000 and 100,000,000,000 Rial.",
"Required for priced replacement lines; omit for repair lines and replacement lines awaiting an invoice (factorNeeded=true). Every supplied amount must be between 1,000,000 and 100,000,000,000 Rial.",
})
@ValidateIf(
(part: PartPricingV2Dto) =>
part.typeOfDamage === TypeOfDamage.Change ||
(part.typeOfDamage === TypeOfDamage.Change && part.factorNeeded !== true) ||
(part.price != null &&
(typeof part.price !== 'string' || part.price.trim() !== '')),
)
@@ -106,7 +106,7 @@ export class PartPricingV2Dto {
@ApiProperty({
example: false,
description:
"If true, the owner must upload a repair factor image for this line; expert confirms cost later (validate-factors). Priced lines omit this or set false.",
"Replacement lines only: if true, the owner uploads the part invoice and the expert confirms its cost later (validate-factors). Omit price in this mode. Repair lines must set false.",
})
@IsBoolean()
factorNeeded: boolean;
@@ -84,6 +84,24 @@ const validV2Reply = {
};
describe("ExpertClaimService expert-reply pricing", () => {
it("saves a replacement invoice without a part price and enters the invoice upload workflow", async () => {
const { service, findByIdAndUpdate } = setupSuccessfulV2Submit(BlameRequestType.THIRD_PARTY);
const response = await service.submitExpertReplyV2("v2-claim", { parts: [{ partId: 1, typeOfDamage: TypeOfDamage.Change, salary: "1000000", totalPayment: "1000000", factorNeeded: true, daghi: { option: DaghiOption.NO_VALUE } }] }, { sub: V2_EXPERT_ID, role: RoleEnum.FIELD_EXPERT });
expect(response).toMatchObject({ factorNeeded: true, allPartsFactorNeeded: true, currentStep: "OWNER_UPLOAD_FACTOR_DOCUMENTS" });
const saved = findByIdAndUpdate.mock.calls[0][1]["evaluation.damageExpertReply"].parts[0];
expect(saved.factorNeeded).toBe(true);
expect(saved.price).toBeUndefined();
});
it("a failed invoice/repair submission cannot persist flags or poison a subsequent priced repair", async () => {
const { service, findByIdAndUpdate } = setupSuccessfulV2Submit(BlameRequestType.THIRD_PARTY);
const actor = { sub: V2_EXPERT_ID, role: RoleEnum.FIELD_EXPERT };
await expect(service.submitExpertReplyV2("v2-claim", { parts: [{ ...validV2Reply.parts[0], factorNeeded: true }] }, actor)).rejects.toMatchObject({ response: { field: "parts[0].factorNeeded" } });
expect(findByIdAndUpdate).not.toHaveBeenCalled();
const response = await service.submitExpertReplyV2("v2-claim", validV2Reply, actor);
expect(response.factorNeeded).toBe(false);
expect(findByIdAndUpdate.mock.calls[0][1]["evaluation.damageExpertReply"].parts[0].factorNeeded).toBe(false);
});
it("requires the current car price for a car-body expert reply", async () => {
const { service, findByIdAndUpdate } = setupSuccessfulV2Submit(
BlameRequestType.CAR_BODY,
@@ -3,6 +3,41 @@ import { TypeOfDamage } from "src/Types&Enums/claim-request-management/type-of-d
import { submitExpertReplyValidationPipe } from "./submit-expert-reply-validation.pipe";
describe("submitExpertReplyValidationPipe", () => {
const replacementInvoice = {
partId: 201,
typeOfDamage: TypeOfDamage.Change,
salary: "1000000",
totalPayment: "1000000",
factorNeeded: true,
daghi: { option: "فاقد ارزش" },
};
it("accepts replacement invoices without a numeric part quote", async () => {
const result = await submitExpertReplyValidationPipe.transform(
{ parts: [replacementInvoice] },
{ type: "body", metatype: SubmitExpertReplyV2Dto },
);
expect(result.parts[0].factorNeeded).toBe(true);
expect(result.parts[0].price).toBeUndefined();
});
it.each([{ factorNeeded: false }, { price: "factor" }, { price: "فاکتور" }])(
"continues rejecting absent priced quotes and nonnumeric price markers: %j",
async (invalid) => {
await expect(
submitExpertReplyValidationPipe.transform(
{ parts: [{ ...replacementInvoice, ...invalid }] },
{ type: "body", metatype: SubmitExpertReplyV2Dto },
),
).rejects.toMatchObject({
response: {
validationErrors: expect.arrayContaining([
expect.objectContaining({ field: "parts[0].price" }),
]),
},
});
},
);
it("converts DTO validation failures into Persian, field-addressable errors", async () => {
await expect(
submitExpertReplyValidationPipe.transform(
@@ -61,7 +96,7 @@ describe("submitExpertReplyValidationPipe", () => {
validationErrors: expect.arrayContaining([
{
field: "parts[0].salary",
message: "باید مبلغ صحیح و غیرمنفی به تومان باشد.",
message: "باید مبلغ صحیح و غیرمنفی به ریال باشد.",
},
]),
},
@@ -18,7 +18,7 @@ const constraintMessages: Record<string, string> = {
isInt: "باید یک عدد صحیح باشد.",
isEnum: "مقدار واردشده معتبر نیست.",
nestedValidation: "اطلاعات این بخش معتبر نیست.",
isMoneyAmountString: "باید مبلغ صحیح و غیرمنفی به تومان باشد.",
isMoneyAmountString: "باید مبلغ صحیح و غیرمنفی به ریال باشد.",
};
function flattenValidationErrors(
@@ -31,11 +31,16 @@ function flattenValidationErrors(
? `${parentPath}[${error.property}]`
: `${parentPath}.${error.property}`
: error.property;
const ownErrors = Object.keys(error.constraints ?? {}).map((constraint) => ({
field,
message: constraintMessages[constraint] ?? "مقدار واردشده معتبر نیست.",
}));
return [...ownErrors, ...flattenValidationErrors(error.children ?? [], field)];
const ownErrors = Object.keys(error.constraints ?? {}).map(
(constraint) => ({
field,
message: constraintMessages[constraint] ?? "مقدار واردشده معتبر نیست.",
}),
);
return [
...ownErrors,
...flattenValidationErrors(error.children ?? [], field),
];
});
}