Enforce claim beneficiary access and retire unused legacy APIs

This commit is contained in:
SepehrYahyaee
2026-10-05 14:39:51 +03:30
parent 0735333467
commit a3f9aab8fb
104 changed files with 6864 additions and 13223 deletions
+61
View File
@@ -0,0 +1,61 @@
type ClaimParty = {
role?: string;
person?: {
userId?: unknown;
fullName?: string;
clientId?: unknown;
phoneNumber?: string;
};
confirmation?: { accepted?: boolean; signature?: { fileId?: unknown } };
};
type ClaimSource = {
type?: string;
status?: string;
parties?: ClaimParty[];
expert?: { decision?: { guiltyPartyId?: unknown } };
};
/** Resolve a specific beneficiary; an unknown guilty id never means everyone is eligible. */
export function resolveClaimBeneficiary(blame: ClaimSource): ClaimParty | null {
const parties = blame.parties ?? [];
const first = parties.find((party) => party.role === "FIRST");
if (blame.type === "CAR_BODY") return first?.person?.userId ? first : null;
if (blame.type !== "THIRD_PARTY" || parties.length !== 2) return null;
const second = parties.find((party) => party.role === "SECOND");
const firstId = first?.person?.userId;
const secondId = second?.person?.userId;
const guiltyId = blame.expert?.decision?.guiltyPartyId;
if (
!firstId ||
!secondId ||
!guiltyId ||
String(firstId) === String(secondId)
)
return null;
if (String(guiltyId) === String(firstId)) return second!;
if (String(guiltyId) === String(secondId)) return first!;
return null;
}
export function canUserCreateClaim(
blame: ClaimSource,
userId: string,
): boolean {
const beneficiary = resolveClaimBeneficiary(blame);
if (
!beneficiary ||
!userId ||
blame.status !== "COMPLETED" ||
String(beneficiary?.person?.userId ?? "") !== userId
)
return false;
return (
blame.type === "CAR_BODY" ||
(blame.parties ?? []).every(
(party) =>
party.confirmation?.accepted === true &&
!!party.confirmation.signature?.fileId,
)
);
}
+7 -2
View File
@@ -10,7 +10,11 @@ export type BlameUserNextAction =
export function resolveBlameUserNextAction(
status: string,
party: { confirmation?: { accepted?: boolean } } | undefined,
claimCreation: { hasClaim: boolean; shouldGuideToCreateClaim: boolean },
claimCreation: {
hasClaim: boolean;
shouldGuideToCreateClaim: boolean;
canContinueClaim?: boolean;
},
): BlameUserNextAction {
if (!party) return null;
if (status === "WAITING_FOR_SIGNATURES") {
@@ -20,7 +24,8 @@ export function resolveBlameUserNextAction(
}
if (status === "COMPLETED") {
if (claimCreation.shouldGuideToCreateClaim) return "CREATE_CLAIM";
if (claimCreation.hasClaim) return "CONTINUE_CLAIM";
if (claimCreation.hasClaim && claimCreation.canContinueClaim !== false)
return "CONTINUE_CLAIM";
return "VIEW_RESULT";
}
if (status === "STOPPED") return "VIEW_RESULT";
-166
View File
@@ -1,166 +0,0 @@
import { existsSync } from "node:fs";
import { join } from "node:path";
import type PDFDocumentType from "pdfkit";
import {
isMostlyAscii,
pdfKitRtlKeyValue,
pdfKitRtlParagraph,
} from "./persian-pdf-text";
// pdfkit publishes CJS (`module.exports = PDFDocument`) without a `.default` export.
// eslint-disable-next-line @typescript-eslint/no-require-imports
const PDFDocument = require("pdfkit") as typeof PDFDocumentType;
const PAGE_WIDTH = 595.28;
const MARGIN = 50;
const CONTENT_WIDTH = PAGE_WIDTH - MARGIN * 2;
const LABEL_COLUMN_WIDTH = CONTENT_WIDTH * 0.44;
const VALUE_COLUMN_WIDTH = CONTENT_WIDTH * 0.5;
const VALUE_COLUMN_X = MARGIN;
const COLON_X = MARGIN + VALUE_COLUMN_WIDTH + 4;
const LABEL_COLUMN_X = COLON_X + 10;
function resolveFontFile(variant: "regular" | "bold"): string {
const file =
variant === "bold" ? "Vazirmatn-Bold.ttf" : "Vazirmatn-Regular.ttf";
const candidates = [
join(process.cwd(), "assets", file),
join(process.cwd(), "assets", "fonts", file),
join(process.cwd(), "dist", "assets", file),
join(process.cwd(), "dist", "assets", "fonts", file),
];
for (const path of candidates) {
if (existsSync(path)) return path;
}
throw new Error(
`Persian PDF font not found (${file}). Place it under assets/ or assets/fonts/.`,
);
}
export type PersianPdfDocument = InstanceType<typeof PDFDocument> & {
addTitle: (text: string) => void;
addSection: (text: string) => void;
addKeyValue: (label: string, value?: string | number | null) => void;
addBlank: (lines?: number) => void;
};
/** Ensure at least `neededPts` of vertical space remain; add a page if not. */
function ensureSpace(
doc: InstanceType<typeof PDFDocument>,
neededPts: number,
): void {
if (doc.y + neededPts > doc.page.maxY()) {
doc.addPage();
}
}
function drawRtlLine(
doc: InstanceType<typeof PDFDocument>,
text: string,
fontSize: number,
bold: boolean,
): void {
const y = doc.y;
doc
.font(bold ? "FaBold" : "FaRegular")
.fontSize(fontSize)
.text(pdfKitRtlParagraph(text), MARGIN, y, {
width: CONTENT_WIDTH,
align: "right",
lineBreak: false,
});
doc.moveDown(fontSize > 12 ? 0.55 : 0.45);
}
function drawKeyValueRow(
doc: InstanceType<typeof PDFDocument>,
label: string,
value: string,
): void {
// Reserve one row height (font 10pt ≈ 14 pts with leading) before drawing.
ensureSpace(doc, 16);
const y = doc.y;
const { label: faLabel, value: faValue } = pdfKitRtlKeyValue(label, value);
if (isMostlyAscii(faValue)) {
doc.font("Helvetica").fontSize(10).text(faValue, VALUE_COLUMN_X, y, {
width: VALUE_COLUMN_WIDTH,
align: "left",
lineBreak: false,
});
} else {
doc.font("FaRegular").fontSize(10).text(faValue, VALUE_COLUMN_X, y, {
width: VALUE_COLUMN_WIDTH,
align: "left",
lineBreak: false,
});
}
doc.font("Helvetica").fontSize(10).text(":", COLON_X, y, {
lineBreak: false,
});
doc.font("FaRegular").fontSize(10).text(faLabel, LABEL_COLUMN_X, y, {
width: LABEL_COLUMN_WIDTH,
align: "right",
lineBreak: false,
});
doc.moveDown(0.5);
}
export function createPersianPdfDocument(): PersianPdfDocument {
const doc = new PDFDocument({
size: "A4",
margin: MARGIN,
autoFirstPage: true,
}) as PersianPdfDocument;
doc.registerFont("FaRegular", resolveFontFile("regular"));
doc.registerFont("FaBold", resolveFontFile("bold"));
doc.font("FaRegular");
doc.addTitle = (text: string) => {
doc.moveDown(0.2);
drawRtlLine(doc, text, 16, true);
doc.font("FaRegular").fontSize(10);
doc.moveDown(0.15);
};
doc.addSection = (text: string) => {
// Reserve space for the gap + section heading + at least one data row below it,
// so the heading is never stranded alone at the bottom of a page.
ensureSpace(doc, 60);
doc.moveDown(0.5);
drawRtlLine(doc, text, 13, true);
doc.font("FaRegular").fontSize(10);
doc.moveDown(0.1);
};
doc.addKeyValue = (label: string, value?: string | number | null) => {
const v =
value === undefined || value === null || value === ""
? "-"
: String(value);
drawKeyValueRow(doc, label, v);
};
doc.addBlank = (lines = 1) => {
doc.moveDown(lines * 0.35);
};
return doc;
}
export function persianPdfToBuffer(
doc: InstanceType<typeof PDFDocument>,
): Promise<Buffer> {
return new Promise((resolve, reject) => {
const chunks: Buffer[] = [];
doc.on("data", (chunk: Buffer) => chunks.push(chunk));
doc.on("end", () => resolve(Buffer.concat(chunks)));
doc.on("error", reject);
doc.end();
});
}
-40
View File
@@ -1,40 +0,0 @@
/** Strip invisible bidi / ZWNJ chars that render as tofu in embedded Arabic fonts. */
export function normalizePersianPdfText(text: string): string {
return text
.replace(/\u200c/g, " ")
.replace(/[\u200e\u200f\u202a-\u202e\u2066-\u2069\ufeff]/g, "")
.replace(/\u2014/g, "-")
.replace(/\s+/g, " ")
.trim();
}
/**
* pdfkit renders glyphs strictly left-to-right.
* fontkit already reshapes each word's glyphs into visual (LTR) order,
* but it does NOT reorder words — so a multi-word RTL string like
* "نام صاحب خودرو" has its words placed LTR on the page and reads
* backwards. Reversing the word sequence here makes pdfkit emit the
* words in the correct visual order for a right-to-left PDF reader.
*/
function reverseRtlWords(text: string): string {
if (!/[\u0600-\u06FF]/.test(text)) return text;
return text.split(" ").reverse().join(" ");
}
export function pdfKitRtlKeyValue(
label: string,
value: string,
): { label: string; value: string } {
return {
label: reverseRtlWords(normalizePersianPdfText(label)),
value: reverseRtlWords(normalizePersianPdfText(value)),
};
}
export function pdfKitRtlParagraph(text: string): string {
return reverseRtlWords(normalizePersianPdfText(text));
}
export function isMostlyAscii(text: string): boolean {
return text.length > 0 && !/[\u0600-\u06FF]/.test(text);
}
-166
View File
@@ -1,166 +0,0 @@
type PdfTextRun = {
pageIndex: number;
x: number;
y: number;
fontSize: number;
bold: boolean;
text: string;
};
/** Minimal PDF 1.4 writer (Helvetica) — no external dependencies. */
export class SimplePdfWriter {
private readonly pageWidth = 595.28;
private readonly pageHeight = 841.89;
private readonly marginX = 50;
private readonly marginTop = 50;
private readonly marginBottom = 50;
private runs: PdfTextRun[] = [];
private pageCount = 1;
private cursorY = this.pageHeight - this.marginTop;
private pageIndex = 0;
private ensureSpace(lineHeight: number): void {
if (this.cursorY - lineHeight < this.marginBottom) {
this.pageIndex += 1;
this.pageCount += 1;
this.cursorY = this.pageHeight - this.marginTop;
}
}
private addRun(text: string, fontSize: number, bold = false): void {
const lineHeight = fontSize + 4;
this.ensureSpace(lineHeight);
this.runs.push({
pageIndex: this.pageIndex,
x: this.marginX,
y: this.cursorY,
fontSize,
bold,
text,
});
this.cursorY -= lineHeight;
}
addTitle(text: string): void {
this.addRun(text, 16, true);
this.cursorY -= 4;
}
addSection(text: string): void {
this.cursorY -= 6;
this.addRun(text, 13, true);
this.cursorY -= 2;
}
addLine(text: string): void {
this.addRun(text, 10, false);
}
addKeyValue(label: string, value?: string | number | null): void {
const v =
value === undefined || value === null || value === ""
? "-"
: String(value);
this.addLine(`${label}: ${v}`);
}
addBlank(): void {
this.cursorY -= 8;
}
toBuffer(): Buffer {
const chunkById: string[] = [];
const offsets: number[] = [0];
let size = Buffer.byteLength("%PDF-1.4\n", "utf8");
const addObj = (content: string): number => {
const id = chunkById.length;
offsets.push(size);
const block = `${id} 0 obj\n${content}\nendobj\n`;
chunkById.push(block);
size += Buffer.byteLength(block, "utf8");
return id;
};
chunkById.push("%PDF-1.4\n");
const fontRegularId = addObj(
"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>",
);
const fontBoldId = addObj(
"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica-Bold /Encoding /WinAnsiEncoding >>",
);
const contentIds: number[] = [];
for (let p = 0; p < this.pageCount; p++) {
const pageRuns = this.runs.filter((r) => r.pageIndex === p);
const cmds: string[] = [];
for (const run of pageRuns) {
const font = run.bold ? "F2" : "F1";
cmds.push("BT");
cmds.push(`/${font} ${run.fontSize} Tf`);
cmds.push(`1 0 0 1 ${run.x.toFixed(2)} ${run.y.toFixed(2)} Tm`);
cmds.push(`${encodePdfText(run.text)} Tj`);
cmds.push("ET");
}
const stream = cmds.join("\n");
contentIds.push(
addObj(
`<< /Length ${Buffer.byteLength(stream, "utf8")} >>\nstream\n${stream}\nendstream`,
),
);
}
const pagesKidsToken = "__PAGES_KIDS__";
const pagesId = addObj(
`<< /Type /Pages /Kids [${pagesKidsToken}] /Count ${this.pageCount} >>`,
);
const pageIds: number[] = [];
for (let p = 0; p < this.pageCount; p++) {
pageIds.push(
addObj(
`<< /Type /Page /Parent ${pagesId} 0 R /Resources << /Font << /F1 ${fontRegularId} 0 R /F2 ${fontBoldId} 0 R >> >> /MediaBox [0 0 ${this.pageWidth} ${this.pageHeight}] /Contents ${contentIds[p]} 0 R >>`,
),
);
}
const catalogId = addObj(`<< /Type /Catalog /Pages ${pagesId} 0 R >>`);
chunkById[pagesId] = chunkById[pagesId].replace(
pagesKidsToken,
pageIds.map((id) => `${id} 0 R`).join(" "),
);
const body = chunkById.join("");
const bodyBuf = Buffer.from(body, "utf8");
const xrefAt = bodyBuf.length;
const xref: string[] = [
`xref\n0 ${offsets.length}\n0000000000 65535 f \n`,
];
for (let i = 1; i < offsets.length; i++) {
xref.push(`${String(offsets[i]).padStart(10, "0")} 00000 n \n`);
}
const trailer = `trailer\n<< /Size ${offsets.length} /Root ${catalogId} 0 R >>\nstartxref\n${xrefAt}\n%%EOF\n`;
return Buffer.concat([bodyBuf, Buffer.from(xref.join("") + trailer, "utf8")]);
}
}
/** PDF literal string for Helvetica (WinAnsi) — Latin text only. */
function encodePdfText(input: string): string {
const text = input.normalize("NFC");
const safe = [...text]
.map((ch) => {
const code = ch.charCodeAt(0);
if (code >= 0x20 && code <= 0x7e) return ch;
if (ch === "—" || ch === "–") return "-";
if (/\s/.test(ch)) return " ";
return "";
})
.join("")
.replace(/\s+/g, " ")
.trim();
return `(${safe
.replace(/\\/g, "\\\\")
.replace(/\(/g, "\\(")
.replace(/\)/g, "\\)")})`;
}