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

190 lines
3.9 KiB
TypeScript
Raw Normal View History

2023-08-17 19:56:18 +10:00
import { prisma } from '@documenso/prisma';
2024-01-22 12:32:19 +11:00
import type { DocumentWithRecipient } from '@documenso/prisma/types/document-with-recipient';
2023-08-17 19:56:18 +10:00
2024-03-28 13:13:29 +08:00
import { AppError, AppErrorCode } from '../../errors/app-error';
import type { TDocumentAuthMethods } from '../../types/document-auth';
import { isRecipientAuthorized } from './is-recipient-authorized';
export interface GetDocumentAndSenderByTokenOptions {
token: string;
userId?: number;
accessAuth?: TDocumentAuthMethods;
/**
* Whether we enforce the access requirement.
*
* Defaults to true.
*/
requireAccessAuth?: boolean;
}
export interface GetDocumentAndRecipientByTokenOptions {
token: string;
userId?: number;
accessAuth?: TDocumentAuthMethods;
/**
* Whether we enforce the access requirement.
*
* Defaults to true.
*/
requireAccessAuth?: boolean;
}
2024-03-21 16:15:29 +00:00
export type GetDocumentByTokenOptions = {
2023-08-17 19:56:18 +10:00
token: string;
2024-03-21 16:15:29 +00:00
};
2023-08-17 19:56:18 +10:00
2024-03-21 16:15:29 +00:00
export const getDocumentByToken = async ({ token }: GetDocumentByTokenOptions) => {
if (!token) {
throw new Error('Missing token');
}
const result = await prisma.document.findFirstOrThrow({
where: {
2025-01-13 13:41:53 +11:00
recipients: {
2024-03-21 16:15:29 +00:00
some: {
token,
},
},
},
});
return result;
};
2023-09-20 13:48:30 +10:00
2024-03-28 13:13:29 +08:00
export type DocumentAndSender = Awaited<ReturnType<typeof getDocumentAndSenderByToken>>;
2023-08-17 19:56:18 +10:00
export const getDocumentAndSenderByToken = async ({
token,
2024-03-28 13:13:29 +08:00
userId,
accessAuth,
requireAccessAuth = true,
2023-08-17 19:56:18 +10:00
}: 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: {
2025-01-13 13:41:53 +11:00
recipients: {
2023-08-17 19:56:18 +10:00
some: {
token,
},
},
},
include: {
2025-01-13 13:41:53 +11:00
user: true,
documentData: true,
documentMeta: true,
2025-01-13 13:41:53 +11:00
recipients: {
2024-03-28 13:13:29 +08:00
where: {
token,
},
},
team: {
select: {
name: true,
teamEmail: true,
teamGlobalSettings: {
select: {
includeSenderDetails: true,
},
},
},
},
2023-08-17 19:56:18 +10:00
},
});
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
2025-01-13 13:41:53 +11:00
const { password: _password, ...user } = result.user;
2023-08-17 19:56:18 +10:00
2025-01-13 13:41:53 +11:00
const recipient = result.recipients[0];
2024-03-28 13:13:29 +08:00
// Sanity check, should not be possible.
if (!recipient) {
throw new Error('Missing recipient');
}
let documentAccessValid = true;
if (requireAccessAuth) {
documentAccessValid = await isRecipientAuthorized({
type: 'ACCESS',
documentAuthOptions: result.authOptions,
2024-03-28 13:13:29 +08:00
recipient,
userId,
authOptions: accessAuth,
});
}
if (!documentAccessValid) {
throw new AppError(AppErrorCode.UNAUTHORIZED, {
message: 'Invalid access values',
});
2024-03-28 13:13:29 +08:00
}
2023-08-17 19:56:18 +10:00
return {
...result,
2025-01-13 13:41:53 +11:00
user,
2023-08-17 19:56:18 +10:00
};
};
2023-09-20 13:48:30 +10:00
/**
* Get a Document and a Recipient by the recipient token.
*/
export const getDocumentAndRecipientByToken = async ({
token,
2024-03-28 13:13:29 +08:00
userId,
accessAuth,
requireAccessAuth = true,
2023-09-20 13:48:30 +10:00
}: GetDocumentAndRecipientByTokenOptions): Promise<DocumentWithRecipient> => {
if (!token) {
throw new Error('Missing token');
}
const result = await prisma.document.findFirstOrThrow({
where: {
2025-01-13 13:41:53 +11:00
recipients: {
2023-09-20 13:48:30 +10:00
some: {
token,
},
},
},
include: {
2025-01-13 13:41:53 +11:00
recipients: {
2024-01-22 12:32:19 +11:00
where: {
token,
},
},
2023-09-20 13:48:30 +10:00
documentData: true,
},
});
2025-01-13 13:41:53 +11:00
const [recipient] = result.recipients;
2024-03-28 13:13:29 +08:00
// Sanity check, should not be possible.
if (!recipient) {
throw new Error('Missing recipient');
}
let documentAccessValid = true;
if (requireAccessAuth) {
documentAccessValid = await isRecipientAuthorized({
type: 'ACCESS',
documentAuthOptions: result.authOptions,
2024-03-28 13:13:29 +08:00
recipient,
userId,
authOptions: accessAuth,
});
}
if (!documentAccessValid) {
throw new AppError(AppErrorCode.UNAUTHORIZED, {
message: 'Invalid access values',
});
2024-03-28 13:13:29 +08:00
}
2025-01-13 13:41:53 +11:00
return result;
2023-09-20 13:48:30 +10:00
};