forked from Yara724/api
Add case identity filters and description validation
This commit is contained in:
@@ -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