forked from Yara724/api
Add case identity filters and description validation
This commit is contained in:
@@ -26,6 +26,45 @@ describe("applyListQueryV2", () => {
|
||||
expect(r.list[0].publicId).toBe("A200");
|
||||
});
|
||||
|
||||
it("filters nested party identities and normalizes Persian digits", () => {
|
||||
const identityRows = [
|
||||
{
|
||||
publicId: "PHONE",
|
||||
createdAt: "2026-01-01",
|
||||
status: "OPEN",
|
||||
parties: [
|
||||
{
|
||||
person: {
|
||||
phoneNumber: "0912-123-4567",
|
||||
nationalCodeOfDriver: "۰۰۱۲۳۴۵۶۷۸",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
publicId: "OTHER",
|
||||
createdAt: "2026-01-02",
|
||||
status: "OPEN",
|
||||
parties: [{ person: { phoneNumber: "09351234567", nationalCode: "1111111111" } }],
|
||||
},
|
||||
];
|
||||
const identityGetters = {
|
||||
publicId: (r: (typeof identityRows)[0]) => r.publicId,
|
||||
createdAt: (r: (typeof identityRows)[0]) => r.createdAt,
|
||||
status: (r: (typeof identityRows)[0]) => r.status,
|
||||
};
|
||||
|
||||
const byPhone = applyListQueryV2(identityRows, identityGetters, {
|
||||
phoneNumber: "09121234567",
|
||||
});
|
||||
expect(byPhone.list.map((row) => row.publicId)).toEqual(["PHONE"]);
|
||||
|
||||
const byNationalCode = applyListQueryV2(identityRows, identityGetters, {
|
||||
nationalCode: "0012345678",
|
||||
});
|
||||
expect(byNationalCode.list.map((row) => row.publicId)).toEqual(["PHONE"]);
|
||||
});
|
||||
|
||||
it("sorts by publicId ascending", () => {
|
||||
const r = applyListQueryV2(rows, getters, {
|
||||
sortBy: "publicId",
|
||||
|
||||
@@ -53,6 +53,48 @@ function normalizeSearch(raw?: string): string | undefined {
|
||||
return t || undefined;
|
||||
}
|
||||
|
||||
function normalizeIdentityValue(raw?: unknown): string {
|
||||
return String(raw ?? "")
|
||||
.trim()
|
||||
.replace(/[۰-۹]/g, (digit) => String("۰۱۲۳۴۵۶۷۸۹".indexOf(digit)))
|
||||
.replace(/[٠-٩]/g, (digit) => String("٠١٢٣٤٥٦٧٨٩".indexOf(digit)))
|
||||
.replace(/\D/g, "");
|
||||
}
|
||||
|
||||
function isPhoneKey(key: string): boolean {
|
||||
return /phone|mobile|tel/i.test(key);
|
||||
}
|
||||
|
||||
function isNationalCodeKey(key: string): boolean {
|
||||
return /national[_-]?(code|id)|code[_-]?mell?i|mell?i[_-]?code/i.test(key);
|
||||
}
|
||||
|
||||
function collectIdentityValues(
|
||||
value: unknown,
|
||||
matchKey: (key: string) => boolean,
|
||||
): string[] {
|
||||
const values: string[] = [];
|
||||
const seen = new WeakSet<object>();
|
||||
const visit = (current: unknown, depth: number) => {
|
||||
if (current == null || depth > 8 || typeof current !== "object") return;
|
||||
const object = current as Record<string, unknown>;
|
||||
if (seen.has(object)) return;
|
||||
seen.add(object);
|
||||
for (const [key, child] of Object.entries(object)) {
|
||||
if (
|
||||
matchKey(key) &&
|
||||
(typeof child === "string" || typeof child === "number")
|
||||
) {
|
||||
const normalized = normalizeIdentityValue(child);
|
||||
if (normalized) values.push(normalized);
|
||||
}
|
||||
if (child && typeof child === "object") visit(child, depth + 1);
|
||||
}
|
||||
};
|
||||
visit(value, 0);
|
||||
return values;
|
||||
}
|
||||
|
||||
function toTime(v: Date | string | number | undefined): number {
|
||||
if (v == null) return 0;
|
||||
const n = v instanceof Date ? v.getTime() : new Date(v as string | number).getTime();
|
||||
@@ -75,12 +117,28 @@ export function applyListQueryV2<T>(
|
||||
totalPages?: number;
|
||||
} {
|
||||
const search = normalizeSearch(query.search);
|
||||
const phoneNumber = normalizeIdentityValue(query.phoneNumber);
|
||||
const nationalCode = normalizeIdentityValue(query.nationalCode);
|
||||
let filtered = rows;
|
||||
if (query.fileType && getters.fileType) {
|
||||
filtered = filtered.filter(
|
||||
(row) => getters.fileType!(row) === query.fileType,
|
||||
);
|
||||
}
|
||||
if (phoneNumber) {
|
||||
filtered = filtered.filter((row) =>
|
||||
collectIdentityValues(row, isPhoneKey).some((value) =>
|
||||
value.includes(phoneNumber),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (nationalCode) {
|
||||
filtered = filtered.filter((row) =>
|
||||
collectIdentityValues(row, isNationalCodeKey).some((value) =>
|
||||
value.includes(nationalCode),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (search) {
|
||||
filtered = filtered.filter((row) => {
|
||||
const chunks: string[] = [];
|
||||
|
||||
Reference in New Issue
Block a user