mirror of
https://github.com/documenso/documenso.git
synced 2025-11-12 15:53:02 +10:00
feat: add inbox
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import { prisma } from '@documenso/prisma';
|
||||
import { Document, DocumentStatus, Prisma } from '@documenso/prisma/client';
|
||||
import { Document, DocumentStatus, Prisma, SigningStatus } from '@documenso/prisma/client';
|
||||
import { DocumentWithRecipientAndSender } from '@documenso/prisma/types/document';
|
||||
import { DocumentWithReciepient } from '@documenso/prisma/types/document-with-recipient';
|
||||
|
||||
import { FindResultSet } from '../../types/find-result-set';
|
||||
@ -68,3 +69,111 @@ export const findDocuments = async ({
|
||||
totalPages: Math.ceil(count / perPage),
|
||||
};
|
||||
};
|
||||
|
||||
export interface FindDocumentsWithRecipientAndSenderOptions {
|
||||
email: string;
|
||||
query?: string;
|
||||
signingStatus?: SigningStatus;
|
||||
page?: number;
|
||||
perPage?: number;
|
||||
orderBy?: {
|
||||
column: keyof Omit<Document, 'document'>;
|
||||
direction: 'asc' | 'desc';
|
||||
};
|
||||
}
|
||||
|
||||
export const findDocumentsWithRecipientAndSender = async ({
|
||||
email,
|
||||
query,
|
||||
signingStatus,
|
||||
page = 1,
|
||||
perPage = 20,
|
||||
orderBy,
|
||||
}: FindDocumentsWithRecipientAndSenderOptions): Promise<
|
||||
FindResultSet<DocumentWithRecipientAndSender>
|
||||
> => {
|
||||
const orderByColumn = orderBy?.column ?? 'created';
|
||||
const orderByDirection = orderBy?.direction ?? 'desc';
|
||||
|
||||
const filters: Prisma.DocumentWhereInput = {
|
||||
Recipient: {
|
||||
some: {
|
||||
email,
|
||||
signingStatus,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
if (query) {
|
||||
filters.OR = [
|
||||
{
|
||||
User: {
|
||||
email: {
|
||||
contains: query,
|
||||
mode: 'insensitive',
|
||||
},
|
||||
},
|
||||
// Todo: Add filter for `Subject`.
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
const [data, count] = await Promise.all([
|
||||
prisma.document.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
created: true,
|
||||
title: true,
|
||||
status: true,
|
||||
userId: true,
|
||||
User: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
Recipient: {
|
||||
where: {
|
||||
email,
|
||||
signingStatus,
|
||||
},
|
||||
},
|
||||
},
|
||||
where: {
|
||||
...filters,
|
||||
},
|
||||
skip: Math.max(page - 1, 0) * perPage,
|
||||
take: perPage,
|
||||
orderBy: {
|
||||
[orderByColumn]: orderByDirection,
|
||||
},
|
||||
}),
|
||||
prisma.document.count({
|
||||
where: {
|
||||
...filters,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
data: data.map((item) => {
|
||||
const { User, Recipient, ...rest } = item;
|
||||
|
||||
const subject = undefined; // Todo.
|
||||
const description = undefined; // Todo.
|
||||
|
||||
return {
|
||||
...rest,
|
||||
sender: User,
|
||||
recipient: Recipient[0],
|
||||
subject: subject ?? 'Please sign this document',
|
||||
description: description ?? `${User.name} has invited you to sign "${item.title}"`,
|
||||
};
|
||||
}),
|
||||
count,
|
||||
currentPage: Math.max(page, 1),
|
||||
perPage,
|
||||
totalPages: Math.ceil(count / perPage),
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user