mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 20:42:34 +10:00
## 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.
129 lines
3.2 KiB
TypeScript
129 lines
3.2 KiB
TypeScript
import { DateTime } from 'luxon';
|
|
|
|
import { prisma } from '@documenso/prisma';
|
|
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, 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'],
|
|
_count: {
|
|
_all: true,
|
|
},
|
|
where: {
|
|
userId: user.id,
|
|
createdAt,
|
|
deletedAt: null,
|
|
},
|
|
}),
|
|
prisma.document.groupBy({
|
|
by: ['status'],
|
|
_count: {
|
|
_all: true,
|
|
},
|
|
where: {
|
|
status: ExtendedDocumentStatus.PENDING,
|
|
Recipient: {
|
|
some: {
|
|
email: user.email,
|
|
signingStatus: SigningStatus.NOT_SIGNED,
|
|
},
|
|
},
|
|
createdAt,
|
|
deletedAt: null,
|
|
},
|
|
}),
|
|
prisma.document.groupBy({
|
|
by: ['status'],
|
|
_count: {
|
|
_all: true,
|
|
},
|
|
where: {
|
|
createdAt,
|
|
User: {
|
|
email: {
|
|
not: user.email,
|
|
},
|
|
},
|
|
OR: [
|
|
{
|
|
status: ExtendedDocumentStatus.PENDING,
|
|
Recipient: {
|
|
some: {
|
|
email: user.email,
|
|
signingStatus: SigningStatus.SIGNED,
|
|
},
|
|
},
|
|
deletedAt: null,
|
|
},
|
|
{
|
|
status: ExtendedDocumentStatus.COMPLETED,
|
|
Recipient: {
|
|
some: {
|
|
email: user.email,
|
|
signingStatus: SigningStatus.SIGNED,
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
}),
|
|
]);
|
|
|
|
const stats: Record<ExtendedDocumentStatus, number> = {
|
|
[ExtendedDocumentStatus.DRAFT]: 0,
|
|
[ExtendedDocumentStatus.PENDING]: 0,
|
|
[ExtendedDocumentStatus.COMPLETED]: 0,
|
|
[ExtendedDocumentStatus.INBOX]: 0,
|
|
[ExtendedDocumentStatus.ALL]: 0,
|
|
};
|
|
|
|
ownerCounts.forEach((stat) => {
|
|
stats[stat.status] = stat._count._all;
|
|
});
|
|
|
|
notSignedCounts.forEach((stat) => {
|
|
stats[ExtendedDocumentStatus.INBOX] += stat._count._all;
|
|
});
|
|
|
|
hasSignedCounts.forEach((stat) => {
|
|
if (stat.status === ExtendedDocumentStatus.COMPLETED) {
|
|
stats[ExtendedDocumentStatus.COMPLETED] += stat._count._all;
|
|
}
|
|
|
|
if (stat.status === ExtendedDocumentStatus.PENDING) {
|
|
stats[ExtendedDocumentStatus.PENDING] += stat._count._all;
|
|
}
|
|
});
|
|
|
|
Object.keys(stats).forEach((key) => {
|
|
if (key !== ExtendedDocumentStatus.ALL && isExtendedDocumentStatus(key)) {
|
|
stats[ExtendedDocumentStatus.ALL] += stats[key];
|
|
}
|
|
});
|
|
|
|
return stats;
|
|
};
|