@hintergrund/worker 0.1.6 → 0.1.8

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 (190) hide show
  1. package/dist/api/cms/handlers/admin-users.handler.d.ts +8 -0
  2. package/dist/api/cms/handlers/admin-users.handler.d.ts.map +1 -0
  3. package/dist/api/cms/handlers/admin-users.handler.js +121 -0
  4. package/dist/api/cms/handlers/agent-conversations.handler.d.ts +7 -0
  5. package/dist/api/cms/handlers/agent-conversations.handler.d.ts.map +1 -0
  6. package/dist/api/cms/handlers/agent-conversations.handler.js +94 -0
  7. package/dist/api/cms/handlers/auth.handler.d.ts +0 -3
  8. package/dist/api/cms/handlers/auth.handler.d.ts.map +1 -1
  9. package/dist/api/cms/handlers/auth.handler.js +74 -153
  10. package/dist/api/cms/handlers/dashboard.handler.js +1 -1
  11. package/dist/api/cms/handlers/website-repo.handler.d.ts +8 -0
  12. package/dist/api/cms/handlers/website-repo.handler.d.ts.map +1 -0
  13. package/dist/api/cms/handlers/website-repo.handler.js +148 -0
  14. package/dist/api/cms/routes/index.d.ts.map +1 -1
  15. package/dist/api/cms/routes/index.js +16 -4
  16. package/dist/api/emails/handlers/campaigns.handler.d.ts +10 -23
  17. package/dist/api/emails/handlers/campaigns.handler.d.ts.map +1 -1
  18. package/dist/api/emails/handlers/campaigns.handler.js +164 -92
  19. package/dist/api/emails/handlers/invoice.handler.d.ts +5 -19
  20. package/dist/api/emails/handlers/invoice.handler.d.ts.map +1 -1
  21. package/dist/api/emails/handlers/invoice.handler.js +58 -340
  22. package/dist/api/emails/handlers/public.handler.d.ts +6 -29
  23. package/dist/api/emails/handlers/public.handler.d.ts.map +1 -1
  24. package/dist/api/emails/handlers/public.handler.js +150 -312
  25. package/dist/api/emails/handlers/send.handler.d.ts +5 -17
  26. package/dist/api/emails/handlers/send.handler.d.ts.map +1 -1
  27. package/dist/api/emails/handlers/send.handler.js +107 -92
  28. package/dist/api/emails/handlers/settings.handler.d.ts +28 -33
  29. package/dist/api/emails/handlers/settings.handler.d.ts.map +1 -1
  30. package/dist/api/emails/handlers/settings.handler.js +96 -287
  31. package/dist/api/emails/handlers/subscribers.handler.d.ts +13 -0
  32. package/dist/api/emails/handlers/subscribers.handler.d.ts.map +1 -0
  33. package/dist/api/emails/handlers/subscribers.handler.js +125 -0
  34. package/dist/api/emails/handlers/webhook.handler.d.ts +1 -2
  35. package/dist/api/emails/handlers/webhook.handler.d.ts.map +1 -1
  36. package/dist/api/emails/handlers/webhook.handler.js +66 -166
  37. package/dist/api/emails/routes/admin.routes.d.ts +1 -3
  38. package/dist/api/emails/routes/admin.routes.d.ts.map +1 -1
  39. package/dist/api/emails/routes/admin.routes.js +19 -49
  40. package/dist/api/emails/routes/public.routes.d.ts +1 -3
  41. package/dist/api/emails/routes/public.routes.d.ts.map +1 -1
  42. package/dist/api/emails/routes/public.routes.js +2 -9
  43. package/dist/api/shop/handlers/prices.handler.d.ts +2 -15
  44. package/dist/api/shop/handlers/prices.handler.d.ts.map +1 -1
  45. package/dist/api/shop/handlers/prices.handler.js +2 -83
  46. package/dist/api/shop/handlers/products.handler.d.ts +29 -20
  47. package/dist/api/shop/handlers/products.handler.d.ts.map +1 -1
  48. package/dist/api/shop/handlers/products.handler.js +195 -162
  49. package/dist/api/shop/handlers/public.handler.d.ts +24 -8
  50. package/dist/api/shop/handlers/public.handler.d.ts.map +1 -1
  51. package/dist/api/shop/handlers/public.handler.js +525 -78
  52. package/dist/api/shop/routes.d.ts.map +1 -1
  53. package/dist/api/shop/routes.js +14 -9
  54. package/dist/hintergrund-assets.d.ts +3 -2
  55. package/dist/hintergrund-assets.d.ts.map +1 -1
  56. package/dist/hintergrund-assets.js +4 -2
  57. package/dist/index.d.ts +15 -8
  58. package/dist/index.d.ts.map +1 -1
  59. package/dist/index.js +44 -4
  60. package/dist/lib/auth/crypto.d.ts +2 -15
  61. package/dist/lib/auth/crypto.d.ts.map +1 -1
  62. package/dist/lib/auth/crypto.js +8 -95
  63. package/dist/lib/auth/jwt.d.ts +0 -4
  64. package/dist/lib/auth/jwt.d.ts.map +1 -1
  65. package/dist/lib/auth/jwt.js +0 -6
  66. package/dist/lib/config.d.ts +8 -10
  67. package/dist/lib/config.d.ts.map +1 -1
  68. package/dist/lib/config.js +30 -106
  69. package/dist/lib/email-template.d.ts +15 -0
  70. package/dist/lib/email-template.d.ts.map +1 -0
  71. package/dist/lib/email-template.js +58 -0
  72. package/dist/lib/email.d.ts +18 -8
  73. package/dist/lib/email.d.ts.map +1 -1
  74. package/dist/lib/email.js +59 -70
  75. package/dist/lib/github-app.d.ts +2 -0
  76. package/dist/lib/github-app.d.ts.map +1 -0
  77. package/dist/lib/github-app.js +49 -0
  78. package/dist/lib/github-config.d.ts +19 -0
  79. package/dist/lib/github-config.d.ts.map +1 -0
  80. package/dist/lib/github-config.js +23 -0
  81. package/dist/lib/github.d.ts +16 -3
  82. package/dist/lib/github.d.ts.map +1 -1
  83. package/dist/lib/github.js +38 -15
  84. package/dist/lib/project-db.d.ts +24 -0
  85. package/dist/lib/project-db.d.ts.map +1 -0
  86. package/dist/lib/project-db.js +172 -0
  87. package/dist/lib/template.js +4 -4
  88. package/dist/middleware/auth.d.ts +0 -2
  89. package/dist/middleware/auth.d.ts.map +1 -1
  90. package/dist/middleware/auth.js +21 -50
  91. package/package.json +3 -2
  92. package/dist/api/builder/handlers/generate.handler.d.ts +0 -12
  93. package/dist/api/builder/handlers/generate.handler.d.ts.map +0 -1
  94. package/dist/api/builder/handlers/generate.handler.js +0 -203
  95. package/dist/api/builder/handlers/template.handler.d.ts +0 -12
  96. package/dist/api/builder/handlers/template.handler.d.ts.map +0 -1
  97. package/dist/api/builder/handlers/template.handler.js +0 -208
  98. package/dist/api/builder/routes.d.ts +0 -7
  99. package/dist/api/builder/routes.d.ts.map +0 -1
  100. package/dist/api/builder/routes.js +0 -15
  101. package/dist/api/cms/handlers/assets.handler.d.ts +0 -31
  102. package/dist/api/cms/handlers/assets.handler.d.ts.map +0 -1
  103. package/dist/api/cms/handlers/assets.handler.js +0 -200
  104. package/dist/api/cms/handlers/config.handler.d.ts +0 -7
  105. package/dist/api/cms/handlers/config.handler.d.ts.map +0 -1
  106. package/dist/api/cms/handlers/config.handler.js +0 -37
  107. package/dist/api/cms/handlers/content.handler.d.ts +0 -21
  108. package/dist/api/cms/handlers/content.handler.d.ts.map +0 -1
  109. package/dist/api/cms/handlers/content.handler.js +0 -117
  110. package/dist/api/cms/routes/assets.routes.d.ts +0 -4
  111. package/dist/api/cms/routes/assets.routes.d.ts.map +0 -1
  112. package/dist/api/cms/routes/assets.routes.js +0 -13
  113. package/dist/api/cms/routes/content.routes.d.ts +0 -4
  114. package/dist/api/cms/routes/content.routes.d.ts.map +0 -1
  115. package/dist/api/cms/routes/content.routes.js +0 -11
  116. package/dist/api/emails/handlers/branding.handler.d.ts +0 -17
  117. package/dist/api/emails/handlers/branding.handler.d.ts.map +0 -1
  118. package/dist/api/emails/handlers/branding.handler.js +0 -120
  119. package/dist/api/emails/handlers/contacts.handler.d.ts +0 -37
  120. package/dist/api/emails/handlers/contacts.handler.d.ts.map +0 -1
  121. package/dist/api/emails/handlers/contacts.handler.js +0 -161
  122. package/dist/api/emails/handlers/templates.handler.d.ts +0 -32
  123. package/dist/api/emails/handlers/templates.handler.d.ts.map +0 -1
  124. package/dist/api/emails/handlers/templates.handler.js +0 -132
  125. package/dist/api/newsletter/handlers/email.handler.d.ts +0 -7
  126. package/dist/api/newsletter/handlers/email.handler.d.ts.map +0 -1
  127. package/dist/api/newsletter/handlers/email.handler.js +0 -102
  128. package/dist/api/newsletter/handlers/emails.handler.d.ts +0 -31
  129. package/dist/api/newsletter/handlers/emails.handler.d.ts.map +0 -1
  130. package/dist/api/newsletter/handlers/emails.handler.js +0 -262
  131. package/dist/api/newsletter/handlers/invoice.handler.d.ts +0 -22
  132. package/dist/api/newsletter/handlers/invoice.handler.d.ts.map +0 -1
  133. package/dist/api/newsletter/handlers/invoice.handler.js +0 -455
  134. package/dist/api/newsletter/handlers/public.handler.d.ts +0 -22
  135. package/dist/api/newsletter/handlers/public.handler.d.ts.map +0 -1
  136. package/dist/api/newsletter/handlers/public.handler.js +0 -300
  137. package/dist/api/newsletter/handlers/send.handler.d.ts +0 -12
  138. package/dist/api/newsletter/handlers/send.handler.d.ts.map +0 -1
  139. package/dist/api/newsletter/handlers/send.handler.js +0 -153
  140. package/dist/api/newsletter/handlers/subscribers.handler.d.ts +0 -22
  141. package/dist/api/newsletter/handlers/subscribers.handler.d.ts.map +0 -1
  142. package/dist/api/newsletter/handlers/subscribers.handler.js +0 -195
  143. package/dist/api/newsletter/routes/admin.routes.d.ts +0 -4
  144. package/dist/api/newsletter/routes/admin.routes.d.ts.map +0 -1
  145. package/dist/api/newsletter/routes/admin.routes.js +0 -28
  146. package/dist/api/newsletter/routes/index.d.ts +0 -4
  147. package/dist/api/newsletter/routes/index.d.ts.map +0 -1
  148. package/dist/api/newsletter/routes/index.js +0 -9
  149. package/dist/api/newsletter/routes/public.routes.d.ts +0 -4
  150. package/dist/api/newsletter/routes/public.routes.d.ts.map +0 -1
  151. package/dist/api/newsletter/routes/public.routes.js +0 -9
  152. package/dist/api/shop/handlers/assets.handler.d.ts +0 -3
  153. package/dist/api/shop/handlers/assets.handler.d.ts.map +0 -1
  154. package/dist/api/shop/handlers/assets.handler.js +0 -62
  155. package/dist/api/shop/handlers/checkout.handler.d.ts +0 -10
  156. package/dist/api/shop/handlers/checkout.handler.d.ts.map +0 -1
  157. package/dist/api/shop/handlers/checkout.handler.js +0 -50
  158. package/dist/api/shop/handlers/customers.handler.d.ts +0 -17
  159. package/dist/api/shop/handlers/customers.handler.d.ts.map +0 -1
  160. package/dist/api/shop/handlers/customers.handler.js +0 -89
  161. package/dist/api/shop/handlers/orders.handler.d.ts +0 -17
  162. package/dist/api/shop/handlers/orders.handler.d.ts.map +0 -1
  163. package/dist/api/shop/handlers/orders.handler.js +0 -84
  164. package/dist/api/shop/handlers/stats.handler.d.ts +0 -10
  165. package/dist/api/shop/handlers/stats.handler.d.ts.map +0 -1
  166. package/dist/api/shop/handlers/stats.handler.js +0 -71
  167. package/dist/api/shop/handlers/webhook.handler.d.ts +0 -10
  168. package/dist/api/shop/handlers/webhook.handler.d.ts.map +0 -1
  169. package/dist/api/shop/handlers/webhook.handler.js +0 -233
  170. package/dist/cms-assets.d.ts +0 -4
  171. package/dist/cms-assets.d.ts.map +0 -1
  172. package/dist/cms-assets.js +0 -5
  173. package/dist/cms-bundle.d.ts +0 -3
  174. package/dist/cms-bundle.d.ts.map +0 -1
  175. package/dist/cms-bundle.js +0 -4
  176. package/dist/config-loader.d.ts +0 -26
  177. package/dist/config-loader.d.ts.map +0 -1
  178. package/dist/config-loader.js +0 -98
  179. package/dist/jwt.d.ts +0 -26
  180. package/dist/jwt.d.ts.map +0 -1
  181. package/dist/jwt.js +0 -102
  182. package/dist/lib/plunk.d.ts +0 -204
  183. package/dist/lib/plunk.d.ts.map +0 -1
  184. package/dist/lib/plunk.js +0 -259
  185. package/dist/template.d.ts +0 -2
  186. package/dist/template.d.ts.map +0 -1
  187. package/dist/template.js +0 -61
  188. package/dist/utils.d.ts +0 -23
  189. package/dist/utils.d.ts.map +0 -1
  190. package/dist/utils.js +0 -110
