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

182 lines
3.7 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: {
Recipient: {
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: {
Recipient: {
some: {
token,
},
},
},
include: {
User: true,
documentData: true,
documentMeta: true,
2024-03-28 13:13:29 +08:00
Recipient: {
where: {
token,
},
},
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;
2024-03-28 13:13:29 +08:00
const recipient = result.Recipient[0];
// 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,
User,
};
};
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: {
Recipient: {
some: {
token,
},
},
},
include: {
2024-01-22 12:32:19 +11:00
Recipient: {
where: {
token,
},
},
2023-09-20 13:48:30 +10:00
documentData: true,
},
});
2024-09-04 23:13:00 +10:00
const [recipient] = result.Recipient;
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-09-20 13:48:30 +10:00
return {
...result,
Recipient: result.Recipient,
2023-09-20 13:48:30 +10:00
};
};