@bash-app/bash-common 30.224.0 → 30.226.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 (109) hide show
  1. package/dist/__tests__/agePolicy.test.d.ts +2 -0
  2. package/dist/__tests__/agePolicy.test.d.ts.map +1 -0
  3. package/dist/__tests__/agePolicy.test.js +203 -0
  4. package/dist/__tests__/agePolicy.test.js.map +1 -0
  5. package/dist/__tests__/getDisplayGender.test.d.ts +2 -0
  6. package/dist/__tests__/getDisplayGender.test.d.ts.map +1 -0
  7. package/dist/__tests__/getDisplayGender.test.js +34 -0
  8. package/dist/__tests__/getDisplayGender.test.js.map +1 -0
  9. package/dist/__tests__/statusEnums.test.d.ts +2 -0
  10. package/dist/__tests__/statusEnums.test.d.ts.map +1 -0
  11. package/dist/__tests__/statusEnums.test.js +24 -0
  12. package/dist/__tests__/statusEnums.test.js.map +1 -0
  13. package/dist/aiApproval.d.ts +64 -0
  14. package/dist/aiApproval.d.ts.map +1 -0
  15. package/dist/aiApproval.js +54 -0
  16. package/dist/aiApproval.js.map +1 -0
  17. package/dist/definitions.d.ts +44 -2
  18. package/dist/definitions.d.ts.map +1 -1
  19. package/dist/definitions.js +106 -2
  20. package/dist/definitions.js.map +1 -1
  21. package/dist/extendedSchemas.d.ts +237 -104
  22. package/dist/extendedSchemas.d.ts.map +1 -1
  23. package/dist/extendedSchemas.js +12 -2
  24. package/dist/extendedSchemas.js.map +1 -1
  25. package/dist/index.d.ts +1 -0
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +1 -0
  28. package/dist/index.js.map +1 -1
  29. package/dist/legalTemplates.d.ts +1 -1
  30. package/dist/legalTemplates.js +1 -1
  31. package/dist/utils/__tests__/badgeUtils.test.d.ts +2 -0
  32. package/dist/utils/__tests__/badgeUtils.test.d.ts.map +1 -0
  33. package/dist/utils/__tests__/badgeUtils.test.js +157 -0
  34. package/dist/utils/__tests__/badgeUtils.test.js.map +1 -0
  35. package/dist/utils/__tests__/bashPointsPaymentUtils.test.d.ts +2 -0
  36. package/dist/utils/__tests__/bashPointsPaymentUtils.test.d.ts.map +1 -0
  37. package/dist/utils/__tests__/bashPointsPaymentUtils.test.js +114 -0
  38. package/dist/utils/__tests__/bashPointsPaymentUtils.test.js.map +1 -0
  39. package/dist/utils/__tests__/contentFilterUtils.test.d.ts +2 -0
  40. package/dist/utils/__tests__/contentFilterUtils.test.d.ts.map +1 -0
  41. package/dist/utils/__tests__/contentFilterUtils.test.js +443 -0
  42. package/dist/utils/__tests__/contentFilterUtils.test.js.map +1 -0
  43. package/dist/utils/__tests__/displayDateUtils.test.d.ts +2 -0
  44. package/dist/utils/__tests__/displayDateUtils.test.d.ts.map +1 -0
  45. package/dist/utils/__tests__/displayDateUtils.test.js +105 -0
  46. package/dist/utils/__tests__/displayDateUtils.test.js.map +1 -0
  47. package/dist/utils/__tests__/getBackendHost.test.d.ts +2 -0
  48. package/dist/utils/__tests__/getBackendHost.test.d.ts.map +1 -0
  49. package/dist/utils/__tests__/getBackendHost.test.js +104 -0
  50. package/dist/utils/__tests__/getBackendHost.test.js.map +1 -0
  51. package/dist/utils/__tests__/getFrontendHost.test.d.ts +2 -0
  52. package/dist/utils/__tests__/getFrontendHost.test.d.ts.map +1 -0
  53. package/dist/utils/__tests__/getFrontendHost.test.js +207 -0
  54. package/dist/utils/__tests__/getFrontendHost.test.js.map +1 -0
  55. package/dist/utils/__tests__/promoCodesUtils.test.d.ts +2 -0
  56. package/dist/utils/__tests__/promoCodesUtils.test.d.ts.map +1 -0
  57. package/dist/utils/__tests__/promoCodesUtils.test.js +183 -0
  58. package/dist/utils/__tests__/promoCodesUtils.test.js.map +1 -0
  59. package/dist/utils/__tests__/qrCodeUtils.test.d.ts +2 -0
  60. package/dist/utils/__tests__/qrCodeUtils.test.d.ts.map +1 -0
  61. package/dist/utils/__tests__/qrCodeUtils.test.js +99 -0
  62. package/dist/utils/__tests__/qrCodeUtils.test.js.map +1 -0
  63. package/dist/utils/__tests__/stripeAccountUtils.test.d.ts +2 -0
  64. package/dist/utils/__tests__/stripeAccountUtils.test.d.ts.map +1 -0
  65. package/dist/utils/__tests__/stripeAccountUtils.test.js +75 -0
  66. package/dist/utils/__tests__/stripeAccountUtils.test.js.map +1 -0
  67. package/dist/utils/blog/__tests__/getRelatedBlogPosts.test.d.ts +2 -0
  68. package/dist/utils/blog/__tests__/getRelatedBlogPosts.test.d.ts.map +1 -0
  69. package/dist/utils/blog/__tests__/getRelatedBlogPosts.test.js +233 -0
  70. package/dist/utils/blog/__tests__/getRelatedBlogPosts.test.js.map +1 -0
  71. package/dist/utils/blog/blogCommentDbUtils.d.ts +161 -0
  72. package/dist/utils/blog/blogCommentDbUtils.d.ts.map +1 -0
  73. package/dist/utils/blog/blogCommentDbUtils.js +111 -0
  74. package/dist/utils/blog/blogCommentDbUtils.js.map +1 -0
  75. package/dist/utils/blog/blogDbUtils.d.ts +10 -0
  76. package/dist/utils/blog/blogDbUtils.d.ts.map +1 -1
  77. package/dist/utils/blog/blogDbUtils.js +71 -0
  78. package/dist/utils/blog/blogDbUtils.js.map +1 -1
  79. package/dist/utils/urlUtils.d.ts +10 -0
  80. package/dist/utils/urlUtils.d.ts.map +1 -1
  81. package/dist/utils/urlUtils.js +24 -5
  82. package/dist/utils/urlUtils.js.map +1 -1
  83. package/dist/utils/userUtils.d.ts +32 -0
  84. package/dist/utils/userUtils.d.ts.map +1 -1
  85. package/dist/utils/userUtils.js +27 -0
  86. package/dist/utils/userUtils.js.map +1 -1
  87. package/package.json +1 -1
  88. package/prisma/schema.prisma +951 -845
  89. package/src/__tests__/agePolicy.test.ts +257 -0
  90. package/src/__tests__/getDisplayGender.test.ts +50 -0
  91. package/src/__tests__/statusEnums.test.ts +36 -0
  92. package/src/definitions.ts +138 -1
  93. package/src/extendedSchemas.ts +41 -2
  94. package/src/index.ts +1 -0
  95. package/src/legalTemplates.ts +1 -1
  96. package/src/utils/__tests__/badgeUtils.test.ts +191 -0
  97. package/src/utils/__tests__/bashPointsPaymentUtils.test.ts +153 -0
  98. package/src/utils/__tests__/contentFilterUtils.test.ts +547 -0
  99. package/src/utils/__tests__/displayDateUtils.test.ts +113 -0
  100. package/src/utils/__tests__/getBackendHost.test.ts +128 -0
  101. package/src/utils/__tests__/getFrontendHost.test.ts +232 -0
  102. package/src/utils/__tests__/promoCodesUtils.test.ts +237 -0
  103. package/src/utils/__tests__/qrCodeUtils.test.ts +143 -0
  104. package/src/utils/__tests__/stripeAccountUtils.test.ts +93 -0
  105. package/src/utils/blog/__tests__/getRelatedBlogPosts.test.ts +271 -0
  106. package/src/utils/blog/blogCommentDbUtils.ts +161 -0
  107. package/src/utils/blog/blogDbUtils.ts +95 -1
  108. package/src/utils/urlUtils.ts +27 -5
  109. package/src/utils/userUtils.ts +40 -0
