forked from Yara724/api
Fix searching on GET APIs
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { ListQueryV2Dto } from "src/common/dto/list-query-v2.dto";
|
||||
import { BadRequestException } from "@nestjs/common";
|
||||
|
||||
const MAX_LIMIT = 100;
|
||||
const DEFAULT_LIMIT_WHEN_PAGE_SET = 20;
|
||||
@@ -8,10 +9,45 @@ export type ListItemGetters<T> = {
|
||||
createdAt: (row: T) => Date | string | number | undefined;
|
||||
requestNo?: (row: T) => string | undefined;
|
||||
status: (row: T) => string | undefined;
|
||||
/** Blame file type (`THIRD_PARTY` | `CAR_BODY`) when available. */
|
||||
fileType?: (row: T) => string | undefined;
|
||||
/** Extra strings to match against `search` (e.g. claimRequestId, vehicle). */
|
||||
searchExtras?: (row: T) => string[];
|
||||
};
|
||||
|
||||
export function parseListDateRange(
|
||||
from?: string,
|
||||
to?: string,
|
||||
): { fromDate?: Date; toDate?: Date } {
|
||||
const fromDate = from ? new Date(from) : undefined;
|
||||
const toDate = to ? new Date(to) : undefined;
|
||||
|
||||
if (fromDate && Number.isNaN(fromDate.getTime())) {
|
||||
throw new BadRequestException("Invalid 'from' date");
|
||||
}
|
||||
if (toDate && Number.isNaN(toDate.getTime())) {
|
||||
throw new BadRequestException("Invalid 'to' date");
|
||||
}
|
||||
if (fromDate && toDate && fromDate > toDate) {
|
||||
throw new BadRequestException("'from' must be before or equal to 'to'");
|
||||
}
|
||||
return { fromDate, toDate };
|
||||
}
|
||||
|
||||
export function isInListDateRange(
|
||||
value: Date | string | number | undefined,
|
||||
fromDate?: Date,
|
||||
toDate?: Date,
|
||||
): boolean {
|
||||
if (!fromDate && !toDate) return true;
|
||||
if (value == null) return false;
|
||||
const d = value instanceof Date ? value : new Date(value);
|
||||
if (Number.isNaN(d.getTime())) return false;
|
||||
if (fromDate && d < fromDate) return false;
|
||||
if (toDate && d > toDate) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function normalizeSearch(raw?: string): string | undefined {
|
||||
const t = raw?.trim().toLowerCase();
|
||||
return t || undefined;
|
||||
@@ -40,8 +76,13 @@ export function applyListQueryV2<T>(
|
||||
} {
|
||||
const search = normalizeSearch(query.search);
|
||||
let filtered = rows;
|
||||
if (query.fileType && getters.fileType) {
|
||||
filtered = filtered.filter(
|
||||
(row) => getters.fileType!(row) === query.fileType,
|
||||
);
|
||||
}
|
||||
if (search) {
|
||||
filtered = rows.filter((row) => {
|
||||
filtered = filtered.filter((row) => {
|
||||
const chunks: string[] = [];
|
||||
const pid = getters.publicId(row);
|
||||
if (pid) chunks.push(pid);
|
||||
|
||||
Reference in New Issue
Block a user