Files
documenso/packages/lib/server-only/document/get-document-by-id.ts
David Nguyen e1464ac2d3 fix: wip
2025-08-22 22:30:02 +10:00

60 lines
1.2 KiB
TypeScript

import { prisma } from '@documenso/prisma';
import { AppError, AppErrorCode } from '../../errors/app-error';
import { getEnvelopeWhereInput } from '../envelope/get-envelope-by-id';
export type GetDocumentByIdOptions = {
documentId: number;
userId: number;
teamId: number;
};
export const getDocumentById = async ({ documentId, userId, teamId }: GetDocumentByIdOptions) => {
const { documentWhereInput } = await getEnvelopeWhereInput({
id: {
type: 'documentId',
id: documentId,
},
validatedUserId: userId,
unvalidatedTeamId: teamId,
});
const envelope = 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 (!envelope) {
throw new AppError(AppErrorCode.NOT_FOUND, {
message: 'Document could not be found',
});
}
return document;
};