mirror of
https://github.com/documenso/documenso.git
synced 2025-11-15 01:01:49 +10:00
fix: incorrect counts and query for teams
This commit is contained in:
@ -302,16 +302,15 @@ const findTeamDocumentsFilter = (
|
||||
return match<ExtendedDocumentStatus, Prisma.DocumentWhereInput | null>(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();
|
||||
};
|
||||
|
||||
@ -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,
|
||||
}),
|
||||
]);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user