@hintergrund/worker 0.1.2 → 0.1.5
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.
- package/dist/api/cms/handlers/assets.handler.d.ts +24 -1
- package/dist/api/cms/handlers/assets.handler.d.ts.map +1 -1
- package/dist/api/cms/handlers/assets.handler.js +111 -210
- package/dist/api/cms/handlers/auth.handler.d.ts +4 -3
- package/dist/api/cms/handlers/auth.handler.d.ts.map +1 -1
- package/dist/api/cms/handlers/auth.handler.js +24 -14
- package/dist/api/cms/handlers/config.handler.d.ts +4 -0
- package/dist/api/cms/handlers/config.handler.d.ts.map +1 -1
- package/dist/api/cms/handlers/config.handler.js +20 -71
- package/dist/api/cms/handlers/content.handler.d.ts +12 -1
- package/dist/api/cms/handlers/content.handler.d.ts.map +1 -1
- package/dist/api/cms/handlers/content.handler.js +57 -233
- package/dist/api/cms/handlers/dashboard.handler.js +1 -1
- package/dist/api/cms/routes/auth.routes.d.ts +2 -1
- package/dist/api/cms/routes/auth.routes.d.ts.map +1 -1
- package/dist/api/cms/routes/auth.routes.js +7 -0
- package/dist/api/cms/routes/index.d.ts +2 -1
- package/dist/api/cms/routes/index.d.ts.map +1 -1
- package/dist/api/emails/handlers/branding.handler.d.ts +17 -0
- package/dist/api/emails/handlers/branding.handler.d.ts.map +1 -0
- package/dist/api/emails/handlers/branding.handler.js +120 -0
- package/dist/api/emails/handlers/campaigns.handler.d.ts +32 -0
- package/dist/api/emails/handlers/campaigns.handler.d.ts.map +1 -0
- package/dist/api/emails/handlers/campaigns.handler.js +139 -0
- package/dist/api/emails/handlers/contacts.handler.d.ts +37 -0
- package/dist/api/emails/handlers/contacts.handler.d.ts.map +1 -0
- package/dist/api/emails/handlers/contacts.handler.js +161 -0
- package/dist/api/emails/handlers/invoice.handler.d.ts +30 -0
- package/dist/api/emails/handlers/invoice.handler.d.ts.map +1 -0
- package/dist/api/emails/handlers/invoice.handler.js +463 -0
- package/dist/api/emails/handlers/public.handler.d.ts +36 -0
- package/dist/api/emails/handlers/public.handler.d.ts.map +1 -0
- package/dist/api/emails/handlers/public.handler.js +350 -0
- package/dist/api/emails/handlers/send.handler.d.ts +23 -0
- package/dist/api/emails/handlers/send.handler.d.ts.map +1 -0
- package/dist/api/emails/handlers/send.handler.js +117 -0
- package/dist/api/emails/handlers/settings.handler.d.ts +36 -0
- package/dist/api/emails/handlers/settings.handler.d.ts.map +1 -0
- package/dist/api/emails/handlers/settings.handler.js +312 -0
- package/dist/api/emails/handlers/templates.handler.d.ts +32 -0
- package/dist/api/emails/handlers/templates.handler.d.ts.map +1 -0
- package/dist/api/emails/handlers/templates.handler.js +132 -0
- package/dist/api/emails/handlers/webhook.handler.d.ts +10 -0
- package/dist/api/emails/handlers/webhook.handler.d.ts.map +1 -0
- package/dist/api/emails/handlers/webhook.handler.js +261 -0
- package/dist/api/emails/routes/admin.routes.d.ts +9 -0
- package/dist/api/emails/routes/admin.routes.d.ts.map +1 -0
- package/dist/api/emails/routes/admin.routes.js +77 -0
- package/dist/api/emails/routes/index.d.ts +9 -0
- package/dist/api/emails/routes/index.d.ts.map +1 -0
- package/dist/api/emails/routes/index.js +14 -0
- package/dist/api/emails/routes/public.routes.d.ts +9 -0
- package/dist/api/emails/routes/public.routes.d.ts.map +1 -0
- package/dist/api/emails/routes/public.routes.js +18 -0
- package/dist/api/shop/handlers/webhook.handler.d.ts.map +1 -1
- package/dist/api/shop/handlers/webhook.handler.js +66 -60
- package/dist/api/shop/routes.d.ts.map +1 -1
- package/dist/api/shop/routes.js +0 -3
- package/dist/hintergrund-assets.d.ts +2 -2
- package/dist/hintergrund-assets.d.ts.map +1 -1
- package/dist/hintergrund-assets.js +2 -2
- package/dist/index.d.ts +20 -19
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -7
- package/dist/lib/config.d.ts +13 -7
- package/dist/lib/config.d.ts.map +1 -1
- package/dist/lib/config.js +44 -46
- package/dist/lib/email.d.ts +2 -2
- package/dist/lib/email.d.ts.map +1 -1
- package/dist/lib/email.js +7 -9
- package/dist/lib/github.d.ts +124 -0
- package/dist/lib/github.d.ts.map +1 -0
- package/dist/lib/github.js +286 -0
- package/dist/lib/plunk.d.ts +204 -0
- package/dist/lib/plunk.d.ts.map +1 -0
- package/dist/lib/plunk.js +259 -0
- package/dist/middleware/auth.d.ts.map +1 -1
- package/dist/middleware/auth.js +2 -5
- package/package.json +32 -32
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public Handler
|
|
3
|
+
*
|
|
4
|
+
* Handles public email subscription/unsubscription endpoints.
|
|
5
|
+
* Uses Plunk for contact management.
|
|
6
|
+
*/
|
|
7
|
+
import { createPlunkClient } from '../../../lib/plunk';
|
|
8
|
+
import { getEmailsConfig } from '../../../lib/config';
|
|
9
|
+
/**
|
|
10
|
+
* POST /api/email/subscribe
|
|
11
|
+
* Public endpoint for newsletter subscription
|
|
12
|
+
*
|
|
13
|
+
* Creates an unsubscribed contact in Plunk. Plunk's built-in double opt-in
|
|
14
|
+
* will handle sending the confirmation email and updating the subscription status.
|
|
15
|
+
*/
|
|
16
|
+
export async function subscribe(c) {
|
|
17
|
+
try {
|
|
18
|
+
const body = await c.req.json();
|
|
19
|
+
if (!body.email) {
|
|
20
|
+
return c.json({ success: false, message: 'Email is required' }, 400);
|
|
21
|
+
}
|
|
22
|
+
// Validate email format
|
|
23
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
24
|
+
if (!emailRegex.test(body.email)) {
|
|
25
|
+
return c.json({ success: false, message: 'Invalid email format' }, 400);
|
|
26
|
+
}
|
|
27
|
+
const email = body.email.toLowerCase().trim();
|
|
28
|
+
const plunk = createPlunkClient(c.env);
|
|
29
|
+
// Check if contact already exists
|
|
30
|
+
const existingContact = await plunk.findContactByEmail(email);
|
|
31
|
+
if (existingContact) {
|
|
32
|
+
if (existingContact.subscribed) {
|
|
33
|
+
return c.json({
|
|
34
|
+
success: true,
|
|
35
|
+
message: 'You are already subscribed to our newsletter.',
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
// If they are pending, send them another confirmation email
|
|
39
|
+
if (existingContact.data?.status === 'pending') {
|
|
40
|
+
try {
|
|
41
|
+
await sendConfirmationEmail(c, plunk, existingContact.id, email);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
const message = error instanceof Error ? error.message : '';
|
|
45
|
+
if (message.includes('verified') || message.includes('domain') || message.includes('identity')) {
|
|
46
|
+
return c.json({
|
|
47
|
+
success: false,
|
|
48
|
+
message: 'Email sending failed: Your sending domain is not verified in Plunk. Please check your email settings.',
|
|
49
|
+
}, 400);
|
|
50
|
+
}
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
return c.json({
|
|
54
|
+
success: true,
|
|
55
|
+
message: 'Please check your email to confirm your subscription.',
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
// Re-subscribe existing contact (e.g. if they previously unsubscribed)
|
|
59
|
+
await plunk.updateContact(existingContact.id, {
|
|
60
|
+
subscribed: true,
|
|
61
|
+
data: {
|
|
62
|
+
...existingContact.data,
|
|
63
|
+
status: 'confirmed',
|
|
64
|
+
reSubscribedAt: new Date().toISOString()
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
return c.json({
|
|
68
|
+
success: true,
|
|
69
|
+
message: 'Welcome back! You have been re-subscribed to our newsletter.',
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
// Create new contact (subscribed = false for double opt-in)
|
|
73
|
+
const contact = await plunk.createContact({
|
|
74
|
+
email,
|
|
75
|
+
subscribed: false,
|
|
76
|
+
data: {
|
|
77
|
+
...(body.name ? { name: body.name } : {}),
|
|
78
|
+
status: 'pending',
|
|
79
|
+
subscribedAt: new Date().toISOString()
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
// Send custom confirmation email
|
|
83
|
+
try {
|
|
84
|
+
await sendConfirmationEmail(c, plunk, contact.id, email);
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
const message = error instanceof Error ? error.message : '';
|
|
88
|
+
if (message.includes('verified') || message.includes('domain') || message.includes('identity')) {
|
|
89
|
+
return c.json({
|
|
90
|
+
success: false,
|
|
91
|
+
message: 'Email sending failed: Your sending domain is not verified in Plunk. Please check your email settings.',
|
|
92
|
+
}, 400);
|
|
93
|
+
}
|
|
94
|
+
throw error; // Re-throw other errors to be caught by the outer catch
|
|
95
|
+
}
|
|
96
|
+
return c.json({
|
|
97
|
+
success: true,
|
|
98
|
+
message: 'Please check your email to confirm your subscription.',
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
console.error('Subscribe error:', error);
|
|
103
|
+
return c.json({
|
|
104
|
+
success: false,
|
|
105
|
+
message: error instanceof Error ? error.message : 'Failed to subscribe',
|
|
106
|
+
}, 500);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Helper to send a confirmation email using the 'Confirmation Email' template
|
|
111
|
+
*/
|
|
112
|
+
async function sendConfirmationEmail(c, plunk, contactId, email) {
|
|
113
|
+
try {
|
|
114
|
+
const config = getEmailsConfig(c.env);
|
|
115
|
+
// 1. Fetch templates from Plunk
|
|
116
|
+
const { templates } = await plunk.getTemplates();
|
|
117
|
+
// 2. Find the "Confirmation Email" template
|
|
118
|
+
const template = templates.find(t => t.name === 'Confirmation Email');
|
|
119
|
+
if (!template) {
|
|
120
|
+
console.warn('Confirmation Email template not found in Plunk');
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
// 3. Construct confirmation URL
|
|
124
|
+
// The URL should point to the frontend confirm page which then calls our confirm API
|
|
125
|
+
const origin = new URL(c.req.url).origin;
|
|
126
|
+
const confirmUrl = `${origin}/newsletter/confirm?token=${contactId}`;
|
|
127
|
+
// 4. Prepare email body (replace placeholders)
|
|
128
|
+
let body = template.body || '';
|
|
129
|
+
body = body.replace(/{{confirm_url}}/g, confirmUrl);
|
|
130
|
+
// 5. Send transactional email
|
|
131
|
+
await plunk.send({
|
|
132
|
+
to: email,
|
|
133
|
+
subject: template.subject,
|
|
134
|
+
body: body,
|
|
135
|
+
from: config.fromEmail,
|
|
136
|
+
replyTo: config.replyTo,
|
|
137
|
+
});
|
|
138
|
+
console.log(`Confirmation email sent to ${email}`);
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
console.error('Failed to send confirmation email:', error);
|
|
142
|
+
throw error;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* POST /api/email/unsubscribe
|
|
147
|
+
* Public endpoint for newsletter unsubscription
|
|
148
|
+
*/
|
|
149
|
+
export async function unsubscribe(c) {
|
|
150
|
+
try {
|
|
151
|
+
const body = await c.req.json();
|
|
152
|
+
if (!body.email) {
|
|
153
|
+
return c.json({ success: false, message: 'Email is required' }, 400);
|
|
154
|
+
}
|
|
155
|
+
// Validate email format
|
|
156
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
157
|
+
if (!emailRegex.test(body.email)) {
|
|
158
|
+
return c.json({ success: false, message: 'Invalid email format' }, 400);
|
|
159
|
+
}
|
|
160
|
+
const email = body.email.toLowerCase().trim();
|
|
161
|
+
const plunk = createPlunkClient(c.env);
|
|
162
|
+
// Find contact by email
|
|
163
|
+
const contact = await plunk.findContactByEmail(email);
|
|
164
|
+
if (!contact) {
|
|
165
|
+
// Don't reveal if email exists for privacy
|
|
166
|
+
return c.json({
|
|
167
|
+
success: true,
|
|
168
|
+
message: 'If you were subscribed, you have been unsubscribed.',
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
// Unsubscribe the contact
|
|
172
|
+
await plunk.unsubscribeContact(contact.id);
|
|
173
|
+
// Also update status in data for tracking
|
|
174
|
+
await plunk.updateContact(contact.id, {
|
|
175
|
+
data: {
|
|
176
|
+
status: 'unsubscribed',
|
|
177
|
+
unsubscribedAt: new Date().toISOString()
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
return c.json({
|
|
181
|
+
success: true,
|
|
182
|
+
message: 'You have been successfully unsubscribed.',
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
console.error('Unsubscribe error:', error);
|
|
187
|
+
return c.json({
|
|
188
|
+
success: false,
|
|
189
|
+
message: error instanceof Error ? error.message : 'Failed to unsubscribe',
|
|
190
|
+
}, 500);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* GET /api/email/unsubscribe
|
|
195
|
+
* Show unsubscribe page (for links in emails)
|
|
196
|
+
*/
|
|
197
|
+
export async function showUnsubscribePage(c) {
|
|
198
|
+
const email = c.req.query('email') || '';
|
|
199
|
+
const html = `
|
|
200
|
+
<!DOCTYPE html>
|
|
201
|
+
<html lang="en">
|
|
202
|
+
<head>
|
|
203
|
+
<meta charset="UTF-8">
|
|
204
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
205
|
+
<title>Unsubscribe</title>
|
|
206
|
+
<style>
|
|
207
|
+
body {
|
|
208
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
209
|
+
max-width: 400px;
|
|
210
|
+
margin: 100px auto;
|
|
211
|
+
padding: 20px;
|
|
212
|
+
text-align: center;
|
|
213
|
+
}
|
|
214
|
+
h1 { font-size: 1.5rem; margin-bottom: 1rem; }
|
|
215
|
+
p { color: #666; margin-bottom: 1.5rem; }
|
|
216
|
+
input {
|
|
217
|
+
width: 100%;
|
|
218
|
+
padding: 12px;
|
|
219
|
+
border: 1px solid #ddd;
|
|
220
|
+
border-radius: 4px;
|
|
221
|
+
margin-bottom: 12px;
|
|
222
|
+
box-sizing: border-box;
|
|
223
|
+
}
|
|
224
|
+
button {
|
|
225
|
+
width: 100%;
|
|
226
|
+
padding: 12px;
|
|
227
|
+
background: #e74c3c;
|
|
228
|
+
color: white;
|
|
229
|
+
border: none;
|
|
230
|
+
border-radius: 4px;
|
|
231
|
+
cursor: pointer;
|
|
232
|
+
font-size: 1rem;
|
|
233
|
+
}
|
|
234
|
+
button:hover { background: #c0392b; }
|
|
235
|
+
.message {
|
|
236
|
+
padding: 12px;
|
|
237
|
+
border-radius: 4px;
|
|
238
|
+
margin-bottom: 1rem;
|
|
239
|
+
display: none;
|
|
240
|
+
}
|
|
241
|
+
.message.success { background: #d4edda; color: #155724; display: block; }
|
|
242
|
+
.message.error { background: #f8d7da; color: #721c24; display: block; }
|
|
243
|
+
</style>
|
|
244
|
+
</head>
|
|
245
|
+
<body>
|
|
246
|
+
<h1>Unsubscribe</h1>
|
|
247
|
+
<p>Enter your email address to unsubscribe from our newsletter.</p>
|
|
248
|
+
<div id="message" class="message"></div>
|
|
249
|
+
<form id="unsubscribe-form">
|
|
250
|
+
<input type="email" id="email" name="email" value="${email}" placeholder="your@email.com" required>
|
|
251
|
+
<button type="submit">Unsubscribe</button>
|
|
252
|
+
</form>
|
|
253
|
+
<script>
|
|
254
|
+
document.getElementById('unsubscribe-form').addEventListener('submit', async (e) => {
|
|
255
|
+
e.preventDefault();
|
|
256
|
+
const email = document.getElementById('email').value;
|
|
257
|
+
const messageEl = document.getElementById('message');
|
|
258
|
+
|
|
259
|
+
try {
|
|
260
|
+
const response = await fetch('/api/email/unsubscribe', {
|
|
261
|
+
method: 'POST',
|
|
262
|
+
headers: { 'Content-Type': 'application/json' },
|
|
263
|
+
body: JSON.stringify({ email })
|
|
264
|
+
});
|
|
265
|
+
const data = await response.json();
|
|
266
|
+
|
|
267
|
+
messageEl.textContent = data.message;
|
|
268
|
+
messageEl.className = 'message ' + (data.success ? 'success' : 'error');
|
|
269
|
+
} catch (error) {
|
|
270
|
+
messageEl.textContent = 'An error occurred. Please try again.';
|
|
271
|
+
messageEl.className = 'message error';
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
</script>
|
|
275
|
+
</body>
|
|
276
|
+
</html>
|
|
277
|
+
`;
|
|
278
|
+
return c.html(html);
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* POST /api/email/track
|
|
282
|
+
* Track an event for a contact (public endpoint using public key)
|
|
283
|
+
*/
|
|
284
|
+
export async function trackEvent(c) {
|
|
285
|
+
try {
|
|
286
|
+
const body = await c.req.json();
|
|
287
|
+
if (!body.email || !body.event) {
|
|
288
|
+
return c.json({ success: false, message: 'email and event are required' }, 400);
|
|
289
|
+
}
|
|
290
|
+
const plunk = createPlunkClient(c.env);
|
|
291
|
+
await plunk.track({
|
|
292
|
+
email: body.email.toLowerCase().trim(),
|
|
293
|
+
event: body.event,
|
|
294
|
+
data: body.data,
|
|
295
|
+
});
|
|
296
|
+
return c.json({ success: true });
|
|
297
|
+
}
|
|
298
|
+
catch (error) {
|
|
299
|
+
console.error('Track event error:', error);
|
|
300
|
+
return c.json({
|
|
301
|
+
success: false,
|
|
302
|
+
message: error instanceof Error ? error.message : 'Failed to track event',
|
|
303
|
+
}, 500);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* GET /api/email/confirm
|
|
308
|
+
* Public endpoint to confirm subscription
|
|
309
|
+
*/
|
|
310
|
+
export async function confirm(c) {
|
|
311
|
+
try {
|
|
312
|
+
const token = c.req.query('token');
|
|
313
|
+
if (!token) {
|
|
314
|
+
return c.json({ success: false, message: 'Token is required' }, 400);
|
|
315
|
+
}
|
|
316
|
+
const plunk = createPlunkClient(c.env);
|
|
317
|
+
// Plunk double opt-in handles the token verification if used via their links
|
|
318
|
+
// But here we are building a custom confirmation flow
|
|
319
|
+
// In a real scenario, the token would be used to find the contact or verify a JWT
|
|
320
|
+
// For now, we assume the token IS the contact ID or we use Plunk's built-in confirmation if possible
|
|
321
|
+
// If we use Plunk's built-in confirmation, we don't necessarily need this endpoint
|
|
322
|
+
// unless we want to redirect to a success page or update local metadata.
|
|
323
|
+
// However, many users prefer a custom flow.
|
|
324
|
+
// Since Plunk's built-in confirmation link goes to their domain,
|
|
325
|
+
// if the user wants a custom confirm page on their frontend, they need to:
|
|
326
|
+
// 1. Set the confirmation URL in Plunk settings to their frontend /newsletter/confirm
|
|
327
|
+
// 2. That page calls THIS endpoint with the token.
|
|
328
|
+
// Assuming the token is the contact ID for simplicity in this implementation,
|
|
329
|
+
// or Plunk provides a way to verify it.
|
|
330
|
+
// Let's assume for this implementation we update the contact status
|
|
331
|
+
await plunk.updateContact(token, {
|
|
332
|
+
subscribed: true,
|
|
333
|
+
data: {
|
|
334
|
+
status: 'confirmed',
|
|
335
|
+
confirmedAt: new Date().toISOString()
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
return c.json({
|
|
339
|
+
success: true,
|
|
340
|
+
message: 'Your subscription has been confirmed!'
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
catch (error) {
|
|
344
|
+
console.error('Confirm error:', error);
|
|
345
|
+
return c.json({
|
|
346
|
+
success: false,
|
|
347
|
+
message: error instanceof Error ? error.message : 'Failed to confirm subscription',
|
|
348
|
+
}, 500);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Send Handler
|
|
3
|
+
*
|
|
4
|
+
* Handles transactional email sending via Plunk.
|
|
5
|
+
*/
|
|
6
|
+
import { Context } from 'hono';
|
|
7
|
+
/**
|
|
8
|
+
* POST /api/email/admin/send
|
|
9
|
+
* Send a transactional email
|
|
10
|
+
*/
|
|
11
|
+
export declare function sendTransactional(c: Context): Promise<Response>;
|
|
12
|
+
/**
|
|
13
|
+
* POST /api/email/admin/send/test
|
|
14
|
+
* Send a test email (for previewing campaigns/templates)
|
|
15
|
+
*/
|
|
16
|
+
export declare function sendTestEmail(c: Context): Promise<Response>;
|
|
17
|
+
/**
|
|
18
|
+
* POST /api/email/admin/send/bulk
|
|
19
|
+
* Send an email to all subscribed contacts
|
|
20
|
+
* Uses Plunk's campaign functionality under the hood
|
|
21
|
+
*/
|
|
22
|
+
export declare function sendBulkEmail(c: Context): Promise<Response>;
|
|
23
|
+
//# sourceMappingURL=send.handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"send.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/emails/handlers/send.handler.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAI/B;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAgDrE;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CA8CjE;AAED;;;;GAIG;AACH,wBAAsB,aAAa,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CA4CjE"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Send Handler
|
|
3
|
+
*
|
|
4
|
+
* Handles transactional email sending via Plunk.
|
|
5
|
+
*/
|
|
6
|
+
import { createPlunkClient } from '../../../lib/plunk';
|
|
7
|
+
import { getEmailsConfig } from '../../../lib/config';
|
|
8
|
+
/**
|
|
9
|
+
* POST /api/email/admin/send
|
|
10
|
+
* Send a transactional email
|
|
11
|
+
*/
|
|
12
|
+
export async function sendTransactional(c) {
|
|
13
|
+
try {
|
|
14
|
+
const body = await c.req.json();
|
|
15
|
+
if (!body.to || !body.subject || !body.body) {
|
|
16
|
+
return c.json({ success: false, message: 'to, subject, and body are required' }, 400);
|
|
17
|
+
}
|
|
18
|
+
// Validate email format
|
|
19
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
20
|
+
if (!emailRegex.test(body.to)) {
|
|
21
|
+
return c.json({ success: false, message: 'Invalid email format' }, 400);
|
|
22
|
+
}
|
|
23
|
+
const config = getEmailsConfig(c.env);
|
|
24
|
+
const plunk = createPlunkClient(c.env);
|
|
25
|
+
await plunk.send({
|
|
26
|
+
to: body.to,
|
|
27
|
+
subject: body.subject,
|
|
28
|
+
body: body.body,
|
|
29
|
+
name: body.name,
|
|
30
|
+
from: config.fromEmail,
|
|
31
|
+
replyTo: config.replyTo,
|
|
32
|
+
});
|
|
33
|
+
return c.json({
|
|
34
|
+
success: true,
|
|
35
|
+
message: `Email sent to ${body.to}`,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
console.error('Send transactional error:', error);
|
|
40
|
+
return c.json({
|
|
41
|
+
success: false,
|
|
42
|
+
message: error instanceof Error ? error.message : 'Failed to send email',
|
|
43
|
+
}, 500);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* POST /api/email/admin/send/test
|
|
48
|
+
* Send a test email (for previewing campaigns/templates)
|
|
49
|
+
*/
|
|
50
|
+
export async function sendTestEmail(c) {
|
|
51
|
+
try {
|
|
52
|
+
const body = await c.req.json();
|
|
53
|
+
if (!body.to || !body.subject || !body.body) {
|
|
54
|
+
return c.json({ success: false, message: 'to, subject, and body are required' }, 400);
|
|
55
|
+
}
|
|
56
|
+
// Validate email format
|
|
57
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
58
|
+
if (!emailRegex.test(body.to)) {
|
|
59
|
+
return c.json({ success: false, message: 'Invalid email format' }, 400);
|
|
60
|
+
}
|
|
61
|
+
const config = getEmailsConfig(c.env);
|
|
62
|
+
const plunk = createPlunkClient(c.env);
|
|
63
|
+
await plunk.send({
|
|
64
|
+
to: body.to,
|
|
65
|
+
subject: `[TEST] ${body.subject}`,
|
|
66
|
+
body: body.body,
|
|
67
|
+
from: config.fromEmail,
|
|
68
|
+
replyTo: config.replyTo,
|
|
69
|
+
});
|
|
70
|
+
return c.json({
|
|
71
|
+
success: true,
|
|
72
|
+
message: `Test email sent to ${body.to}`,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
console.error('Send test email error:', error);
|
|
77
|
+
return c.json({
|
|
78
|
+
success: false,
|
|
79
|
+
message: error instanceof Error ? error.message : 'Failed to send test email',
|
|
80
|
+
}, 500);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* POST /api/email/admin/send/bulk
|
|
85
|
+
* Send an email to all subscribed contacts
|
|
86
|
+
* Uses Plunk's campaign functionality under the hood
|
|
87
|
+
*/
|
|
88
|
+
export async function sendBulkEmail(c) {
|
|
89
|
+
try {
|
|
90
|
+
const body = await c.req.json();
|
|
91
|
+
if (!body.subject || !body.body) {
|
|
92
|
+
return c.json({ success: false, message: 'subject and body are required' }, 400);
|
|
93
|
+
}
|
|
94
|
+
const plunk = createPlunkClient(c.env);
|
|
95
|
+
// Create a campaign and immediately send it
|
|
96
|
+
const campaignName = body.name || `Bulk Email - ${new Date().toISOString()}`;
|
|
97
|
+
const campaign = await plunk.createCampaign({
|
|
98
|
+
name: campaignName,
|
|
99
|
+
subject: body.subject,
|
|
100
|
+
body: body.body,
|
|
101
|
+
});
|
|
102
|
+
// Send the campaign immediately
|
|
103
|
+
const result = await plunk.sendCampaign(campaign.id);
|
|
104
|
+
return c.json({
|
|
105
|
+
success: true,
|
|
106
|
+
message: `Email sent to ${result.emails?.length || 0} contacts`,
|
|
107
|
+
sent: result.emails?.length || 0,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
console.error('Send bulk email error:', error);
|
|
112
|
+
return c.json({
|
|
113
|
+
success: false,
|
|
114
|
+
message: error instanceof Error ? error.message : 'Failed to send bulk email',
|
|
115
|
+
}, 500);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Settings Handler
|
|
3
|
+
*
|
|
4
|
+
* Manages combined email settings (branding + invoice) stored in GitHub.
|
|
5
|
+
* Now uses split files: branding.json, invoice.json, and templates/*.json
|
|
6
|
+
* Provides local-first sync support with commit SHA tracking.
|
|
7
|
+
*/
|
|
8
|
+
import { Context } from 'hono';
|
|
9
|
+
/**
|
|
10
|
+
* GET /api/email/admin/settings
|
|
11
|
+
* Fetch all email settings (branding + invoice) with commit SHA
|
|
12
|
+
*/
|
|
13
|
+
export declare function getSettings(c: Context): Promise<Response>;
|
|
14
|
+
/**
|
|
15
|
+
* GET /api/email/admin/settings/sync
|
|
16
|
+
* Sync settings - only returns data if commit SHA has changed
|
|
17
|
+
* Client sends current SHA in query param, we return new data only if different
|
|
18
|
+
*/
|
|
19
|
+
export declare function syncSettings(c: Context): Promise<Response>;
|
|
20
|
+
/**
|
|
21
|
+
* PUT /api/email/admin/settings
|
|
22
|
+
* Save all email settings atomically with conflict detection
|
|
23
|
+
* Now saves to split files instead of combined settings.json
|
|
24
|
+
*/
|
|
25
|
+
export declare function saveSettings(c: Context): Promise<Response>;
|
|
26
|
+
/**
|
|
27
|
+
* GET /api/email/admin/settings/templates
|
|
28
|
+
* Get all transactional email templates from GitHub
|
|
29
|
+
*/
|
|
30
|
+
export declare function getEmailTemplates(c: Context): Promise<Response>;
|
|
31
|
+
/**
|
|
32
|
+
* PUT /api/email/admin/settings/templates/:type
|
|
33
|
+
* Save a transactional email template to GitHub
|
|
34
|
+
*/
|
|
35
|
+
export declare function saveEmailTemplate(c: Context): Promise<Response>;
|
|
36
|
+
//# sourceMappingURL=settings.handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"settings.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/emails/handlers/settings.handler.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AA2F/B;;;GAGG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CA2C/D;AAED;;;;GAIG;AACH,wBAAsB,YAAY,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CA2ChE;AAED;;;;GAIG;AACH,wBAAsB,YAAY,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAyGhE;AAED;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsBrE;AAED;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CA2CrE"}
|