From 1f953457db090cf3669b94cfe5f8990294d77fd9 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Fri, 3 Nov 2023 07:04:11 +0530 Subject: [PATCH] fix: days filter working (#623) --- .../src/app/(dashboard)/documents/page.tsx | 8 +++-- .../server-only/document/find-documents.ts | 35 ++++++++++++++----- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/apps/web/src/app/(dashboard)/documents/page.tsx b/apps/web/src/app/(dashboard)/documents/page.tsx index d8a5a5bd8..e23f00af3 100644 --- a/apps/web/src/app/(dashboard)/documents/page.tsx +++ b/apps/web/src/app/(dashboard)/documents/page.tsx @@ -8,7 +8,10 @@ import { ExtendedDocumentStatus } from '@documenso/prisma/types/extended-documen import { Tabs, TabsList, TabsTrigger } from '@documenso/ui/primitives/tabs'; import { PeriodSelector } from '~/components/(dashboard)/period-selector/period-selector'; -import { PeriodSelectorValue } from '~/components/(dashboard)/period-selector/types'; +import { + PeriodSelectorValue, + isPeriodSelectorValue, +} from '~/components/(dashboard)/period-selector/types'; import { DocumentStatus } from '~/components/formatter/document-status'; import { DocumentsDataTable } from './data-table'; @@ -32,7 +35,7 @@ export default async function DocumentsPage({ searchParams = {} }: DocumentsPage }); const status = isExtendedDocumentStatus(searchParams.status) ? searchParams.status : 'ALL'; - // const period = isPeriodSelectorValue(searchParams.period) ? searchParams.period : ''; + const period = isPeriodSelectorValue(searchParams.period) ? searchParams.period : ''; const page = Number(searchParams.page) || 1; const perPage = Number(searchParams.perPage) || 20; @@ -45,6 +48,7 @@ export default async function DocumentsPage({ searchParams = {} }: DocumentsPage }, page, perPage, + period, }); const getTabHref = (value: typeof status) => { diff --git a/packages/lib/server-only/document/find-documents.ts b/packages/lib/server-only/document/find-documents.ts index aa5410c17..2581c5738 100644 --- a/packages/lib/server-only/document/find-documents.ts +++ b/packages/lib/server-only/document/find-documents.ts @@ -1,4 +1,5 @@ -import { match } from 'ts-pattern'; +import { DateTime } from 'luxon'; +import { P, match } from 'ts-pattern'; import { prisma } from '@documenso/prisma'; import { Document, Prisma, SigningStatus } from '@documenso/prisma/client'; @@ -16,6 +17,7 @@ export interface FindDocumentsOptions { column: keyof Omit; direction: 'asc' | 'desc'; }; + period?: '' | '7d' | '14d' | '30d'; } export const findDocuments = async ({ @@ -25,6 +27,7 @@ export const findDocuments = async ({ page = 1, perPage = 10, orderBy, + period, }: FindDocumentsOptions) => { const user = await prisma.user.findFirstOrThrow({ where: { @@ -35,14 +38,16 @@ export const findDocuments = async ({ const orderByColumn = orderBy?.column ?? 'createdAt'; const orderByDirection = orderBy?.direction ?? 'desc'; - const termFilters = !term - ? undefined - : ({ + const termFilters = match(term) + .with(P.string.minLength(1), () => { + return { title: { contains: term, mode: 'insensitive', }, - } as const); + } as const; + }) + .otherwise(() => undefined); const filters = match(status) .with(ExtendedDocumentStatus.ALL, () => ({ @@ -113,12 +118,24 @@ export const findDocuments = async ({ })) .exhaustive(); + const whereClause = { + ...termFilters, + ...filters, + }; + + if (period) { + const daysAgo = parseInt(period.replace(/d$/, ''), 10); + + const startOfPeriod = DateTime.now().minus({ days: daysAgo }).startOf('day'); + + whereClause.createdAt = { + gte: startOfPeriod.toJSDate(), + }; + } + const [data, count] = await Promise.all([ prisma.document.findMany({ - where: { - ...termFilters, - ...filters, - }, + where: whereClause, skip: Math.max(page - 1, 0) * perPage, take: perPage, orderBy: {