Sub topic 8
Workflows, automations and logic
This chapter assumes you have a scoped core flow and a data model with an owner field on each record. You should also be comfortable with the idea of a trigger, such as a button click or a form submission.
The working model
A workflow is a chain of steps that runs when something happens. Every workflow has three parts:
- Trigger: the event that starts it, such as a form submit, a new database row, a schedule or an incoming webhook.
- Conditions: checks that decide which path to take.
- Actions: the work itself, such as writing a record, sending an email or calling another service.
Keep the first version small. Build only the workflows your core flow needs. Anything else is out of scope for now.
Where logic should live
Your tools let you put logic in several places. Choose by what the logic protects.
| Place | Good for | Risk |
|---|---|---|
| Screen (UI) | Showing or hiding buttons, form hints | A user can bypass it |
| Workflow or automation | Multi-step actions, emails, calls to other services | Can run twice or fail halfway |
| Database | Access rules, allowed values | Needs some SQL knowledge |
Put rules that protect data in the database. In the TaskNest example, the check constraint on status and the row-level security policies hold no matter which screen or workflow sends the request. Use the screen only for convenience, such as hiding a "done" button on finished tasks.
Writing a reliable automation
Most automation platforms retry failed steps, and webhook senders often deliver the same event more than once. Plan for this.
- Make actions idempotent. Running the step twice should give the same result as running it once. Set a status to
doneinstead of toggling it. - Store an event ID. If an event ID is already recorded, skip the event.
- Verify incoming webhooks. Check the signature on the raw body before you act on the event.
- Handle failure on purpose. Decide what happens when a step fails: retry, notify you, or mark the record as needing attention.
import { createHmac, timingSafeEqual } from "node:crypto";
// Verifies an HMAC-SHA256 signature (hex) sent by a service with a shared secret.
export function isValidSignature(rawBody, signatureHex, secret) {
const expected = createHmac("sha256", secret).update(rawBody).digest();
const received = Buffer.from(signatureHex, "hex");
return received.length === expected.length && timingSafeEqual(received, expected);
}Compute the signature over the raw body, not a parsed and re-serialized copy. Re-serializing can change the bytes and make a valid signature fail.
Common errors
- Infinite loops. A workflow that updates a row can trigger itself. Add a condition that stops it when nothing has changed.
- Logic only in the UI. Hiding a button does not stop a direct request.
- Toggling instead of setting. A retried toggle flips the state back.
- No failure path. Silent failures look like missing data later.
- Too many automations. Each one is something you must test and maintain.
Exercise
A payment service sends your app the same "payment succeeded" webhook twice. Your workflow sends a welcome email on each event. Name two changes that stop the duplicate email.
Show answer
First, store the event ID and skip the event if it was already processed. Second, make the action idempotent, for example by checking a "welcome_sent" flag on the user's record before sending. Also verify the webhook signature so only genuine events reach the workflow.