@@ -0,0 +1,128 @@
1
+ /**
2
+ * getBackendHost — chooses the API origin (api.bash.community vs localhost:3500
3
+ * vs the bundled API_HOST for native Capacitor builds).
4
+ *
5
+ * This file pins the contract so a future "let's tidy up env handling" change
6
+ * doesn't accidentally re-introduce the same class of bug we just fixed in
7
+ * getFrontendHost (server-only env vars leaking into the browser code path).
8
+ */
9
+ import { getBackendHost } from "../urlUtils.js";
10
+
11
+ type WindowLocationStub = {
12
+ protocol: string;
13
+ hostname: string;
14
+ host: string;
15
+ port: string;
16
+ };
17
+
18
+ type CapacitorStub = { isNativePlatform?: () => boolean };
19
+
20
+ const ORIGINAL_WINDOW = (globalThis as { window?: unknown }).window;
21
+
22
+ function setWindow(loc: WindowLocationStub, capacitor?: CapacitorStub): void {
23
+ (
24
+ globalThis as unknown as {
25
+ window: { location: WindowLocationStub; Capacitor?: CapacitorStub };
26
+ }
27
+ ).window = {
28
+ location: loc,
29
+ ...(capacitor ? { Capacitor: capacitor } : {}),
30
+ };
31
+ }
32
+
33
+ function clearWindow(): void {
34
+ if (ORIGINAL_WINDOW === undefined) {
35
+ delete (globalThis as { window?: unknown }).window;
36
+ } else {
37
+ (globalThis as { window?: unknown }).window = ORIGINAL_WINDOW;
38
+ }
39
+ }
40
+
41
+ afterEach(() => {
42
+ clearWindow();
43
+ });
44
+
45
+ describe("getBackendHost — local dev", () => {
46
+ it("returns http://localhost:3500 for localhost (does NOT reuse :3000 from the page)", () => {
47
+ setWindow({
48
+ protocol: "http:",
49
+ hostname: "localhost",
50
+ host: "localhost:3000",
51
+ port: "3000",
52
+ });
53
+ expect(getBackendHost()).toBe("http://localhost:3500");
54
+ });
55
+
56
+ it("returns http://127.0.0.1:3500 for 127.0.0.1", () => {
57
+ setWindow({
58
+ protocol: "http:",
59
+ hostname: "127.0.0.1",
60
+ host: "127.0.0.1:3000",
61
+ port: "3000",
62
+ });
63
+ expect(getBackendHost()).toBe("http://127.0.0.1:3500");
64
+ });
65
+
66
+ it("uses the LAN IP for 192.168.x.x (so a phone on the same wifi can hit the dev API)", () => {
67
+ setWindow({
68
+ protocol: "http:",
69
+ hostname: "192.168.1.42",
70
+ host: "192.168.1.42:3000",
71
+ port: "3000",
72
+ });
73
+ expect(getBackendHost()).toBe("http://192.168.1.42:3500");
74
+ });
75
+ });
76
+
77
+ describe("getBackendHost — deployed web", () => {
78
+ // For deployed web, getBackendHost falls back to API_HOST (compiled in from
79
+ // process.env.REACT_APP_API at bundle time; defaults to http://localhost:3500
80
+ // when unset, which is what jest sees here). The contract we pin is "for any
81
+ // non-local hostname, do NOT derive the API URL from window.location" — that
82
+ // would silently route prod browsers at api.bash.community… instead of the
83
+ // configured API_HOST.
84
+ it("does not derive the backend host from window.location for deployed sites", () => {
85
+ setWindow({
86
+ protocol: "https:",
87
+ hostname: "bash.community",
88
+ host: "bash.community",
89
+ port: "",
90
+ });
91
+ const result = getBackendHost();
92
+ expect(result).not.toContain("bash.community:3500");
93
+ expect(result).not.toContain("bash.community:3000");
94
+ });
95
+
96
+ it("does not derive the backend host from a QA subdomain either", () => {
97
+ setWindow({
98
+ protocol: "https:",
99
+ hostname: "qa.bash.community",
100
+ host: "qa.bash.community",
101
+ port: "",
102
+ });
103
+ const result = getBackendHost();
104
+ expect(result).not.toContain("qa.bash.community:3500");
105
+ });
106
+ });
107
+
108
+ describe("getBackendHost — Capacitor native (iOS / Android)", () => {
109
+ it("uses the bundled API_HOST when running inside a Capacitor WebView (hostname is 'localhost' but it's not really local dev)", () => {
110
+ // Capacitor loads from a localhost-like origin inside the WebView. If we
111
+ // routed to the localhost dev API based on hostname alone, the app would
112
+ // hit a port that doesn't exist on the device. The native check must win.
113
+ setWindow(
114
+ {
115
+ protocol: "capacitor:",
116
+ hostname: "localhost",
117
+ host: "localhost",
118
+ port: "",
119
+ },
120
+ { isNativePlatform: () => true }
121
+ );
122
+ const result = getBackendHost();
123
+ // We can't pin the exact value (depends on REACT_APP_API at build time),
124
+ // but we CAN pin the negative: it must not be the dev-server URL just
125
+ // because the WebView happens to say hostname=localhost.
126
+ expect(result).toBe(process.env.REACT_APP_API ?? "http://localhost:3500");
127
+ });
128
+ });
@@ -0,0 +1,232 @@
1
+ /**
2
+ * getFrontendHost — the single source of truth for the URL prefix that goes
3
+ * into every shareable Bash link (SharePopup, embed snippets, LinkedIn
4
+ * Announce, Facebook event helper, QR codes…).
5
+ *
6
+ * THE BUG THIS FILE PINS
7
+ * ----------------------
8
+ * Pre-fix, getFrontendHost() called isProduction() which inspects
9
+ * process.env.ENVIRONMENT / BASH_ENV / NODE_ENV. Those env vars are server-
10
+ * only — Vite's envPlugin only inlines REACT_APP_* / VITE_* / NODE_ENV /
11
+ * PUBLIC_URL into the browser bundle. As a result, in the production browser
12
+ * `process.env.ENVIRONMENT` is undefined → isProduction() returns false →
13
+ * getFrontendHost() falls into the dev branch → appends ":3000" to whatever
14
+ * hostname the user is on. Real user-visible breakage:
15
+ *
16
+ * https://bash.community:3000/bash/cmo3...-empanada-fest-slc?ref=...
17
+ *
18
+ * Facebook / iMessage / LinkedIn cannot fetch a preview because :3000 isn't
19
+ * publicly reachable, and the link itself looks broken to recipients.
20
+ *
21
+ * THE CONTRACT WE PIN
22
+ * -------------------
23
+ * localhost / 127.0.0.1 / 192.168.x.x → preserve the dev port (3000 default)
24
+ * <anything else> → use window.location.host AS-IS
25
+ * (no port appending, no env-var dependency)
26
+ *
27
+ * Browser-hostname-driven, not env-var-driven, so the answer is correct
28
+ * regardless of how CI sets ENVIRONMENT / BASH_ENV.
29
+ */
30
+ import { getFrontendHost } from "../urlUtils.js";
31
+
32
+ type WindowLocationStub = {
33
+ protocol: string;
34
+ hostname: string;
35
+ host: string;
36
+ port: string;
37
+ };
38
+
39
+ const ORIGINAL_WINDOW = (globalThis as { window?: unknown }).window;
40
+
41
+ function setLocation(loc: WindowLocationStub): void {
42
+ (globalThis as unknown as { window: { location: WindowLocationStub } }).window = {
43
+ location: loc,
44
+ };
45
+ }
46
+
47
+ function clearLocation(): void {
48
+ if (ORIGINAL_WINDOW === undefined) {
49
+ delete (globalThis as { window?: unknown }).window;
50
+ } else {
51
+ (globalThis as { window?: unknown }).window = ORIGINAL_WINDOW;
52
+ }
53
+ }
54
+
55
+ afterEach(() => {
56
+ clearLocation();
57
+ });
58
+
59
+ // ============================================================================
60
+ // PRODUCTION (the regression we're fixing)
61
+ // ============================================================================
62
+
63
+ describe("getFrontendHost — production (the share-link bug)", () => {
64
+ it("returns 'https://bash.community' for the production domain (NO :3000)", () => {
65
+ setLocation({
66
+ protocol: "https:",
67
+ hostname: "bash.community",
68
+ host: "bash.community",
69
+ port: "",
70
+ });
71
+ const host = getFrontendHost();
72
+ expect(host).toBe("https://bash.community");
73
+ expect(host).not.toMatch(/:3000/);
74
+ });
75
+
76
+ it("does NOT append :3000 to bash.community even if the bundle has no ENVIRONMENT env var", () => {
77
+ // Simulate the production browser bundle: ENVIRONMENT/BASH_ENV are NOT inlined.
78
+ const origEnv = process.env.ENVIRONMENT;
79
+ const origBashEnv = process.env.BASH_ENV;
80
+ const origNodeEnv = process.env.NODE_ENV;
81
+ try {
82
+ delete process.env.ENVIRONMENT;
83
+ delete process.env.BASH_ENV;
84
+ // Vite would replace process.env.NODE_ENV with "production" at build time.
85
+ (process.env as { NODE_ENV?: string }).NODE_ENV = "production";
86
+
87
+ setLocation({
88
+ protocol: "https:",
89
+ hostname: "bash.community",
90
+ host: "bash.community",
91
+ port: "",
92
+ });
93
+
94
+ expect(getFrontendHost()).toBe("https://bash.community");
95
+ } finally {
96
+ if (origEnv === undefined) {
97
+ delete process.env.ENVIRONMENT;
98
+ } else {
99
+ process.env.ENVIRONMENT = origEnv;
100
+ }
101
+ if (origBashEnv === undefined) {
102
+ delete process.env.BASH_ENV;
103
+ } else {
104
+ process.env.BASH_ENV = origBashEnv;
105
+ }
106
+ if (origNodeEnv === undefined) {
107
+ delete (process.env as { NODE_ENV?: string }).NODE_ENV;
108
+ } else {
109
+ (process.env as { NODE_ENV?: string }).NODE_ENV = origNodeEnv;
110
+ }
111
+ }
112
+ });
113
+
114
+ it("works for the QA subdomain (qa.bash.community → no :3000)", () => {
115
+ setLocation({
116
+ protocol: "https:",
117
+ hostname: "qa.bash.community",
118
+ host: "qa.bash.community",
119
+ port: "",
120
+ });
121
+ expect(getFrontendHost()).toBe("https://qa.bash.community");
122
+ });
123
+
124
+ it("works for any vanity subdomain (org.bash.community → no :3000)", () => {
125
+ setLocation({
126
+ protocol: "https:",
127
+ hostname: "myorg.bash.community",
128
+ host: "myorg.bash.community",
129
+ port: "",
130
+ });
131
+ expect(getFrontendHost()).toBe("https://myorg.bash.community");
132
+ });
133
+
134
+ it("preserves an EXPLICIT non-default port if the deployed site is running on one", () => {
135
+ // (uncommon but legal — e.g. preview deploy on :8080)
136
+ setLocation({
137
+ protocol: "https:",
138
+ hostname: "preview.bash.community",
139
+ host: "preview.bash.community:8080",
140
+ port: "8080",
141
+ });
142
+ expect(getFrontendHost()).toBe("https://preview.bash.community:8080");
143
+ });
144
+ });
145
+
146
+ // ============================================================================
147
+ // LOCAL DEV (must keep working)
148
+ // ============================================================================
149
+
150
+ describe("getFrontendHost — local development", () => {
151
+ it("returns http://localhost:3000 for hostname=localhost", () => {
152
+ setLocation({
153
+ protocol: "http:",
154
+ hostname: "localhost",
155
+ host: "localhost:3000",
156
+ port: "3000",
157
+ });
158
+ expect(getFrontendHost()).toBe("http://localhost:3000");
159
+ });
160
+
161
+ it("returns http://127.0.0.1:3000 for hostname=127.0.0.1", () => {
162
+ setLocation({
163
+ protocol: "http:",
164
+ hostname: "127.0.0.1",
165
+ host: "127.0.0.1:3000",
166
+ port: "3000",
167
+ });
168
+ expect(getFrontendHost()).toBe("http://127.0.0.1:3000");
169
+ });
170
+
171
+ it("returns the LAN-IP dev URL with :3000 for 192.168.x.x (mobile-on-same-wifi dev)", () => {
172
+ setLocation({
173
+ protocol: "http:",
174
+ hostname: "192.168.1.42",
175
+ host: "192.168.1.42:3000",
176
+ port: "3000",
177
+ });
178
+ expect(getFrontendHost()).toBe("http://192.168.1.42:3000");
179
+ });
180
+ });
181
+
182
+ // ============================================================================
183
+ // NGROK / TUNNELS (deployed-equivalent — should NOT get :3000)
184
+ // ============================================================================
185
+
186
+ describe("getFrontendHost — ngrok / public tunnels", () => {
187
+ it("does NOT append :3000 to an ngrok-free.dev tunnel hostname", () => {
188
+ // ngrok forwards port 443 publicly to the local :3000; the link Facebook
189
+ // crawls must hit the public 443, never the local 3000.
190
+ setLocation({
191
+ protocol: "https:",
192
+ hostname: "abc-123.ngrok-free.dev",
193
+ host: "abc-123.ngrok-free.dev",
194
+ port: "",
195
+ });
196
+ expect(getFrontendHost()).toBe("https://abc-123.ngrok-free.dev");
197
+ });
198
+
199
+ it("does NOT append :3000 to a .lhr.life tunnel", () => {
200
+ setLocation({
201
+ protocol: "https:",
202
+ hostname: "myapp.lhr.life",
203
+ host: "myapp.lhr.life",
204
+ port: "",
205
+ });
206
+ expect(getFrontendHost()).toBe("https://myapp.lhr.life");
207
+ });
208
+ });
209
+
210
+ // ============================================================================
211
+ // SHARE-URL ASSEMBLY (the exact concatenation SharePopup performs)
212
+ // ============================================================================
213
+
214
+ describe("getFrontendHost — composed share URL (SharePopup integration)", () => {
215
+ it("composes the canonical share URL Facebook can crawl", () => {
216
+ setLocation({
217
+ protocol: "https:",
218
+ hostname: "bash.community",
219
+ host: "bash.community",
220
+ port: "",
221
+ });
222
+ const host = getFrontendHost();
223
+ const path = "/bash/evt_123-empanada-fest-slc";
224
+ const fullShareUrl = host + path;
225
+
226
+ expect(fullShareUrl).toBe(
227
+ "https://bash.community/bash/evt_123-empanada-fest-slc"
228
+ );
229
+ // Hard guard — this is the literal regression we're preventing.
230
+ expect(fullShareUrl).not.toMatch(/bash\.community:3000/);
231
+ });
232
+ });
@@ -0,0 +1,237 @@
1
+ /**
2
+ * promoCodesUtils — locate / aggregate / re-distribute promo codes across a
3
+ * BashEvent's ticket tiers.
4
+ *
5
+ * These three helpers are tiny but they sit on the path between checkout
6
+ * UI ↔ pricing engine. A bug in `findPromoCodeFromBashEvent` would cause a
7
+ * promo to silently fail to apply at checkout (lost revenue / angry user).
8
+ * A bug in `updatePromoCodesOnBashEventTicketTiers` would scramble which
9
+ * tier a promo code belongs to after edits in the host wizard.
10
+ */
11
+ import {
12
+ findPromoCodeFromBashEvent,
13
+ getPromoCodesFromBashEvent,
14
+ updatePromoCodesOnBashEventTicketTiers,
15
+ } from "../promoCodesUtils.js";
16
+ import type {
17
+ BashEventExt,
18
+ TicketTierExt,
19
+ } from "../../extendedSchemas.js";
20
+ import type { BashEventPromoCode } from "@prisma/client";
21
+
22
+ function makePromo(
23
+ id: string,
24
+ ticketTierId: string,
25
+ overrides: Partial<BashEventPromoCode> = {}
26
+ ): BashEventPromoCode {
27
+ return {
28
+ id,
29
+ ticketTierId,
30
+ code: `CODE-${id}`,
31
+ discountPercent: 10,
32
+ discountAmount: null,
33
+ maxRedemptions: null,
34
+ redemptionCount: 0,
35
+ expiresAt: null,
36
+ createdAt: new Date(0),
37
+ updatedAt: new Date(0),
38
+ ...overrides,
39
+ } as unknown as BashEventPromoCode;
40
+ }
41
+
42
+ function makeTier(
43
+ id: string,
44
+ promoCodes: BashEventPromoCode[]
45
+ ): TicketTierExt {
46
+ return {
47
+ id,
48
+ title: `Tier ${id}`,
49
+ promoCodes,
50
+ } as unknown as TicketTierExt;
51
+ }
52
+
53
+ function makeBashEvent(tiers: TicketTierExt[]): BashEventExt {
54
+ return { ticketTiers: tiers } as unknown as BashEventExt;
55
+ }
56
+
57
+ // ============================================================================
58
+ // findPromoCodeFromBashEvent
59
+ // ============================================================================
60
+
61
+ describe("findPromoCodeFromBashEvent", () => {
62
+ it("returns undefined when promoCodeId is undefined (no lookup attempted)", () => {
63
+ const bash = makeBashEvent([makeTier("t1", [makePromo("p1", "t1")])]);
64
+ expect(findPromoCodeFromBashEvent(bash, undefined)).toBeUndefined();
65
+ });
66
+
67
+ it("returns undefined when promoCodeId is the empty string (falsy guard)", () => {
68
+ const bash = makeBashEvent([makeTier("t1", [makePromo("p1", "t1")])]);
69
+ expect(findPromoCodeFromBashEvent(bash, "")).toBeUndefined();
70
+ });
71
+
72
+ it("returns undefined when bashEvent is null", () => {
73
+ expect(findPromoCodeFromBashEvent(null, "p1")).toBeUndefined();
74
+ });
75
+
76
+ it("returns undefined when bashEvent is undefined", () => {
77
+ expect(findPromoCodeFromBashEvent(undefined, "p1")).toBeUndefined();
78
+ });
79
+
80
+ it("returns undefined when ticketTiers is empty", () => {
81
+ expect(
82
+ findPromoCodeFromBashEvent(makeBashEvent([]), "p1")
83
+ ).toBeUndefined();
84
+ });
85
+
86
+ it("finds a promo on the FIRST tier", () => {
87
+ const promo = makePromo("p1", "t1");
88
+ const bash = makeBashEvent([
89
+ makeTier("t1", [promo]),
90
+ makeTier("t2", [makePromo("p2", "t2")]),
91
+ ]);
92
+ expect(findPromoCodeFromBashEvent(bash, "p1")).toBe(promo);
93
+ });
94
+
95
+ it("finds a promo on a NON-FIRST tier (iterates across tiers)", () => {
96
+ const promo = makePromo("p2", "t2");
97
+ const bash = makeBashEvent([
98
+ makeTier("t1", [makePromo("p1", "t1")]),
99
+ makeTier("t2", [promo]),
100
+ ]);
101
+ expect(findPromoCodeFromBashEvent(bash, "p2")).toBe(promo);
102
+ });
103
+
104
+ it("returns the FIRST match if the same id appears on multiple tiers (defensive — shouldn't happen but guard the order)", () => {
105
+ const dupOnT1 = makePromo("dup", "t1");
106
+ const dupOnT2 = makePromo("dup", "t2");
107
+ const bash = makeBashEvent([
108
+ makeTier("t1", [dupOnT1]),
109
+ makeTier("t2", [dupOnT2]),
110
+ ]);
111
+ expect(findPromoCodeFromBashEvent(bash, "dup")).toBe(dupOnT1);
112
+ });
113
+
114
+ it("returns undefined when promoCodeId doesn't match any tier's promo", () => {
115
+ const bash = makeBashEvent([makeTier("t1", [makePromo("p1", "t1")])]);
116
+ expect(findPromoCodeFromBashEvent(bash, "missing")).toBeUndefined();
117
+ });
118
+
119
+ it("does not throw when a tier has no promoCodes array (defensive)", () => {
120
+ const tier = { id: "t1", promoCodes: [] } as unknown as TicketTierExt;
121
+ expect(() =>
122
+ findPromoCodeFromBashEvent(makeBashEvent([tier]), "p1")
123
+ ).not.toThrow();
124
+ });
125
+ });
126
+
127
+ // ============================================================================
128
+ // getPromoCodesFromBashEvent
129
+ // ============================================================================
130
+
131
+ describe("getPromoCodesFromBashEvent", () => {
132
+ it("returns [] when bashEvent is null", () => {
133
+ expect(getPromoCodesFromBashEvent(null)).toEqual([]);
134
+ });
135
+
136
+ it("returns [] when bashEvent is undefined", () => {
137
+ expect(getPromoCodesFromBashEvent(undefined)).toEqual([]);
138
+ });
139
+
140
+ it("returns [] when there are no ticket tiers", () => {
141
+ expect(getPromoCodesFromBashEvent(makeBashEvent([]))).toEqual([]);
142
+ });
143
+
144
+ it("returns [] when no tier has any promo codes", () => {
145
+ const bash = makeBashEvent([makeTier("t1", []), makeTier("t2", [])]);
146
+ expect(getPromoCodesFromBashEvent(bash)).toEqual([]);
147
+ });
148
+
149
+ it("flattens promo codes across multiple tiers in tier order", () => {
150
+ const p1 = makePromo("p1", "t1");
151
+ const p2 = makePromo("p2", "t2");
152
+ const p3 = makePromo("p3", "t2");
153
+ const bash = makeBashEvent([
154
+ makeTier("t1", [p1]),
155
+ makeTier("t2", [p2, p3]),
156
+ ]);
157
+ expect(getPromoCodesFromBashEvent(bash)).toEqual([p1, p2, p3]);
158
+ });
159
+
160
+ it("skips tiers with empty promoCodes (filter step)", () => {
161
+ const p1 = makePromo("p1", "t1");
162
+ const bash = makeBashEvent([
163
+ makeTier("t1", [p1]),
164
+ makeTier("t2", []),
165
+ makeTier("t3", [makePromo("p3", "t3")]),
166
+ ]);
167
+ const all = getPromoCodesFromBashEvent(bash);
168
+ expect(all.map((p) => p.id)).toEqual(["p1", "p3"]);
169
+ });
170
+ });
171
+
172
+ // ============================================================================
173
+ // updatePromoCodesOnBashEventTicketTiers
174
+ // ============================================================================
175
+
176
+ describe("updatePromoCodesOnBashEventTicketTiers", () => {
177
+ it("re-buckets promos onto tiers by ticketTierId (mutates in place)", () => {
178
+ const bash = makeBashEvent([
179
+ makeTier("t1", []),
180
+ makeTier("t2", []),
181
+ ]);
182
+ const promos = [
183
+ makePromo("p1", "t1"),
184
+ makePromo("p2", "t2"),
185
+ makePromo("p3", "t1"),
186
+ ];
187
+
188
+ updatePromoCodesOnBashEventTicketTiers(bash, promos);
189
+
190
+ expect(bash.ticketTiers?.[0].promoCodes.map((p) => p.id)).toEqual([
191
+ "p1",
192
+ "p3",
193
+ ]);
194
+ expect(bash.ticketTiers?.[1].promoCodes.map((p) => p.id)).toEqual(["p2"]);
195
+ });
196
+
197
+ it("REPLACES existing promo codes on each tier (not appended)", () => {
198
+ const oldPromo = makePromo("old", "t1");
199
+ const bash = makeBashEvent([makeTier("t1", [oldPromo])]);
200
+
201
+ updatePromoCodesOnBashEventTicketTiers(bash, [makePromo("new", "t1")]);
202
+
203
+ expect(bash.ticketTiers?.[0].promoCodes.map((p) => p.id)).toEqual([
204
+ "new",
205
+ ]);
206
+ });
207
+
208
+ it("clears tiers that have no matching promos in the new list", () => {
209
+ const bash = makeBashEvent([
210
+ makeTier("t1", [makePromo("oldA", "t1")]),
211
+ makeTier("t2", [makePromo("oldB", "t2")]),
212
+ ]);
213
+
214
+ updatePromoCodesOnBashEventTicketTiers(bash, [makePromo("newA", "t1")]);
215
+
216
+ expect(bash.ticketTiers?.[0].promoCodes.map((p) => p.id)).toEqual([
217
+ "newA",
218
+ ]);
219
+ expect(bash.ticketTiers?.[1].promoCodes).toEqual([]);
220
+ });
221
+
222
+ it("ignores promos whose ticketTierId doesn't match any tier on the event (orphans dropped)", () => {
223
+ const bash = makeBashEvent([makeTier("t1", [])]);
224
+ updatePromoCodesOnBashEventTicketTiers(bash, [
225
+ makePromo("p1", "t1"),
226
+ makePromo("orphan", "tDoesNotExist"),
227
+ ]);
228
+ expect(bash.ticketTiers?.[0].promoCodes.map((p) => p.id)).toEqual(["p1"]);
229
+ });
230
+
231
+ it("is a no-op when ticketTiers is undefined", () => {
232
+ const bash = { ticketTiers: undefined } as unknown as BashEventExt;
233
+ expect(() =>
234
+ updatePromoCodesOnBashEventTicketTiers(bash, [makePromo("p1", "t1")])
235
+ ).not.toThrow();
236
+ });
237
+ });