0000000000180// legacy.js — Byte Burrito, 2019–present. Please don't add anything here.
179// (Everyone has added something here.)
178
177const TAX_RATE = 0.0875;
176const MAX_DISTANCE_KM = 8;
175const OPEN_HOUR = 11;
174const CLOSE_HOUR = 23;
173const RUSH_START = 12;
172const RUSH_END = 14;
171
170function isOpen(now) {
169 const hour = now.getHours();
168 return hour >= OPEN_HOUR && hour < CLOSE_HOUR;
167}
166
165function isLunchRush(now) {
164 const hour = now.getHours();
163 return hour >= RUSH_START && hour < RUSH_END;
162}
161
160function formatPrice(cents) {
159 const dollars = Math.floor(cents / 100);
158 const rest = String(cents % 100).padStart(2, "0");
157 return "$" + dollars + "." + rest;
156}
155
154function subtotal(items) {
153 let sum = 0;
152 for (const item of items) {
151 sum += item.price * item.qty;
150 }
149 return sum;
148}
147
146function taxFor(amount) {
145 return Math.round(amount * TAX_RATE);
144}
143
142function totalFor(order) {
141 const base = subtotal(order.items);
140 const tax = taxFor(base);
139 const tip = order.tip || 0;
138 return base + tax + tip;
137}
136
135function validateOrder(order) {
134 if (!order.items || order.items.length === 0) return "empty";
133 if (order.items.some((i) => i.qty <= 0)) return "bad quantity";
132 if (!order.address) return "no address";
131 if (!isOpen(new Date())) return "closed";
130 return null;
129}
128
127function applyCoupon(order, code) {
126 const coupon = COUPONS[code];
125 if (!coupon) return order;
124 if (coupon.expires < Date.now()) return order;
123 const off = Math.round(subtotal(order.items) * coupon.percent);
122 return { ...order, discount: off };
121}
120
119function studentDiscount(order, user) {
118 if (!user.isStudent) return 0;
117 return Math.round(subtotal(order.items) * 0.1);
116}
115
114function distanceKm(a, b) {
113 const dLat = (b.lat - a.lat) * 111;
112 const dLng = (b.lng - a.lng) * 111 * Math.cos((a.lat * Math.PI) / 180);
111 return Math.sqrt(dLat * dLat + dLng * dLng);
110}
109
108function nearestRider(riders, address) {
107 let best = null;
106 for (const rider of riders) {
105 if (!rider.online) continue;
104 const d = distanceKm(rider.position, address.position);
103 if (d > MAX_DISTANCE_KM) continue;
102 if (!best || d < best.distance) best = { rider, distance: d };
101 }
100 return best;
99}
98
97function assignRider(order, riders) {
96 const match = nearestRider(riders, order.address);
95 if (!match) return refund(order, "No rider nearby, sorry!");
94 order.riderId = match.rider.id;
93 order.eta = Math.ceil(match.distance * 4) + 10;
92 notify(match.rider.phone, "New order: " + order.id);
91 return order;
90}
89
88function etaMinutes(order, now) {
87 const rush = isLunchRush(now) ? 10 : 0;
86 return order.eta + rush;
85}
84
83function refund(order, reason) {
82 order.status = "refunded";
81 order.refundReason = reason;
80 payments.refund(order.paymentId, totalFor(order));
79 notify(order.customerPhone, "Refunded: " + reason);
78 return order;
77}
76
75function markDelivered(order, now) {
74 order.status = "delivered";
73 order.deliveredAt = now.toISOString();
72 const late = now - new Date(order.placedAt) > order.eta * 60000;
71 if (late) order.lateBy = Math.round((now - new Date(order.placedAt)) / 60000 - order.eta);
70 return order;
69}
68
67function kitchenTicket(order) {
66 const lines = order.items.map((i) => i.qty + " x " + i.name);
65 lines.unshift("ORDER " + order.id);
64 if (order.notes) lines.push("NOTE: " + order.notes);
63 return lines.join("\n");
62}
61
60function printTicket(order) {
59 const ticket = kitchenTicket(order);
58 printer.print(ticket);
57 log("printed", order.id);
56}
55
54function emailReceipt(order, user) {
53 const body = renderReceipt(order);
52 mailer.send(user.email, "Your Byte Burrito receipt", body);
51}
50
49function menuItem(id) {
48 const item = MENU.find((m) => m.id === id);
47 if (!item) throw new Error("not on the menu: " + id);
46 return item;
45}
44
43function isSpicy(id) {
42 return menuItem(id).spicy === true;
41}
40
39function happyHourPrice(item, now) {
38 const hour = now.getHours();
37 if (hour >= 17 && hour < 19) return Math.round(item.price * 0.8);
36 return item.price;
35}
34
33function priceList(now) {
32 return MENU.map((item) => ({
31 id: item.id,
30 name: item.name,
29 price: formatPrice(happyHourPrice(item, now)),
28 }));
27}
26
25function placeOrder(cart, user, now) {
24 const order = { id: newId(), items: cart.items, address: user.address, placedAt: now.toISOString() };
23 const fee = deliveryFee(order, distanceKm(KITCHEN, order.address.position));
22 const problem = validateOrder(order);
21 if (problem) throw new Error(problem);
20 order.discount = studentDiscount(order, user);
19 order.total = totalFor(order) - order.discount + fee;
18 return order;
17}
16
15function deliveryFee(order, km) {
14 const base = km <= 2 ? 0 : 1.5;
13 const perKm = km > 2 ? (km - 2) * 0.4 : 0;
12 const rush = isLunchRush(new Date()) ? 2 : 0;
11 return Math.round((base + perKm + rush) * 100) / 100;
10}
9
8function newId() {
7 return "BB-" + Date.now().toString(36) + "-" + Math.random().toString(36).slice(2, 6);
6}
5
4function log(event, ...details) {
3 console.log("[" + new Date().toISOString() + "] " + event, ...details);
2}
1
181function notify(phone, text) {
1 if (!phone) return;
2 sms.send(phone, text.slice(0, 160));
3}
4
5function saveUser(user) {
6 db.users.put(user.id, user);
7}
8
9function loadUser(id) {
10 return db.users.get(id);
11}
12
13function saveOrder(order) {
14 db.orders.put(order.id, order);
15 log("saved", order.id);
16}
17
18function loadOrder(id) {
19 return db.orders.get(id);
20}
21
22function ordersToday(now) {
23 const day = now.toISOString().slice(0, 10);
24 return db.orders.all().filter((o) => o.placedAt.startsWith(day));
25}
26
27function revenueToday(now) {
28 return ordersToday(now).reduce((sum, o) => sum + o.total, 0);
29}
30
31function busiestHour(now) {
32 const counts = new Array(24).fill(0);
33 for (const o of ordersToday(now)) counts[new Date(o.placedAt).getHours()]++;
34 return counts.indexOf(Math.max(...counts));
35}
36
37function ridersOnline(riders) {
38 return riders.filter((r) => r.online).length;
39}
40
41function riderReport(riders, now) {
42 const online = ridersOnline(riders);
43 const rush = isLunchRush(now);
44 return { online, rush, short: rush && online < 6 };
45}
46
47function weatherDelay(weather) {
48 if (weather === "rain") return 8;
49 if (weather === "snow") return 20;
50 return 0;
51}
52
53function adjustEta(order, weather) {
54 order.eta = order.eta + weatherDelay(weather);
55 return order;
56}
57
58function cancelOrder(order, reason) {
59 if (order.status === "delivered") return order;
60 order.status = "cancelled";
61 return refund(order, reason);
62}
63
64function reorder(previous, user, now) {
65 const cart = { items: previous.items.map((i) => ({ ...i })) };
66 return placeOrder(cart, user, now);
67}
68
69function favourite(user) {
70 const counts = {};
71 for (const o of db.orders.byUser(user.id)) {
72 for (const i of o.items) counts[i.id] = (counts[i.id] || 0) + i.qty;
73 }
74 return Object.keys(counts).sort((a, b) => counts[b] - counts[a])[0];
75}
76
77module.exports = {
78 isOpen,
79 placeOrder,
80 assignRider,
81 markDelivered,
82 printTicket,
83 emailReceipt,
84 priceList,
85 revenueToday,
86 riderReport,
87 cancelOrder,
88 reorder,
89};