feat: avoid sending document if the owner is the only recipient

This commit is contained in:
Ephraim Atta-Duncan
2024-10-16 17:10:01 +00:00
parent e0c948c2ac
commit f9e3993519
4 changed files with 73 additions and 3 deletions

View File

@ -37,6 +37,7 @@ import {
ZMoveDocumentsToTeamSchema,
ZResendDocumentMutationSchema,
ZSearchDocumentsMutationSchema,
ZSelfSignDocumentMutationSchema,
ZSendDocumentMutationSchema,
ZSetPasswordForDocumentMutationSchema,
ZSetSettingsForDocumentMutationSchema,
@ -367,6 +368,29 @@ export const documentRouter = router({
}
}),
selfSignDocument: authenticatedProcedure
.input(ZSelfSignDocumentMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
const { documentId, teamId } = input;
return await sendDocument({
userId: ctx.user.id,
documentId,
teamId,
sendEmail: false,
requestMetadata: extractNextApiRequestMetadata(ctx.req),
});
} catch (err) {
console.error(err);
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to self sign this document. Please try again later.',
});
}
}),
resendDocument: authenticatedProcedure
.input(ZResendDocumentMutationSchema)
.mutation(async ({ input, ctx }) => {

View File

@ -140,6 +140,11 @@ export const ZSendDocumentMutationSchema = z.object({
}),
});
export const ZSelfSignDocumentMutationSchema = z.object({
documentId: z.number(),
teamId: z.number().optional(),
});
export const ZSetPasswordForDocumentMutationSchema = z.object({
documentId: z.number(),
password: z.string(),

View File

@ -20,6 +20,7 @@ import {
Type,
User,
} from 'lucide-react';
import { useSession } from 'next-auth/react';
import { useFieldArray, useForm } from 'react-hook-form';
import { useHotkeys } from 'react-hotkeys-hook';
import { prop, sortBy } from 'remeda';
@ -99,6 +100,13 @@ export type AddFieldsFormProps = {
teamId?: number;
};
// Self Sign
// If the recipient is the user and it is the only recipient,
// dont send the document, after adding the fields, skip the next step and go straight to the point
// no need for the sending section
// it will create the document
// and redirect you to the signing page.
export const AddFieldsFormPartial = ({
documentFlow,
hideRecipients = false,
@ -110,6 +118,7 @@ export const AddFieldsFormPartial = ({
teamId,
}: AddFieldsFormProps) => {
const { toast } = useToast();
const { data: session } = useSession();
const [isMissingSignatureDialogVisible, setIsMissingSignatureDialogVisible] = useState(false);
@ -530,6 +539,10 @@ export const AddFieldsFormPartial = ({
);
}, [recipientsByRole]);
const hasSameOwnerAsRecipient =
recipientsByRole.SIGNER.length === 1 &&
recipientsByRole.SIGNER[0].email === session?.user?.email;
const handleAdvancedSettings = () => {
setShowAdvancedSettings((prev) => !prev);
};
@ -1067,6 +1080,7 @@ export const AddFieldsFormPartial = ({
documentFlow.onBackStep?.();
}}
goBackLabel={canRenderBackButtonAsRemove ? msg`Remove` : undefined}
goNextLabel={hasSameOwnerAsRecipient ? msg`Self Sign` : undefined}
onGoNextClick={handleGoNextClick}
/>
</DocumentFlowFormContainerFooter>