Files
sign/packages/lib/server-only/document/find-documents.ts

200 lines
4.5 KiB
TypeScript
Raw Normal View History

2023-11-03 07:04:11 +05:30
import { DateTime } from 'luxon';
import { P, match } from 'ts-pattern';
2023-08-24 16:50:40 +10:00
2023-06-09 18:21:18 +10:00
import { prisma } from '@documenso/prisma';
import type { Document, Prisma } from '@documenso/prisma/client';
import { RecipientRole, SigningStatus } from '@documenso/prisma/client';
2023-08-24 16:50:40 +10:00
import { ExtendedDocumentStatus } from '@documenso/prisma/types/extended-document-status';
2023-06-09 18:21:18 +10:00
import type { FindResultSet } from '../../types/find-result-set';
import { maskRecipientTokensForDocument } from '../../utils/mask-recipient-tokens-for-document';
2023-06-09 18:21:18 +10:00
export type PeriodSelectorValue = '' | '7d' | '14d' | '30d';
2023-10-06 22:54:24 +00:00
export type FindDocumentsOptions = {
2023-06-09 18:21:18 +10:00
userId: number;
term?: string;
2023-08-24 16:50:40 +10:00
status?: ExtendedDocumentStatus;
2023-06-09 18:21:18 +10:00
page?: number;
perPage?: number;
orderBy?: {
column: keyof Omit<Document, 'document'>;
direction: 'asc' | 'desc';
};
period?: PeriodSelectorValue;
2023-10-06 22:54:24 +00:00
};
2023-06-09 18:21:18 +10:00
export const findDocuments = async ({
userId,
term,
2023-08-24 16:50:40 +10:00
status = ExtendedDocumentStatus.ALL,
2023-06-09 18:21:18 +10:00
page = 1,
perPage = 10,
orderBy,
2023-11-03 07:04:11 +05:30
period,
2023-08-29 14:30:57 +10:00
}: FindDocumentsOptions) => {
2023-08-24 16:50:40 +10:00
const user = await prisma.user.findFirstOrThrow({
where: {
id: userId,
},
});
const orderByColumn = orderBy?.column ?? 'createdAt';
2023-06-09 18:21:18 +10:00
const orderByDirection = orderBy?.direction ?? 'desc';
2023-11-03 07:04:11 +05:30
const termFilters = match(term)
.with(P.string.minLength(1), () => {
return {
2023-08-24 16:50:40 +10:00
title: {
contains: term,
mode: 'insensitive',
},
2023-11-03 07:04:11 +05:30
} as const;
})
.otherwise(() => undefined);
2023-08-24 16:50:40 +10:00
const filters = match<ExtendedDocumentStatus, Prisma.DocumentWhereInput>(status)
.with(ExtendedDocumentStatus.ALL, () => ({
OR: [
{
userId,
deletedAt: null,
2023-08-24 16:50:40 +10:00
},
{
status: ExtendedDocumentStatus.COMPLETED,
Recipient: {
some: {
email: user.email,
},
2023-08-24 16:50:40 +10:00
},
},
{
status: ExtendedDocumentStatus.PENDING,
2023-08-24 16:50:40 +10:00
Recipient: {
some: {
email: user.email,
},
},
deletedAt: null,
2023-08-24 16:50:40 +10:00
},
],
}))
.with(ExtendedDocumentStatus.INBOX, () => ({
status: {
not: ExtendedDocumentStatus.DRAFT,
},
Recipient: {
some: {
email: user.email,
signingStatus: SigningStatus.NOT_SIGNED,
role: {
not: RecipientRole.CC,
},
2023-08-24 16:50:40 +10:00
},
},
deletedAt: null,
2023-08-24 16:50:40 +10:00
}))
.with(ExtendedDocumentStatus.DRAFT, () => ({
userId,
status: ExtendedDocumentStatus.DRAFT,
deletedAt: null,
2023-08-24 16:50:40 +10:00
}))
.with(ExtendedDocumentStatus.PENDING, () => ({
OR: [
{
userId,
status: ExtendedDocumentStatus.PENDING,
deletedAt: null,
2023-08-24 16:50:40 +10:00
},
{
status: ExtendedDocumentStatus.PENDING,
Recipient: {
some: {
email: user.email,
signingStatus: SigningStatus.SIGNED,
role: {
not: RecipientRole.CC,
},
2023-08-24 16:50:40 +10:00
},
},
deletedAt: null,
2023-08-24 16:50:40 +10:00
},
],
}))
.with(ExtendedDocumentStatus.COMPLETED, () => ({
OR: [
{
userId,
status: ExtendedDocumentStatus.COMPLETED,
deletedAt: null,
2023-08-24 16:50:40 +10:00
},
{
status: ExtendedDocumentStatus.COMPLETED,
Recipient: {
some: {
email: user.email,
},
},
},
],
}))
.exhaustive();
2023-06-09 18:21:18 +10:00
2023-11-03 07:04:11 +05:30
const whereClause = {
...termFilters,
...filters,
};
if (period) {
const daysAgo = parseInt(period.replace(/d$/, ''), 10);
const startOfPeriod = DateTime.now().minus({ days: daysAgo }).startOf('day');
whereClause.createdAt = {
gte: startOfPeriod.toJSDate(),
};
}
2023-06-09 18:21:18 +10:00
const [data, count] = await Promise.all([
prisma.document.findMany({
2023-11-03 07:04:11 +05:30
where: whereClause,
2023-06-09 18:21:18 +10:00
skip: Math.max(page - 1, 0) * perPage,
take: perPage,
orderBy: {
[orderByColumn]: orderByDirection,
},
include: {
2023-08-24 16:50:40 +10:00
User: {
select: {
id: true,
name: true,
email: true,
},
},
Recipient: true,
},
2023-06-09 18:21:18 +10:00
}),
prisma.document.count({
where: {
2023-08-24 16:50:40 +10:00
...termFilters,
2023-06-09 18:21:18 +10:00
...filters,
},
}),
]);
const maskedData = data.map((document) =>
maskRecipientTokensForDocument({
document,
user,
}),
);
2023-06-09 18:21:18 +10:00
return {
data: maskedData,
2023-06-09 18:21:18 +10:00
count,
currentPage: Math.max(page, 1),
perPage,
totalPages: Math.ceil(count / perPage),
} satisfies FindResultSet<typeof data>;
2023-06-09 18:21:18 +10:00
};