The Open Source DocuSign Alternative.
TypeScript digital-signaturedocument-signingdocusign-alternativee-signatureesignesignaturenext-authnextjsopen-sourcepades-standardpdfpdf-signpdf-signaturepostgresqlprismaself-hostedsigningtypescript
♻️ 🧹 upsert reciient, deleteRecipient, send
Timur Ercan10b81bc7577f
diff --git a/apps/web/pages/api/documents/[id]/fields/index.ts b/apps/web/pages/api/documents/[id]/fields/index.tsindex 9a4633948..4497bdc56 100644--- a/apps/web/pages/api/documents/[id]/fields/index.ts+++ b/apps/web/pages/api/documents/[id]/fields/index.ts@@ -25,7 +25,7 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {return;}- // todo encapsulate entity ownerships checks+ // todo entity ownerships checksconst fields = await prisma.field.findMany({where: { documentId: +documentId },@@ -57,7 +57,7 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {const document: PrismaDocument = await getDocument(+documentId, req, res);- // todo encapsulate entity ownerships checks+ // todo entity ownerships checksif (document.userId !== user.id) {return res.status(401).send("User does not have access to this document.");}diff --git a/apps/web/pages/api/documents/[id]/recipients/index.ts b/apps/web/pages/api/documents/[id]/recipients/index.tsindex b79e46739..f2243e7aa 100644--- a/apps/web/pages/api/documents/[id]/recipients/index.ts+++ b/apps/web/pages/api/documents/[id]/recipients/index.ts@@ -23,7 +23,7 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {const document: PrismaDocument = await getDocument(+documentId, req, res);- // todo encapsulate entity ownerships checks+ // todo entity ownerships checksif (document.userId !== user.id) {return res.status(401).send("User does not have access to this document.");}diff --git a/apps/web/pages/documents.tsx b/apps/web/pages/documents.tsxindex 8ffb9639d..148efc914 100644--- a/apps/web/pages/documents.tsx+++ b/apps/web/pages/documents.tsx@@ -351,7 +351,6 @@ const DocumentsPage: NextPageWithLayout = (props: any) => {const removedItem: any =documentsWithoutIndex.splice(index, 1);setDocuments(documentsWithoutIndex);- // todo encapsulatedeleteDocument(document.id).catch((err) => {documentsWithoutIndex.splice(diff --git a/apps/web/pages/documents/[id]/recipients.tsx b/apps/web/pages/documents/[id]/recipients.tsxindex 42447d809..549b4407e 100644--- a/apps/web/pages/documents/[id]/recipients.tsx+++ b/apps/web/pages/documents/[id]/recipients.tsx@@ -17,8 +17,12 @@ import { getUserFromToken } from "@documenso/lib/server";import { getDocument } from "@documenso/lib/query";import { Document as PrismaDocument } from "@prisma/client";import { Breadcrumb, Button, IconButton } from "@documenso/ui";-import toast from "react-hot-toast";import { Dialog, Transition } from "@headlessui/react";+import {+ createOrUpdateRecipient,+ deleteRecipient,+ sendSigningRequests,+} from "@documenso/lib/api";const RecipientsPage: NextPageWithLayout = (props: any) => {const title: string =@@ -138,10 +142,11 @@ const RecipientsPage: NextPageWithLayout = (props: any) => {}}onBlur={() => {item.documentId = props.document.id;- upsertRecipient(item);+ createOrUpdateRecipient(item);}}onKeyDown={(event: any) => {- if (event.key === "Enter") upsertRecipient(item);+ if (event.key === "Enter")+ createOrUpdateRecipient(item);}}className="block w-full border-0 p-0 text-gray-900 placeholder-gray-500 sm:text-sm outline-none bg-inherit"placeholder="[email protected]"@@ -171,10 +176,11 @@ const RecipientsPage: NextPageWithLayout = (props: any) => {}}onBlur={() => {item.documentId = props.document.id;- upsertRecipient(item);+ createOrUpdateRecipient(item);}}onKeyDown={(event: any) => {- if (event.key === "Enter") upsertRecipient(item);+ if (event.key === "Enter")+ createOrUpdateRecipient(item);}}className="block w-full border-0 p-0 text-gray-900 placeholder-gray-500 sm:text-sm outline-none bg-inherit"placeholder="John Dorian"@@ -250,7 +256,9 @@ const RecipientsPage: NextPageWithLayout = (props: any) => {onClick={() => {if (confirm("Resend this signing request?")) {setLoading(true);- send(props.document, [item.id]).finally(() => {+ sendSigningRequests(props.document, [+ item.id,+ ]).finally(() => {setLoading(false);});}@@ -270,7 +278,7 @@ const RecipientsPage: NextPageWithLayout = (props: any) => {1);setSigners(signersWithoutIndex);- deleteRecipient(item).catch((err) => {+ deleteRecipient(item)?.catch((err) => {setSigners(signersWithoutIndex.concat(removedItem));});}}@@ -285,7 +293,7 @@ const RecipientsPage: NextPageWithLayout = (props: any) => {icon={UserPlusIcon}className="mt-3"onClick={() => {- upsertRecipient({+ createOrUpdateRecipient({id: "",email: "",name: "",@@ -365,7 +373,7 @@ const RecipientsPage: NextPageWithLayout = (props: any) => {<ButtononClick={() => {setOpen(false);- send(props.document).finally(() => {+ sendSigningRequests(props.document).finally(() => {setLoading(false);});}}@@ -383,69 +391,6 @@ const RecipientsPage: NextPageWithLayout = (props: any) => {);};-// todo encapsulate-async function deleteRecipient(recipient: any) {- if (!recipient.id) {- return;- }-- return toast.promise(- fetch(- "/api/documents/" + recipient.documentId + "/recipients/" + recipient.id,- {- method: "DELETE",- headers: {- "Content-Type": "application/json",- },- body: JSON.stringify(recipient),- }- ),- {- loading: "Deleting...",- success: "Deleted.",- error: "Could not delete :/",- },- {- id: "delete",- style: {- minWidth: "200px",- },- }- );-}--// todo encapsulate-async function upsertRecipient(recipient: any): Promise<any> {- try {- const created = await toast.promise(- fetch("/api/documents/" + recipient.documentId + "/recipients", {- method: "POST",- headers: {- "Content-Type": "application/json",- },- body: JSON.stringify(recipient),- }).then((res) => {- if (!res.ok) {- throw new Error(res.status.toString());- }- return res.json();- }),- {- loading: "Saving...",- success: "Saved.",- error: "Could not save :/",- },- {- id: "saving",- style: {- minWidth: "200px",- },- }- );- return created;- } catch (error) {}-}-RecipientsPage.getLayout = function getLayout(page: ReactElement) {return <Layout>{page}</Layout>;};@@ -468,33 +413,4 @@ export async function getServerSideProps(context: any) {};}-// todo encapsulate-async function send(document: any, resendTo: number[] = []) {- if (!document || !document.id) return;- try {- const sent = await toast.promise(- fetch(`/api/documents/${document.id}/send`, {- body: JSON.stringify({ resendTo: resendTo }),- headers: { "Content-Type": "application/json" },- method: "POST",- })- .then((res: any) => {- if (!res.ok) {- throw new Error(res.status.toString());- }- })- .finally(() => {- location.reload();- }),- {- loading: "Sending...",- success: `Sent!`,- error: "Could not send :/",- }- );- } catch (err) {- console.log(err);- }-}-export default RecipientsPage;diff --git a/packages/lib/api/createOrUpdateRecipient.ts b/packages/lib/api/createOrUpdateRecipient.tsnew file mode 100644index 000000000..6c1e87b9c--- /dev/null+++ b/packages/lib/api/createOrUpdateRecipient.ts@@ -0,0 +1,32 @@+import toast from "react-hot-toast";++export const createOrUpdateRecipient = async (recipient: any): Promise<any> => {+ try {+ const created = await toast.promise(+ fetch("/api/documents/" + recipient.documentId + "/recipients", {+ method: "POST",+ headers: {+ "Content-Type": "application/json",+ },+ body: JSON.stringify(recipient),+ }).then((res) => {+ if (!res.ok) {+ throw new Error(res.status.toString());+ }+ return res.json();+ }),+ {+ loading: "Saving...",+ success: "Saved.",+ error: "Could not save :/",+ },+ {+ id: "saving",+ style: {+ minWidth: "200px",+ },+ }+ );+ return created;+ } catch (error) {}+};diff --git a/packages/lib/api/deleteRecipient.ts b/packages/lib/api/deleteRecipient.tsnew file mode 100644index 000000000..138cb52c2--- /dev/null+++ b/packages/lib/api/deleteRecipient.ts@@ -0,0 +1,31 @@+import toast from "react-hot-toast";++export const deleteRecipient = (recipient: any) => {+ if (!recipient.id) {+ return;+ }++ return toast.promise(+ fetch(+ "/api/documents/" + recipient.documentId + "/recipients/" + recipient.id,+ {+ method: "DELETE",+ headers: {+ "Content-Type": "application/json",+ },+ body: JSON.stringify(recipient),+ }+ ),+ {+ loading: "Deleting...",+ success: "Deleted.",+ error: "Could not delete :/",+ },+ {+ id: "delete",+ style: {+ minWidth: "200px",+ },+ }+ );+};diff --git a/packages/lib/api/index.ts b/packages/lib/api/index.tsindex 007affcda..b6cca406f 100644--- a/packages/lib/api/index.ts+++ b/packages/lib/api/index.ts@@ -4,4 +4,7 @@ export { signDocument } from "./signDocument";export { getUser } from "./getUser";export { signup } from "./signup";export { getDocuments } from "./getDocuments";-export { deleteDocument } from "./deleteDocument";\ No newline at end of file+export { deleteDocument } from "./deleteDocument";+export { deleteRecipient } from "./deleteRecipient";+export { createOrUpdateRecipient } from "./createOrUpdateRecipient";+export { sendSigningRequests } from "./sendSigningRequests";\ No newline at end of filediff --git a/packages/lib/api/sendSigningRequests.ts b/packages/lib/api/sendSigningRequests.tsnew file mode 100644index 000000000..75ab99d50--- /dev/null+++ b/packages/lib/api/sendSigningRequests.ts@@ -0,0 +1,32 @@+import toast from "react-hot-toast";++export const sendSigningRequests = async (+ document: any,+ resendTo: number[] = []+) => {+ if (!document || !document.id) return;+ try {+ const sent = await toast.promise(+ fetch(`/api/documents/${document.id}/send`, {+ body: JSON.stringify({ resendTo: resendTo }),+ headers: { "Content-Type": "application/json" },+ method: "POST",+ })+ .then((res: any) => {+ if (!res.ok) {+ throw new Error(res.status.toString());+ }+ })+ .finally(() => {+ location.reload();+ }),+ {+ loading: "Sending...",+ success: `Sent!`,+ error: "Could not send :/",+ }+ );+ } catch (err) {+ console.log(err);+ }+};