Merge pull request 'Preserved FE contact' (#306) from s.yahyaee/yara724-api:main into main

Reviewed-on: Yara724/api#306
This commit is contained in:
2026-09-12 12:20:26 +03:30
2 changed files with 13 additions and 191 deletions

View File

@@ -43,18 +43,18 @@ describe("insurer timeline Persian labels", () => {
);
});
it("localizes human-readable assignment metadata", () => {
it("preserves human-readable assignment metadata exactly", () => {
expect(
localizeTimelineMetadata({
type: "CLAIM_ASSIGNED",
metadata: { note: "Expert assigned via review assign endpoint" },
}),
).toEqual({
note: "پرونده از طریق فرایند بررسی به کارشناس تخصیص داده شد.",
note: "Expert assigned via review assign endpoint",
});
});
it("localizes Fanavaran errors while retaining machine-facing metadata", () => {
it("preserves Fanavaran metadata keys and values exactly", () => {
expect(
localizeTimelineMetadata({
type: "FANAVARAN_EXPERTISE_AUTO_SUBMIT_FAILED",
@@ -65,12 +65,11 @@ describe("insurer timeline Persian labels", () => {
}),
).toEqual({
clientKey: "parsian",
clientFaLabel: "پارسیان",
error: "اطلاعات کارشناسی برای ارسال به فناوران آماده نیست.",
error: "Fanavaran expertise payload is not ready.",
});
});
it("adds Persian labels for displayable metadata enums", () => {
it("does not add fields to existing metadata", () => {
expect(
localizeTimelineMetadata({
type: "STEP_COMPLETED",
@@ -84,15 +83,10 @@ describe("insurer timeline Persian labels", () => {
}),
).toEqual({
role: "FIRST",
roleFaLabel: "طرف اول",
stepKey: "USER_SUBMISSION_COMPLETE",
stepFaLabel: "ارسال اطلاعات توسط کاربر",
carType: "sedan",
carTypeFaLabel: "سواری",
captureType: "angle",
captureTypeFaLabel: "زاویه خودرو",
captureKey: "front",
captureKeyFaLabel: "نمای جلو",
});
});
});

View File

@@ -187,79 +187,6 @@ export const STEP_KEY_FA_LABELS: Record<string, string> = {
CLAIM_COMPLETED: "پرونده خسارت تکمیل شد",
};
const TIMELINE_STEP_FA_LABELS: Record<string, string> = {
...STEP_KEY_FA_LABELS,
FIRST_VIDEO: "ویدیوی طرف اول",
SECOND_VIDEO: "ویدیوی طرف دوم",
};
const ROLE_FA_LABELS: Record<string, string> = {
FIRST: "طرف اول",
SECOND: "طرف دوم",
FIRST_PARTY: "طرف اول",
SECOND_PARTY: "طرف دوم",
};
const VEHICLE_TYPE_FA_LABELS: Record<string, string> = {
sedan: "سواری",
suv: "شاسی‌بلند",
hatchback: "هاچ‌بک",
pickup: "وانت",
van: "ون",
};
const CAPTURE_TYPE_FA_LABELS: Record<string, string> = {
angle: "زاویه خودرو",
part: "قطعه آسیب‌دیده",
};
const CAPTURE_KEY_FA_LABELS: Record<string, string> = {
front: "نمای جلو",
back: "نمای عقب",
left: "نمای چپ",
right: "نمای راست",
top: "نمای بالا",
};
const DOCUMENT_KEY_FA_LABELS: Record<string, string> = {
car_green_card: "کارت سبز خودرو",
car_certificate: "سند خودرو",
national_card: "کارت ملی",
damaged_driving_license_front: "روی گواهینامه طرف آسیب‌دیده",
damaged_driving_license_back: "پشت گواهینامه طرف آسیب‌دیده",
damaged_chassis_number: "شماره شاسی خودروی آسیب‌دیده",
damaged_engine_photo: "تصویر موتور خودروی آسیب‌دیده",
damaged_car_card_front: "روی کارت خودروی آسیب‌دیده",
damaged_car_card_back: "پشت کارت خودروی آسیب‌دیده",
damaged_metal_plate: "پلاک خودروی آسیب‌دیده",
guilty_driving_license_front: "روی گواهینامه طرف مقصر",
guilty_driving_license_back: "پشت گواهینامه طرف مقصر",
guilty_car_card_front: "روی کارت خودروی مقصر",
guilty_car_card_back: "پشت کارت خودروی مقصر",
guilty_metal_plate: "پلاک خودروی مقصر",
guilty_damage_area: "ناحیه آسیب‌دیده خودروی مقصر",
};
const CLIENT_FA_LABELS: Record<string, string> = {
parsian: "پارسیان",
};
const REPLY_FIELD_FA_LABELS: Record<string, string> = {
damageExpertReply: "پاسخ کارشناس خسارت",
damageExpertReplyFinal: "پاسخ نهایی کارشناس خسارت",
};
const TIMELINE_SYSTEM_TEXT_FA: Record<string, string> = {
"Expert assigned via review assign endpoint":
"پرونده از طریق فرایند بررسی به کارشناس تخصیص داده شد.",
"Expert assigned for factor validation review":
"پرونده برای بررسی و تأیید فاکتورها به کارشناس تخصیص داده شد.",
"Field expert portfolio lock (no status change)":
"پرونده برای بررسی در اختیار کارشناس میدانی قرار گرفت.",
"Fanavaran expertise payload is not ready.":
"اطلاعات کارشناسی برای ارسال به فناوران آماده نیست.",
};
const GENERIC_EVENT_FA_LABEL = "رویداد پرونده";
const GENERIC_EVENT_FA_DESCRIPTION = "رویدادی در روند پرونده ثبت شد.";
@@ -339,35 +266,9 @@ export function getEventFaDescription(event: {
return label ? `${label}.` : GENERIC_EVENT_FA_DESCRIPTION;
}
function containsLatinText(value: string): boolean {
return /[A-Za-z]/.test(value);
}
function localizeHumanReadableMetadataText(
value: unknown,
event: { type: string; metadata?: Record<string, unknown> },
field: "note" | "error" | "message" | "warning",
): unknown {
if (typeof value !== "string" || !containsLatinText(value)) return value;
const knownTranslation = TIMELINE_SYSTEM_TEXT_FA[value.trim()];
if (knownTranslation) return knownTranslation;
if (field === "error" || field === "warning") {
return event.type.endsWith("_FAILED")
? getEventFaDescription(event)
: "در اجرای این عملیات خطایی رخ داد.";
}
return field === "note"
? "یادداشت سیستمی این رویداد ثبت شد."
: getEventFaDescription(event);
}
/**
* Builds presentation-safe timeline metadata. Raw identifiers and enum values
* remain available for integrations; Persian companion labels are added for
* UI rendering, while persisted human-readable English text is replaced.
* Keeps the established metadata contract intact. Only a pre-existing
* description field is localized; no keys are added, removed, or renamed.
*/
export function localizeTimelineMetadata(event: {
type: string;
@@ -375,83 +276,10 @@ export function localizeTimelineMetadata(event: {
}): Record<string, unknown> | null {
if (!event.metadata) return null;
const metadata: Record<string, unknown> = { ...event.metadata };
if (Object.prototype.hasOwnProperty.call(metadata, "description")) {
metadata.description = getEventFaDescription(event);
}
for (const field of ["note", "error", "message", "warning"] as const) {
if (Object.prototype.hasOwnProperty.call(metadata, field)) {
metadata[field] = localizeHumanReadableMetadataText(
metadata[field],
event,
field,
);
}
}
if (Array.isArray(metadata.warnings)) {
metadata.warnings = metadata.warnings.map((warning) =>
localizeHumanReadableMetadataText(warning, event, "warning"),
);
}
const role = typeof metadata.role === "string" ? metadata.role : undefined;
if (role && ROLE_FA_LABELS[role]) {
metadata.roleFaLabel = ROLE_FA_LABELS[role];
}
const stepKey =
typeof metadata.stepKey === "string" ? metadata.stepKey : undefined;
if (stepKey && TIMELINE_STEP_FA_LABELS[stepKey]) {
metadata.stepFaLabel = TIMELINE_STEP_FA_LABELS[stepKey];
}
const carType =
typeof metadata.carType === "string" ? metadata.carType : undefined;
if (carType && VEHICLE_TYPE_FA_LABELS[carType]) {
metadata.carTypeFaLabel = VEHICLE_TYPE_FA_LABELS[carType];
}
const captureType =
typeof metadata.captureType === "string" ? metadata.captureType : undefined;
if (captureType && CAPTURE_TYPE_FA_LABELS[captureType]) {
metadata.captureTypeFaLabel = CAPTURE_TYPE_FA_LABELS[captureType];
}
const captureKey =
typeof metadata.captureKey === "string" ? metadata.captureKey : undefined;
if (captureKey && CAPTURE_KEY_FA_LABELS[captureKey]) {
metadata.captureKeyFaLabel = CAPTURE_KEY_FA_LABELS[captureKey];
}
const resolvedCarAngleKey =
typeof metadata.resolvedCarAngleKey === "string"
? metadata.resolvedCarAngleKey
: undefined;
if (resolvedCarAngleKey && CAPTURE_KEY_FA_LABELS[resolvedCarAngleKey]) {
metadata.resolvedCarAngleFaLabel =
CAPTURE_KEY_FA_LABELS[resolvedCarAngleKey];
}
const documentKey =
typeof metadata.documentKey === "string" ? metadata.documentKey : undefined;
if (documentKey && DOCUMENT_KEY_FA_LABELS[documentKey]) {
metadata.documentFaLabel = DOCUMENT_KEY_FA_LABELS[documentKey];
}
const clientKey =
typeof metadata.clientKey === "string" ? metadata.clientKey : undefined;
if (clientKey && CLIENT_FA_LABELS[clientKey]) {
metadata.clientFaLabel = CLIENT_FA_LABELS[clientKey];
}
const replyField =
typeof metadata.replyField === "string" ? metadata.replyField : undefined;
if (replyField && REPLY_FIELD_FA_LABELS[replyField]) {
metadata.replyFieldFaLabel = REPLY_FIELD_FA_LABELS[replyField];
}
return metadata;
return {
...event.metadata,
...(Object.prototype.hasOwnProperty.call(event.metadata, "description")
? { description: getEventFaDescription(event) }
: {}),
};
}