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