From f9935adb57a62ffd86507bda084230fa7e716a7e Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Fri, 20 Sep 2024 06:28:08 +0000 Subject: [PATCH] fix: incorrect counts and query for teams --- .../server-only/document/find-documents.ts | 14 +- .../lib/server-only/document/get-stats.ts | 225 ++++++++---------- 2 files changed, 108 insertions(+), 131 deletions(-) diff --git a/packages/lib/server-only/document/find-documents.ts b/packages/lib/server-only/document/find-documents.ts index a3caa2831..e3fd73aaa 100644 --- a/packages/lib/server-only/document/find-documents.ts +++ b/packages/lib/server-only/document/find-documents.ts @@ -302,16 +302,15 @@ const findTeamDocumentsFilter = ( return match(status) .with(ExtendedDocumentStatus.ALL, () => { const filter: Prisma.DocumentWhereInput = { - // Filter to display all documents that belong to the team. OR: [ { teamId: team.id, + deletedAt: null, }, ], }; if (teamEmail && filter.OR) { - // Filter to display all documents received by the team email that are not draft. filter.OR.push({ status: { not: ExtendedDocumentStatus.DRAFT, @@ -321,20 +320,20 @@ const findTeamDocumentsFilter = ( email: teamEmail, }, }, + deletedAt: null, }); - // Filter to display all documents that have been sent by the team email. filter.OR.push({ User: { email: teamEmail, }, + deletedAt: null, }); } return filter; }) .with(ExtendedDocumentStatus.INBOX, () => { - // Return a filter that will return nothing. if (!teamEmail) { return null; } @@ -352,6 +351,7 @@ const findTeamDocumentsFilter = ( }, }, }, + deletedAt: null, }; }) .with(ExtendedDocumentStatus.DRAFT, () => { @@ -360,6 +360,7 @@ const findTeamDocumentsFilter = ( { teamId: team.id, status: ExtendedDocumentStatus.DRAFT, + deletedAt: null, }, ], }; @@ -370,6 +371,7 @@ const findTeamDocumentsFilter = ( User: { email: teamEmail, }, + deletedAt: null, }); } @@ -381,6 +383,7 @@ const findTeamDocumentsFilter = ( { teamId: team.id, status: ExtendedDocumentStatus.PENDING, + deletedAt: null, }, ], }; @@ -406,6 +409,7 @@ const findTeamDocumentsFilter = ( }, }, ], + deletedAt: null, }); } @@ -414,6 +418,7 @@ const findTeamDocumentsFilter = ( .with(ExtendedDocumentStatus.COMPLETED, () => { const filter: Prisma.DocumentWhereInput = { status: ExtendedDocumentStatus.COMPLETED, + deletedAt: null, OR: [ { teamId: team.id, @@ -477,6 +482,5 @@ const findTeamDocumentsFilter = ( OR: filters, }; }) - .exhaustive(); }; diff --git a/packages/lib/server-only/document/get-stats.ts b/packages/lib/server-only/document/get-stats.ts index 632ce2412..2dffd2976 100644 --- a/packages/lib/server-only/document/get-stats.ts +++ b/packages/lib/server-only/document/get-stats.ts @@ -206,146 +206,119 @@ const getTeamCounts = async (options: GetTeamCountsOption) => { } : undefined; - let ownerCountsWhereInput: Prisma.DocumentWhereInput = { + const ownerCountsWhereInput: Prisma.DocumentWhereInput = { userId: userIdWhereClause, createdAt, - teamId, + OR: [{ teamId }, ...(teamEmail ? [{ User: { email: teamEmail } }] : [])], deletedAt: null, }; - let notSignedCountsGroupByArgs = null; - let hasSignedCountsGroupByArgs = null; - let deletedCountsGroupByArgs = null; - - if (teamEmail) { - ownerCountsWhereInput = { - userId: userIdWhereClause, - createdAt, - OR: [ - { - teamId, - }, - { - User: { - email: teamEmail, - }, - }, - ], - deletedAt: null, - }; - - notSignedCountsGroupByArgs = { - by: ['status'], - _count: { - _all: true, - }, - where: { - userId: userIdWhereClause, - createdAt, - status: ExtendedDocumentStatus.PENDING, - Recipient: { - some: { - email: teamEmail, - signingStatus: SigningStatus.NOT_SIGNED, - documentDeletedAt: null, - }, - }, - deletedAt: null, - }, - } satisfies Prisma.DocumentGroupByArgs; - - hasSignedCountsGroupByArgs = { - by: ['status'], - _count: { - _all: true, - }, - where: { - userId: userIdWhereClause, - createdAt, - OR: [ - { - status: ExtendedDocumentStatus.PENDING, - Recipient: { - some: { - email: teamEmail, - signingStatus: SigningStatus.SIGNED, - documentDeletedAt: null, - }, - }, - deletedAt: null, - }, - { - status: ExtendedDocumentStatus.COMPLETED, - Recipient: { - some: { - email: teamEmail, - signingStatus: SigningStatus.SIGNED, - documentDeletedAt: null, - }, - }, - deletedAt: null, - }, - ], - }, - } satisfies Prisma.DocumentGroupByArgs; - - // Deleted counts. - deletedCountsGroupByArgs = { - by: ['status'], - _count: { - _all: true, - }, - where: { - OR: [ - { - teamId, - deletedAt: { - gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(), - }, - }, - { - status: ExtendedDocumentStatus.PENDING, - Recipient: { - some: { - email: teamEmail, - signingStatus: SigningStatus.SIGNED, - documentDeletedAt: null, - }, - }, - deletedAt: { - gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(), - }, - }, - { - status: ExtendedDocumentStatus.COMPLETED, - Recipient: { - some: { - email: teamEmail, - signingStatus: SigningStatus.SIGNED, - documentDeletedAt: { - gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(), + const notSignedCountsWhereInput: Prisma.DocumentWhereInput = { + userId: userIdWhereClause, + createdAt, + status: ExtendedDocumentStatus.PENDING, + OR: [ + { teamId }, + ...(teamEmail + ? [ + { + Recipient: { + some: { + email: teamEmail, + signingStatus: SigningStatus.NOT_SIGNED, + documentDeletedAt: null, }, }, }, - deletedAt: { - gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(), - }, - }, - ], + ] + : []), + ], + deletedAt: null, + }; + + const hasSignedCountsWhereInput: Prisma.DocumentWhereInput = { + userId: userIdWhereClause, + createdAt, + OR: [ + { + teamId, + status: { + in: [ExtendedDocumentStatus.PENDING, ExtendedDocumentStatus.COMPLETED], + }, + deletedAt: { + gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(), + }, }, - } satisfies Prisma.DocumentGroupByArgs; - } + ...(teamEmail + ? [ + { + Recipient: { + some: { + email: teamEmail, + signingStatus: SigningStatus.SIGNED, + documentDeletedAt: { + gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(), + }, + }, + }, + status: { + in: [ExtendedDocumentStatus.PENDING, ExtendedDocumentStatus.COMPLETED], + }, + deletedAt: { + gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(), + }, + }, + ] + : []), + ], + deletedAt: null, + }; + + const deletedCountsWhereInput: Prisma.DocumentWhereInput = { + OR: [ + { + teamId, + deletedAt: { + gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(), + }, + }, + ...(teamEmail + ? [ + { + Recipient: { + some: { + email: teamEmail, + documentDeletedAt: { + gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(), + }, + }, + }, + }, + ] + : []), + ], + }; return Promise.all([ prisma.document.groupBy({ by: ['status'], - _count: { - _all: true, - }, + _count: { _all: true }, where: ownerCountsWhereInput, }), - notSignedCountsGroupByArgs ? prisma.document.groupBy(notSignedCountsGroupByArgs) : [], - hasSignedCountsGroupByArgs ? prisma.document.groupBy(hasSignedCountsGroupByArgs) : [], - deletedCountsGroupByArgs ? prisma.document.groupBy(deletedCountsGroupByArgs) : [], + prisma.document.groupBy({ + by: ['status'], + _count: { _all: true }, + where: notSignedCountsWhereInput, + }), + prisma.document.groupBy({ + by: ['status'], + _count: { _all: true }, + where: hasSignedCountsWhereInput, + }), + prisma.document.groupBy({ + by: ['status'], + _count: { _all: true }, + where: deletedCountsWhereInput, + }), ]); };