chore: remove unnecessary constant extraction and defensive optional chaining

This commit is contained in:
ephraimduncan
2026-03-04 17:08:10 +00:00
parent 77e463e850
commit 5ce4b59f52
9 changed files with 171 additions and 164 deletions
@@ -1,7 +1,8 @@
import { EnvelopeType } from '@prisma/client';
import { DocumentStatus, EnvelopeType } from '@prisma/client';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import { getEnvelopeWhereInput } from '@documenso/lib/server-only/envelope/get-envelope-by-id';
import { isPublicDocumentAccessEnabled } from '@documenso/lib/universal/document-access';
import { prisma } from '@documenso/prisma';
import { maybeAuthenticatedProcedure } from '../trpc';
@@ -62,15 +63,15 @@ const handleGetEnvelopeItemsByToken = async ({
envelopeId: string;
token: string;
}) => {
const isQrToken = token.startsWith('qr_');
const envelope = await prisma.envelope.findFirst({
where: {
id: envelopeId,
type: EnvelopeType.DOCUMENT, // You cannot get template envelope items by token.
recipients: {
some: {
token,
},
},
type: EnvelopeType.DOCUMENT,
...(isQrToken
? { qrToken: token, status: DocumentStatus.COMPLETED }
: { recipients: { some: { token } } }),
},
include: {
envelopeItems: {
@@ -78,6 +79,20 @@ const handleGetEnvelopeItemsByToken = async ({
documentData: true,
},
},
team: {
include: {
teamGlobalSettings: {
select: { allowPublicCompletedDocumentAccess: true },
},
organisation: {
include: {
organisationGlobalSettings: {
select: { allowPublicCompletedDocumentAccess: true },
},
},
},
},
},
},
});
@@ -87,6 +102,12 @@ const handleGetEnvelopeItemsByToken = async ({
});
}
if (isQrToken && !isPublicDocumentAccessEnabled(envelope.team)) {
throw new AppError(AppErrorCode.UNAUTHORIZED, {
message: 'Public completed-document access is disabled',
});
}
return {
envelopeItems: envelope.envelopeItems,
};