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