mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
The searchDocuments function is used for the shortcuts commands, afaik.
The function returns the documents that match the user query (if any),
alongside all their recipients.
The reason for that is so it can build the path for the document. E.g.
if you're the document owner, the document path will be
`..../documents/{id}`. But if you're a signer for example, the document
path (link) will be `..../sign/{token}`.
So instead of doing that on the frontend, I moved it to the backend.
At least that's what I understood. If I'm wrong, please correct me.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
- **New Features**
- Enhanced the `CommandMenu` component to simplify search result
generation and improve document link management based on user roles.
- **Refactor**
- Updated document search logic to include recipient token masking and
refined document mapping.
- **Style**
- Minor formatting improvement in document routing code.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
97 lines
2.3 KiB
TypeScript
97 lines
2.3 KiB
TypeScript
import { prisma } from '@documenso/prisma';
|
|
import { DocumentStatus } from '@documenso/prisma/client';
|
|
import type { Document, Recipient, User } from '@documenso/prisma/client';
|
|
|
|
export type SearchDocumentsWithKeywordOptions = {
|
|
query: string;
|
|
userId: number;
|
|
limit?: number;
|
|
};
|
|
|
|
export const searchDocumentsWithKeyword = async ({
|
|
query,
|
|
userId,
|
|
limit = 5,
|
|
}: SearchDocumentsWithKeywordOptions) => {
|
|
const user = await prisma.user.findFirstOrThrow({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
});
|
|
|
|
const documents = await prisma.document.findMany({
|
|
where: {
|
|
OR: [
|
|
{
|
|
title: {
|
|
contains: query,
|
|
mode: 'insensitive',
|
|
},
|
|
userId: userId,
|
|
deletedAt: null,
|
|
},
|
|
{
|
|
Recipient: {
|
|
some: {
|
|
email: {
|
|
contains: query,
|
|
mode: 'insensitive',
|
|
},
|
|
},
|
|
},
|
|
userId: userId,
|
|
deletedAt: null,
|
|
},
|
|
{
|
|
status: DocumentStatus.COMPLETED,
|
|
Recipient: {
|
|
some: {
|
|
email: user.email,
|
|
},
|
|
},
|
|
title: {
|
|
contains: query,
|
|
mode: 'insensitive',
|
|
},
|
|
},
|
|
{
|
|
status: DocumentStatus.PENDING,
|
|
Recipient: {
|
|
some: {
|
|
email: user.email,
|
|
},
|
|
},
|
|
title: {
|
|
contains: query,
|
|
mode: 'insensitive',
|
|
},
|
|
deletedAt: null,
|
|
},
|
|
],
|
|
},
|
|
include: {
|
|
Recipient: true,
|
|
},
|
|
orderBy: {
|
|
createdAt: 'desc',
|
|
},
|
|
take: limit,
|
|
});
|
|
|
|
const isOwner = (document: Document, user: User) => document.userId === user.id;
|
|
const getSigningLink = (recipients: Recipient[], user: User) =>
|
|
`/sign/${recipients.find((r) => r.email === user.email)?.token}`;
|
|
|
|
const maskedDocuments = documents.map((document) => {
|
|
const { Recipient, ...documentWithoutRecipient } = document;
|
|
|
|
return {
|
|
...documentWithoutRecipient,
|
|
path: isOwner(document, user) ? `/documents/${document.id}` : getSigningLink(Recipient, user),
|
|
value: [document.id, document.title, ...document.Recipient.map((r) => r.email)].join(' '),
|
|
};
|
|
});
|
|
|
|
return maskedDocuments;
|
|
};
|