@hintergrund/worker 0.1.0 → 0.1.2
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/newsletter/handlers/emails.handler.d.ts +13 -3
- package/dist/api/newsletter/handlers/emails.handler.d.ts.map +1 -1
- package/dist/api/newsletter/handlers/emails.handler.js +140 -12
- package/dist/api/newsletter/handlers/invoice.handler.d.ts +22 -0
- package/dist/api/newsletter/handlers/invoice.handler.d.ts.map +1 -0
- package/dist/api/newsletter/handlers/invoice.handler.js +455 -0
- package/dist/api/newsletter/handlers/public.handler.d.ts +4 -4
- package/dist/api/newsletter/handlers/public.handler.d.ts.map +1 -1
- package/dist/api/newsletter/handlers/public.handler.js +78 -20
- package/dist/api/newsletter/handlers/send.handler.d.ts +2 -2
- package/dist/api/newsletter/handlers/send.handler.js +3 -3
- package/dist/api/newsletter/handlers/subscribers.handler.d.ts +4 -4
- package/dist/api/newsletter/handlers/subscribers.handler.d.ts.map +1 -1
- package/dist/api/newsletter/handlers/subscribers.handler.js +8 -4
- package/dist/api/newsletter/routes/admin.routes.d.ts.map +1 -1
- package/dist/api/newsletter/routes/admin.routes.js +8 -1
- package/dist/api/shop/handlers/assets.handler.d.ts +3 -0
- package/dist/api/shop/handlers/assets.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/assets.handler.js +62 -0
- package/dist/api/shop/handlers/checkout.handler.d.ts +10 -0
- package/dist/api/shop/handlers/checkout.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/checkout.handler.js +50 -0
- package/dist/api/shop/handlers/products.handler.d.ts +0 -7
- package/dist/api/shop/handlers/products.handler.d.ts.map +1 -1
- package/dist/api/shop/handlers/products.handler.js +0 -23
- package/dist/api/shop/handlers/public.handler.d.ts +17 -0
- package/dist/api/shop/handlers/public.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/public.handler.js +108 -0
- package/dist/api/shop/handlers/webhook.handler.d.ts +10 -0
- package/dist/api/shop/handlers/webhook.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/webhook.handler.js +227 -0
- package/dist/api/shop/routes.d.ts.map +1 -1
- package/dist/api/shop/routes.js +9 -12
- package/dist/index.js +4 -4
- package/dist/lib/config.js +3 -3
- package/dist/middleware/auth.js +2 -2
- package/package.json +3 -2
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
import { PDFDocument, rgb, StandardFonts } from 'pdf-lib';
|
|
2
|
+
// Default invoice configuration
|
|
3
|
+
const DEFAULT_INVOICE_CONFIG = {
|
|
4
|
+
primaryColor: '#1f2937',
|
|
5
|
+
accentColor: '#3b82f6',
|
|
6
|
+
fontFamily: 'helvetica',
|
|
7
|
+
logoPosition: 'left',
|
|
8
|
+
showLogo: true,
|
|
9
|
+
companyName: 'Your Company',
|
|
10
|
+
companyAddress: '123 Main Street, City, Country',
|
|
11
|
+
companyEmail: 'contact@company.com',
|
|
12
|
+
companyPhone: '+1 234 567 890',
|
|
13
|
+
footerText: 'Thank you for your business!',
|
|
14
|
+
currencySymbol: '€',
|
|
15
|
+
currencyPosition: 'before',
|
|
16
|
+
showTaxBreakdown: true,
|
|
17
|
+
taxLabel: 'VAT',
|
|
18
|
+
taxRate: 19,
|
|
19
|
+
dueDays: 14
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* GET /api/email/admin/invoice-template
|
|
23
|
+
* Fetch the invoice template configuration
|
|
24
|
+
*/
|
|
25
|
+
export async function getInvoiceTemplate(c) {
|
|
26
|
+
try {
|
|
27
|
+
const db = c.env.DB;
|
|
28
|
+
if (!db) {
|
|
29
|
+
return c.json({ success: false, error: 'Database not configured' }, 500);
|
|
30
|
+
}
|
|
31
|
+
const result = await db
|
|
32
|
+
.prepare("SELECT * FROM templates WHERE type = 'invoice'")
|
|
33
|
+
.first();
|
|
34
|
+
if (!result) {
|
|
35
|
+
// Return default template if none exists
|
|
36
|
+
const template = {
|
|
37
|
+
id: 'invoice',
|
|
38
|
+
config: DEFAULT_INVOICE_CONFIG,
|
|
39
|
+
createdAt: Date.now(),
|
|
40
|
+
updatedAt: Date.now()
|
|
41
|
+
};
|
|
42
|
+
return c.json({ success: true, template });
|
|
43
|
+
}
|
|
44
|
+
const template = {
|
|
45
|
+
id: result.id,
|
|
46
|
+
config: JSON.parse(result.config),
|
|
47
|
+
logo: result.logo,
|
|
48
|
+
createdAt: result.created_at,
|
|
49
|
+
updatedAt: result.updated_at
|
|
50
|
+
};
|
|
51
|
+
return c.json({ success: true, template });
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
console.error('Failed to fetch invoice template:', error);
|
|
55
|
+
return c.json({ success: false, error: 'Failed to fetch invoice template' }, 500);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* POST /api/email/admin/invoice-template
|
|
60
|
+
* Save the invoice template configuration
|
|
61
|
+
*/
|
|
62
|
+
export async function saveInvoiceTemplate(c) {
|
|
63
|
+
try {
|
|
64
|
+
const db = c.env.DB;
|
|
65
|
+
if (!db) {
|
|
66
|
+
return c.json({ success: false, error: 'Database not configured' }, 500);
|
|
67
|
+
}
|
|
68
|
+
const body = await c.req.json();
|
|
69
|
+
const { config, logo } = body;
|
|
70
|
+
if (!config) {
|
|
71
|
+
return c.json({ success: false, error: 'Config is required' }, 400);
|
|
72
|
+
}
|
|
73
|
+
const now = Date.now();
|
|
74
|
+
const configJson = JSON.stringify(config);
|
|
75
|
+
// Check if template exists
|
|
76
|
+
const existing = await db
|
|
77
|
+
.prepare("SELECT id FROM templates WHERE type = 'invoice'")
|
|
78
|
+
.first();
|
|
79
|
+
if (existing) {
|
|
80
|
+
// Update existing template
|
|
81
|
+
await db
|
|
82
|
+
.prepare(`UPDATE templates
|
|
83
|
+
SET config = ?, logo = ?, updated_at = ?
|
|
84
|
+
WHERE type = 'invoice'`)
|
|
85
|
+
.bind(configJson, logo || null, now)
|
|
86
|
+
.run();
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
// Insert new template
|
|
90
|
+
await db
|
|
91
|
+
.prepare(`INSERT INTO templates (id, type, config, logo, created_at, updated_at)
|
|
92
|
+
VALUES ('invoice', 'invoice', ?, ?, ?, ?)`)
|
|
93
|
+
.bind(configJson, logo || null, now, now)
|
|
94
|
+
.run();
|
|
95
|
+
}
|
|
96
|
+
const template = {
|
|
97
|
+
id: 'invoice',
|
|
98
|
+
config,
|
|
99
|
+
logo,
|
|
100
|
+
createdAt: existing ? existing.created_at : now,
|
|
101
|
+
updatedAt: now
|
|
102
|
+
};
|
|
103
|
+
return c.json({ success: true, template });
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
console.error('Failed to save invoice template:', error);
|
|
107
|
+
return c.json({ success: false, error: 'Failed to save invoice template' }, 500);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Helper to parse hex color to RGB values
|
|
112
|
+
*/
|
|
113
|
+
function hexToRgb(hex) {
|
|
114
|
+
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
|
115
|
+
return result
|
|
116
|
+
? {
|
|
117
|
+
r: parseInt(result[1], 16) / 255,
|
|
118
|
+
g: parseInt(result[2], 16) / 255,
|
|
119
|
+
b: parseInt(result[3], 16) / 255
|
|
120
|
+
}
|
|
121
|
+
: { r: 0, g: 0, b: 0 };
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Format currency amount
|
|
125
|
+
*/
|
|
126
|
+
function formatCurrency(amount, symbol, position) {
|
|
127
|
+
const formattedAmount = amount.toFixed(2);
|
|
128
|
+
return position === 'before' ? `${symbol}${formattedAmount}` : `${formattedAmount}${symbol}`;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Generate PDF invoice
|
|
132
|
+
*/
|
|
133
|
+
export async function generateInvoicePdf(template, data) {
|
|
134
|
+
const pdfDoc = await PDFDocument.create();
|
|
135
|
+
const page = pdfDoc.addPage([595.28, 841.89]); // A4 size in points
|
|
136
|
+
const { width, height } = page.getSize();
|
|
137
|
+
// Get fonts
|
|
138
|
+
const config = template.config;
|
|
139
|
+
let font;
|
|
140
|
+
let boldFont;
|
|
141
|
+
switch (config.fontFamily) {
|
|
142
|
+
case 'times':
|
|
143
|
+
font = await pdfDoc.embedFont(StandardFonts.TimesRoman);
|
|
144
|
+
boldFont = await pdfDoc.embedFont(StandardFonts.TimesRomanBold);
|
|
145
|
+
break;
|
|
146
|
+
case 'courier':
|
|
147
|
+
font = await pdfDoc.embedFont(StandardFonts.Courier);
|
|
148
|
+
boldFont = await pdfDoc.embedFont(StandardFonts.CourierBold);
|
|
149
|
+
break;
|
|
150
|
+
default:
|
|
151
|
+
font = await pdfDoc.embedFont(StandardFonts.Helvetica);
|
|
152
|
+
boldFont = await pdfDoc.embedFont(StandardFonts.HelveticaBold);
|
|
153
|
+
}
|
|
154
|
+
const primaryColor = hexToRgb(config.primaryColor);
|
|
155
|
+
const accentColor = hexToRgb(config.accentColor);
|
|
156
|
+
const margin = 50;
|
|
157
|
+
let yPos = height - margin;
|
|
158
|
+
// Logo (if available and enabled)
|
|
159
|
+
if (config.showLogo && template.logo) {
|
|
160
|
+
try {
|
|
161
|
+
// Try to embed logo
|
|
162
|
+
const logoData = template.logo.split(',')[1]; // Remove data:image/... prefix
|
|
163
|
+
const logoBytes = Uint8Array.from(atob(logoData), (c) => c.charCodeAt(0));
|
|
164
|
+
let logoImage;
|
|
165
|
+
if (template.logo.includes('image/png')) {
|
|
166
|
+
logoImage = await pdfDoc.embedPng(logoBytes);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
logoImage = await pdfDoc.embedJpg(logoBytes);
|
|
170
|
+
}
|
|
171
|
+
const logoMaxHeight = 50;
|
|
172
|
+
const logoMaxWidth = 150;
|
|
173
|
+
const scale = Math.min(logoMaxWidth / logoImage.width, logoMaxHeight / logoImage.height);
|
|
174
|
+
const logoWidth = logoImage.width * scale;
|
|
175
|
+
const logoHeight = logoImage.height * scale;
|
|
176
|
+
let logoX = margin;
|
|
177
|
+
if (config.logoPosition === 'center') {
|
|
178
|
+
logoX = (width - logoWidth) / 2;
|
|
179
|
+
}
|
|
180
|
+
else if (config.logoPosition === 'right') {
|
|
181
|
+
logoX = width - margin - logoWidth;
|
|
182
|
+
}
|
|
183
|
+
page.drawImage(logoImage, {
|
|
184
|
+
x: logoX,
|
|
185
|
+
y: yPos - logoHeight,
|
|
186
|
+
width: logoWidth,
|
|
187
|
+
height: logoHeight
|
|
188
|
+
});
|
|
189
|
+
yPos -= logoHeight + 20;
|
|
190
|
+
}
|
|
191
|
+
catch (e) {
|
|
192
|
+
console.error('Failed to embed logo:', e);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// Company info (right-aligned)
|
|
196
|
+
const companyInfoX = width - margin;
|
|
197
|
+
page.drawText(config.companyName, {
|
|
198
|
+
x: companyInfoX - boldFont.widthOfTextAtSize(config.companyName, 12),
|
|
199
|
+
y: yPos,
|
|
200
|
+
size: 12,
|
|
201
|
+
font: boldFont,
|
|
202
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
203
|
+
});
|
|
204
|
+
yPos -= 15;
|
|
205
|
+
const addressLines = config.companyAddress.split(',').map((s) => s.trim());
|
|
206
|
+
for (const line of addressLines) {
|
|
207
|
+
page.drawText(line, {
|
|
208
|
+
x: companyInfoX - font.widthOfTextAtSize(line, 10),
|
|
209
|
+
y: yPos,
|
|
210
|
+
size: 10,
|
|
211
|
+
font,
|
|
212
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
213
|
+
});
|
|
214
|
+
yPos -= 12;
|
|
215
|
+
}
|
|
216
|
+
if (config.companyEmail) {
|
|
217
|
+
page.drawText(config.companyEmail, {
|
|
218
|
+
x: companyInfoX - font.widthOfTextAtSize(config.companyEmail, 10),
|
|
219
|
+
y: yPos,
|
|
220
|
+
size: 10,
|
|
221
|
+
font,
|
|
222
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
223
|
+
});
|
|
224
|
+
yPos -= 12;
|
|
225
|
+
}
|
|
226
|
+
if (config.companyPhone) {
|
|
227
|
+
page.drawText(config.companyPhone, {
|
|
228
|
+
x: companyInfoX - font.widthOfTextAtSize(config.companyPhone, 10),
|
|
229
|
+
y: yPos,
|
|
230
|
+
size: 10,
|
|
231
|
+
font,
|
|
232
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
233
|
+
});
|
|
234
|
+
yPos -= 12;
|
|
235
|
+
}
|
|
236
|
+
yPos -= 30;
|
|
237
|
+
// Invoice title
|
|
238
|
+
page.drawText('INVOICE', {
|
|
239
|
+
x: margin,
|
|
240
|
+
y: yPos,
|
|
241
|
+
size: 24,
|
|
242
|
+
font: boldFont,
|
|
243
|
+
color: rgb(accentColor.r, accentColor.g, accentColor.b)
|
|
244
|
+
});
|
|
245
|
+
yPos -= 30;
|
|
246
|
+
// Invoice details
|
|
247
|
+
page.drawText(`Invoice #: ${data.invoiceNumber}`, {
|
|
248
|
+
x: margin,
|
|
249
|
+
y: yPos,
|
|
250
|
+
size: 10,
|
|
251
|
+
font,
|
|
252
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
253
|
+
});
|
|
254
|
+
yPos -= 15;
|
|
255
|
+
page.drawText(`Date: ${data.invoiceDate}`, {
|
|
256
|
+
x: margin,
|
|
257
|
+
y: yPos,
|
|
258
|
+
size: 10,
|
|
259
|
+
font,
|
|
260
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
261
|
+
});
|
|
262
|
+
yPos -= 30;
|
|
263
|
+
// Bill To
|
|
264
|
+
page.drawText('Bill To:', {
|
|
265
|
+
x: margin,
|
|
266
|
+
y: yPos,
|
|
267
|
+
size: 10,
|
|
268
|
+
font: boldFont,
|
|
269
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
270
|
+
});
|
|
271
|
+
yPos -= 15;
|
|
272
|
+
page.drawText(data.customerName, {
|
|
273
|
+
x: margin,
|
|
274
|
+
y: yPos,
|
|
275
|
+
size: 10,
|
|
276
|
+
font,
|
|
277
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
278
|
+
});
|
|
279
|
+
yPos -= 12;
|
|
280
|
+
page.drawText(data.customerEmail, {
|
|
281
|
+
x: margin,
|
|
282
|
+
y: yPos,
|
|
283
|
+
size: 10,
|
|
284
|
+
font,
|
|
285
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
286
|
+
});
|
|
287
|
+
yPos -= 12;
|
|
288
|
+
if (data.customerAddress) {
|
|
289
|
+
page.drawText(data.customerAddress, {
|
|
290
|
+
x: margin,
|
|
291
|
+
y: yPos,
|
|
292
|
+
size: 10,
|
|
293
|
+
font,
|
|
294
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
295
|
+
});
|
|
296
|
+
yPos -= 12;
|
|
297
|
+
}
|
|
298
|
+
yPos -= 30;
|
|
299
|
+
// Table header
|
|
300
|
+
const tableTop = yPos;
|
|
301
|
+
const tableHeaderHeight = 25;
|
|
302
|
+
const colDescription = margin;
|
|
303
|
+
const colQty = width - margin - 200;
|
|
304
|
+
const colPrice = width - margin - 120;
|
|
305
|
+
const colTotal = width - margin - 40;
|
|
306
|
+
// Header background
|
|
307
|
+
page.drawRectangle({
|
|
308
|
+
x: margin,
|
|
309
|
+
y: yPos - tableHeaderHeight,
|
|
310
|
+
width: width - 2 * margin,
|
|
311
|
+
height: tableHeaderHeight,
|
|
312
|
+
color: rgb(accentColor.r, accentColor.g, accentColor.b)
|
|
313
|
+
});
|
|
314
|
+
// Header text
|
|
315
|
+
page.drawText('Description', {
|
|
316
|
+
x: colDescription + 10,
|
|
317
|
+
y: yPos - 17,
|
|
318
|
+
size: 10,
|
|
319
|
+
font: boldFont,
|
|
320
|
+
color: rgb(1, 1, 1)
|
|
321
|
+
});
|
|
322
|
+
page.drawText('Qty', {
|
|
323
|
+
x: colQty,
|
|
324
|
+
y: yPos - 17,
|
|
325
|
+
size: 10,
|
|
326
|
+
font: boldFont,
|
|
327
|
+
color: rgb(1, 1, 1)
|
|
328
|
+
});
|
|
329
|
+
page.drawText('Price', {
|
|
330
|
+
x: colPrice,
|
|
331
|
+
y: yPos - 17,
|
|
332
|
+
size: 10,
|
|
333
|
+
font: boldFont,
|
|
334
|
+
color: rgb(1, 1, 1)
|
|
335
|
+
});
|
|
336
|
+
page.drawText('Total', {
|
|
337
|
+
x: colTotal - font.widthOfTextAtSize('Total', 10),
|
|
338
|
+
y: yPos - 17,
|
|
339
|
+
size: 10,
|
|
340
|
+
font: boldFont,
|
|
341
|
+
color: rgb(1, 1, 1)
|
|
342
|
+
});
|
|
343
|
+
yPos -= tableHeaderHeight + 15;
|
|
344
|
+
// Table rows
|
|
345
|
+
for (const item of data.items) {
|
|
346
|
+
page.drawText(item.description, {
|
|
347
|
+
x: colDescription + 10,
|
|
348
|
+
y: yPos,
|
|
349
|
+
size: 10,
|
|
350
|
+
font,
|
|
351
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
352
|
+
});
|
|
353
|
+
page.drawText(item.quantity.toString(), {
|
|
354
|
+
x: colQty,
|
|
355
|
+
y: yPos,
|
|
356
|
+
size: 10,
|
|
357
|
+
font,
|
|
358
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
359
|
+
});
|
|
360
|
+
const priceStr = formatCurrency(item.unitPrice, config.currencySymbol, config.currencyPosition);
|
|
361
|
+
page.drawText(priceStr, {
|
|
362
|
+
x: colPrice,
|
|
363
|
+
y: yPos,
|
|
364
|
+
size: 10,
|
|
365
|
+
font,
|
|
366
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
367
|
+
});
|
|
368
|
+
const totalStr = formatCurrency(item.total, config.currencySymbol, config.currencyPosition);
|
|
369
|
+
page.drawText(totalStr, {
|
|
370
|
+
x: colTotal - font.widthOfTextAtSize(totalStr, 10),
|
|
371
|
+
y: yPos,
|
|
372
|
+
size: 10,
|
|
373
|
+
font,
|
|
374
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
375
|
+
});
|
|
376
|
+
yPos -= 20;
|
|
377
|
+
}
|
|
378
|
+
// Line separator
|
|
379
|
+
yPos -= 10;
|
|
380
|
+
page.drawLine({
|
|
381
|
+
start: { x: margin, y: yPos },
|
|
382
|
+
end: { x: width - margin, y: yPos },
|
|
383
|
+
thickness: 1,
|
|
384
|
+
color: rgb(0.8, 0.8, 0.8)
|
|
385
|
+
});
|
|
386
|
+
yPos -= 20;
|
|
387
|
+
// Subtotal
|
|
388
|
+
if (config.showTaxBreakdown) {
|
|
389
|
+
const subtotalLabel = 'Subtotal:';
|
|
390
|
+
const subtotalValue = formatCurrency(data.subtotal, config.currencySymbol, config.currencyPosition);
|
|
391
|
+
page.drawText(subtotalLabel, {
|
|
392
|
+
x: colPrice - font.widthOfTextAtSize(subtotalLabel, 10),
|
|
393
|
+
y: yPos,
|
|
394
|
+
size: 10,
|
|
395
|
+
font,
|
|
396
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
397
|
+
});
|
|
398
|
+
page.drawText(subtotalValue, {
|
|
399
|
+
x: colTotal - font.widthOfTextAtSize(subtotalValue, 10),
|
|
400
|
+
y: yPos,
|
|
401
|
+
size: 10,
|
|
402
|
+
font,
|
|
403
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
404
|
+
});
|
|
405
|
+
yPos -= 15;
|
|
406
|
+
// Tax
|
|
407
|
+
if (data.tax !== undefined) {
|
|
408
|
+
const taxLabel = `${config.taxLabel} (${config.taxRate}%):`;
|
|
409
|
+
const taxValue = formatCurrency(data.tax, config.currencySymbol, config.currencyPosition);
|
|
410
|
+
page.drawText(taxLabel, {
|
|
411
|
+
x: colPrice - font.widthOfTextAtSize(taxLabel, 10),
|
|
412
|
+
y: yPos,
|
|
413
|
+
size: 10,
|
|
414
|
+
font,
|
|
415
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
416
|
+
});
|
|
417
|
+
page.drawText(taxValue, {
|
|
418
|
+
x: colTotal - font.widthOfTextAtSize(taxValue, 10),
|
|
419
|
+
y: yPos,
|
|
420
|
+
size: 10,
|
|
421
|
+
font,
|
|
422
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
423
|
+
});
|
|
424
|
+
yPos -= 15;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
// Total
|
|
428
|
+
const totalLabel = 'Total:';
|
|
429
|
+
const totalValue = formatCurrency(data.total, config.currencySymbol, config.currencyPosition);
|
|
430
|
+
page.drawText(totalLabel, {
|
|
431
|
+
x: colPrice - boldFont.widthOfTextAtSize(totalLabel, 12),
|
|
432
|
+
y: yPos,
|
|
433
|
+
size: 12,
|
|
434
|
+
font: boldFont,
|
|
435
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
436
|
+
});
|
|
437
|
+
page.drawText(totalValue, {
|
|
438
|
+
x: colTotal - boldFont.widthOfTextAtSize(totalValue, 12),
|
|
439
|
+
y: yPos,
|
|
440
|
+
size: 12,
|
|
441
|
+
font: boldFont,
|
|
442
|
+
color: rgb(accentColor.r, accentColor.g, accentColor.b)
|
|
443
|
+
});
|
|
444
|
+
// Footer
|
|
445
|
+
if (config.footerText) {
|
|
446
|
+
page.drawText(config.footerText, {
|
|
447
|
+
x: (width - font.widthOfTextAtSize(config.footerText, 10)) / 2,
|
|
448
|
+
y: margin,
|
|
449
|
+
size: 10,
|
|
450
|
+
font,
|
|
451
|
+
color: rgb(0.5, 0.5, 0.5)
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
return await pdfDoc.save();
|
|
455
|
+
}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import { Context } from 'hono';
|
|
2
2
|
/**
|
|
3
|
-
* POST /api/
|
|
3
|
+
* POST /api/email/subscribe
|
|
4
4
|
* Public endpoint to subscribe to newsletter
|
|
5
5
|
*/
|
|
6
6
|
export declare function subscribe(c: Context): Promise<Response>;
|
|
7
7
|
/**
|
|
8
|
-
* GET /api/
|
|
8
|
+
* GET /api/email/confirm?token=xxx
|
|
9
9
|
* Public endpoint to confirm subscription
|
|
10
10
|
*/
|
|
11
11
|
export declare function confirm(c: Context): Promise<Response>;
|
|
12
12
|
/**
|
|
13
|
-
* GET /api/
|
|
13
|
+
* GET /api/email/unsubscribe?token=xxx
|
|
14
14
|
* Public endpoint to show unsubscribe confirmation page
|
|
15
15
|
*/
|
|
16
16
|
export declare function showUnsubscribePage(c: Context): Promise<Response>;
|
|
17
17
|
/**
|
|
18
|
-
* POST /api/
|
|
18
|
+
* POST /api/email/unsubscribe
|
|
19
19
|
* Public endpoint to unsubscribe from newsletter
|
|
20
20
|
*/
|
|
21
21
|
export declare function unsubscribe(c: Context): Promise<Response>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"public.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/newsletter/handlers/public.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"public.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/newsletter/handlers/public.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AA0F/B;;;GAGG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAuH7D;AAED;;;GAGG;AACH,wBAAsB,OAAO,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CA8F3D;AAED;;;GAGG;AACH,wBAAsB,mBAAmB,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAcvE;AAED;;;GAGG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAqD/D"}
|
|
@@ -12,15 +12,16 @@ function isValidEmail(email) {
|
|
|
12
12
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
13
13
|
return emailRegex.test(email);
|
|
14
14
|
}
|
|
15
|
-
//
|
|
16
|
-
async function
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
15
|
+
// Helper to get auto email template
|
|
16
|
+
async function getAutoEmailTemplate(db, type) {
|
|
17
|
+
const template = await db
|
|
18
|
+
.prepare('SELECT subject, html_content FROM emails WHERE email_type = ? LIMIT 1')
|
|
19
|
+
.bind(type)
|
|
20
|
+
.first();
|
|
21
|
+
return template;
|
|
22
|
+
}
|
|
23
|
+
// Send email via Resend
|
|
24
|
+
async function sendEmailViaResend(apiKey, to, from, fromName, subject, html) {
|
|
24
25
|
const response = await fetch('https://api.resend.com/emails', {
|
|
25
26
|
method: 'POST',
|
|
26
27
|
headers: {
|
|
@@ -29,18 +30,39 @@ async function sendConfirmationEmail(email, token, apiKey, from, fromName, confi
|
|
|
29
30
|
},
|
|
30
31
|
body: JSON.stringify({
|
|
31
32
|
from: `${fromName} <${from}>`,
|
|
32
|
-
to
|
|
33
|
-
subject
|
|
33
|
+
to,
|
|
34
|
+
subject,
|
|
34
35
|
html,
|
|
35
36
|
}),
|
|
36
37
|
});
|
|
37
38
|
if (!response.ok) {
|
|
38
39
|
const error = await response.text();
|
|
39
|
-
throw new Error(`Failed to send
|
|
40
|
+
throw new Error(`Failed to send email: ${error}`);
|
|
40
41
|
}
|
|
41
42
|
}
|
|
43
|
+
// Send confirmation email via Resend
|
|
44
|
+
async function sendConfirmationEmail(db, email, token, apiKey, from, fromName, confirmationUrl) {
|
|
45
|
+
const confirmLink = `${confirmationUrl}?token=${token}`;
|
|
46
|
+
const template = await getAutoEmailTemplate(db, 'confirm');
|
|
47
|
+
let html = '';
|
|
48
|
+
let subject = 'Confirm your subscription';
|
|
49
|
+
if (template) {
|
|
50
|
+
subject = template.subject;
|
|
51
|
+
html = template.html_content.replace(/{{confirm_url}}/g, confirmLink);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
// Default template
|
|
55
|
+
html = `
|
|
56
|
+
<h2>Confirm your subscription</h2>
|
|
57
|
+
<p>Thank you for subscribing! Please confirm your email address by clicking the link below:</p>
|
|
58
|
+
<p><a href="${confirmLink}">${confirmLink}</a></p>
|
|
59
|
+
<p>If you didn't request this, you can safely ignore this email.</p>
|
|
60
|
+
`;
|
|
61
|
+
}
|
|
62
|
+
await sendEmailViaResend(apiKey, email, from, fromName, subject, html);
|
|
63
|
+
}
|
|
42
64
|
/**
|
|
43
|
-
* POST /api/
|
|
65
|
+
* POST /api/email/subscribe
|
|
44
66
|
* Public endpoint to subscribe to newsletter
|
|
45
67
|
*/
|
|
46
68
|
export async function subscribe(c) {
|
|
@@ -86,7 +108,7 @@ export async function subscribe(c) {
|
|
|
86
108
|
.bind(token, now, now, normalizedEmail)
|
|
87
109
|
.run();
|
|
88
110
|
// Send confirmation email
|
|
89
|
-
await sendConfirmationEmail(normalizedEmail, token, apiKey, fromEmail, fromName || 'Newsletter', confirmationUrl);
|
|
111
|
+
await sendConfirmationEmail(db, normalizedEmail, token, apiKey, fromEmail, fromName || 'Newsletter', confirmationUrl);
|
|
90
112
|
return c.json({
|
|
91
113
|
success: true,
|
|
92
114
|
message: 'Please check your email to confirm your subscription',
|
|
@@ -104,7 +126,7 @@ export async function subscribe(c) {
|
|
|
104
126
|
.bind(token, now, now, normalizedEmail)
|
|
105
127
|
.run();
|
|
106
128
|
// Send confirmation email
|
|
107
|
-
await sendConfirmationEmail(normalizedEmail, token, apiKey, fromEmail, fromName || 'Newsletter', confirmationUrl);
|
|
129
|
+
await sendConfirmationEmail(db, normalizedEmail, token, apiKey, fromEmail, fromName || 'Newsletter', confirmationUrl);
|
|
108
130
|
return c.json({
|
|
109
131
|
success: true,
|
|
110
132
|
message: 'Please check your email to confirm your subscription',
|
|
@@ -123,7 +145,7 @@ export async function subscribe(c) {
|
|
|
123
145
|
.bind(id, normalizedEmail, name || null, token, unsubscribeToken, now, now, now)
|
|
124
146
|
.run();
|
|
125
147
|
// Send confirmation email
|
|
126
|
-
await sendConfirmationEmail(normalizedEmail, token, apiKey, fromEmail, fromName || 'Newsletter', confirmationUrl);
|
|
148
|
+
await sendConfirmationEmail(db, normalizedEmail, token, apiKey, fromEmail, fromName || 'Newsletter', confirmationUrl);
|
|
127
149
|
return c.json({
|
|
128
150
|
success: true,
|
|
129
151
|
message: 'Please check your email to confirm your subscription',
|
|
@@ -138,7 +160,7 @@ export async function subscribe(c) {
|
|
|
138
160
|
}
|
|
139
161
|
}
|
|
140
162
|
/**
|
|
141
|
-
* GET /api/
|
|
163
|
+
* GET /api/email/confirm?token=xxx
|
|
142
164
|
* Public endpoint to confirm subscription
|
|
143
165
|
*/
|
|
144
166
|
export async function confirm(c) {
|
|
@@ -150,7 +172,7 @@ export async function confirm(c) {
|
|
|
150
172
|
const db = c.env.DB;
|
|
151
173
|
// Find subscriber by confirmation token
|
|
152
174
|
const subscriber = await db
|
|
153
|
-
.prepare('SELECT id, status FROM subscribers WHERE confirmation_token = ?')
|
|
175
|
+
.prepare('SELECT id, status, email FROM subscribers WHERE confirmation_token = ?')
|
|
154
176
|
.bind(token)
|
|
155
177
|
.first();
|
|
156
178
|
if (!subscriber) {
|
|
@@ -164,6 +186,42 @@ export async function confirm(c) {
|
|
|
164
186
|
WHERE id = ?`)
|
|
165
187
|
.bind(now, now, now, subscriber.id)
|
|
166
188
|
.run();
|
|
189
|
+
// Send welcome email if configured
|
|
190
|
+
try {
|
|
191
|
+
const welcomeTemplate = await getAutoEmailTemplate(db, 'welcome');
|
|
192
|
+
if (welcomeTemplate) {
|
|
193
|
+
const apiKey = c.env.HG_NEWSLETTER_EMAIL_API_KEY;
|
|
194
|
+
const fromEmail = c.env.HG_NEWSLETTER_FROM_EMAIL;
|
|
195
|
+
const fromName = c.env.HG_NEWSLETTER_FROM_NAME;
|
|
196
|
+
if (apiKey && fromEmail) {
|
|
197
|
+
// We need unsubscribe link for welcome email usually, but public handler might not have easy access to generate it
|
|
198
|
+
// without the unsubscribe token. Wait, we have the subscriber, we can get their unsubscribe token.
|
|
199
|
+
// But we just updated the record. Let's fetch the unsubscribe token.
|
|
200
|
+
const subData = await db
|
|
201
|
+
.prepare('SELECT unsubscribe_token FROM subscribers WHERE id = ?')
|
|
202
|
+
.bind(subscriber.id)
|
|
203
|
+
.first();
|
|
204
|
+
// If we can't get it easily, we might skip it or use a placeholder.
|
|
205
|
+
// Assuming the template has {{unsubscribe_url}}.
|
|
206
|
+
// The unsubscribe URL is likely /newsletter/unsubscribe?token=...
|
|
207
|
+
// Base URL? We can use c.req.url origin or an env var.
|
|
208
|
+
// The confirmation URL env var base can be used.
|
|
209
|
+
const confirmationUrl = c.env.HG_NEWSLETTER_CONFIRMATION_URL;
|
|
210
|
+
let unsubscribeUrl = '#';
|
|
211
|
+
if (confirmationUrl && subData?.unsubscribe_token) {
|
|
212
|
+
// Assuming confirmationUrl is .../newsletter/confirm
|
|
213
|
+
const baseUrl = new URL(confirmationUrl).origin;
|
|
214
|
+
unsubscribeUrl = `${baseUrl}/newsletter/unsubscribe?token=${subData.unsubscribe_token}`;
|
|
215
|
+
}
|
|
216
|
+
const html = welcomeTemplate.html_content.replace(/{{unsubscribe_url}}/g, unsubscribeUrl);
|
|
217
|
+
await sendEmailViaResend(apiKey, subscriber.email, fromEmail, fromName || 'Newsletter', welcomeTemplate.subject, html);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
catch (emailError) {
|
|
222
|
+
console.error('Failed to send welcome email:', emailError);
|
|
223
|
+
// Don't fail the confirmation request just because welcome email failed
|
|
224
|
+
}
|
|
167
225
|
return c.json({
|
|
168
226
|
success: true,
|
|
169
227
|
message: 'Your subscription has been confirmed! Thank you for subscribing.',
|
|
@@ -178,7 +236,7 @@ export async function confirm(c) {
|
|
|
178
236
|
}
|
|
179
237
|
}
|
|
180
238
|
/**
|
|
181
|
-
* GET /api/
|
|
239
|
+
* GET /api/email/unsubscribe?token=xxx
|
|
182
240
|
* Public endpoint to show unsubscribe confirmation page
|
|
183
241
|
*/
|
|
184
242
|
export async function showUnsubscribePage(c) {
|
|
@@ -193,7 +251,7 @@ export async function showUnsubscribePage(c) {
|
|
|
193
251
|
return c.html(getTemplate('unsubscribe', '', tokenScript));
|
|
194
252
|
}
|
|
195
253
|
/**
|
|
196
|
-
* POST /api/
|
|
254
|
+
* POST /api/email/unsubscribe
|
|
197
255
|
* Public endpoint to unsubscribe from newsletter
|
|
198
256
|
*/
|
|
199
257
|
export async function unsubscribe(c) {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Context } from 'hono';
|
|
2
2
|
/**
|
|
3
|
-
* POST /api/
|
|
3
|
+
* POST /api/email/admin/send
|
|
4
4
|
* Send bulk email to all subscribed users (admin only)
|
|
5
5
|
*/
|
|
6
6
|
export declare function sendBulkEmail(c: Context): Promise<Response>;
|
|
7
7
|
/**
|
|
8
|
-
* POST /api/
|
|
8
|
+
* POST /api/email/admin/emails/test
|
|
9
9
|
* Send test email to a single email address
|
|
10
10
|
*/
|
|
11
11
|
export declare function sendTestEmail(c: Context): Promise<Response>;
|