forked from Yara724/api
feat(claims): require optional expert signatures and enrich PDF reports
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
import { buildFileLink } from "src/helpers/urlCreator";
|
||||
import { personDisplayName } from "src/helpers/person-display-name";
|
||||
import type { ReportAttachment } from "./case-expert-report.types";
|
||||
|
||||
const documentLabels: Record<string, string> = {
|
||||
accident_sketch: "کروکی",
|
||||
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: "آسیب خودروی مقصر",
|
||||
drivingLicense: "گواهینامه",
|
||||
nationalCertificate: "مدرک هویتی",
|
||||
carCertificate: "سند خودرو",
|
||||
carGreenCard: "برگه سبز خودرو",
|
||||
};
|
||||
const angleLabels: Record<string, string> = {
|
||||
front: "نمای جلو",
|
||||
back: "نمای عقب",
|
||||
left: "نمای چپ",
|
||||
right: "نمای راست",
|
||||
};
|
||||
|
||||
export function buildReportAttachments(
|
||||
claim: any,
|
||||
blame: any,
|
||||
blameDocuments: { path: string; documentType: string }[],
|
||||
invoices: { path: string }[],
|
||||
): ReportAttachment[] {
|
||||
const attachments = new Map<string, ReportAttachment>();
|
||||
function add(label: string, url: unknown) {
|
||||
if (typeof url !== "string" || !/(?:^|\/)files\/[^?#]+/.test(url)) return;
|
||||
const path =
|
||||
url
|
||||
.split(/[?#]/)[0]
|
||||
.split(/(?:^|\/)files\//)
|
||||
.at(-1) ?? "";
|
||||
if (!attachments.has(path)) attachments.set(path, { label, url });
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(claim?.carAngles ?? {})) {
|
||||
const angle = value as { url?: string };
|
||||
add(angleLabels[key] ?? "نمای خودرو", angle.url);
|
||||
}
|
||||
for (const part of claim?.damagedParts ?? []) {
|
||||
const name =
|
||||
typeof part.label_fa === "string" && /[\u0600-\u06ff]/.test(part.label_fa)
|
||||
? part.label_fa
|
||||
: "تصویر قطعه آسیبدیده";
|
||||
add(name, part.url);
|
||||
}
|
||||
for (const [key, value] of Object.entries(claim?.requiredDocuments ?? {})) {
|
||||
add(
|
||||
documentLabels[key] ?? "مدرک خسارت",
|
||||
(value as { fileUrl?: string }).fileUrl,
|
||||
);
|
||||
}
|
||||
for (const party of blame?.parties ?? []) {
|
||||
for (const image of party.evidence?.images ?? []) {
|
||||
if (typeof image === "string" && /(?:^|\/)files\//.test(image)) {
|
||||
add("تصویر حادثه", buildFileLink(image));
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const document of blameDocuments) {
|
||||
if (document.path)
|
||||
add(
|
||||
documentLabels[document.documentType] ?? "مدرک حادثه",
|
||||
buildFileLink(document.path),
|
||||
);
|
||||
}
|
||||
|
||||
const evaluation = claim?.evaluation;
|
||||
for (const invoice of invoices) {
|
||||
if (invoice.path) add("فاکتور قطعه", buildFileLink(invoice.path));
|
||||
}
|
||||
for (const invoice of evaluation?.objection?.invoices ?? [])
|
||||
add("فاکتور اعتراض", invoice.url);
|
||||
return [...attachments.values()];
|
||||
}
|
||||
|
||||
export function buildReportSignatures(
|
||||
blame: any,
|
||||
claim: any,
|
||||
): ReportAttachment[] {
|
||||
const parties: any[] = blame?.parties ?? claim?.snapshot?.parties ?? [];
|
||||
const signatures: ReportAttachment[] = [];
|
||||
for (const [index, party] of parties.entries()) {
|
||||
const signature = party.confirmation?.signature;
|
||||
const url =
|
||||
signature?.link ||
|
||||
(signature?.fileUrl ? buildFileLink(signature.fileUrl) : undefined);
|
||||
if (!url) continue;
|
||||
const isSecond = party.role ? party.role === "SECOND" : index === 1;
|
||||
const role = isSecond ? "طرف دوم" : "طرف اول";
|
||||
const name = personDisplayName(party.person);
|
||||
signatures.push({
|
||||
label: name ? `امضای ${role} · ${name}` : `امضای ${role}`,
|
||||
url,
|
||||
});
|
||||
}
|
||||
const ownerSign =
|
||||
claim?.evaluation?.ownerInsurerApproval?.signLink ??
|
||||
claim?.evaluation?.ownerPricedPartsApproval?.signLink;
|
||||
if (ownerSign)
|
||||
signatures.push({
|
||||
label: "امضای زیاندیده برای تأیید خسارت",
|
||||
url: ownerSign,
|
||||
});
|
||||
const expert = claim?.fanavaranExpertSignature;
|
||||
if (expert?.image) {
|
||||
signatures.push({
|
||||
label:
|
||||
expert.name && expert.name !== "کارشناس پرونده"
|
||||
? `امضای کارشناس · ${expert.name}`
|
||||
: "امضای کارشناس",
|
||||
image: expert.image,
|
||||
});
|
||||
}
|
||||
return signatures;
|
||||
}
|
||||
Reference in New Issue
Block a user