Refactored filter parameters.

This commit is contained in:
Mike Cao 2024-03-26 17:31:16 -07:00
parent 1a839d1cae
commit cff2d00536
13 changed files with 291 additions and 123 deletions

View file

@ -13,7 +13,7 @@ export interface InsightsRequestBody {
endDate: string;
};
fields: { name: string; type: string; label: string }[];
filters: { name: string; type: string; filter: string; value: string }[];
filters: { name: string; type: string; operator: string; value: string }[];
groups: { name: string; type: string }[];
}
@ -42,7 +42,7 @@ const schema = {
yup.object().shape({
name: yup.string().required(),
type: yup.string().required(),
filter: yup.string().required(),
operator: yup.string().required(),
value: yup.string().required(),
}),
),

View file

@ -109,8 +109,8 @@ export default async (
if (search) {
filters[column] = {
column,
op: OPERATORS.contains,
value: '%' + search + '%',
operator: OPERATORS.contains,
value: search,
};
}

View file

@ -2,23 +2,33 @@ import { NextApiRequestQueryBody } from 'lib/types';
import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiResponse } from 'next';
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
import {
badRequest,
methodNotAllowed,
ok,
safeDecodeURIComponent,
unauthorized,
} from 'next-basics';
import { EVENT_COLUMNS, FILTER_COLUMNS, SESSION_COLUMNS } from 'lib/constants';
import { getValues } from 'queries';
import { parseDateRangeQuery } from 'lib/query';
import * as yup from 'yup';
export interface ValuesRequestQuery {
websiteId: string;
type: string;
startAt: number;
endAt: number;
search?: string;
}
import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
websiteId: yup.string().uuid().required(),
type: yup.string().required(),
startAt: yup.number().required(),
endAt: yup.number().required(),
search: yup.string(),
}),
};
@ -27,7 +37,7 @@ export default async (req: NextApiRequestQueryBody<ValuesRequestQuery>, res: Nex
await useAuth(req, res);
await useValidate(schema, req, res);
const { websiteId, type } = req.query;
const { websiteId, type, search } = req.query;
const { startDate, endDate } = await parseDateRangeQuery(req);
if (req.method === 'GET') {
@ -39,12 +49,18 @@ export default async (req: NextApiRequestQueryBody<ValuesRequestQuery>, res: Nex
return unauthorized(res);
}
const values = await getValues(websiteId, FILTER_COLUMNS[type as string], startDate, endDate);
const values = await getValues(
websiteId,
FILTER_COLUMNS[type as string],
startDate,
endDate,
search,
);
return ok(
res,
values
.map(({ value }) => value)
.map(({ value }) => safeDecodeURIComponent(value))
.filter(n => n)
.sort(),
);