import { customAlphabet } from 'nanoid'; export const ORDER_STATUSES = [ // ── Pre-studio (customer hasn't paid; not visible in /orders) ────── // Row exists, customer hasn't completed Stripe yet. Live state during // checkout; gets resumed via /checkout?orderId=…&canceled=1. 'pending_payment', // Swept here by the hourly pg_cron job once pending_payment ages past // 7 days without payment. Distinct from `cancelled` (which implies a // refund) — these never paid in the first place. 'abandoned', // ── Studio queue (admin works the design) ────────────────────────── 'pending_design', 'awaiting_approval', 'revision_requested', // ── Post-customer-approval (admin sends to printer) ──────────────── // Customer approved the proof. Admin still needs to click "Send to // printer" — Prodigi is not auto-triggered. 'approved', // ── Fulfillment ──────────────────────────────────────────────────── 'fulfillment_submitting', 'fulfillment_review', 'in_production', 'shipped', // ── Terminal ─────────────────────────────────────────────────────── 'cancelled', ] as const; export type OrderStatus = (typeof ORDER_STATUSES)[number]; /** Statuses where the order is "in the studio" — paid and being worked * on by the team or fulfilled. Used to gate visibility on the * customer's commissions list. pending_payment + abandoned are * intentionally *not* in this set (no money received, no commission). */ export const IN_STUDIO_STATUSES = ORDER_STATUSES.filter( s => s !== 'pending_payment' && s !== 'abandoned', ); /** Customer never paid — Stripe session expired or was abandoned. Used * for routing customers who deep-link to a stale order back to the * resume-payment screen. */ export const PRE_STUDIO_STATUSES: readonly OrderStatus[] = ['pending_payment', 'abandoned']; // 10-char order ID, no ambiguous chars (no 0/O/1/I/l). Prefixed `lg_`. const nano = customAlphabet('23456789abcdefghjkmnpqrstuvwxyz', 10); export function generateOrderId(): string { return `lg_${nano()}`; } // Friendly display: lg_a1b2c3d4e5 → LG-A1B2-C3D4E5 export function displayOrderId(id: string): string { const body = id.replace(/^lg_/, '').toUpperCase(); return `LG-${body.slice(0, 4)}-${body.slice(4)}`; } // Terse admin-side label for an order status — used in the orders queue // table and the order detail header. Trades the customer-facing prose // ("In the design queue") for a short noun phrase that scans fast in a // dense list. Kept here so the table view and the detail view stay in // lockstep — a status that renames in one place renames in both. export function adminStatusLabel(s: string): string { switch (s) { case 'pending_payment': return 'Unpaid'; case 'abandoned': return 'Abandoned'; case 'pending_design': return 'Design'; case 'awaiting_approval': return 'Awaiting approval'; case 'revision_requested': return 'Revision requested'; case 'approved': return 'Approved · send to print'; case 'fulfillment_submitting': return 'Paid · sending to printer'; case 'fulfillment_review': return 'Paid · fulfillment exception'; case 'in_production': return 'In production'; case 'shipped': return 'Shipped'; case 'cancelled': return 'Cancelled'; default: return s; } }