fix: incorrect counts and query for teams

This commit is contained in:
Ephraim Atta-Duncan
2024-09-20 06:28:08 +00:00
parent d2b99303f9
commit f9935adb57
2 changed files with 108 additions and 131 deletions

View File

@ -302,16 +302,15 @@ const findTeamDocumentsFilter = (
return match<ExtendedDocumentStatus, Prisma.DocumentWhereInput | null>(status) return match<ExtendedDocumentStatus, Prisma.DocumentWhereInput | null>(status)
.with(ExtendedDocumentStatus.ALL, () => { .with(ExtendedDocumentStatus.ALL, () => {
const filter: Prisma.DocumentWhereInput = { const filter: Prisma.DocumentWhereInput = {
// Filter to display all documents that belong to the team.
OR: [ OR: [
{ {
teamId: team.id, teamId: team.id,
deletedAt: null,
}, },
], ],
}; };
if (teamEmail && filter.OR) { if (teamEmail && filter.OR) {
// Filter to display all documents received by the team email that are not draft.
filter.OR.push({ filter.OR.push({
status: { status: {
not: ExtendedDocumentStatus.DRAFT, not: ExtendedDocumentStatus.DRAFT,
@ -321,20 +320,20 @@ const findTeamDocumentsFilter = (
email: teamEmail, email: teamEmail,
}, },
}, },
deletedAt: null,
}); });
// Filter to display all documents that have been sent by the team email.
filter.OR.push({ filter.OR.push({
User: { User: {
email: teamEmail, email: teamEmail,
}, },
deletedAt: null,
}); });
} }
return filter; return filter;
}) })
.with(ExtendedDocumentStatus.INBOX, () => { .with(ExtendedDocumentStatus.INBOX, () => {
// Return a filter that will return nothing.
if (!teamEmail) { if (!teamEmail) {
return null; return null;
} }
@ -352,6 +351,7 @@ const findTeamDocumentsFilter = (
}, },
}, },
}, },
deletedAt: null,
}; };
}) })
.with(ExtendedDocumentStatus.DRAFT, () => { .with(ExtendedDocumentStatus.DRAFT, () => {
@ -360,6 +360,7 @@ const findTeamDocumentsFilter = (
{ {
teamId: team.id, teamId: team.id,
status: ExtendedDocumentStatus.DRAFT, status: ExtendedDocumentStatus.DRAFT,
deletedAt: null,
}, },
], ],
}; };
@ -370,6 +371,7 @@ const findTeamDocumentsFilter = (
User: { User: {
email: teamEmail, email: teamEmail,
}, },
deletedAt: null,
}); });
} }
@ -381,6 +383,7 @@ const findTeamDocumentsFilter = (
{ {
teamId: team.id, teamId: team.id,
status: ExtendedDocumentStatus.PENDING, status: ExtendedDocumentStatus.PENDING,
deletedAt: null,
}, },
], ],
}; };
@ -406,6 +409,7 @@ const findTeamDocumentsFilter = (
}, },
}, },
], ],
deletedAt: null,
}); });
} }
@ -414,6 +418,7 @@ const findTeamDocumentsFilter = (
.with(ExtendedDocumentStatus.COMPLETED, () => { .with(ExtendedDocumentStatus.COMPLETED, () => {
const filter: Prisma.DocumentWhereInput = { const filter: Prisma.DocumentWhereInput = {
status: ExtendedDocumentStatus.COMPLETED, status: ExtendedDocumentStatus.COMPLETED,
deletedAt: null,
OR: [ OR: [
{ {
teamId: team.id, teamId: team.id,
@ -477,6 +482,5 @@ const findTeamDocumentsFilter = (
OR: filters, OR: filters,
}; };
}) })
.exhaustive(); .exhaustive();
}; };

View File

@ -206,43 +206,22 @@ const getTeamCounts = async (options: GetTeamCountsOption) => {
} }
: undefined; : undefined;
let ownerCountsWhereInput: Prisma.DocumentWhereInput = { const ownerCountsWhereInput: Prisma.DocumentWhereInput = {
userId: userIdWhereClause, userId: userIdWhereClause,
createdAt, createdAt,
teamId, OR: [{ teamId }, ...(teamEmail ? [{ User: { email: teamEmail } }] : [])],
deletedAt: null, deletedAt: null,
}; };
let notSignedCountsGroupByArgs = null; const notSignedCountsWhereInput: Prisma.DocumentWhereInput = {
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, userId: userIdWhereClause,
createdAt, createdAt,
status: ExtendedDocumentStatus.PENDING, status: ExtendedDocumentStatus.PENDING,
OR: [
{ teamId },
...(teamEmail
? [
{
Recipient: { Recipient: {
some: { some: {
email: teamEmail, email: teamEmail,
@ -250,74 +229,29 @@ const getTeamCounts = async (options: GetTeamCountsOption) => {
documentDeletedAt: null, documentDeletedAt: null,
}, },
}, },
},
]
: []),
],
deletedAt: null, deletedAt: null,
}, };
} satisfies Prisma.DocumentGroupByArgs;
hasSignedCountsGroupByArgs = { const hasSignedCountsWhereInput: Prisma.DocumentWhereInput = {
by: ['status'],
_count: {
_all: true,
},
where: {
userId: userIdWhereClause, userId: userIdWhereClause,
createdAt, 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: [ OR: [
{ {
teamId, teamId,
deletedAt: { status: {
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(), in: [ExtendedDocumentStatus.PENDING, ExtendedDocumentStatus.COMPLETED],
},
},
{
status: ExtendedDocumentStatus.PENDING,
Recipient: {
some: {
email: teamEmail,
signingStatus: SigningStatus.SIGNED,
documentDeletedAt: null,
},
}, },
deletedAt: { deletedAt: {
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(), gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
}, },
}, },
...(teamEmail
? [
{ {
status: ExtendedDocumentStatus.COMPLETED,
Recipient: { Recipient: {
some: { some: {
email: teamEmail, email: teamEmail,
@ -327,25 +261,64 @@ const getTeamCounts = async (options: GetTeamCountsOption) => {
}, },
}, },
}, },
status: {
in: [ExtendedDocumentStatus.PENDING, ExtendedDocumentStatus.COMPLETED],
},
deletedAt: { deletedAt: {
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(), 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(),
}, },
} satisfies Prisma.DocumentGroupByArgs; },
} ...(teamEmail
? [
{
Recipient: {
some: {
email: teamEmail,
documentDeletedAt: {
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
},
},
},
},
]
: []),
],
};
return Promise.all([ return Promise.all([
prisma.document.groupBy({ prisma.document.groupBy({
by: ['status'], by: ['status'],
_count: { _count: { _all: true },
_all: true,
},
where: ownerCountsWhereInput, where: ownerCountsWhereInput,
}), }),
notSignedCountsGroupByArgs ? prisma.document.groupBy(notSignedCountsGroupByArgs) : [], prisma.document.groupBy({
hasSignedCountsGroupByArgs ? prisma.document.groupBy(hasSignedCountsGroupByArgs) : [], by: ['status'],
deletedCountsGroupByArgs ? prisma.document.groupBy(deletedCountsGroupByArgs) : [], _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,
}),
]); ]);
}; };