Branchwell

Lumen & Gesso / website · lumengesso.com / app / lib / orders.ts

Raw 3.5 KB on f4d070c0cd6547ed6432bda0f7f1dc2cb2181b18
1import { customAlphabet } from 'nanoid';
2
3export const ORDER_STATUSES = [
4 // ── Pre-studio (customer hasn't paid; not visible in /orders) ──────
5 // Row exists, customer hasn't completed Stripe yet. Live state during
6 // checkout; gets resumed via /checkout?orderId=…&canceled=1.
7 'pending_payment',
8 // Swept here by the hourly pg_cron job once pending_payment ages past
9 // 7 days without payment. Distinct from `cancelled` (which implies a
10 // refund) — these never paid in the first place.
11 'abandoned',
12 // ── Studio queue (admin works the design) ──────────────────────────
13 'pending_design',
14 'awaiting_approval',
15 'revision_requested',
16 // ── Post-customer-approval (admin sends to printer) ────────────────
17 // Customer approved the proof. Admin still needs to click "Send to
18 // printer" — Prodigi is not auto-triggered.
19 'approved',
20 // ── Fulfillment ────────────────────────────────────────────────────
21 'fulfillment_submitting',
22 'fulfillment_review',
23 'in_production',
24 'shipped',
25 // ── Terminal ───────────────────────────────────────────────────────
26 'cancelled',
27] as const;
28export type OrderStatus = (typeof ORDER_STATUSES)[number];
29
30/** Statuses where the order is "in the studio" — paid and being worked
31 * on by the team or fulfilled. Used to gate visibility on the
32 * customer's commissions list. pending_payment + abandoned are
33 * intentionally *not* in this set (no money received, no commission). */
34export const IN_STUDIO_STATUSES = ORDER_STATUSES.filter(
35 s => s !== 'pending_payment' && s !== 'abandoned',
36);
37
38/** Customer never paid — Stripe session expired or was abandoned. Used
39 * for routing customers who deep-link to a stale order back to the
40 * resume-payment screen. */
41export const PRE_STUDIO_STATUSES: readonly OrderStatus[] = ['pending_payment', 'abandoned'];
42
43// 10-char order ID, no ambiguous chars (no 0/O/1/I/l). Prefixed `lg_`.
44const nano = customAlphabet('23456789abcdefghjkmnpqrstuvwxyz', 10);
45export function generateOrderId(): string {
46 return `lg_${nano()}`;
47}
48
49// Friendly display: lg_a1b2c3d4e5 → LG-A1B2-C3D4E5
50export function displayOrderId(id: string): string {
51 const body = id.replace(/^lg_/, '').toUpperCase();
52 return `LG-${body.slice(0, 4)}-${body.slice(4)}`;
53}
54
55// Terse admin-side label for an order status — used in the orders queue
56// table and the order detail header. Trades the customer-facing prose
57// ("In the design queue") for a short noun phrase that scans fast in a
58// dense list. Kept here so the table view and the detail view stay in
59// lockstep — a status that renames in one place renames in both.
60export function adminStatusLabel(s: string): string {
61 switch (s) {
62 case 'pending_payment': return 'Unpaid';
63 case 'abandoned': return 'Abandoned';
64 case 'pending_design': return 'Design';
65 case 'awaiting_approval': return 'Awaiting approval';
66 case 'revision_requested': return 'Revision requested';
67 case 'approved': return 'Approved · send to print';
68 case 'fulfillment_submitting': return 'Paid · sending to printer';
69 case 'fulfillment_review': return 'Paid · fulfillment exception';
70 case 'in_production': return 'In production';
71 case 'shipped': return 'Shipped';
72 case 'cancelled': return 'Cancelled';
73 default: return s;
74 }
75}