chore: refactor find documents

This commit is contained in:
Ephraim Atta-Duncan
2024-06-17 23:19:48 +00:00
committed by Mythie
parent 2837b178fb
commit 754e9e6428
2 changed files with 142 additions and 113 deletions

View File

@ -2,8 +2,8 @@ import { DateTime } from 'luxon';
import { P, match } from 'ts-pattern';
import { prisma } from '@documenso/prisma';
import { RecipientRole, SigningStatus } from '@documenso/prisma/client';
import type { Document, Prisma, Team, TeamEmail, User } from '@documenso/prisma/client';
import { Prisma, RecipientRole, SigningStatus } from '@documenso/prisma/client';
import type { Document, Team, TeamEmail, User } from '@documenso/prisma/client';
import { ExtendedDocumentStatus } from '@documenso/prisma/types/extended-document-status';
import type { FindResultSet } from '../../types/find-result-set';
@ -26,78 +26,67 @@ export type FindDocumentsOptions = {
senderIds?: number[];
};
export const findDocuments = async ({
userId,
teamId,
term,
status = ExtendedDocumentStatus.ALL,
page = 1,
perPage = 10,
orderBy,
period,
senderIds,
}: FindDocumentsOptions) => {
const { user, team } = await prisma.$transaction(async (tx) => {
const user = await tx.user.findFirstOrThrow({
where: {
id: userId,
const getDeletedFilter = (
status: ExtendedDocumentStatus,
user: User,
team?: (Team & { teamEmail: TeamEmail | null }) | null,
) => {
if (status === ExtendedDocumentStatus.BIN) {
return {
OR: [
{
userId: user.id,
deletedAt: {
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
},
});
let team = null;
if (teamId !== undefined) {
team = await tx.team.findFirstOrThrow({
where: {
id: teamId,
members: {
},
{
Recipient: {
some: {
userId,
email: user.email,
documentDeletedAt: {
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
},
},
},
include: {
teamEmail: true,
},
});
}
return {
user,
team,
};
});
const orderByColumn = orderBy?.column ?? 'createdAt';
const orderByDirection = orderBy?.direction ?? 'desc';
const termFilters = match(term)
.with(P.string.minLength(1), () => {
return {
title: {
contains: term,
mode: 'insensitive',
...(team
? [
{
teamId: team.id,
deletedAt: {
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
},
} as const;
})
.otherwise(() => undefined);
const filters = team ? findTeamDocumentsFilter(status, team) : findDocumentsFilter(status, user);
if (filters === null) {
return {
data: [],
count: 0,
currentPage: 1,
perPage,
totalPages: 0,
},
...(team.teamEmail
? [
{
User: {
email: team.teamEmail.email,
},
deletedAt: {
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
},
},
{
Recipient: {
some: {
email: team.teamEmail.email,
documentDeletedAt: {
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
},
},
},
},
]
: []),
]
: []),
],
};
}
let deletedFilter: Prisma.DocumentWhereInput | undefined;
if (status !== ExtendedDocumentStatus.BIN) {
deletedFilter = {
return {
AND: {
OR: [
{
@ -112,14 +101,8 @@ export const findDocuments = async ({
},
},
},
],
},
};
if (team) {
deletedFilter = {
AND: {
OR: team.teamEmail
...(team
? team.teamEmail
? [
{
teamId: team.id,
@ -145,12 +128,70 @@ export const findDocuments = async ({
teamId: team.id,
deletedAt: null,
},
]
: []),
],
},
};
};
export const findDocuments = async ({
userId,
teamId,
term,
status = ExtendedDocumentStatus.ALL,
page = 1,
perPage = 10,
orderBy,
period,
senderIds,
}: FindDocumentsOptions) => {
const { user, team } = await prisma.$transaction(async (tx) => {
const user = await tx.user.findFirstOrThrow({
where: { id: userId },
});
let team = null;
if (teamId !== undefined) {
team = await tx.team.findFirstOrThrow({
where: {
id: teamId,
members: { some: { userId } },
},
include: { teamEmail: true },
});
}
return { user, team };
});
const orderByColumn = orderBy?.column ?? 'createdAt';
const orderByDirection = orderBy?.direction ?? 'desc';
const termFilters = match(term)
.with(P.string.minLength(1), () => ({
title: {
contains: term,
mode: Prisma.QueryMode.insensitive,
},
}))
.otherwise(() => undefined);
const filters = team ? findTeamDocumentsFilter(status, team) : findDocumentsFilter(status, user);
if (filters === null) {
return {
data: [],
count: 0,
currentPage: 1,
perPage,
totalPages: 0,
};
}
const deletedFilter = getDeletedFilter(status, user, team);
const whereClause: Prisma.DocumentWhereInput = {
...termFilters,
...filters,
@ -167,27 +208,6 @@ export const findDocuments = async ({
};
}
if (status === ExtendedDocumentStatus.BIN) {
whereClause.OR = [
{
userId: user.id,
deletedAt: {
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
},
},
{
Recipient: {
some: {
email: user.email,
documentDeletedAt: {
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
},
},
},
},
];
}
if (senderIds && senderIds.length > 0) {
whereClause.userId = {
in: senderIds,

View File

@ -137,6 +137,15 @@ const handleDocumentOwnerRestore = async ({
}),
});
await tx.recipient.updateMany({
where: {
documentId: document.id,
},
data: {
documentDeletedAt: null,
},
});
return await tx.document.update({
where: {
id: document.id,