@bash-app/bash-common 30.466.0 → 30.469.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 (62) hide show
  1. package/dist/__tests__/audienceImport.test.js +3 -1
  2. package/dist/__tests__/audienceImport.test.js.map +1 -1
  3. package/dist/__tests__/promoCodeScope.test.js +62 -1
  4. package/dist/__tests__/promoCodeScope.test.js.map +1 -1
  5. package/dist/audienceImport.d.ts +1 -1
  6. package/dist/audienceImport.d.ts.map +1 -1
  7. package/dist/audienceImport.js +1 -0
  8. package/dist/audienceImport.js.map +1 -1
  9. package/dist/extendedSchemas.d.ts +36 -0
  10. package/dist/extendedSchemas.d.ts.map +1 -1
  11. package/dist/extendedSchemas.js +3 -0
  12. package/dist/extendedSchemas.js.map +1 -1
  13. package/dist/index.d.ts +1 -0
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +1 -0
  16. package/dist/index.js.map +1 -1
  17. package/dist/membershipDefinitions.js +2 -2
  18. package/dist/membershipDefinitions.js.map +1 -1
  19. package/dist/promoCodeScope.d.ts +27 -0
  20. package/dist/promoCodeScope.d.ts.map +1 -1
  21. package/dist/promoCodeScope.js +38 -3
  22. package/dist/promoCodeScope.js.map +1 -1
  23. package/dist/utils/__tests__/addressUtils.test.js +3 -0
  24. package/dist/utils/__tests__/addressUtils.test.js.map +1 -1
  25. package/dist/utils/__tests__/crowdSizeValidation.test.js +1 -1
  26. package/dist/utils/__tests__/crowdSizeValidation.test.js.map +1 -1
  27. package/dist/utils/__tests__/eventTaskVolunteerRewardUtils.test.js +6 -0
  28. package/dist/utils/__tests__/eventTaskVolunteerRewardUtils.test.js.map +1 -1
  29. package/dist/utils/addressUtils.d.ts.map +1 -1
  30. package/dist/utils/addressUtils.js +28 -10
  31. package/dist/utils/addressUtils.js.map +1 -1
  32. package/dist/utils/crowdSizeValidation.d.ts.map +1 -1
  33. package/dist/utils/crowdSizeValidation.js +10 -0
  34. package/dist/utils/crowdSizeValidation.js.map +1 -1
  35. package/dist/utils/eventTimezoneIana.d.ts.map +1 -1
  36. package/dist/utils/eventTimezoneIana.js +16 -5
  37. package/dist/utils/eventTimezoneIana.js.map +1 -1
  38. package/dist/utils/service/__tests__/vendorPaymentWays.test.d.ts +2 -0
  39. package/dist/utils/service/__tests__/vendorPaymentWays.test.d.ts.map +1 -0
  40. package/dist/utils/service/__tests__/vendorPaymentWays.test.js +61 -0
  41. package/dist/utils/service/__tests__/vendorPaymentWays.test.js.map +1 -0
  42. package/dist/utils/service/vendorPaymentWays.d.ts +83 -0
  43. package/dist/utils/service/vendorPaymentWays.d.ts.map +1 -0
  44. package/dist/utils/service/vendorPaymentWays.js +306 -0
  45. package/dist/utils/service/vendorPaymentWays.js.map +1 -0
  46. package/package.json +1 -1
  47. package/prisma/schema.prisma +77 -0
  48. package/src/__tests__/audienceImport.test.ts +3 -0
  49. package/src/__tests__/promoCodeScope.test.ts +74 -0
  50. package/src/audienceImport.ts +1 -0
  51. package/src/extendedSchemas.ts +6 -0
  52. package/src/index.ts +1 -0
  53. package/src/membershipDefinitions.ts +2 -2
  54. package/src/promoCodeScope.ts +56 -4
  55. package/src/utils/__tests__/addressUtils.test.ts +6 -0
  56. package/src/utils/__tests__/crowdSizeValidation.test.ts +1 -1
  57. package/src/utils/__tests__/eventTaskVolunteerRewardUtils.test.ts +8 -0
  58. package/src/utils/addressUtils.ts +26 -10
  59. package/src/utils/crowdSizeValidation.ts +14 -0
  60. package/src/utils/eventTimezoneIana.ts +22 -10
  61. package/src/utils/service/__tests__/vendorPaymentWays.test.ts +87 -0
  62. package/src/utils/service/vendorPaymentWays.ts +384 -0
