From 73e375938cd305f374102a437f2edd990dbfe0b3 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Thu, 13 Jun 2024 06:39:43 +0000 Subject: [PATCH] fix: show deleted counts on tab --- .../lib/server-only/document/get-stats.ts | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/lib/server-only/document/get-stats.ts b/packages/lib/server-only/document/get-stats.ts index 326807649..7fa6dfc56 100644 --- a/packages/lib/server-only/document/get-stats.ts +++ b/packages/lib/server-only/document/get-stats.ts @@ -26,7 +26,7 @@ export const getStats = async ({ user, period, ...options }: GetStatsInput) => { }; } - const [ownerCounts, notSignedCounts, hasSignedCounts] = await (options.team + const [ownerCounts, notSignedCounts, hasSignedCounts, deletedCounts] = await (options.team ? getTeamCounts({ ...options.team, createdAt }) : getCounts({ user, createdAt })); @@ -57,6 +57,10 @@ export const getStats = async ({ user, period, ...options }: GetStatsInput) => { } }); + deletedCounts.forEach((stat) => { + stats[ExtendedDocumentStatus.BIN] += stat._count._all; + }); + Object.keys(stats).forEach((key) => { if (key !== ExtendedDocumentStatus.ALL && isExtendedDocumentStatus(key)) { stats[ExtendedDocumentStatus.ALL] += stats[key]; @@ -141,6 +145,20 @@ const getCounts = async ({ user, createdAt }: GetCountsOption) => { ], }, }), + // Deleted counts. + prisma.document.groupBy({ + by: ['status'], + _count: { + _all: true, + }, + where: { + userId: user.id, + createdAt, + deletedAt: { + not: null, + }, + }, + }), ]); }; @@ -172,6 +190,7 @@ const getTeamCounts = async (options: GetTeamCountsOption) => { let notSignedCountsGroupByArgs = null; let hasSignedCountsGroupByArgs = null; + let deletedCountsGroupByArgs = null; if (teamEmail) { ownerCountsWhereInput = { @@ -244,6 +263,20 @@ const getTeamCounts = async (options: GetTeamCountsOption) => { ], }, } satisfies Prisma.DocumentGroupByArgs; + + deletedCountsGroupByArgs = { + by: ['status'], + _count: { + _all: true, + }, + where: { + userId: userIdWhereClause, + createdAt, + deletedAt: { + not: null, + }, + }, + } satisfies Prisma.DocumentGroupByArgs; } return Promise.all([ @@ -256,5 +289,6 @@ const getTeamCounts = async (options: GetTeamCountsOption) => { }), notSignedCountsGroupByArgs ? prisma.document.groupBy(notSignedCountsGroupByArgs) : [], hasSignedCountsGroupByArgs ? prisma.document.groupBy(hasSignedCountsGroupByArgs) : [], + deletedCountsGroupByArgs ? prisma.document.groupBy(deletedCountsGroupByArgs) : [], ]); };