Files
sign/packages/lib/server-only/document/get-document-by-token.ts

71 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-08-17 19:56:18 +10:00
import { prisma } from '@documenso/prisma';
2023-09-20 13:48:30 +10:00
import { DocumentWithRecipient } from '@documenso/prisma/types/document-with-recipient';
2023-08-17 19:56:18 +10:00
export interface GetDocumentAndSenderByTokenOptions {
token: string;
}
2023-09-20 13:48:30 +10:00
export interface GetDocumentAndRecipientByTokenOptions {
token: string;
}
2023-08-17 19:56:18 +10:00
export const getDocumentAndSenderByToken = async ({
token,
}: GetDocumentAndSenderByTokenOptions) => {
2023-09-20 13:48:30 +10:00
if (!token) {
throw new Error('Missing token');
}
2023-08-17 19:56:18 +10:00
const result = await prisma.document.findFirstOrThrow({
where: {
Recipient: {
some: {
token,
},
},
},
include: {
User: true,
documentData: true,
2023-08-17 19:56:18 +10:00
},
});
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
const { password: _password, ...User } = result.User;
return {
...result,
User,
};
};
2023-09-20 13:48:30 +10:00
/**
* Get a Document and a Recipient by the recipient token.
*/
export const getDocumentAndRecipientByToken = async ({
token,
}: GetDocumentAndRecipientByTokenOptions): Promise<DocumentWithRecipient> => {
if (!token) {
throw new Error('Missing token');
}
const result = await prisma.document.findFirstOrThrow({
where: {
Recipient: {
some: {
token,
},
},
},
include: {
Recipient: true,
documentData: true,
},
});
return {
...result,
Recipient: result.Recipient[0],
};
};