Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Start a new bulk-send batch when a different list is checked

Matt Van Horn7d0e2b15b687
diff --git a/src/components/templates/bulk-send-shell.tsx b/src/components/templates/bulk-send-shell.tsx
index 966b03d..8a12308 100644
--- a/src/components/templates/bulk-send-shell.tsx
+++ b/src/components/templates/bulk-send-shell.tsx
@@ -15,8 +15,10 @@ import {
DialogTitle,
} from "@/components/ui/dialog";
import {
+ type BulkSendBatchKey,
type BulkSendResult,
type BulkSendRowResult,
+ bulkSendBatchKeyForCheck,
bulkSendPreviewKey,
copyRecipientPairError,
serializeBulkSendResultsCsv,
@@ -127,7 +129,10 @@ export function BulkSendShell({
);
const [copyName, setCopyName] = useState("");
const [copyEmail, setCopyEmail] = useState("");
- const batchKeyRef = useRef(newBulkSendBatchKey());
+ const batchKeyRef = useRef<BulkSendBatchKey>({
+ key: newBulkSendBatchKey(),
+ requestKey: null,
+ });
const [requireEmailCode, setRequireEmailCode] = useState(false);
const [signatureMeaning, setSignatureMeaning] = useState<
SignatureMeaning | ""
@@ -201,7 +206,7 @@ export function BulkSendShell({
const response = await fetch(`/api/templates/${current.id}/bulk-send`, {
body: JSON.stringify({
...request,
- batchKey: confirm ? batchKeyRef.current : undefined,
+ batchKey: confirm ? batchKeyRef.current.key : undefined,
confirm,
permittedUseConfirmed,
}),
@@ -242,6 +247,11 @@ export function BulkSendShell({
try {
const checkedPreview = await postBulkSend(request, false);
+ batchKeyRef.current = bulkSendBatchKeyForCheck(
+ batchKeyRef.current,
+ key,
+ newBulkSendBatchKey,
+ );
setChecked({
key,
preview: checkedPreview,
@@ -286,7 +296,10 @@ export function BulkSendShell({
setResults(sent);
setChecked(null);
setIsConfirming(false);
- batchKeyRef.current = newBulkSendBatchKey();
+ batchKeyRef.current = {
+ key: newBulkSendBatchKey(),
+ requestKey: null,
+ };
} catch (error) {
setActionError(error instanceof Error ? error.message : genericError);
} finally {
@@ -331,7 +344,10 @@ export function BulkSendShell({
</ShellButton>
<ShellButton
onClick={() => {
- batchKeyRef.current = newBulkSendBatchKey();
+ batchKeyRef.current = {
+ key: newBulkSendBatchKey(),
+ requestKey: null,
+ };
setResults(null);
setChecked(null);
setActionError(null);
diff --git a/src/lib/bulk-send-csv.test.ts b/src/lib/bulk-send-csv.test.ts
index c336ddc..3c36a25 100644
--- a/src/lib/bulk-send-csv.test.ts
+++ b/src/lib/bulk-send-csv.test.ts
@@ -3,6 +3,7 @@ import { test } from "node:test";
import {
ambiguousBulkSendColumnsMessage,
+ bulkSendBatchKeyForCheck,
bulkSendCapMessage,
bulkSendPreviewKey,
copyRecipientPairError,
@@ -335,3 +336,20 @@ test("a results CSV records each row without turning a cell into a formula", ()
"a,b\r\n1,2\r\n",
);
});
+
+test("a changed checked list starts a new batch key, a retry keeps it", () => {
+ let next = 0;
+ const newKey = () => `key-${String(++next)}`;
+ const first = bulkSendBatchKeyForCheck(
+ { key: "key-0", requestKey: null },
+ "list-a",
+ newKey,
+ );
+
+ assert.deepEqual(first, { key: "key-0", requestKey: "list-a" });
+ assert.deepEqual(bulkSendBatchKeyForCheck(first, "list-a", newKey), first);
+ assert.deepEqual(bulkSendBatchKeyForCheck(first, "list-b", newKey), {
+ key: "key-1",
+ requestKey: "list-b",
+ });
+});
diff --git a/src/lib/bulk-send-csv.ts b/src/lib/bulk-send-csv.ts
index 4aeabfc..96459ea 100644
--- a/src/lib/bulk-send-csv.ts
+++ b/src/lib/bulk-send-csv.ts
@@ -97,6 +97,27 @@ export function bulkSendPreviewKey(input: {
});
}
+/** The batch key a send will confirm with, and the checked request it is for. */
+export type BulkSendBatchKey = { key: string; requestKey: string | null };
+
+/**
+ * A retry of the same checked request keeps its batch key, so a send that
+ * failed after the server saved the batch does not send twice. Checking a
+ * changed request starts a new batch, since the server refuses a saved key
+ * with a different list.
+ */
+export function bulkSendBatchKeyForCheck(
+ current: BulkSendBatchKey,
+ checkedRequestKey: string,
+ newKey: () => string,
+): BulkSendBatchKey {
+ if (current.requestKey === null || current.requestKey === checkedRequestKey) {
+ return { key: current.key, requestKey: checkedRequestKey };
+ }
+
+ return { key: newKey(), requestKey: checkedRequestKey };
+}
+
/**
* A copy recipient needs both a name and an email, or neither. One filled
* field is not sent as an empty list: the sender has to finish it or clear