fix: document count period filter (#882)

## Description

Currently the count for the documents table tabs do not display the
correct values when the period filter is applied.

## Changes Made

- Updated `getStats` to support filtering on period

## Testing Performed

- Tested to see if the documents tab count were being filtered based on
the period

## Checklist

- [X] I have tested these changes locally and they work as expected.
- [X] I have followed the project's coding style guidelines.
This commit is contained in:
David Nguyen
2024-01-31 12:40:37 +11:00
committed by GitHub
parent 620ae41fcc
commit 27d8098511
4 changed files with 32 additions and 9 deletions

View File

@ -9,6 +9,8 @@ import { ExtendedDocumentStatus } from '@documenso/prisma/types/extended-documen
import type { FindResultSet } from '../../types/find-result-set';
import { maskRecipientTokensForDocument } from '../../utils/mask-recipient-tokens-for-document';
export type PeriodSelectorValue = '' | '7d' | '14d' | '30d';
export type FindDocumentsOptions = {
userId: number;
term?: string;
@ -19,7 +21,7 @@ export type FindDocumentsOptions = {
column: keyof Omit<Document, 'document'>;
direction: 'asc' | 'desc';
};
period?: '' | '7d' | '14d' | '30d';
period?: PeriodSelectorValue;
};
export const findDocuments = async ({

View File

@ -1,14 +1,31 @@
import { DateTime } from 'luxon';
import { prisma } from '@documenso/prisma';
import type { User } from '@documenso/prisma/client';
import type { Prisma, User } from '@documenso/prisma/client';
import { SigningStatus } from '@documenso/prisma/client';
import { isExtendedDocumentStatus } from '@documenso/prisma/guards/is-extended-document-status';
import { ExtendedDocumentStatus } from '@documenso/prisma/types/extended-document-status';
import type { PeriodSelectorValue } from './find-documents';
export type GetStatsInput = {
user: User;
period?: PeriodSelectorValue;
};
export const getStats = async ({ user }: GetStatsInput) => {
export const getStats = async ({ user, period }: GetStatsInput) => {
let createdAt: Prisma.DocumentWhereInput['createdAt'];
if (period) {
const daysAgo = parseInt(period.replace(/d$/, ''), 10);
const startOfPeriod = DateTime.now().minus({ days: daysAgo }).startOf('day');
createdAt = {
gte: startOfPeriod.toJSDate(),
};
}
const [ownerCounts, notSignedCounts, hasSignedCounts] = await Promise.all([
prisma.document.groupBy({
by: ['status'],
@ -17,6 +34,7 @@ export const getStats = async ({ user }: GetStatsInput) => {
},
where: {
userId: user.id,
createdAt,
deletedAt: null,
},
}),
@ -33,6 +51,7 @@ export const getStats = async ({ user }: GetStatsInput) => {
signingStatus: SigningStatus.NOT_SIGNED,
},
},
createdAt,
deletedAt: null,
},
}),
@ -42,6 +61,7 @@ export const getStats = async ({ user }: GetStatsInput) => {
_all: true,
},
where: {
createdAt,
User: {
email: {
not: user.email,