Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: force signature fields for document signers #1139

Merged
merged 16 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/lib/server-only/document/get-document-by-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export const getDocumentById = async ({ id, userId, teamId }: GetDocumentByIdOpt
url: true,
},
},
Recipient: {
select: {
email: true,
role: true,
},
},
},
});
};
Expand Down
22 changes: 22 additions & 0 deletions packages/trpc/server/field-router/router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TRPCError } from '@trpc/server';

import { AppError } from '@documenso/lib/errors/app-error';
import { getDocumentById } from '@documenso/lib/server-only/document/get-document-by-id';
import { removeSignedFieldWithToken } from '@documenso/lib/server-only/field/remove-signed-field-with-token';
import { setFieldsForDocument } from '@documenso/lib/server-only/field/set-fields-for-document';
import { setFieldsForTemplate } from '@documenso/lib/server-only/field/set-fields-for-template';
Expand All @@ -22,6 +23,27 @@ export const fieldRouter = router({
try {
const { documentId, fields } = input;

const document = await getDocumentById({
id: documentId,
userId: ctx.user.id,
}).catch(() => null);

const everySignerHasSignature = document?.Recipient.every(
(recipient) =>
recipient.role !== 'SIGNER' ||
fields.some(
(field) => field.type === 'SIGNATURE' && field.signerEmail === recipient.email,
),
);

if (!everySignerHasSignature) {
throw new TRPCError({
code: 'BAD_REQUEST',
message:
'Some signers have not been assigned a signature field. Please assign a signature field to each signer before proceeding.',
catalinpit marked this conversation as resolved.
Show resolved Hide resolved
});
}

return await setFieldsForDocument({
documentId,
userId: ctx.user.id,
Expand Down
Loading