forked from Yara724/api
Merge pull request 'fix(validation): normalize Persian accident condition labels' (#412) from s.yahyaee/yara724-api:main into main
Reviewed-on: Yara724/api#412
This commit is contained in:
@@ -0,0 +1,93 @@
|
|||||||
|
import { BadRequestException } from "@nestjs/common";
|
||||||
|
import {
|
||||||
|
LightCondition,
|
||||||
|
RoadCondition,
|
||||||
|
WeatherCondition,
|
||||||
|
} from "src/Types&Enums/blame-request-management/accident-conditions.enum";
|
||||||
|
import { normalizeAccidentConditions } from "./accident-conditions";
|
||||||
|
|
||||||
|
describe("accident condition labels from both frontends", () => {
|
||||||
|
const uiConditions = ["صاف", "بارانی", "برفی", "مه آلود"].flatMap(
|
||||||
|
(weatherCondition) =>
|
||||||
|
["گِلی", "یخ زده", "مرطوب", "خشک"].flatMap((roadCondition) =>
|
||||||
|
["روز", "شب", "کم نور"].map((lightCondition) => ({
|
||||||
|
weatherCondition,
|
||||||
|
roadCondition,
|
||||||
|
lightCondition,
|
||||||
|
})),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
it.each(uiConditions)(
|
||||||
|
"accepts $weatherCondition / $roadCondition / $lightCondition",
|
||||||
|
(body) => {
|
||||||
|
const normalized = normalizeAccidentConditions(body);
|
||||||
|
expect(Object.values(WeatherCondition)).toContain(
|
||||||
|
normalized.weatherCondition,
|
||||||
|
);
|
||||||
|
expect(Object.values(RoadCondition)).toContain(normalized.roadCondition);
|
||||||
|
expect(Object.values(LightCondition)).toContain(
|
||||||
|
normalized.lightCondition,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
it("accepts the reported production condition payload and preserves other fields", () => {
|
||||||
|
const body = {
|
||||||
|
desc: "شرح حادثه تستی",
|
||||||
|
accidentDate: "2026-11-22",
|
||||||
|
accidentTime: "15:15",
|
||||||
|
weatherCondition: "مه آلود",
|
||||||
|
roadCondition: "گِلی",
|
||||||
|
lightCondition: "روز",
|
||||||
|
};
|
||||||
|
expect({ ...body, ...normalizeAccidentConditions(body) }).toEqual({
|
||||||
|
...body,
|
||||||
|
roadCondition: RoadCondition.MUDDY,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
["weatherCondition", "مهآلود", WeatherCondition.FOGGY],
|
||||||
|
["weatherCondition", "باراني", WeatherCondition.RAINY],
|
||||||
|
["weatherCondition", "بارونی", WeatherCondition.RAINY],
|
||||||
|
["roadCondition", " گِلي ", RoadCondition.MUDDY],
|
||||||
|
["roadCondition", "یخزده", RoadCondition.ICY],
|
||||||
|
["lightCondition", "كمنور", LightCondition.LOW_LIGHT],
|
||||||
|
])("normalizes %s spelling %s", (field, value, expected) => {
|
||||||
|
expect(normalizeAccidentConditions({ [field]: value })).toEqual({
|
||||||
|
[field]: expected,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps omitted optional conditions omitted", () => {
|
||||||
|
expect(normalizeAccidentConditions({})).toEqual({});
|
||||||
|
expect(normalizeAccidentConditions({ weatherCondition: null })).toEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each(["weatherCondition", "roadCondition", "lightCondition"])(
|
||||||
|
"still rejects invalid %s without echoing user input",
|
||||||
|
(field) => {
|
||||||
|
for (const value of [
|
||||||
|
"invalid",
|
||||||
|
"",
|
||||||
|
" ",
|
||||||
|
"روز ناشناخته",
|
||||||
|
12,
|
||||||
|
[],
|
||||||
|
{},
|
||||||
|
true,
|
||||||
|
]) {
|
||||||
|
expect(() => normalizeAccidentConditions({ [field]: value })).toThrow(
|
||||||
|
BadRequestException,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
it("does not accept a label from a different condition field", () => {
|
||||||
|
expect(() =>
|
||||||
|
normalizeAccidentConditions({ weatherCondition: "روز" }),
|
||||||
|
).toThrow("شرایط آبوهوای حادثه معتبر نیست");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import { BadRequestException } from "@nestjs/common";
|
||||||
|
import {
|
||||||
|
LightCondition,
|
||||||
|
RoadCondition,
|
||||||
|
WeatherCondition,
|
||||||
|
} from "src/Types&Enums/blame-request-management/accident-conditions.enum";
|
||||||
|
|
||||||
|
type AccidentConditions = {
|
||||||
|
weatherCondition?: unknown;
|
||||||
|
roadCondition?: unknown;
|
||||||
|
lightCondition?: unknown;
|
||||||
|
};
|
||||||
|
|
||||||
|
function normalizeLabel(value: string): string {
|
||||||
|
return value
|
||||||
|
.normalize("NFC")
|
||||||
|
.replace(/[\u064B-\u065F\u0670]/g, "")
|
||||||
|
.replace(/ي/g, "ی")
|
||||||
|
.replace(/ك/g, "ک")
|
||||||
|
.replace(/\u200c/g, " ")
|
||||||
|
.replace(/\s+/g, " ")
|
||||||
|
.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Accept existing UI spellings, while keeping stored enum values stable. */
|
||||||
|
export function normalizeAccidentConditions(body: AccidentConditions) {
|
||||||
|
const result: Partial<Record<keyof AccidentConditions, string>> = {};
|
||||||
|
const fields = [
|
||||||
|
["weatherCondition", WeatherCondition, "شرایط آبوهوای حادثه"],
|
||||||
|
["roadCondition", RoadCondition, "وضعیت راه محل حادثه"],
|
||||||
|
["lightCondition", LightCondition, "شرایط روشنایی حادثه"],
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
for (const [field, choices, label] of fields) {
|
||||||
|
const value = body[field];
|
||||||
|
if (value == null) continue;
|
||||||
|
let normalized = typeof value === "string" ? normalizeLabel(value) : "";
|
||||||
|
// Both frontends use the formal spelling; the historical enum is colloquial.
|
||||||
|
if (field === "weatherCondition" && normalized === "بارانی") {
|
||||||
|
normalized = WeatherCondition.RAINY;
|
||||||
|
}
|
||||||
|
const canonical = Object.values(choices).find(
|
||||||
|
(choice) => normalizeLabel(choice) === normalized,
|
||||||
|
);
|
||||||
|
if (!canonical) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
`${label} معتبر نیست. یکی از گزینههای فرم را انتخاب کنید.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
result[field] = canonical;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
@@ -192,6 +192,55 @@ describe("V6 call-center workflow", () => {
|
|||||||
else process.env.URL = originalUrl;
|
else process.env.URL = originalUrl;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it.each([false, true].flatMap((callCenterInitiated) =>
|
||||||
|
["بارانی", "مه آلود", "مهآلود"].map((weatherCondition) => ({
|
||||||
|
callCenterInitiated, weatherCondition,
|
||||||
|
})),
|
||||||
|
))(
|
||||||
|
"car-body description advances with UI conditions (v6=$callCenterInitiated, $weatherCondition)",
|
||||||
|
async ({ callCenterInitiated, weatherCondition }) => {
|
||||||
|
const { service, req, first, id } = setup();
|
||||||
|
req.callCenterInitiated = callCenterInitiated;
|
||||||
|
req.parties[0].person.userId = new Types.ObjectId(first.sub);
|
||||||
|
req.workflow.currentStep = Step.FIRST_DESCRIPTION;
|
||||||
|
await service.addDescriptionV2(id, {
|
||||||
|
desc: "شرح حادثه برای بررسی کارشناس ".repeat(6),
|
||||||
|
accidentDate: "2026-11-22",
|
||||||
|
accidentTime: "15:15",
|
||||||
|
weatherCondition,
|
||||||
|
roadCondition: "گِلی",
|
||||||
|
lightCondition: "روز",
|
||||||
|
} as any, first);
|
||||||
|
expect(req.parties[0].statement).toMatchObject({
|
||||||
|
weatherCondition: weatherCondition === "بارانی" ? "بارونی" : "مه آلود",
|
||||||
|
roadCondition: "گلی",
|
||||||
|
lightCondition: "روز",
|
||||||
|
});
|
||||||
|
expect(req.workflow.currentStep).toBe(Step.WAITING_FOR_SIGNATURES);
|
||||||
|
expect(req.save).toHaveBeenCalled();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
it.each(["weatherCondition", "roadCondition", "lightCondition"])(
|
||||||
|
"invalid %s cannot advance the description step",
|
||||||
|
async (field) => {
|
||||||
|
const { service, req, first, id } = setup();
|
||||||
|
req.parties[0].person.userId = new Types.ObjectId(first.sub);
|
||||||
|
req.workflow.currentStep = Step.FIRST_DESCRIPTION;
|
||||||
|
await expect(service.addDescriptionV2(id, {
|
||||||
|
desc: "شرح حادثه برای بررسی کارشناس ".repeat(6),
|
||||||
|
accidentDate: "2026-11-22",
|
||||||
|
accidentTime: "15:15",
|
||||||
|
weatherCondition: "بارانی",
|
||||||
|
roadCondition: "گِلی",
|
||||||
|
lightCondition: "روز",
|
||||||
|
[field]: "گزینه نامعتبر",
|
||||||
|
} as any, first)).rejects.toThrow("معتبر نیست");
|
||||||
|
expect(req.workflow.currentStep).toBe(Step.FIRST_DESCRIPTION);
|
||||||
|
expect(req.save).not.toHaveBeenCalled();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
it.each(
|
it.each(
|
||||||
[Kind.CAR_BODY, Kind.THIRD_PARTY].flatMap((type) =>
|
[Kind.CAR_BODY, Kind.THIRD_PARTY].flatMap((type) =>
|
||||||
["plate", "vin"].map((mode) => [type, mode]),
|
["plate", "vin"].map((mode) => [type, mode]),
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { WeatherCondition, RoadCondition, LightCondition } from "src/Types&Enums/blame-request-management/accident-conditions.enum";
|
import { normalizeAccidentConditions } from "./accident-conditions";
|
||||||
import { UserClaimBranchService, userClaimBranchContext, usesUserClaimBranch } from "src/client/user-claim-branch.service";
|
import { UserClaimBranchService, userClaimBranchContext, usesUserClaimBranch } from "src/client/user-claim-branch.service";
|
||||||
import { normalizeAccidentLocation } from "./accident-location";
|
import { normalizeAccidentLocation } from "./accident-location";
|
||||||
import {
|
import {
|
||||||
@@ -215,12 +215,13 @@ export class RequestManagementService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Keep eligibility rejections distinct from an unavailable CAR_BODY provider. */
|
/** Normalize existing UI labels before validation and persistence in every flow. */
|
||||||
private assertAccidentConditions(body: {weatherCondition?: unknown; roadCondition?: unknown; lightCondition?: unknown}): void {
|
private assertAccidentConditions(body: {
|
||||||
for (const [value, choices] of [[body.weatherCondition, WeatherCondition], [body.roadCondition, RoadCondition], [body.lightCondition, LightCondition]] as const) {
|
weatherCondition?: unknown;
|
||||||
if (value != null && !Object.values(choices).includes(value as never))
|
roadCondition?: unknown;
|
||||||
throw new BadRequestException("شرایط آبوهوا، راه یا روشنایی حادثه معتبر نیست. گزینههای فرم را دوباره انتخاب کنید.");
|
lightCondition?: unknown;
|
||||||
}
|
}): void {
|
||||||
|
Object.assign(body, normalizeAccidentConditions(body));
|
||||||
}
|
}
|
||||||
|
|
||||||
private assertCarBodyDescriptionMinimum(
|
private assertCarBodyDescriptionMinimum(
|
||||||
|
|||||||
Reference in New Issue
Block a user