@bash-app/bash-common 30.385.0 → 30.391.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/__tests__/scenePulse.test.d.ts +2 -0
  2. package/dist/__tests__/scenePulse.test.d.ts.map +1 -0
  3. package/dist/__tests__/scenePulse.test.js +52 -0
  4. package/dist/__tests__/scenePulse.test.js.map +1 -0
  5. package/dist/definitions.d.ts +19 -13
  6. package/dist/definitions.d.ts.map +1 -1
  7. package/dist/definitions.js +13 -12
  8. package/dist/definitions.js.map +1 -1
  9. package/dist/extendedSchemas.d.ts +22 -0
  10. package/dist/extendedSchemas.d.ts.map +1 -1
  11. package/dist/extendedSchemas.js +17 -0
  12. package/dist/extendedSchemas.js.map +1 -1
  13. package/dist/index.d.ts +3 -1
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +3 -1
  16. package/dist/index.js.map +1 -1
  17. package/dist/scenePulse.d.ts +66 -0
  18. package/dist/scenePulse.d.ts.map +1 -0
  19. package/dist/scenePulse.js +54 -0
  20. package/dist/scenePulse.js.map +1 -0
  21. package/dist/utils/__tests__/paymentUtils.test.js +28 -1
  22. package/dist/utils/__tests__/paymentUtils.test.js.map +1 -1
  23. package/dist/utils/__tests__/serviceAvailabilityDisplay.test.js +56 -1
  24. package/dist/utils/__tests__/serviceAvailabilityDisplay.test.js.map +1 -1
  25. package/dist/utils/__tests__/slugUtils.test.js +5 -3
  26. package/dist/utils/__tests__/slugUtils.test.js.map +1 -1
  27. package/dist/utils/paymentUtils.d.ts +18 -1
  28. package/dist/utils/paymentUtils.d.ts.map +1 -1
  29. package/dist/utils/paymentUtils.js +29 -2
  30. package/dist/utils/paymentUtils.js.map +1 -1
  31. package/dist/utils/service/__tests__/foodAndBeverageQuote.test.d.ts +2 -0
  32. package/dist/utils/service/__tests__/foodAndBeverageQuote.test.d.ts.map +1 -0
  33. package/dist/utils/service/__tests__/foodAndBeverageQuote.test.js +353 -0
  34. package/dist/utils/service/__tests__/foodAndBeverageQuote.test.js.map +1 -0
  35. package/dist/utils/service/foodAndBeverageQuote.d.ts +171 -0
  36. package/dist/utils/service/foodAndBeverageQuote.d.ts.map +1 -0
  37. package/dist/utils/service/foodAndBeverageQuote.js +467 -0
  38. package/dist/utils/service/foodAndBeverageQuote.js.map +1 -0
  39. package/dist/utils/serviceAvailabilityDisplay.d.ts +28 -2
  40. package/dist/utils/serviceAvailabilityDisplay.d.ts.map +1 -1
  41. package/dist/utils/serviceAvailabilityDisplay.js +39 -0
  42. package/dist/utils/serviceAvailabilityDisplay.js.map +1 -1
  43. package/package.json +1 -1
  44. package/prisma/schema.prisma +95 -4
  45. package/src/__tests__/scenePulse.test.ts +69 -0
  46. package/src/definitions.ts +18 -12
  47. package/src/extendedSchemas.ts +23 -0
  48. package/src/index.ts +6 -0
  49. package/src/scenePulse.ts +113 -0
  50. package/src/utils/__tests__/paymentUtils.test.ts +36 -0
  51. package/src/utils/__tests__/serviceAvailabilityDisplay.test.ts +73 -0
  52. package/src/utils/__tests__/slugUtils.test.ts +5 -3
  53. package/src/utils/paymentUtils.ts +47 -2
  54. package/src/utils/service/__tests__/foodAndBeverageQuote.test.ts +502 -0
  55. package/src/utils/service/foodAndBeverageQuote.ts +613 -0
  56. package/src/utils/serviceAvailabilityDisplay.ts +82 -2