@@ -1,71 +0,0 @@
1
- import { getShopConfig } from '../../../lib/config';
2
- import Stripe from 'stripe';
3
- function getStripe(env) {
4
- const config = getShopConfig(env);
5
- return new Stripe(config.stripeSecretKey, {
6
- apiVersion: '2025-02-24.acacia'
7
- });
8
- }
9
- /**
10
- * GET /api/shop/admin/stats
11
- * Get shop statistics
12
- */
13
- export async function getStats(c) {
14
- try {
15
- const stripe = getStripe(c.env);
16
- // Fetch data in parallel
17
- const [products, activeProducts, paymentIntents] = await Promise.all([
18
- stripe.products.list({ limit: 1 }),
19
- stripe.products.list({ active: true, limit: 1 }),
20
- stripe.paymentIntents.list({
21
- limit: 100,
22
- expand: ['data.customer']
23
- })
24
- ]);
25
- // Get total counts (using total_count if available)
26
- const totalProducts = products.data.length > 0 ? (await stripe.products.list({ limit: 100 })).data.length : 0;
27
- const activeProductCount = activeProducts.data.length > 0 ? (await stripe.products.list({ active: true, limit: 100 })).data.length : 0;
28
- // Calculate order stats
29
- const succeededPayments = paymentIntents.data.filter((pi) => pi.status === 'succeeded');
30
- const totalRevenue = succeededPayments.reduce((sum, pi) => sum + pi.amount_received, 0);
31
- // Get default currency from first payment or default to USD
32
- const currency = succeededPayments[0]?.currency || 'usd';
33
- // Map recent orders
34
- const recentOrders = paymentIntents.data.slice(0, 10).map((pi) => {
35
- let customerEmail = pi.receipt_email || null;
36
- let customerName = null;
37
- if (typeof pi.customer !== 'string' && pi.customer && !pi.customer.deleted) {
38
- const customer = pi.customer;
39
- customerEmail = customerEmail || customer.email || null;
40
- customerName = customer.name || null;
41
- }
42
- return {
43
- id: pi.id,
44
- customerId: typeof pi.customer === 'string' ? pi.customer : pi.customer?.id || null,
45
- customerEmail,
46
- customerName,
47
- amount: pi.amount,
48
- amountReceived: pi.amount_received,
49
- currency: pi.currency,
50
- status: pi.status,
51
- paymentMethod: typeof pi.payment_method === 'string' ? pi.payment_method : pi.payment_method?.id || null,
52
- lineItems: [],
53
- metadata: pi.metadata || {},
54
- createdAt: pi.created
55
- };
56
- });
57
- const response = {
58
- totalProducts,
59
- activeProducts: activeProductCount,
60
- totalOrders: succeededPayments.length,
61
- totalRevenue,
62
- currency,
63
- recentOrders
64
- };
65
- return c.json(response);
66
- }
67
- catch (error) {
68
- console.error('Failed to fetch stats:', error);
69
- return c.json({ success: false, error: 'Failed to fetch stats' }, 500);
70
- }
71
- }
@@ -1,10 +0,0 @@
1
- import type { Context } from 'hono';
2
- import type { Bindings } from '../../../index';
3
- /**
4
- * POST /api/shop/webhook
5
- * Handle Stripe webhook events
6
- */
7
- export declare function handleStripeWebhook(c: Context<{
8
- Bindings: Bindings;
9
- }>): Promise<Response>;
10
- //# sourceMappingURL=webhook.handler.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"webhook.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/shop/handlers/webhook.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAkC/C;;;GAGG;AACH,wBAAsB,mBAAmB,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsC/F"}
@@ -1,233 +0,0 @@
1
- import { getShopConfig, getEmailsConfig, getGitHubConfig } from '../../../lib/config';
2
- import { createGitHubClient } from '../../../lib/github';
3
- import Stripe from 'stripe';
4
- import { generateInvoicePdf } from '../../emails/handlers/invoice.handler';
5
- function getStripe(env) {
6
- const config = getShopConfig(env);
7
- return new Stripe(config.stripeSecretKey, {
8
- apiVersion: '2025-02-24.acacia'
9
- });
10
- }
11
- // Default invoice configuration
12
- const DEFAULT_INVOICE_CONFIG = {
13
- showLogo: true,
14
- primaryColor: '#1f2937',
15
- accentColor: '#3b82f6',
16
- logoPosition: 'left',
17
- fontFamily: 'helvetica',
18
- companyName: 'Your Company',
19
- companyAddress: '123 Main Street, City, Country',
20
- companyEmail: 'contact@company.com',
21
- companyPhone: '+1 234 567 890',
22
- currencySymbol: '€',
23
- currencyPosition: 'before',
24
- showTaxBreakdown: true,
25
- taxLabel: 'VAT',
26
- taxRate: 19,
27
- dueDays: 14,
28
- footerText: 'Thank you for your business!'
29
- };
30
- /**
31
- * POST /api/shop/webhook
32
- * Handle Stripe webhook events
33
- */
34
- export async function handleStripeWebhook(c) {
35
- const stripe = getStripe(c.env);
36
- const webhookSecret = c.env.HG_SHOP_STRIPE_WEBHOOK_SECRET;
37
- if (!webhookSecret) {
38
- console.error('Stripe webhook secret not configured');
39
- return c.json({ error: 'Webhook not configured' }, 500);
40
- }
41
- // Get the raw body and signature
42
- const signature = c.req.header('stripe-signature');
43
- if (!signature) {
44
- return c.json({ error: 'Missing stripe-signature header' }, 400);
45
- }
46
- const rawBody = await c.req.text();
47
- let event;
48
- try {
49
- event = await stripe.webhooks.constructEventAsync(rawBody, signature, webhookSecret);
50
- }
51
- catch (err) {
52
- console.error('Webhook signature verification failed:', err.message);
53
- return c.json({ error: 'Webhook signature verification failed' }, 400);
54
- }
55
- // Handle the event
56
- switch (event.type) {
57
- case 'checkout.session.completed': {
58
- const session = event.data.object;
59
- await handleCheckoutSessionCompleted(c, stripe, session);
60
- break;
61
- }
62
- // Add more event handlers as needed
63
- default:
64
- console.log(`Unhandled event type: ${event.type}`);
65
- }
66
- return c.json({ received: true });
67
- }
68
- /**
69
- * Handle checkout.session.completed event
70
- * Generate and send invoice PDF to customer
71
- */
72
- async function handleCheckoutSessionCompleted(c, stripe, session) {
73
- try {
74
- // Get customer email
75
- const customerEmail = session.customer_details?.email;
76
- if (!customerEmail) {
77
- console.error('No customer email in checkout session');
78
- return;
79
- }
80
- // Get line items
81
- const lineItems = await stripe.checkout.sessions.listLineItems(session.id, {
82
- expand: ['data.price.product']
83
- });
84
- // Get invoice template and branding from GitHub
85
- let template;
86
- let branding;
87
- try {
88
- const gitConfig = getGitHubConfig(c.env);
89
- const github = createGitHubClient(gitConfig);
90
- const settingsPath = `emails/settings.json`;
91
- const result = await github.readJsonFile(settingsPath);
92
- if (result?.data.invoice) {
93
- template = result.data.invoice;
94
- }
95
- else {
96
- template = {
97
- id: 'invoice',
98
- config: DEFAULT_INVOICE_CONFIG,
99
- createdAt: Date.now(),
100
- updatedAt: Date.now()
101
- };
102
- }
103
- if (result?.data.branding) {
104
- branding = result.data.branding;
105
- }
106
- }
107
- catch (error) {
108
- console.error('Failed to fetch settings from GitHub:', error);
109
- template = {
110
- id: 'invoice',
111
- config: DEFAULT_INVOICE_CONFIG,
112
- createdAt: Date.now(),
113
- updatedAt: Date.now()
114
- };
115
- }
116
- // Build invoice data
117
- const invoiceItems = lineItems.data.map((item) => {
118
- const product = item.price?.product;
119
- return {
120
- description: product?.name || item.description || 'Item',
121
- quantity: item.quantity || 1,
122
- unitPrice: (item.price?.unit_amount || 0) / 100,
123
- total: (item.amount_total || 0) / 100
124
- };
125
- });
126
- const subtotal = (session.amount_subtotal || 0) / 100;
127
- const tax = session.total_details?.amount_tax ? session.total_details.amount_tax / 100 : undefined;
128
- const total = (session.amount_total || 0) / 100;
129
- const invoiceData = {
130
- invoiceNumber: `INV-${session.id.slice(-8).toUpperCase()}`,
131
- invoiceDate: new Date().toLocaleDateString('en-US', {
132
- year: 'numeric',
133
- month: 'long',
134
- day: 'numeric'
135
- }),
136
- customerName: session.customer_details?.name || 'Customer',
137
- customerEmail,
138
- customerAddress: session.customer_details?.address
139
- ? formatAddress(session.customer_details.address)
140
- : undefined,
141
- items: invoiceItems,
142
- subtotal,
143
- tax,
144
- total,
145
- currency: session.currency?.toUpperCase() || 'EUR',
146
- paymentMethod: session.payment_method_types?.[0] || 'card'
147
- };
148
- // Generate PDF with branding
149
- const pdfBytes = await generateInvoicePdf(template, invoiceData, branding ? {
150
- logo: branding.logo,
151
- companyName: branding.config.companyName,
152
- primaryColor: branding.config.primaryColor,
153
- } : undefined);
154
- // Send email with PDF attachment
155
- await sendInvoiceEmail(c, customerEmail, invoiceData, pdfBytes);
156
- console.log(`Invoice sent to ${customerEmail} for session ${session.id}`);
157
- }
158
- catch (error) {
159
- console.error('Failed to handle checkout session completed:', error);
160
- }
161
- }
162
- /**
163
- * Format Stripe address object to string
164
- */
165
- function formatAddress(address) {
166
- const parts = [];
167
- if (address.line1)
168
- parts.push(address.line1);
169
- if (address.line2)
170
- parts.push(address.line2);
171
- if (address.city)
172
- parts.push(address.city);
173
- if (address.postal_code)
174
- parts.push(address.postal_code);
175
- if (address.country)
176
- parts.push(address.country);
177
- return parts.join(', ');
178
- }
179
- /**
180
- * Send invoice email with PDF attachment
181
- */
182
- async function sendInvoiceEmail(c, to, invoiceData, pdfBytes) {
183
- const emailsConfig = getEmailsConfig(c.env);
184
- const apiKey = emailsConfig.plunkSecretKey;
185
- const fromEmail = emailsConfig.fromEmail;
186
- const fromName = emailsConfig.fromName || 'Shop';
187
- if (!apiKey || !fromEmail) {
188
- console.error('Email not configured');
189
- return;
190
- }
191
- // Convert PDF to base64
192
- const pdfBase64 = btoa(String.fromCharCode(...pdfBytes));
193
- // Build email HTML
194
- const html = `
195
- <div style="font-family: sans-serif; max-width: 600px; margin: 0 auto; padding: 20px;">
196
- <h1 style="color: #1f2937;">Thank you for your purchase!</h1>
197
- <p style="color: #4b5563;">Dear ${invoiceData.customerName},</p>
198
- <p style="color: #4b5563;">Thank you for your order. Please find your invoice attached to this email.</p>
199
-
200
- <div style="background-color: #f3f4f6; padding: 20px; border-radius: 8px; margin: 20px 0;">
201
- <p style="margin: 0; color: #1f2937;"><strong>Invoice Number:</strong> ${invoiceData.invoiceNumber}</p>
202
- <p style="margin: 8px 0; color: #1f2937;"><strong>Date:</strong> ${invoiceData.invoiceDate}</p>
203
- <p style="margin: 8px 0; color: #1f2937;"><strong>Total:</strong> ${invoiceData.currency} ${invoiceData.total.toFixed(2)}</p>
204
- </div>
205
-
206
- <p style="color: #4b5563;">If you have any questions about your order, please don't hesitate to contact us.</p>
207
-
208
- <p style="color: #9ca3af; font-size: 12px; margin-top: 40px;">
209
- This is an automated email. Please do not reply directly to this message.
210
- </p>
211
- </div>
212
- `;
213
- // Send via Plunk API
214
- const payload = {
215
- to,
216
- subject: `Your Invoice ${invoiceData.invoiceNumber}`,
217
- body: html,
218
- from: fromEmail,
219
- name: fromName,
220
- };
221
- const response = await fetch('https://api.useplunk.com/v1/send', {
222
- method: 'POST',
223
- headers: {
224
- Authorization: `Bearer ${apiKey}`,
225
- 'Content-Type': 'application/json'
226
- },
227
- body: JSON.stringify(payload)
228
- });
229
- if (!response.ok) {
230
- const error = await response.text();
231
- throw new Error(`Failed to send invoice email: ${error}`);
232
- }
233
- }