Sub topic 8
Workflows, automations and logic
This chapter assumes you have a data model with ownership fields (see "Modeling your data") and at least a rough set of screens. It covers the layer that makes the app do things: workflows, automations and business logic. You should be comfortable with tables, forms and the idea of a user account.
What a workflow is
A workflow is a sequence of steps that runs when something happens. It has three parts:
- Trigger: the event that starts it, such as a button click, a new row, a schedule or an incoming webhook.
- Conditions: checks that decide whether to continue or which branch to take.
- Actions: the work itself, such as creating or updating a row, sending an email or calling an API.
Business logic is the set of rules your product enforces through these workflows. "A task cannot be marked done without a client name" is business logic. "Send a reminder the day before a due date" is an automation built from a trigger, a condition and an action.
Where logic can live
The same rule can run in different places. The place you choose decides how safe and how maintainable it is.
| Location | Examples | Strength | Weakness |
|---|---|---|---|
| Screen (client) | Hiding a button, form validation | Instant feedback | A user can bypass it |
| Platform workflow | Bubble workflows, Make or Zapier scenarios, builder-generated server functions | Easy to build and read | Tied to the platform, may have run limits |
| Database | Constraints, row-level security, triggers | Cannot be bypassed by the UI | Needs some SQL |
Use this rule of thumb. Anything about permission or data integrity belongs in the database or a server-side workflow. Anything about user experience can live on the screen. A hidden button is not access control.
For example, the check (status in ('todo', 'doing', 'done')) constraint and the row-level security policies from the data chapter are business logic. They hold even if the UI has a bug.
Designing a workflow before you build it
Write each workflow as a short specification first. This also gives you something precise to paste into an AI builder prompt.
- Name the trigger.
- List the inputs the trigger provides.
- List the conditions, including what happens when each one fails.
- List the actions in order.
- State what the user sees on success and on failure.
For TaskNest, a "due-date reminder" could be specified like this:
- Trigger: a schedule that runs every day at 08:00.
- Condition: tasks where
statusis notdoneanddue_dateis tomorrow. - Action: send one email per user listing those tasks.
- Failure: if the email service returns an error, log it and retry once the next day. Do not send duplicates.
The last line matters. Most real bugs in automations come from failure and repetition, not from the main path.
Common patterns
Most MVP automations fit a few patterns.
- Record-triggered: when a row is created or changed, do something. Example: when a task becomes
done, set acompleted_attimestamp. - Scheduled: run at fixed times. Example: the daily reminder above.
- Event-driven (webhook): an outside service calls your app. Example: a payment provider reports a successful charge.
- User-initiated: a button runs a multi-step action. Example: "Duplicate this task for next week."
You can combine them. A user-initiated action can write a row, and that write can trigger a record-triggered workflow. Keep chains short, because each link makes a failure harder to trace.
Idempotency and duplicates
An action is idempotent if running it twice has the same result as running it once. Triggers in no-code tools can fire more than once. A webhook may be retried, a schedule may overlap with a slow run, and a user may double-click a button.
Protect yourself with a marker. Add a field such as reminder_sent_on to the task and include "reminder not yet sent" in the condition. After sending, set the field. A second run then finds nothing to do.
For incoming webhooks, store the event ID the sender provides and ignore any ID you have already processed. Also verify the sender is genuine. When a service signs requests with a shared secret, check the signature against the raw request body before acting:
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);
}Use the raw body exactly as received. If a framework parses the JSON and you re-serialize it, the bytes can differ and valid signatures will fail.
Errors, limits and cost
Plan for these edge cases before launch:
- Partial failure. A workflow that creates a row and then sends an email can fail between the two steps. Order steps so that the state is safe if it stops midway, and prefer recording the result after the external call succeeds.
- Rate limits. Email, AI and other APIs reject too many calls in a short time. Process in small batches and wait before retrying.
- Run quotas. Automation platforms bill by task or operation. A loop over 500 rows can use 500 operations in one run. Estimate monthly usage before choosing a plan.
- Time zones. "Tomorrow" depends on the user's zone. Store dates and times with a zone, or decide explicitly that the app uses one zone.
- Silent failures. Many tools do not notify you when a scenario fails. Turn on error alerts, or write failures to a log table you check.
- Loops. A workflow that updates a row can trigger itself. Add a condition, such as "only if the value actually changed."
Prompting an AI builder for logic
AI builders handle screens well but often guess at rules. Describe logic as the specification above, including failure behavior and where the rule is enforced. Then review the generated code or workflow against that specification, which the later chapter on reviewing output covers.
Add a "mark done" action to TaskNest.
Trigger: the user clicks Mark done on a task.
Condition: the task's owner_id equals the signed-in user and status is not already done.
Action: set status to done and completed_at to the current time, on the server.
Failure: if the condition fails, show "You can't change this task" and change nothing.Keep it small
Each workflow you add is something to test, monitor and pay for. For an MVP, automate a step only if doing it by hand would stop you from testing your hypothesis, or if it would not be practical at your expected number of users. Doing a step manually for the first ten users is often the faster experiment.
Exercise
A no-code scenario sends a "payment received" email whenever the payment provider calls your webhook. A customer reports receiving the email three times for one payment. What is the likely cause, and what two changes would you make?
Show answer
The provider retried the webhook, or the trigger fired more than once, and the workflow is not idempotent. Store the event ID from each webhook and skip any ID already processed, so repeats do nothing. Also verify the request signature so only genuine events trigger the workflow. Recording a "email sent" marker on the order is a second way to prevent duplicate emails.
Exercise
You want to ensure a user can never mark another user's task as done. You hide the "Mark done" button for tasks they do not own. Is that enough? Where should the rule be enforced?
Show answer
No. Hiding the button only changes the screen, and a user can send the request directly. Enforce the rule in the database with row-level security (or in a server-side workflow that checks owner_id against the signed-in user), so the request is rejected regardless of the UI.