-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaitlist.ts
More file actions
389 lines (348 loc) · 17.5 KB
/
Copy pathwaitlist.ts
File metadata and controls
389 lines (348 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Waitlist handler — PLOS signup capture. Persists to the WAITLIST KV and,
// on success, fires three fire-and-forget side effects:
// 1. Resend confirmation email to the user.
// 2. Slack webhook post to #plos-admin (if SLACK_WEBHOOK_URL set).
// 3. Resend team-copy email to WAITLIST_INBOX_TO (if both set).
//
// All three run inside ctx.waitUntil() so a delivery failure never blocks
// the 200 response back to the form.
import type { Env } from '../env';
import { json } from '../lib/cors';
interface WaitlistEntry {
joinedAt: string;
}
const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/i;
// ─── Email Template ───────────────────────────────────────────────────────────
function buildConfirmationEmail(): string {
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="color-scheme" content="dark">
<title>You're on the list. | CWK. Experience</title>
</head>
<body style="margin:0;padding:0;background-color:#0A0A0A;-webkit-font-smoothing:antialiased;">
<table width="100%" cellpadding="0" cellspacing="0" role="presentation"
style="background-color:#0A0A0A;padding:48px 20px 64px;">
<tr>
<td align="center">
<table width="560" cellpadding="0" cellspacing="0" role="presentation"
style="max-width:560px;width:100%;">
<!-- Logo row -->
<tr>
<td style="padding-bottom:36px;">
<span style="font-size:20px;font-weight:800;color:#FAFAF9;
letter-spacing:-0.5px;font-family:-apple-system,BlinkMacSystemFont,
'Segoe UI',Helvetica,Arial,sans-serif;">CWK.</span>
<span style="font-size:10px;font-weight:700;color:#38B2F6;letter-spacing:2.5px;
text-transform:uppercase;margin-left:10px;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">Experience</span>
</td>
</tr>
<!-- Main card -->
<tr>
<td style="background:#111111;border:1px solid rgba(56,178,246,0.18);
border-radius:12px;padding:48px 40px 44px;">
<!-- Gradient accent bar -->
<table width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td style="height:3px;background:linear-gradient(90deg,#38B2F6,#B721FF);
border-radius:2px;"> </td>
</tr>
</table>
<div style="height:28px;line-height:28px;"> </div>
<!-- Eyebrow -->
<p style="margin:0 0 14px;font-size:10px;font-weight:700;letter-spacing:3px;
text-transform:uppercase;color:#38B2F6;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">
PLOS · Personal Leverage OS
</p>
<!-- Headline -->
<h1 style="margin:0 0 20px;font-size:34px;font-weight:800;color:#FAFAF9;
line-height:1.15;letter-spacing:-0.8px;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">
You're on the list.
</h1>
<!-- Body copy -->
<p style="margin:0 0 14px;font-size:16px;color:#A8A29E;line-height:1.7;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">
We've locked in your spot. When PLOS opens, you'll be among the first to get access. No scrambling, no waiting in line.
</p>
<p style="margin:0 0 36px;font-size:16px;color:#A8A29E;line-height:1.7;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">
In the meantime, take a look at the work. See what's possible when infrastructure stops leaking and your business actually scales.
</p>
<!-- Divider -->
<table width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td style="border-top:1px solid rgba(255,255,255,0.07);padding-bottom:32px;"></td>
</tr>
</table>
<!-- Four Pillars row -->
<table width="100%" cellpadding="0" cellspacing="0" role="presentation"
style="margin-bottom:36px;">
<tr>
<td width="25%" style="text-align:center;padding:0 6px;">
<p style="margin:0 0 4px;font-size:10px;font-weight:700;letter-spacing:2px;
text-transform:uppercase;color:#38B2F6;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">Mind</p>
<p style="margin:0;font-size:11px;color:#57534E;line-height:1.5;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">Strategy & clarity</p>
</td>
<td width="25%" style="text-align:center;padding:0 6px;">
<p style="margin:0 0 4px;font-size:10px;font-weight:700;letter-spacing:2px;
text-transform:uppercase;color:#B721FF;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">Body</p>
<p style="margin:0;font-size:11px;color:#57534E;line-height:1.5;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">Systems & ops</p>
</td>
<td width="25%" style="text-align:center;padding:0 6px;">
<p style="margin:0 0 4px;font-size:10px;font-weight:700;letter-spacing:2px;
text-transform:uppercase;color:#FF4EF0;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">Soul</p>
<p style="margin:0;font-size:11px;color:#57534E;line-height:1.5;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">Legacy & IP</p>
</td>
<td width="25%" style="text-align:center;padding:0 6px;">
<p style="margin:0 0 4px;font-size:10px;font-weight:700;letter-spacing:2px;
text-transform:uppercase;color:#38B2F6;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">Pocket</p>
<p style="margin:0;font-size:11px;color:#57534E;line-height:1.5;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">Revenue & deals</p>
</td>
</tr>
</table>
<!-- CTA button -->
<a href="https://cwkexperience.com/work"
style="display:inline-block;background:#38B2F6;color:#0A0A0A;
font-size:14px;font-weight:700;text-decoration:none;
padding:14px 28px;border-radius:8px;letter-spacing:0.2px;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">
See The Work →
</a>
</td>
</tr>
<!-- Footer -->
<tr>
<td style="padding-top:32px;">
<p style="margin:0 0 6px;font-size:12px;color:#44403C;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">
Build. Learn. Earn. Play.
</p>
<p style="margin:0 0 10px;font-size:12px;color:#44403C;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">
© 2026 CWK. LLC. All rights reserved.
·
<a href="https://cwkexperience.com"
style="color:#38B2F6;text-decoration:none;">cwkexperience.com</a>
·
<a href="https://cwkexperience.com/privacy"
style="color:#57534E;text-decoration:none;">Privacy</a>
</p>
<p style="margin:0 0 4px;font-size:11px;color:#292524;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">
You received this email because you joined the PLOS waitlist at
<a href="https://cwkexperience.com" style="color:#292524;text-decoration:underline;">cwkexperience.com</a>.
</p>
<p style="margin:0;font-size:11px;color:#292524;
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',
Helvetica,Arial,sans-serif;">
CWK. LLC · 447 Broadway, 2nd FL #2056 · New York, NY 10013
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>`;
}
// ─── Email Delivery (Resend) ──────────────────────────────────────────────────
async function sendConfirmation(env: Env, recipientEmail: string): Promise<void> {
const response = await fetch('https://api.resend.com/emails', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${env.RESEND_API}`,
},
body: JSON.stringify({
from: `${env.FROM_NAME} <${env.FROM_EMAIL}>`,
to: [recipientEmail],
reply_to: `${env.REPLY_TO_NAME} <${env.REPLY_TO_EMAIL}>`,
subject: "You're on the PLOS waitlist | CWK. Experience",
html: buildConfirmationEmail(),
headers: {
'List-Unsubscribe': '<mailto:unsubscribe@cwkexperience.com?subject=unsubscribe>',
'List-Unsubscribe-Post': 'List-Unsubscribe=One-Click',
},
text: [
'CWK. Experience | PLOS Waitlist Confirmation',
'============================================',
'',
"You're on the list.",
'',
"We've reserved your spot. When PLOS opens, you'll be among the first to get access. No scrambling, no waiting in line.",
'',
"In the meantime, take a look at the work. See what's possible when infrastructure stops leaking and your business actually scales.",
'',
'See the work: https://cwkexperience.com/work',
'',
'----',
'PLOS · Personal Leverage OS',
'Mind · Body · Soul · Pocket',
'',
'Build. Learn. Earn. Play.',
'© 2026 CWK. LLC',
'cwkexperience.com',
'',
'You received this email because you joined the PLOS waitlist at cwkexperience.com.',
'To unsubscribe, reply with "unsubscribe" in the subject line.',
'',
'CWK. LLC · 447 Broadway, 2nd FL #2056 · New York, NY 10013',
].join('\n'),
}),
});
if (!response.ok) {
const err = await response.text().catch(() => '');
throw new Error(`Resend error ${response.status}: ${err}`);
}
}
// ─── Slack notification ───────────────────────────────────────────────────────
async function sendSlackNotification(env: Env, email: string): Promise<void> {
if (!env.SLACK_WEBHOOK_URL) return;
const payload = {
text: `New PLOS waitlist signup: ${email}`,
blocks: [
{
type: 'header',
text: { type: 'plain_text', text: 'New PLOS waitlist signup', emoji: true },
},
{
type: 'section',
fields: [
{ type: 'mrkdwn', text: `*Email*\n${email}` },
],
},
],
};
const res = await fetch(env.SLACK_WEBHOOK_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
if (!res.ok) {
const err = await res.text().catch(() => '');
throw new Error(`Slack webhook error ${res.status}: ${err}`);
}
}
// ─── Team-copy email (Resend) ─────────────────────────────────────────────────
function buildTeamNotificationEmail(email: string, joinedAt: string, environment: string): {
html: string;
text: string;
} {
const safeEmail = email.replace(/[<>&"']/g, (c) => ({ '<': '<', '>': '>', '&': '&', '"': '"', "'": ''' }[c] as string));
const safeJoined = joinedAt.replace(/[<>&"']/g, (c) => ({ '<': '<', '>': '>', '&': '&', '"': '"', "'": ''' }[c] as string));
const html = `<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>New PLOS waitlist signup</title></head>
<body style="margin:0;padding:24px;background:#0A0A0A;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif;color:#EEF0FF;">
<div style="max-width:560px;margin:0 auto;background:#111;border:1px solid rgba(56,178,246,0.18);border-radius:12px;padding:32px;">
<p style="margin:0 0 6px;font-size:11px;font-weight:700;letter-spacing:3px;text-transform:uppercase;color:#38B2F6;">New PLOS waitlist signup</p>
<h1 style="margin:0 0 20px;font-size:22px;font-weight:800;letter-spacing:-0.5px;">${safeEmail}</h1>
<table cellpadding="0" cellspacing="0" style="width:100%;border-collapse:collapse;">
<tr><td style="padding:6px 0;font-size:12px;color:#A8A29E;width:110px;">Email</td><td style="padding:6px 0;font-size:13px;color:#EEF0FF;">${safeEmail}</td></tr>
<tr><td style="padding:6px 0;font-size:12px;color:#A8A29E;">Joined</td><td style="padding:6px 0;font-size:13px;color:#EEF0FF;">${safeJoined}</td></tr>
<tr><td style="padding:6px 0;font-size:12px;color:#A8A29E;">Source</td><td style="padding:6px 0;font-size:13px;color:#EEF0FF;">cwkexperience.com</td></tr>
<tr><td style="padding:6px 0;font-size:12px;color:#A8A29E;">Env</td><td style="padding:6px 0;font-size:13px;color:#EEF0FF;">${environment}</td></tr>
</table>
</div>
</body></html>`;
const text = [
'New PLOS waitlist signup',
'========================',
`Email: ${email}`,
`Joined: ${joinedAt}`,
'Source: cwkexperience.com',
`Env: ${environment}`,
].join('\n');
return { html, text };
}
async function sendTeamNotification(env: Env, email: string, joinedAt: string): Promise<void> {
if (!env.RESEND_API || !env.WAITLIST_INBOX_TO) return;
const { html, text } = buildTeamNotificationEmail(email, joinedAt, env.ENVIRONMENT);
const res = await fetch('https://api.resend.com/emails', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${env.RESEND_API}`,
},
body: JSON.stringify({
from: `${env.FROM_NAME} <${env.FROM_EMAIL}>`,
to: [env.WAITLIST_INBOX_TO],
reply_to: email,
subject: `[CWK Waitlist] new signup: ${email}`,
html,
text,
}),
});
if (!res.ok) {
const err = await res.text().catch(() => '');
throw new Error(`Resend error ${res.status}: ${err}`);
}
}
// ─── Route handler ────────────────────────────────────────────────────────────
export async function handleWaitlistPost(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
if (!env.WAITLIST) return json({ error: 'server_error' }, 500, env);
let body: { email?: unknown };
try {
body = await request.json();
} catch {
return json({ error: 'invalid_request' }, 400, env);
}
const rawEmail = typeof body.email === 'string' ? body.email.trim().toLowerCase() : '';
if (!rawEmail || !EMAIL_REGEX.test(rawEmail)) return json({ error: 'invalid_email' }, 422, env);
try {
const existing = await env.WAITLIST.get(rawEmail);
if (existing !== null) return json({ error: 'already_on_list' }, 409, env);
const entry: WaitlistEntry = { joinedAt: new Date().toISOString() };
await env.WAITLIST.put(rawEmail, JSON.stringify(entry));
// Fire-and-forget side effects — don't block the response on delivery.
// Each call swallows its own failure so one outage doesn't kill the rest.
if (env.RESEND_API) {
ctx.waitUntil(sendConfirmation(env, rawEmail).catch((e) => {
console.error('[waitlist] sendConfirmation failed:', e instanceof Error ? e.message : e);
}));
ctx.waitUntil(sendTeamNotification(env, rawEmail, entry.joinedAt).catch((e) => {
console.error('[waitlist] sendTeamNotification failed:', e instanceof Error ? e.message : e);
}));
} else {
console.warn('[waitlist] RESEND_API not set — skipping confirmation + team-copy emails');
}
if (env.SLACK_WEBHOOK_URL) {
ctx.waitUntil(sendSlackNotification(env, rawEmail).catch((e) => {
console.error('[waitlist] sendSlackNotification failed:', e instanceof Error ? e.message : e);
}));
}
return json({ success: true }, 200, env);
} catch {
return json({ error: 'server_error' }, 500, env);
}
}