DEV Community

tempat simpanan
tempat simpanan

Posted on AI-assisted

A public page for every customer order: unguessable codes, a 4-letter ref, and data that deletes itself

Small shops get the same message all day: "is my order ready yet?" Each reply takes a minute, and it's often the same customer asking twice. So in iBuat.Link every order a shop takes gets its own page at ibuat.link/o/<code>. The shop sends the link on WhatsApp or prints it as a QR code on the receipt, and the customer checks the status whenever they like.

The page has no login. Anyone with the link can open it. That one fact drives almost every decision below, so here's how the pieces fit together.

1. Twelve characters nobody can guess

A sequential ID (/o/1042) would be a disaster: change one digit and you're looking at a stranger's order. The code has to be random, long enough that guessing is pointless, and short enough to print on a 58 mm receipt.

// Lowercase letters and digits without the look-alikes l, o, 0 and 1,
// so a code typed from a receipt still works.
export const ORDER_CODE_ALPHABET = 'abcdefghijkmnpqrstuvwxyz23456789';
export const ORDER_CODE_LENGTH = 12;

export function generateOrderCode(randomBytes: (size: number) => Uint8Array): string {
  const bytes = randomBytes(ORDER_CODE_LENGTH);
  let code = '';
  for (let i = 0; i < ORDER_CODE_LENGTH; i += 1) code += ORDER_CODE_ALPHABET[bytes[i] & 31];
  return code;
}
Enter fullscreen mode Exit fullscreen mode

Two details matter here:

  • The alphabet has exactly 32 characters. A random byte has 256 values, and 256 is a multiple of 32, so masking it to 5 bits (& 31) picks every character with the same probability. With a 30- or 36-character alphabet, the usual byte % length trick quietly favours some characters over others.
  • 12 × 5 bits = 60 bits. That's about 10¹⁸ possible codes. Nobody is enumerating that, and the page is rate limited on top.

The bytes come from crypto.randomBytes on the server, never Math.random. A collision is astronomically unlikely, but "unlikely" isn't a reason to throw a 500, so the insert simply tries again:

for (let attempt = 0; attempt < 4; attempt += 1) {
  const [created] = await tx.insert(orders)
    .values({ /* ... */ code: generateOrderCode((size) => crypto.randomBytes(size)) })
    .onConflictDoNothing({ target: orders.code })
    .returning();
  if (created) return created;
}
throw new Error('Could not pick an unused order code');
Enter fullscreen mode Exit fullscreen mode

The unique index on code does the real work. The loop only decides what happens when it fires.

2. A 4-letter ref for humans

Twelve random characters are great for security and terrible to read out loud across a counter. So the first four characters double as a short reference:

export function orderRef(code: string): string {
  return code.slice(0, 4).toUpperCase(); // "k7qm…" → "K7QM"
}
Enter fullscreen mode Exit fullscreen mode

The ref is not an identifier. Four characters from 32 give about a million combinations, so two orders in a busy shop can share one, and that's fine: the shop also sees the customer's name next to it. The ref is for "I'm here for K7QM" and for the WhatsApp chat button, which quotes it so the shop knows which order the customer means. The full code is the only thing that opens the page.

3. Two phones, one order

Moving an order along is one big button: Mark as In progress, then Mark as Ready. In a shop with two staff, both phones show the same order. If both people tap at the same moment, a naive "advance to the next step" would advance it twice and skip a step the customer never sees.

So the client sends the step it thinks the order is at, and the server checks it under a row lock:

return db.transaction(async (tx) => {
  const [current] = await tx.select().from(orders)
    .where(and(eq(orders.id, id), eq(orders.workspaceId, workspaceId), isNull(orders.deletedAt)))
    .for('update');
  if (!current) return { ok: false, reason: 'not_found' };
  if (change.from && current.status !== change.from) {
    return { ok: false, reason: 'conflict', order: current };
  }
  // ...update status, write a history row
});
Enter fullscreen mode Exit fullscreen mode

The second tap waits for the first transaction, sees that the status has already moved, and gets conflict back with the current order, so its screen catches up instead of skipping ahead. A double tap on one phone hits the same check, so it can't skip a step either. Undo sends from too, so it can't undo a step someone else has already moved past.

4. The page is public, so it shows as little as possible

Anyone who gets the link (forwarded in a family group, screenshotted, scanned off a receipt left on a table) sees the page. So it shows what the customer needs to answer "is it ready?" and nothing else:

  • First name only. "Dimas Prasetyo" becomes "Dimas". One wrinkle from Indonesia: names often start with an honorific, and "Bu Sari" shortened to "Bu" is useless, so a short list of honorifics (bu, pak, mbak, kak, mr, mrs…) keeps the next word.
  • A masked number. 6281234567789 is shown as +62 8••-••••-•789: enough for the customer to recognise their own number, not enough for anyone else to learn it.
  • The shop's private note never appears. "Customer was rude, double-check payment" stays in the dashboard.
  • Not for search engines, not for referrers. The page sends noindex, nofollow and referrer: no-referrer, so a link someone clicks from it doesn't hand the order URL to another site.

5. Data that deletes itself

A status link is useful for a few weeks. Keeping customer names and numbers forever "just in case" is a liability, and Indonesia's personal data law (UU PDP) says you keep data only as long as it serves its purpose. So the lifetimes are written down as constants:

export const ORDER_LINK_DAYS_AFTER_DONE = 30; // link stops opening
export const ORDER_DATA_DAYS_AFTER_DONE = 90; // order + customer data deleted
Enter fullscreen mode Exit fullscreen mode

A daily job deletes completed orders past the 90-day mark. It deletes in batches of 500 rows, so a large backlog never holds one long lock, and it has a cap per run. Whatever is left goes the next day. Running it twice does nothing extra.

Deleting an order by hand is two steps on purpose. The customer's name, number and note are wiped immediately, and the link stops working. The bare row stays a little longer, because it still counts toward the shop's monthly order limit; otherwise "create, delete, create" would be a free way around it. The retention job removes those bare rows once they're older than any monthly window.

What I'd tell myself at the start

  • If a URL is the only key, it needs real randomness and an alphabet you can type from paper.
  • Give humans a short ref, but never let the short ref open anything.
  • Any button two people can press at once needs to send "from" as well as "to".
  • Decide how long data lives on day one and put the numbers in code, not in a policy page nobody reads.

There's a picture of the customer page on the order status links page, and this write-up covers the idea from the shop's side. Happy to answer questions about any of it in the comments.

Top comments (0)