@@ -0,0 +1,384 @@
1
+ /**
2
+ * Host and vendor payment ways, and the cut of booth sales.
3
+ * A combined booth fee plus a percent is one option (`ways` lists both).
4
+ */
5
+
6
+ export const VENDOR_PAYMENT_WAYS = [
7
+ "free",
8
+ "flat",
9
+ "hourly",
10
+ "daily",
11
+ "percentage",
12
+ "negotiable",
13
+ ] as const;
14
+
15
+ export type VendorPaymentWay = (typeof VENDOR_PAYMENT_WAYS)[number];
16
+
17
+ export const VENDOR_PAYMENT_WAY_LABEL: Record<VendorPaymentWay, string> = {
18
+ free: "Free space",
19
+ flat: "Flat booth fee",
20
+ hourly: "Hourly",
21
+ daily: "Daily",
22
+ percentage: "Percentage of sales",
23
+ negotiable: "Open to negotiation",
24
+ };
25
+
26
+ export type VendorPaymentOptionInput = {
27
+ ways: readonly string[];
28
+ amountCents?: number | null;
29
+ percentage?: number | null;
30
+ };
31
+
32
+ export type HostVendorPaymentPrefs = {
33
+ ways: VendorPaymentWay[];
34
+ flatFeeCents: number | null;
35
+ percentage: number | null;
36
+ };
37
+
38
+ export type ListingPaymentSource = {
39
+ pricingModel?: string | null;
40
+ baseRateCents?: number | null;
41
+ preferredBoothFeeCents?: number | null;
42
+ flatRateCents?: number | null;
43
+ hourlyRateCents?: number | null;
44
+ dailyRateCents?: number | null;
45
+ };
46
+
47
+ export type SalesCut = {
48
+ owedCents: number;
49
+ remainingCents: number;
50
+ };
51
+
52
+ const WAY_SET = new Set<string>(VENDOR_PAYMENT_WAYS);
53
+
54
+ export function normalizeWays(raw: unknown): VendorPaymentWay[] {
55
+ const list = Array.isArray(raw) ? raw : [];
56
+ const seen = new Set<VendorPaymentWay>();
57
+ const out: VendorPaymentWay[] = [];
58
+ for (const item of list) {
59
+ if (typeof item !== "string" || !WAY_SET.has(item)) continue;
60
+ const way = item as VendorPaymentWay;
61
+ if (seen.has(way)) continue;
62
+ seen.add(way);
63
+ out.push(way);
64
+ }
65
+ return out;
66
+ }
67
+
68
+ export function clampPercent(raw: unknown): number | null {
69
+ if (raw == null || raw === "") return null;
70
+ const n = typeof raw === "number" ? raw : Number(raw);
71
+ if (!Number.isFinite(n)) return null;
72
+ return Math.min(100, Math.max(0, Math.round(n)));
73
+ }
74
+
75
+ export function clampCents(raw: unknown): number | null {
76
+ if (raw == null || raw === "") return null;
77
+ const n = typeof raw === "number" ? raw : Number(raw);
78
+ if (!Number.isFinite(n) || n < 0) return null;
79
+ return Math.round(n);
80
+ }
81
+
82
+ export function sanitizeHostVendorPayment(input: {
83
+ ways: unknown;
84
+ flatFeeCents?: unknown;
85
+ percentage?: unknown;
86
+ }): HostVendorPaymentPrefs {
87
+ const ways = normalizeWays(input.ways);
88
+ return {
89
+ ways,
90
+ flatFeeCents: ways.includes("flat") ? clampCents(input.flatFeeCents) : null,
91
+ percentage: ways.includes("percentage")
92
+ ? clampPercent(input.percentage)
93
+ : null,
94
+ };
95
+ }
96
+
97
+ export function sanitizePaymentOption(
98
+ input: unknown
99
+ ): VendorPaymentOptionInput | null {
100
+ if (!input || typeof input !== "object") return null;
101
+ const row = input as Record<string, unknown>;
102
+ const ways = normalizeWays(row.ways);
103
+ if (ways.length === 0) return null;
104
+ const needsAmount =
105
+ ways.includes("flat") || ways.includes("hourly") || ways.includes("daily");
106
+ const needsPercent = ways.includes("percentage");
107
+ return {
108
+ ways,
109
+ amountCents: needsAmount ? clampCents(row.amountCents) : null,
110
+ percentage: needsPercent ? clampPercent(row.percentage) : null,
111
+ };
112
+ }
113
+
114
+ export function sanitizePaymentOptions(
115
+ raw: unknown
116
+ ): VendorPaymentOptionInput[] {
117
+ if (!Array.isArray(raw)) return [];
118
+ return raw
119
+ .map((row) => sanitizePaymentOption(row))
120
+ .filter((row): row is VendorPaymentOptionInput => row != null);
121
+ }
122
+
123
+ function sameWays(a: readonly string[], b: readonly string[]): boolean {
124
+ const left = normalizeWays(a).slice().sort();
125
+ const right = normalizeWays(b).slice().sort();
126
+ if (left.length !== right.length) return false;
127
+ return left.every((way, i) => way === right[i]);
128
+ }
129
+
130
+ export function optionMatchesHost(
131
+ option: VendorPaymentOptionInput,
132
+ hostWays: readonly string[]
133
+ ): boolean {
134
+ const ways = normalizeWays(option.ways);
135
+ const host = normalizeWays(hostWays);
136
+ if (ways.length === 0 || host.length === 0) return false;
137
+ return ways.every((way) => host.includes(way));
138
+ }
139
+
140
+ /**
141
+ * Null means the host has not listed ways yet, so callers keep today's
142
+ * dropdown. An empty array means they listed ways and nothing overlaps.
143
+ */
144
+ export function overlappingPaymentOptions(
145
+ options: readonly VendorPaymentOptionInput[],
146
+ hostWays: readonly string[]
147
+ ): VendorPaymentOptionInput[] | null {
148
+ const host = normalizeWays(hostWays);
149
+ if (host.length === 0) return null;
150
+ return options.filter((option) => optionMatchesHost(option, host));
151
+ }
152
+
153
+ export function vendorFitsHostPayment(
154
+ options: readonly VendorPaymentOptionInput[],
155
+ hostWays: readonly string[]
156
+ ): boolean {
157
+ const overlap = overlappingPaymentOptions(options, hostWays);
158
+ if (overlap === null) return true;
159
+ return overlap.length > 0;
160
+ }
161
+
162
+ export function derivePaymentOptionsFromListing(
163
+ listing: ListingPaymentSource
164
+ ): VendorPaymentOptionInput[] {
165
+ const options: VendorPaymentOptionInput[] = [];
166
+ const add = (option: VendorPaymentOptionInput) => {
167
+ const cleaned = sanitizePaymentOption(option);
168
+ if (!cleaned) return;
169
+ const exists = options.some(
170
+ (current) =>
171
+ sameWays(current.ways, cleaned.ways) &&
172
+ current.amountCents === cleaned.amountCents &&
173
+ current.percentage === cleaned.percentage
174
+ );
175
+ if (!exists) options.push(cleaned);
176
+ };
177
+
178
+ if ((listing.preferredBoothFeeCents ?? 0) > 0) {
179
+ add({ ways: ["flat"], amountCents: listing.preferredBoothFeeCents });
180
+ }
181
+
182
+ const base = listing.baseRateCents ?? null;
183
+ switch (listing.pricingModel) {
184
+ case "FLAT_FEE":
185
+ case "PER_ATTENDEE":
186
+ if ((base ?? 0) > 0) add({ ways: ["flat"], amountCents: base });
187
+ break;
188
+ case "HOURLY":
189
+ if ((base ?? 0) > 0) add({ ways: ["hourly"], amountCents: base });
190
+ break;
191
+ case "PER_DAY":
192
+ if ((base ?? 0) > 0) add({ ways: ["daily"], amountCents: base });
193
+ break;
194
+ case "PRODUCT_SALES_PERCENTAGE":
195
+ add({
196
+ ways: ["percentage"],
197
+ percentage: base != null && base <= 100 ? base : null,
198
+ });
199
+ break;
200
+ case "TIERED":
201
+ case "CUSTOM":
202
+ add({ ways: ["negotiable"] });
203
+ break;
204
+ default:
205
+ break;
206
+ }
207
+
208
+ if ((listing.flatRateCents ?? 0) > 0) {
209
+ add({ ways: ["flat"], amountCents: listing.flatRateCents });
210
+ }
211
+ if ((listing.hourlyRateCents ?? 0) > 0) {
212
+ add({ ways: ["hourly"], amountCents: listing.hourlyRateCents });
213
+ }
214
+ if ((listing.dailyRateCents ?? 0) > 0) {
215
+ add({ ways: ["daily"], amountCents: listing.dailyRateCents });
216
+ }
217
+
218
+ const hasUnit = options.some((option) => {
219
+ const ways = normalizeWays(option.ways);
220
+ return (
221
+ ways.includes("flat") ||
222
+ ways.includes("hourly") ||
223
+ ways.includes("daily") ||
224
+ ways.includes("percentage") ||
225
+ ways.includes("free")
226
+ );
227
+ });
228
+ if (!hasUnit) add({ ways: ["negotiable"] });
229
+ return options;
230
+ }
231
+
232
+ export function effectivePaymentOptions(
233
+ explicit: readonly VendorPaymentOptionInput[] | null | undefined,
234
+ listing: ListingPaymentSource
235
+ ): VendorPaymentOptionInput[] {
236
+ const cleaned = sanitizePaymentOptions(explicit ?? []);
237
+ if (cleaned.length > 0) return cleaned;
238
+ return derivePaymentOptionsFromListing(listing);
239
+ }
240
+
241
+ export function feeStructureToWays(
242
+ feeStructure: string,
243
+ hasAmount: boolean
244
+ ): VendorPaymentWay[] {
245
+ switch (feeStructure) {
246
+ case "free":
247
+ return ["free"];
248
+ case "flat":
249
+ case "fixed":
250
+ return ["flat"];
251
+ case "hourly":
252
+ return ["hourly"];
253
+ case "daily":
254
+ return ["daily"];
255
+ case "negotiable":
256
+ return ["negotiable"];
257
+ case "flat_plus_percentage":
258
+ return ["flat", "percentage"];
259
+ case "percentage":
260
+ return hasAmount ? ["flat", "percentage"] : ["percentage"];
261
+ default:
262
+ return [];
263
+ }
264
+ }
265
+
266
+ export function waysToFeeStructure(ways: readonly string[]): string {
267
+ const normalized = normalizeWays(ways);
268
+ if (normalized.includes("flat") && normalized.includes("percentage")) {
269
+ return "flat_plus_percentage";
270
+ }
271
+ if (normalized.length === 1) return normalized[0];
272
+ return normalized[0] ?? "negotiable";
273
+ }
274
+
275
+ export function requestMatchesOverlap(args: {
276
+ hostWays: readonly string[];
277
+ vendorOptions: readonly VendorPaymentOptionInput[];
278
+ feeStructure: string;
279
+ hasAmount: boolean;
280
+ }): { ok: true } | { ok: false; reason: string } {
281
+ const overlap = overlappingPaymentOptions(args.vendorOptions, args.hostWays);
282
+ if (overlap === null) return { ok: true };
283
+ const requested = feeStructureToWays(args.feeStructure, args.hasAmount);
284
+ if (requested.length === 0) {
285
+ return {
286
+ ok: false,
287
+ reason: "Choose a way to pay that this bash accepts.",
288
+ };
289
+ }
290
+ const match = overlap.some((option) => sameWays(option.ways, requested));
291
+ if (!match) {
292
+ return {
293
+ ok: false,
294
+ reason: "That way to pay is not one you both accept.",
295
+ };
296
+ }
297
+ return { ok: true };
298
+ }
299
+
300
+ export function paymentOptionLabel(option: VendorPaymentOptionInput): string {
301
+ const ways = normalizeWays(option.ways);
302
+ const dollars =
303
+ option.amountCents != null
304
+ ? `$${(option.amountCents / 100).toFixed(2)}`
305
+ : null;
306
+ if (ways.includes("flat") && ways.includes("percentage")) {
307
+ const pct = option.percentage ?? 0;
308
+ return dollars
309
+ ? `${dollars} plus ${pct}% of sales`
310
+ : `${pct}% of sales plus a booth fee`;
311
+ }
312
+ if (ways.length === 1 && ways[0] === "percentage") {
313
+ return `${option.percentage ?? 0}% of sales`;
314
+ }
315
+ if (ways.length === 1 && ways[0] === "flat") {
316
+ return dollars ? `${dollars} flat fee` : "Flat booth fee";
317
+ }
318
+ if (ways.length === 1 && ways[0] === "hourly") {
319
+ return dollars ? `${dollars} per hour` : "Hourly";
320
+ }
321
+ if (ways.length === 1 && ways[0] === "daily") {
322
+ return dollars ? `${dollars} per day` : "Daily";
323
+ }
324
+ if (ways.length === 1 && ways[0] === "free") return "Free space";
325
+ if (ways.length === 1 && ways[0] === "negotiable") return "Open to negotiation";
326
+ return ways.map((way) => VENDOR_PAYMENT_WAY_LABEL[way]).join(" + ");
327
+ }
328
+
329
+ export function snapshotFromBookingRequest(request: {
330
+ feeStructure: string;
331
+ vendorFee?: number | null;
332
+ percentageRate?: number | null;
333
+ }): {
334
+ paymentOptionWays: VendorPaymentWay[];
335
+ paymentOptionAmountCents: number | null;
336
+ paymentOptionPercentage: number | null;
337
+ paymentOptionBasis: "ALL_SALES" | null;
338
+ } {
339
+ const hasAmount = request.vendorFee != null && request.vendorFee > 0;
340
+ const ways = feeStructureToWays(request.feeStructure, hasAmount);
341
+ const includesPercent = ways.includes("percentage");
342
+ return {
343
+ paymentOptionWays: ways,
344
+ paymentOptionAmountCents: hasAmount
345
+ ? Math.round(request.vendorFee! * 100)
346
+ : null,
347
+ paymentOptionPercentage: includesPercent
348
+ ? clampPercent(request.percentageRate)
349
+ : null,
350
+ paymentOptionBasis: includesPercent ? "ALL_SALES" : null,
351
+ };
352
+ }
353
+
354
+ /**
355
+ * Null when sales are not confirmed yet. Nothing is owed until the host
356
+ * confirms the number the vendor reported.
357
+ */
358
+ export function computeSalesCut(input: {
359
+ confirmedSalesCents: number | null | undefined;
360
+ percentage: number | null | undefined;
361
+ boothFeeAlreadyPaidCents?: number | null;
362
+ }): SalesCut | null {
363
+ if (input.confirmedSalesCents == null) return null;
364
+ const sales = Math.max(0, Math.round(input.confirmedSalesCents));
365
+ const pct = clampPercent(input.percentage) ?? 0;
366
+ const owedCents = Math.round((sales * pct) / 100);
367
+ const paid = Math.max(0, Math.round(input.boothFeeAlreadyPaidCents ?? 0));
368
+ return {
369
+ owedCents,
370
+ remainingCents: Math.max(0, owedCents - paid),
371
+ };
372
+ }
373
+
374
+ export function readHostVendorPayment(source: unknown): HostVendorPaymentPrefs {
375
+ if (!source || typeof source !== "object") {
376
+ return { ways: [], flatFeeCents: null, percentage: null };
377
+ }
378
+ const row = source as Record<string, unknown>;
379
+ return sanitizeHostVendorPayment({
380
+ ways: row.vendorPaymentWays ?? row.ways,
381
+ flatFeeCents: row.vendorPaymentFlatFeeCents ?? row.flatFeeCents,
382
+ percentage: row.vendorPaymentPercentage ?? row.percentage,
383
+ });
384
+ }