<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[DocuMantra Engineering Notes]]></title><description><![CDATA[DocuMantra Engineering Notes]]></description><link>https://documantra.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 04 Dec 2026 22:29:33 GMT</lastBuildDate><atom:link href="https://documantra.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Model Your Signing Workflow as a State Machine: Reminders, Expiry and Polling Without Surprises]]></title><description><![CDATA[A practical pattern for tracking document-signing requests in your own app, so nothing sits unsigned forever and nothing gets sent twice]]></description><link>https://documantra.hashnode.dev/model-your-signing-workflow-as-a-state-machine-reminders-expiry-and-polling-without-surprises</link><guid isPermaLink="true">https://documantra.hashnode.dev/model-your-signing-workflow-as-a-state-machine-reminders-expiry-and-polling-without-surprises</guid><category><![CDATA[software architecture]]></category><category><![CDATA[state machine]]></category><category><![CDATA[backend]]></category><category><![CDATA[Workflow Automation]]></category><category><![CDATA[Node.js]]></category><dc:creator><![CDATA[DocuMantra]]></dc:creator><pubDate>Tue, 01 Dec 2026 05:50:57 GMT</pubDate><content:encoded><![CDATA[<p>If your product sends contracts, offer letters or vendor forms out for signature, you will eventually get the same support tickets: "Did the client sign?", "Why did they get the email twice?", "The link from last month still works, is that a problem?" Almost all of them come from treating a signing request as a boolean (<code>signed: true/false</code>) instead of what it really is: a small, long-running process with several states.</p>
<p>This post walks through a state-machine approach that works with any signing provider, and keeps your database as the source of truth.</p>
<h2>1. Name the states explicitly</h2>
<p>A signing request usually moves through a handful of states:</p>
<pre><code class="language-plaintext">DRAFT -&gt; SENT -&gt; PARTIALLY_SIGNED -&gt; COMPLETED
           \            \
            -&gt; DECLINED  -&gt; EXPIRED
            -&gt; CANCELLED
</code></pre>
<p>Store the state in its own column with an allowed list of values, not as a set of flags. <code>COMPLETED</code>, <code>DECLINED</code>, <code>EXPIRED</code> and <code>CANCELLED</code> are terminal: once there, the record never moves again. That one rule removes a whole class of bugs, like a reminder job emailing someone about a document that was cancelled yesterday.</p>
<pre><code class="language-ts">type SignState =
  | "DRAFT" | "SENT" | "PARTIALLY_SIGNED"
  | "COMPLETED" | "DECLINED" | "EXPIRED" | "CANCELLED";

const allowed: Record&lt;SignState, SignState[]&gt; = {
  DRAFT: ["SENT", "CANCELLED"],
  SENT: ["PARTIALLY_SIGNED", "COMPLETED", "DECLINED", "EXPIRED", "CANCELLED"],
  PARTIALLY_SIGNED: ["COMPLETED", "DECLINED", "EXPIRED", "CANCELLED"],
  COMPLETED: [], DECLINED: [], EXPIRED: [], CANCELLED: [],
};

function transition(from: SignState, to: SignState): SignState {
  if (!allowed[from].includes(to)) {
    throw new Error(`Illegal transition ${from} -&gt; ${to}`);
  }
  return to;
}
</code></pre>
<p>Do the transition inside the same database transaction that writes the new state, with a <code>WHERE state = :from</code> guard. If two workers race, one update affects zero rows and simply stops.</p>
<h2>2. Make "send" idempotent</h2>
<p>Duplicate sends are the most visible failure: the signer gets two emails, signs one, and the other stays open. Generate an idempotency key when the user clicks <em>Send</em> (for example <code>contractId + version</code>) and store it with the request. Before calling the provider, check whether a request with that key already exists. If the provider call times out, look the request up again instead of retrying blindly.</p>
<p>Sending a corrected document should be a new version with a new key, and the old request should move to <code>CANCELLED</code> first.</p>
<h2>3. Reminders are a schedule, not a loop</h2>
<p>A reminder is not "send again". It is a nudge about the <em>same</em> request. Store <code>next_reminder_at</code> and <code>reminders_sent</code> on the row, and let one scheduled job pick up due rows:</p>
<pre><code class="language-sql">SELECT id FROM sign_requests
WHERE state IN ('SENT', 'PARTIALLY_SIGNED')
  AND next_reminder_at &lt;= now()
  AND reminders_sent &lt; 3
FOR UPDATE SKIP LOCKED
LIMIT 100;
</code></pre>
<p>After each reminder, push <code>next_reminder_at</code> forward (2 days, then 4, then 7 works well) and increment the counter. Cap it: three polite reminders are useful, ten are spam.</p>
<p>If your provider has its own reminder feature, decide which side owns reminders and switch the other off. Two systems nudging the same signer is a common source of complaints. For one example of how a provider handles this, see this note on <a href="https://documantra.in/blog/esignature-reminders-and-expiration">how reminders and expiry work on unsigned envelopes</a>.</p>
<h2>4. Expiry closes the loop</h2>
<p>Every request should have an <code>expires_at</code>. Stale signing links are a risk: a document signed two months late may reflect terms nobody agreed to anymore. The expiry job is the same pattern as reminders: select open rows where <code>expires_at &lt;= now()</code>, transition them to <code>EXPIRED</code>, and tell the sender so they can decide whether to send a fresh version.</p>
<h2>5. Poll on a schedule, and reconcile</h2>
<p>Not every integration pushes status changes to you, and even push-based ones can drop events. A small reconciliation job that polls open requests is cheap insurance:</p>
<ul>
<li>Poll only non-terminal rows, oldest <code>last_checked_at</code> first.</li>
<li>Back off for requests that have not changed in days.</li>
<li>Map the provider's status onto your states through <code>transition()</code>, so an unexpected value raises an error instead of silently corrupting data.</li>
<li>When a request reaches <code>COMPLETED</code>, download the signed PDF and the provider's audit trail and store them with a checksum.</li>
</ul>
<h2>6. Keep an event log</h2>
<p>Alongside the current state, append every change to a <code>sign_request_events</code> table: who, what, when, old state, new state. When someone asks "when exactly did the vendor sign?", you answer with a query, not a guess.</p>
<h2>A note on India</h2>
<p>Under the Information Technology Act, 2000, electronic signatures are recognised for most documents, but some, such as wills, powers of attorney and contracts for the sale of immovable property, are excluded. Build a document-type check into the <code>DRAFT -&gt; SENT</code> transition so excluded types never go out for e-signature by mistake, and confirm the list with your legal team.</p>
<h2>Wrapping up</h2>
<p>Explicit states, guarded transitions, idempotent sends, capped reminders, hard expiry and a reconciliation poller. None of it is complicated, but together it turns "did they sign?" from a support ticket into a dashboard.</p>
<p><em>The DocuMantra team</em></p>
]]></content:encoded></item></channel></rss>