@@ -0,0 +1,613 @@
1
+ /**
2
+ * Shared quote logic for Food & Beverage event-service listings.
3
+ *
4
+ * Rules:
5
+ * - Priced per-guest (PER_ATTENDEE pricingModel).
6
+ * - Headcount floors at minimumQuantity if set.
7
+ * - Dollar total floors at minimumChargeCents if set.
8
+ * - Packages and add-ons still stack on top.
9
+ * - No check-in true-up (promoter settlement) — headcount is snapshotted
10
+ * at booking time only.
11
+ *
12
+ * Keep this module pure (no Prisma, no React). Import it in bash-app, api,
13
+ * and frontendServiceBookingUtils via the index.
14
+ */
15
+
16
+ // ---------------------------------------------------------------------------
17
+ // Subtype detection
18
+ // ---------------------------------------------------------------------------
19
+
20
+ /**
21
+ * All FoodAndBeverageSubType values (mirrors the Prisma enum).
22
+ * Also include legacy/alias strings the wizard uses (Caterer, PrivateChef, etc.)
23
+ * so detection works before the DB normalises them.
24
+ */
25
+ export const FOOD_AND_BEVERAGE_SUBTYPES = new Set([
26
+ // Canonical FoodAndBeverageSubType values
27
+ "Caterer",
28
+ "FoodTruck",
29
+ "FoodCartStand",
30
+ "PrivateChefs",
31
+ "Bartenders",
32
+ "Baristas",
33
+ "Mixologist",
34
+ "Sommelier",
35
+ "DessertBar",
36
+ "BeverageCart",
37
+ "ConcessionStand",
38
+ "CelebrationCake",
39
+ "GrazingTable",
40
+ "FoodAndBeverageOther",
41
+ // Wizard aliases / legacy keys
42
+ "PrivateChef",
43
+ "Bartender",
44
+ "Barista",
45
+ ]);
46
+
47
+ /**
48
+ * Custom-order celebration cakes — consultation, serving-size packages,
49
+ * pickup/delivery. Not a dessert station (DessertBar) and not per-guest catering.
50
+ */
51
+ export function isCelebrationCakeEventService(opts: {
52
+ eventServiceSubType?: string | null;
53
+ }): boolean {
54
+ return opts.eventServiceSubType === "CelebrationCake";
55
+ }
56
+
57
+ /**
58
+ * Charcuterie boards and grazing tables — board/table packages, not per-guest catering.
59
+ */
60
+ export function isGrazingTableEventService(opts: {
61
+ eventServiceSubType?: string | null;
62
+ }): boolean {
63
+ return opts.eventServiceSubType === "GrazingTable";
64
+ }
65
+
66
+ /**
67
+ * Wedding / ceremony officiant. Hosting Support — not F&B, not Entertainment Emcee.
68
+ */
69
+ export function isOfficiantEventService(opts: {
70
+ eventServiceSubType?: string | null;
71
+ }): boolean {
72
+ return opts.eventServiceSubType === "Officiant";
73
+ }
74
+
75
+ /** Cake and grazing: CUSTOM starting price + ServicePackage tiers. Never per-guest. */
76
+ export const PACKAGE_PRICED_FOOD_SUBTYPES = new Set([
77
+ "CelebrationCake",
78
+ "GrazingTable",
79
+ ]);
80
+
81
+ export function isPackagePricedFoodEventService(opts: {
82
+ eventServiceSubType?: string | null;
83
+ }): boolean {
84
+ return (
85
+ !!opts.eventServiceSubType &&
86
+ PACKAGE_PRICED_FOOD_SUBTYPES.has(opts.eventServiceSubType)
87
+ );
88
+ }
89
+
90
+ /**
91
+ * Return true when a listing is an F&B EventService — either by top-level
92
+ * type or by subtype string (for legacy/alias safety).
93
+ */
94
+ export function isFoodAndBeverageEventService(opts: {
95
+ eventServiceType?: string | null;
96
+ eventServiceSubType?: string | null;
97
+ }): boolean {
98
+ if (opts.eventServiceType === "FoodAndBeverage") return true;
99
+ if (opts.eventServiceSubType && FOOD_AND_BEVERAGE_SUBTYPES.has(opts.eventServiceSubType))
100
+ return true;
101
+ return false;
102
+ }
103
+
104
+ /**
105
+ * Per-guest F&B only. Cake and grazing use packages — never this path.
106
+ */
107
+ export function isPerGuestFoodAndBeverageEventService(opts: {
108
+ eventServiceType?: string | null;
109
+ eventServiceSubType?: string | null;
110
+ }): boolean {
111
+ return (
112
+ isFoodAndBeverageEventService(opts) && !isPackagePricedFoodEventService(opts)
113
+ );
114
+ }
115
+
116
+ // ---------------------------------------------------------------------------
117
+ // Fulfillment modes
118
+ // ---------------------------------------------------------------------------
119
+
120
+ export const EVENT_SERVICE_FULFILLMENT_MODES = [
121
+ "Pickup",
122
+ "Delivery",
123
+ "FullService",
124
+ ] as const;
125
+
126
+ export type FoodAndBeverageFulfillmentMode =
127
+ (typeof EVENT_SERVICE_FULFILLMENT_MODES)[number];
128
+
129
+ const FULFILLMENT_MODE_SET: ReadonlySet<string> = new Set(
130
+ EVENT_SERVICE_FULFILLMENT_MODES
131
+ );
132
+
133
+ export function isFulfillmentMode(
134
+ mode: string
135
+ ): mode is FoodAndBeverageFulfillmentMode {
136
+ return FULFILLMENT_MODE_SET.has(mode);
137
+ }
138
+
139
+ export function filterFulfillmentModes(
140
+ modes?: readonly string[] | null
141
+ ): FoodAndBeverageFulfillmentMode[] {
142
+ return (modes ?? []).filter(isFulfillmentMode);
143
+ }
144
+
145
+ export const FULFILLMENT_MODE_LABEL: Record<FoodAndBeverageFulfillmentMode, string> = {
146
+ Pickup: "Pickup",
147
+ Delivery: "Delivery",
148
+ FullService: "Full Service",
149
+ };
150
+
151
+ // ---------------------------------------------------------------------------
152
+ // Menu category chips per subtype
153
+ // ---------------------------------------------------------------------------
154
+
155
+ /**
156
+ * Suggested menu category chips shown in the wizard and on the listing.
157
+ * Providers can add custom values; these are just pre-populated starting points.
158
+ * Not SKUs — no flavors, quantities, or prices here.
159
+ */
160
+ export const FB_MENU_CATEGORY_SUGGESTIONS: Record<string, readonly string[]> = {
161
+ Caterer: [
162
+ "Appetizers",
163
+ "Salads",
164
+ "Sandwiches",
165
+ "Mains",
166
+ "Sides",
167
+ "Desserts",
168
+ "Beverages",
169
+ "Vegetarian",
170
+ "Vegan",
171
+ "Gluten-Free",
172
+ ],
173
+ DessertBar: [
174
+ "Cakes",
175
+ "Cupcakes",
176
+ "Cookies",
177
+ "Dessert Table",
178
+ "Chocolate Fountain",
179
+ "Ice Cream",
180
+ "Candy Buffet",
181
+ "Gluten-Free",
182
+ "Vegan",
183
+ "Custom Orders",
184
+ ],
185
+ FoodTruck: [
186
+ "Street Food",
187
+ "Burgers",
188
+ "Tacos",
189
+ "Pizza",
190
+ "BBQ",
191
+ "Ethnic Cuisine",
192
+ "Desserts",
193
+ "Vegan",
194
+ "Gluten-Free",
195
+ ],
196
+ PrivateChef: [
197
+ "Appetizers",
198
+ "Multi-Course Dinner",
199
+ "Wine Pairing",
200
+ "Dietary Specialist",
201
+ "Tasting Menu",
202
+ "Family-Style",
203
+ ],
204
+ PrivateChefs: [
205
+ "Appetizers",
206
+ "Multi-Course Dinner",
207
+ "Wine Pairing",
208
+ "Dietary Specialist",
209
+ "Tasting Menu",
210
+ "Family-Style",
211
+ ],
212
+ Bartenders: [
213
+ "Cocktails",
214
+ "Beer & Wine",
215
+ "Open Bar",
216
+ "Mocktails",
217
+ "Craft Beer",
218
+ "Specialty Drinks",
219
+ ],
220
+ Bartender: [
221
+ "Cocktails",
222
+ "Beer & Wine",
223
+ "Open Bar",
224
+ "Mocktails",
225
+ "Craft Beer",
226
+ "Specialty Drinks",
227
+ ],
228
+ Baristas: ["Espresso", "Cold Brew", "Lattes", "Tea", "Specialty Beverages"],
229
+ Barista: ["Espresso", "Cold Brew", "Lattes", "Tea", "Specialty Beverages"],
230
+ FoodCartStand: [
231
+ "Street Food",
232
+ "Snacks",
233
+ "Desserts",
234
+ "Coffee",
235
+ "Specialty Station",
236
+ ],
237
+ BeverageCart: ["Coffee", "Tea", "Juice", "Smoothies", "Mocktails", "Water"],
238
+ };
239
+
240
+ /**
241
+ * Get suggested menu categories for a subtype.
242
+ * Falls back to a generic list if the subtype is not found.
243
+ */
244
+ export function getFoodAndBeverageMenuCategories(subtype?: string | null): readonly string[] {
245
+ if (subtype && FB_MENU_CATEGORY_SUGGESTIONS[subtype]) {
246
+ return FB_MENU_CATEGORY_SUGGESTIONS[subtype];
247
+ }
248
+ return ["Mains", "Sides", "Desserts", "Beverages", "Dietary Options"];
249
+ }
250
+
251
+ // ---------------------------------------------------------------------------
252
+ // Quote math
253
+ // ---------------------------------------------------------------------------
254
+
255
+ export interface FoodAndBeverageQuoteParams {
256
+ /** Cents per guest (baseRateCents from EventService). */
257
+ baseRateCents: number;
258
+ /** Host-entered guest count for the booking. */
259
+ headcount: number;
260
+ /** Minimum bookable guests (minimumQuantity from EventService). 0 = no floor. */
261
+ minimumQuantity?: number | null;
262
+ /** Hard dollar floor regardless of headcount (minimumChargeCents). 0 = no floor. */
263
+ minimumChargeCents?: number | null;
264
+ }
265
+
266
+ /**
267
+ * The effective guest count used for the quote.
268
+ * Floors at minimumQuantity so the provider is never charged less than their floor.
269
+ */
270
+ export function quotedGuestCount(
271
+ headcount: number,
272
+ minimumQuantity?: number | null
273
+ ): number {
274
+ if (!minimumQuantity || minimumQuantity <= 0) return Math.max(0, headcount);
275
+ return Math.max(headcount, minimumQuantity);
276
+ }
277
+
278
+ /**
279
+ * Total food & beverage quote in cents, before packages, add-ons, and fees.
280
+ *
281
+ * Calculation order (mirrors the brochure's structure):
282
+ * 1. Floor headcount at minimumQuantity → quotedGuests
283
+ * 2. Compute rate total: baseRateCents × quotedGuests
284
+ * 3. Floor at minimumChargeCents
285
+ *
286
+ * Packages and add-ons are NOT included here — they are added on top by
287
+ * frontendServiceBookingUtils (same as for all other service types).
288
+ */
289
+ export function foodAndBeverageQuoteCents({
290
+ baseRateCents,
291
+ headcount,
292
+ minimumQuantity,
293
+ minimumChargeCents,
294
+ }: FoodAndBeverageQuoteParams): number {
295
+ if (baseRateCents <= 0 || headcount < 0) return 0;
296
+ const guests = quotedGuestCount(headcount, minimumQuantity);
297
+ const rateTotalCents = baseRateCents * guests;
298
+ const floorCents = minimumChargeCents ?? 0;
299
+ return Math.max(rateTotalCents, floorCents);
300
+ }
301
+
302
+ /**
303
+ * Human-readable summary string, e.g. "$18 / guest · min 25 guests".
304
+ */
305
+ export function foodAndBeverageRateSummary(
306
+ baseRateCents: number,
307
+ minimumQuantity?: number | null
308
+ ): string {
309
+ const formatted = new Intl.NumberFormat("en-US", {
310
+ style: "currency",
311
+ currency: "USD",
312
+ minimumFractionDigits: 0,
313
+ maximumFractionDigits: 2,
314
+ }).format(baseRateCents / 100);
315
+ const perGuest = `${formatted} / guest`;
316
+ if (minimumQuantity && minimumQuantity > 0) {
317
+ return `${perGuest} · min ${minimumQuantity} guests`;
318
+ }
319
+ return perGuest;
320
+ }
321
+
322
+ /**
323
+ * Return true if the entered headcount is below the listing's minimum.
324
+ * Use this to block checkout with a human-readable message.
325
+ */
326
+ export function isBelowGuestMinimum(
327
+ headcount: number,
328
+ minimumQuantity?: number | null
329
+ ): boolean {
330
+ if (!minimumQuantity || minimumQuantity <= 0) return false;
331
+ return headcount < minimumQuantity;
332
+ }
333
+
334
+ // ---------------------------------------------------------------------------
335
+ // Lead time
336
+ // ---------------------------------------------------------------------------
337
+
338
+ /**
339
+ * Human-readable lead time label, e.g. "Needs 7 days notice" or "Needs 2 weeks notice".
340
+ * Rounds partial weeks only when evenly divisible.
341
+ */
342
+ export function foodAndBeverageLeadTimeLabel(leadTimeDays?: number | null): string | null {
343
+ if (!leadTimeDays || leadTimeDays <= 0) return null;
344
+ if (leadTimeDays % 7 === 0) {
345
+ const weeks = leadTimeDays / 7;
346
+ return `Needs ${weeks} ${weeks === 1 ? "week" : "weeks"} notice`;
347
+ }
348
+ return `Needs ${leadTimeDays} ${leadTimeDays === 1 ? "day" : "days"} notice`;
349
+ }
350
+
351
+ // ---------------------------------------------------------------------------
352
+ // Celebration cake chips (stored on goodsOrServices with lowercase prefixes)
353
+ // ---------------------------------------------------------------------------
354
+
355
+ export const CELEBRATION_CAKE_FULFILLMENT_MODES = ["Pickup", "Delivery"] as const;
356
+
357
+ export type CelebrationCakeFulfillmentMode =
358
+ (typeof CELEBRATION_CAKE_FULFILLMENT_MODES)[number];
359
+
360
+ const CAKE_FULFILLMENT_MODE_SET: ReadonlySet<string> = new Set(
361
+ CELEBRATION_CAKE_FULFILLMENT_MODES
362
+ );
363
+
364
+ export function isCelebrationCakeFulfillmentMode(
365
+ mode: string
366
+ ): mode is CelebrationCakeFulfillmentMode {
367
+ return CAKE_FULFILLMENT_MODE_SET.has(mode);
368
+ }
369
+
370
+ export function filterCelebrationCakeFulfillmentModes(
371
+ modes?: readonly string[] | null
372
+ ): CelebrationCakeFulfillmentMode[] {
373
+ return (modes ?? []).filter(isCelebrationCakeFulfillmentMode);
374
+ }
375
+
376
+ export const CAKE_OPTION_KINDS = [
377
+ "flavor",
378
+ "frosting",
379
+ "filling",
380
+ "dietary",
381
+ ] as const;
382
+
383
+ export type CakeOptionKind = (typeof CAKE_OPTION_KINDS)[number];
384
+
385
+ export const CELEBRATION_CAKE_FLAVORS = [
386
+ "Vanilla",
387
+ "Chocolate",
388
+ "Red Velvet",
389
+ "Marble",
390
+ "Carrot",
391
+ "Lemon",
392
+ "Almond",
393
+ "Funfetti",
394
+ "Spice",
395
+ "Coconut",
396
+ ] as const;
397
+
398
+ export const CELEBRATION_CAKE_FROSTINGS = [
399
+ "Vanilla Buttercream",
400
+ "Chocolate Buttercream",
401
+ "Cream Cheese",
402
+ "Whipped Cream",
403
+ "Fondant",
404
+ "Ganache",
405
+ "Swiss Meringue",
406
+ ] as const;
407
+
408
+ export const CELEBRATION_CAKE_FILLINGS = [
409
+ "Raspberry",
410
+ "Lemon Custard",
411
+ "Chocolate Mousse",
412
+ "Strawberry",
413
+ "Cream Cheese",
414
+ "Cannoli",
415
+ "Vanilla Buttercream",
416
+ ] as const;
417
+
418
+ export const CELEBRATION_CAKE_DIETARY = [
419
+ "Gluten-Free",
420
+ "Vegan",
421
+ "Nut-Free",
422
+ "Sugar-Free",
423
+ "Dairy-Free",
424
+ ] as const;
425
+
426
+ export function encodeCakeOption(kind: CakeOptionKind, value: string): string {
427
+ return `${kind}:${value}`;
428
+ }
429
+
430
+ export interface ParsedCakeOptions {
431
+ flavors: string[];
432
+ frostings: string[];
433
+ fillings: string[];
434
+ dietary: string[];
435
+ other: string[];
436
+ }
437
+
438
+ /**
439
+ * Split prefixed cake chips back into grouped lists.
440
+ * Unprefixed leftovers land in `other` (caterer-style chips, if mixed).
441
+ */
442
+ export function parseCakeOptions(
443
+ goodsOrServices?: readonly string[] | null
444
+ ): ParsedCakeOptions {
445
+ const result: ParsedCakeOptions = {
446
+ flavors: [],
447
+ frostings: [],
448
+ fillings: [],
449
+ dietary: [],
450
+ other: [],
451
+ };
452
+ if (!goodsOrServices) return result;
453
+
454
+ for (const raw of goodsOrServices) {
455
+ if (raw.startsWith("flavor:")) {
456
+ result.flavors.push(raw.slice("flavor:".length));
457
+ } else if (raw.startsWith("frosting:")) {
458
+ result.frostings.push(raw.slice("frosting:".length));
459
+ } else if (raw.startsWith("filling:")) {
460
+ result.fillings.push(raw.slice("filling:".length));
461
+ } else if (raw.startsWith("dietary:")) {
462
+ result.dietary.push(raw.slice("dietary:".length));
463
+ } else if (raw.length > 0) {
464
+ result.other.push(raw);
465
+ }
466
+ }
467
+ return result;
468
+ }
469
+
470
+ export function serializeCakeOptions(opts: {
471
+ flavors?: readonly string[];
472
+ frostings?: readonly string[];
473
+ fillings?: readonly string[];
474
+ dietary?: readonly string[];
475
+ }): string[] {
476
+ return [
477
+ ...(opts.flavors ?? []).map((v) => encodeCakeOption("flavor", v)),
478
+ ...(opts.frostings ?? []).map((v) => encodeCakeOption("frosting", v)),
479
+ ...(opts.fillings ?? []).map((v) => encodeCakeOption("filling", v)),
480
+ ...(opts.dietary ?? []).map((v) => encodeCakeOption("dietary", v)),
481
+ ];
482
+ }
483
+
484
+ /**
485
+ * Listing line, e.g. "Cakes start at $350". Null when no starting price.
486
+ */
487
+ export function celebrationCakeStartingPriceLabel(
488
+ minimumChargeCents?: number | null
489
+ ): string | null {
490
+ return startingPriceLabel("Cakes start at", minimumChargeCents);
491
+ }
492
+
493
+ function startingPriceLabel(
494
+ prefix: string,
495
+ minimumChargeCents?: number | null
496
+ ): string | null {
497
+ if (!minimumChargeCents || minimumChargeCents <= 0) return null;
498
+ const formatted = new Intl.NumberFormat("en-US", {
499
+ style: "currency",
500
+ currency: "USD",
501
+ minimumFractionDigits: 0,
502
+ maximumFractionDigits: 2,
503
+ }).format(minimumChargeCents / 100);
504
+ return `${prefix} ${formatted}`;
505
+ }
506
+
507
+ export function grazingTableStartingPriceLabel(
508
+ minimumChargeCents?: number | null
509
+ ): string | null {
510
+ return startingPriceLabel("Boards start at", minimumChargeCents);
511
+ }
512
+
513
+ export function packagePricedFoodStartingPriceLabel(
514
+ eventServiceSubType?: string | null,
515
+ minimumChargeCents?: number | null
516
+ ): string | null {
517
+ if (eventServiceSubType === "GrazingTable") {
518
+ return grazingTableStartingPriceLabel(minimumChargeCents);
519
+ }
520
+ if (eventServiceSubType === "CelebrationCake") {
521
+ return celebrationCakeStartingPriceLabel(minimumChargeCents);
522
+ }
523
+ return null;
524
+ }
525
+
526
+ // ---------------------------------------------------------------------------
527
+ // Grazing / charcuterie chips (goodsOrServices with lowercase prefixes)
528
+ // ---------------------------------------------------------------------------
529
+
530
+ export const GRAZING_TABLE_FULFILLMENT_MODES = [
531
+ "Pickup",
532
+ "Delivery",
533
+ "FullService",
534
+ ] as const;
535
+
536
+ export const GRAZING_OPTION_KINDS = ["style", "protein", "dietary"] as const;
537
+
538
+ export type GrazingOptionKind = (typeof GRAZING_OPTION_KINDS)[number];
539
+
540
+ export const GRAZING_TABLE_STYLES = [
541
+ "Classic Charcuterie",
542
+ "Modern Grazing",
543
+ "Cheese Board",
544
+ "Dessert Board",
545
+ "Breakfast Board",
546
+ "Seasonal",
547
+ ] as const;
548
+
549
+ export const GRAZING_TABLE_PROTEINS = [
550
+ "Cured Meats",
551
+ "Artisan Cheeses",
552
+ "Smoked Fish",
553
+ "Plant-Based",
554
+ "Crudité",
555
+ ] as const;
556
+
557
+ export const GRAZING_TABLE_DIETARY = [
558
+ "Gluten-Free",
559
+ "Vegan",
560
+ "Nut-Free",
561
+ "Dairy-Free",
562
+ ] as const;
563
+
564
+ export function encodeGrazingOption(
565
+ kind: GrazingOptionKind,
566
+ value: string
567
+ ): string {
568
+ return `${kind}:${value}`;
569
+ }
570
+
571
+ export interface ParsedGrazingOptions {
572
+ styles: string[];
573
+ proteins: string[];
574
+ dietary: string[];
575
+ other: string[];
576
+ }
577
+
578
+ export function parseGrazingOptions(
579
+ goodsOrServices?: readonly string[] | null
580
+ ): ParsedGrazingOptions {
581
+ const result: ParsedGrazingOptions = {
582
+ styles: [],
583
+ proteins: [],
584
+ dietary: [],
585
+ other: [],
586
+ };
587
+ if (!goodsOrServices) return result;
588
+
589
+ for (const raw of goodsOrServices) {
590
+ if (raw.startsWith("style:")) {
591
+ result.styles.push(raw.slice("style:".length));
592
+ } else if (raw.startsWith("protein:")) {
593
+ result.proteins.push(raw.slice("protein:".length));
594
+ } else if (raw.startsWith("dietary:")) {
595
+ result.dietary.push(raw.slice("dietary:".length));
596
+ } else if (raw.length > 0) {
597
+ result.other.push(raw);
598
+ }
599
+ }
600
+ return result;
601
+ }
602
+
603
+ export function serializeGrazingOptions(opts: {
604
+ styles?: readonly string[];
605
+ proteins?: readonly string[];
606
+ dietary?: readonly string[];
607
+ }): string[] {
608
+ return [
609
+ ...(opts.styles ?? []).map((v) => encodeGrazingOption("style", v)),
610
+ ...(opts.proteins ?? []).map((v) => encodeGrazingOption("protein", v)),
611
+ ...(opts.dietary ?? []).map((v) => encodeGrazingOption("dietary", v)),
612
+ ];
613
+ }
@@ -30,6 +30,23 @@ export interface AvailabilityBlockedRange {
30
30
  end: Date | string;
31
31
  }
