This commit is contained in:
David Nguyen
2025-08-22 22:30:02 +10:00
parent e7e2aa9bd8
commit e1464ac2d3
17 changed files with 787 additions and 258 deletions

View File

@ -1,39 +1,32 @@
import type { Prisma } from '@prisma/client';
import { DocumentStatus, TeamMemberRole } from '@prisma/client';
import { match } from 'ts-pattern';
import { prisma } from '@documenso/prisma';
import { AppError, AppErrorCode } from '../../errors/app-error';
import { DocumentVisibility } from '../../types/document-visibility';
import { getTeamById } from '../team/get-team';
import { getEnvelopeWhereInput } from '../envelope/get-envelope-by-id';
export type GetDocumentByIdOptions = {
documentId: number;
userId: number;
teamId: number;
folderId?: string;
};
export const getDocumentById = async ({
documentId,
userId,
teamId,
folderId,
}: GetDocumentByIdOptions) => {
const { documentWhereInput } = await getDocumentWhereInput({
documentId,
userId,
teamId,
export const getDocumentById = async ({ documentId, userId, teamId }: GetDocumentByIdOptions) => {
const { documentWhereInput } = await getEnvelopeWhereInput({
id: {
type: 'documentId',
id: documentId,
},
validatedUserId: userId,
unvalidatedTeamId: teamId,
});
const document = await prisma.document.findFirst({
where: {
...documentWhereInput,
folderId,
},
const envelope = await prisma.envelope.findFirst({
where: documentWhereInput,
include: {
documentData: true,
documents: {
include: {
documentData: true,
},
},
documentMeta: true,
user: {
select: {
@ -56,7 +49,7 @@ export const getDocumentById = async ({
},
});
if (!document) {
if (!envelope) {
throw new AppError(AppErrorCode.NOT_FOUND, {
message: 'Document could not be found',
});
@ -64,93 +57,3 @@ export const getDocumentById = async ({
return document;
};
export type GetDocumentWhereInputOptions = {
documentId: number;
userId: number;
teamId: number;
};
/**
* Generate the where input for a given Prisma document query.
*
* This will return a query that allows a user to get a document if they have valid access to it.
*/
export const getDocumentWhereInput = async ({
documentId,
userId,
teamId,
}: GetDocumentWhereInputOptions) => {
const team = await getTeamById({ teamId, userId });
const user = await prisma.user.findFirstOrThrow({
where: {
id: userId,
},
});
const teamVisibilityFilters = match(team.currentTeamRole)
.with(TeamMemberRole.ADMIN, () => [
DocumentVisibility.EVERYONE,
DocumentVisibility.MANAGER_AND_ABOVE,
DocumentVisibility.ADMIN,
])
.with(TeamMemberRole.MANAGER, () => [
DocumentVisibility.EVERYONE,
DocumentVisibility.MANAGER_AND_ABOVE,
])
.otherwise(() => [DocumentVisibility.EVERYONE]);
const documentOrInput: Prisma.DocumentWhereInput[] = [
// Allow access if they own the document.
{
userId,
},
// Or, if they belong to the team that the document is associated with.
{
visibility: {
in: teamVisibilityFilters,
},
teamId: team.id,
},
// Or, if they are a recipient of the document.
{
status: {
not: DocumentStatus.DRAFT,
},
recipients: {
some: {
email: user.email,
},
},
},
];
// Allow access to documents sent to or from the team email.
if (team.teamEmail) {
documentOrInput.push(
{
recipients: {
some: {
email: team.teamEmail.email,
},
},
},
{
user: {
email: team.teamEmail.email,
},
},
);
}
const documentWhereInput: Prisma.DocumentWhereUniqueInput = {
id: documentId,
OR: documentOrInput,
};
return {
documentWhereInput,
team,
};
};

View File

@ -0,0 +1,157 @@
import type { Prisma } from '@prisma/client';
import { TeamMemberRole } from '@prisma/client';
import { match } from 'ts-pattern';
import { prisma } from '@documenso/prisma';
import { AppError, AppErrorCode } from '../../errors/app-error';
import { DocumentVisibility } from '../../types/document-visibility';
import type { EnvelopeIdOptions } from '../../utils/envelope';
import { buildEnvelopeIdQuery } from '../../utils/envelope';
import { getTeamById } from '../team/get-team';
export type GetEnvelopeByIdOptions = {
id: EnvelopeIdOptions;
userId: number;
teamId: number;
};
export const getEnvelopeById = async ({ id, userId, teamId }: GetEnvelopeByIdOptions) => {
const { documentWhereInput } = await getEnvelopeWhereInput({
id,
validatedUserId: userId,
unvalidatedTeamId: teamId,
});
const document = await prisma.envelope.findFirst({
where: documentWhereInput,
include: {
documents: {
include: {
documentData: true,
},
},
documentMeta: true,
user: {
select: {
id: true,
name: true,
email: true,
},
},
recipients: {
select: {
email: true,
},
},
team: {
select: {
id: true,
url: true,
},
},
},
});
if (!document) {
throw new AppError(AppErrorCode.NOT_FOUND, {
message: 'Document could not be found',
});
}
return document;
};
export type GetEnvelopeWhereInputOptions = {
id: EnvelopeIdOptions;
/**
* The user ID who has been authenticated.
*/
validatedUserId: number;
/**
* The unknown teamId from the request.
*/
unvalidatedTeamId: number;
};
/**
* Generate the where input for a given Prisma envelope query.
*
* This will return a query that allows a user to get a document if they have valid access to it.
*/
export const getEnvelopeWhereInput = async ({
id,
validatedUserId,
unvalidatedTeamId,
}: GetEnvelopeWhereInputOptions) => {
const team = await getTeamById({ teamId: unvalidatedTeamId, userId: validatedUserId });
const teamVisibilityFilters = match(team.currentTeamRole)
.with(TeamMemberRole.ADMIN, () => [
DocumentVisibility.EVERYONE,
DocumentVisibility.MANAGER_AND_ABOVE,
DocumentVisibility.ADMIN,
])
.with(TeamMemberRole.MANAGER, () => [
DocumentVisibility.EVERYONE,
DocumentVisibility.MANAGER_AND_ABOVE,
])
.otherwise(() => [DocumentVisibility.EVERYONE]);
const documentOrInput: Prisma.EnvelopeWhereInput[] = [
// Allow access if they own the document.
{
userId: validatedUserId,
},
// Or, if they belong to the team that the document is associated with.
{
visibility: {
in: teamVisibilityFilters,
},
teamId: team.id,
},
// Or, if they are a recipient of the document.
// ????????????? should recipients be able to do X?
// {
// status: {
// not: DocumentStatus.DRAFT,
// },
// recipients: {
// some: {
// email: user.email,
// },
// },
// },
];
// Allow access to documents sent to or from the team email.
if (team.teamEmail) {
documentOrInput.push(
{
recipients: {
some: {
email: team.teamEmail.email,
},
},
},
{
user: {
email: team.teamEmail.email,
},
},
);
}
const documentWhereInput: Prisma.EnvelopeWhereUniqueInput = {
...buildEnvelopeIdQuery(id),
OR: documentOrInput,
};
return {
documentWhereInput,
team,
};
};

View File

@ -1,4 +1,4 @@
import { DocumentStatus } from '@prisma/client';
import { DocumentStatus, EnvelopeType } from '@prisma/client';
import { prisma } from '@documenso/prisma';
@ -25,9 +25,10 @@ export const deleteUser = async ({ id }: DeleteUserOptions) => {
const serviceAccount = await deletedAccountServiceAccount();
// TODO: Send out cancellations for all pending docs
await prisma.document.updateMany({
await prisma.envelope.updateMany({
where: {
userId: user.id,
type: EnvelopeType.DOCUMENT,
status: {
in: [DocumentStatus.PENDING, DocumentStatus.REJECTED, DocumentStatus.COMPLETED],
},

View File

@ -1,4 +1,4 @@
import { Prisma } from '@prisma/client';
import { EnvelopeType, Prisma } from '@prisma/client';
import { prisma } from '@documenso/prisma';
@ -34,12 +34,20 @@ export const findUsers = async ({
const [users, count] = await Promise.all([
prisma.user.findMany({
include: {
documents: {
select: {
_count: {
select: {
id: true,
envelopes: {
where: {
type: EnvelopeType.DOCUMENT,
},
},
},
},
id: true,
name: true,
email: true,
roles: true,
},
where: whereClause,
skip: Math.max(page - 1, 0) * perPage,
@ -51,7 +59,10 @@ export const findUsers = async ({
]);
return {
users,
users: users.map((user) => ({
...user,
documentCount: user._count.envelopes,
})),
totalPages: Math.ceil(count / perPage),
};
};