32
32
 
33
+ /**
34
+ * Minimum shape needed to decide whether a service can take a bash on a
35
+ * calendar day. One confirmed/approved booking occupies the whole day.
36
+ */
37
+ export interface ServiceDayAvailabilityInput {
38
+ availableHours?: unknown;
39
+ serviceRatesAssociation?: {
40
+ serviceSpecialRates?: Array<{
41
+ isAvailable?: boolean | null;
42
+ startDate?: Date | string | null;
43
+ endDate?: Date | string | null;
44
+ }> | null;
45
+ } | null;
46
+ /** Occupied ranges from Confirmed/Approved bookings (public list). */
47
+ bookedRanges?: readonly AvailabilityBlockedRange[] | null;
48
+ }
49
+
33
50
  const WEEKDAY_NAMES = [
34
51
  "Monday",
35
52
  "Tuesday",
@@ -156,9 +173,72 @@ export function isAvailabilityDayOpen(
156
173
  return openDays[name] !== false;
157
174
  }
158
175
 
176
+ /** Owner-blocked ranges from `ServiceSpecialRates` where `isAvailable` is false. */
177
+ export function blockedRangesFromSpecialRates(
178
+ specialRates:
179
+ | NonNullable<
180
+ NonNullable<
181
+ ServiceDayAvailabilityInput["serviceRatesAssociation"]
182
+ >["serviceSpecialRates"]
183
+ >
184
+ | null
185
+ | undefined
186
+ ): AvailabilityBlockedRange[] {
187
+ if (!specialRates) return [];
188
+ const ranges: AvailabilityBlockedRange[] = [];
189
+ for (const rate of specialRates) {
190
+ if (rate?.isAvailable === false && rate.startDate && rate.endDate) {
191
+ ranges.push({ start: rate.startDate, end: rate.endDate });
192
+ }
193
+ }
194
+ return ranges;
195
+ }
196
+
197
+ /**
198
+ * True when the service can take a bash on `date`: weekly hours are open,
199
+ * the owner has not blocked the day, and no confirmed/approved booking
200
+ * already occupies it. Missing hours mean every weekday is treated as open.
201
+ */
202
+ export function isServiceAvailableOnDate(
203
+ service: ServiceDayAvailabilityInput,
204
+ date: Date
205
+ ): boolean {
206
+ const config = parseAvailableHours(service.availableHours);
207
+ if (!isAvailabilityDayOpen(config?.openDays, date)) return false;
208
+ if (
209
+ isDateBlocked(
210
+ blockedRangesFromSpecialRates(
211
+ service.serviceRatesAssociation?.serviceSpecialRates
212
+ ),
213
+ date
214
+ )
215
+ ) {
216
+ return false;
217
+ }
218
+ if (isDateBlocked(service.bookedRanges ?? [], date)) return false;
219
+ return true;
220
+ }
221
+
222
+ export function filterServicesAvailableOnDate<T extends ServiceDayAvailabilityInput>(
223
+ services: readonly T[],
224
+ date: Date
225
+ ): T[] {
226
+ return services.filter((service) => isServiceAvailableOnDate(service, date));
227
+ }
228
+
229
+ export function countServicesAvailableOnDate<
230
+ T extends ServiceDayAvailabilityInput,
231
+ >(services: readonly T[], date: Date): number {
232
+ let count = 0;
233
+ for (const service of services) {
234
+ if (isServiceAvailableOnDate(service, date)) count += 1;
235
+ }
236
+ return count;
237
+ }
238
+
159
239
  /** Whether a date falls within any owner-blocked (unavailable) range. */
160
240
  export function isDateBlocked(
161
- blockedRanges: AvailabilityBlockedRange[],
241
+ blockedRanges: readonly AvailabilityBlockedRange[],
162
242
  date: Date
163
243
  ): boolean {
164
244
  const day = startOfLocalDay(date).getTime();
@@ -177,7 +257,7 @@ export function isDateBlocked(
177
257
  */
178
258
  export function computeEarliestOpenDate(params: {
179
259
  openDays?: Record<string, boolean> | null;
180
- blockedRanges?: AvailabilityBlockedRange[] | null;
260
+ blockedRanges?: readonly AvailabilityBlockedRange[] | null;
181
261
  fromDate?: Date;
182
262
  maxLookaheadDays?: number;
183
263
  }): Date | null {