@hintergrund/worker 0.1.2 → 0.1.3

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 (63) hide show
  1. package/dist/api/cms/handlers/assets.handler.d.ts +24 -1
  2. package/dist/api/cms/handlers/assets.handler.d.ts.map +1 -1
  3. package/dist/api/cms/handlers/assets.handler.js +111 -210
  4. package/dist/api/cms/handlers/config.handler.d.ts +4 -0
  5. package/dist/api/cms/handlers/config.handler.d.ts.map +1 -1
  6. package/dist/api/cms/handlers/config.handler.js +20 -71
  7. package/dist/api/cms/handlers/content.handler.d.ts +12 -1
  8. package/dist/api/cms/handlers/content.handler.d.ts.map +1 -1
  9. package/dist/api/cms/handlers/content.handler.js +57 -233
  10. package/dist/api/cms/handlers/dashboard.handler.js +1 -1
  11. package/dist/api/emails/handlers/branding.handler.d.ts +17 -0
  12. package/dist/api/emails/handlers/branding.handler.d.ts.map +1 -0
  13. package/dist/api/emails/handlers/branding.handler.js +120 -0
  14. package/dist/api/emails/handlers/campaigns.handler.d.ts +32 -0
  15. package/dist/api/emails/handlers/campaigns.handler.d.ts.map +1 -0
  16. package/dist/api/emails/handlers/campaigns.handler.js +139 -0
  17. package/dist/api/emails/handlers/contacts.handler.d.ts +37 -0
  18. package/dist/api/emails/handlers/contacts.handler.d.ts.map +1 -0
  19. package/dist/api/emails/handlers/contacts.handler.js +161 -0
  20. package/dist/api/emails/handlers/invoice.handler.d.ts +30 -0
  21. package/dist/api/emails/handlers/invoice.handler.d.ts.map +1 -0
  22. package/dist/api/emails/handlers/invoice.handler.js +463 -0
  23. package/dist/api/emails/handlers/public.handler.d.ts +36 -0
  24. package/dist/api/emails/handlers/public.handler.d.ts.map +1 -0
  25. package/dist/api/emails/handlers/public.handler.js +350 -0
  26. package/dist/api/emails/handlers/send.handler.d.ts +23 -0
  27. package/dist/api/emails/handlers/send.handler.d.ts.map +1 -0
  28. package/dist/api/emails/handlers/send.handler.js +117 -0
  29. package/dist/api/emails/handlers/settings.handler.d.ts +36 -0
  30. package/dist/api/emails/handlers/settings.handler.d.ts.map +1 -0
  31. package/dist/api/emails/handlers/settings.handler.js +312 -0
  32. package/dist/api/emails/handlers/templates.handler.d.ts +32 -0
  33. package/dist/api/emails/handlers/templates.handler.d.ts.map +1 -0
  34. package/dist/api/emails/handlers/templates.handler.js +132 -0
  35. package/dist/api/emails/routes/admin.routes.d.ts +9 -0
  36. package/dist/api/emails/routes/admin.routes.d.ts.map +1 -0
  37. package/dist/api/emails/routes/admin.routes.js +77 -0
  38. package/dist/api/emails/routes/index.d.ts +9 -0
  39. package/dist/api/emails/routes/index.d.ts.map +1 -0
  40. package/dist/api/emails/routes/index.js +14 -0
  41. package/dist/api/emails/routes/public.routes.d.ts +9 -0
  42. package/dist/api/emails/routes/public.routes.d.ts.map +1 -0
  43. package/dist/api/emails/routes/public.routes.js +15 -0
  44. package/dist/api/shop/handlers/webhook.handler.d.ts.map +1 -1
  45. package/dist/api/shop/handlers/webhook.handler.js +66 -60
  46. package/dist/hintergrund-assets.d.ts +2 -2
  47. package/dist/hintergrund-assets.d.ts.map +1 -1
  48. package/dist/hintergrund-assets.js +2 -2
  49. package/dist/index.d.ts +9 -17
  50. package/dist/index.d.ts.map +1 -1
  51. package/dist/index.js +4 -7
  52. package/dist/lib/config.d.ts +13 -7
  53. package/dist/lib/config.d.ts.map +1 -1
  54. package/dist/lib/config.js +39 -44
  55. package/dist/lib/github.d.ts +124 -0
  56. package/dist/lib/github.d.ts.map +1 -0
  57. package/dist/lib/github.js +286 -0
  58. package/dist/lib/plunk.d.ts +207 -0
  59. package/dist/lib/plunk.d.ts.map +1 -0
  60. package/dist/lib/plunk.js +262 -0
  61. package/dist/middleware/auth.d.ts.map +1 -1
  62. package/dist/middleware/auth.js +2 -5
  63. package/package.json +2 -2
@@ -1,12 +1,32 @@
1
- import { getShopConfig } from '../../../lib/config';
1
+ import { getShopConfig, getEmailsConfig, getGitHubConfig } from '../../../lib/config';
2
+ import { createGitHubClient } from '../../../lib/github';
2
3
  import Stripe from 'stripe';
3
- import { generateInvoicePdf } from '../../newsletter/handlers/invoice.handler';
4
+ import { generateInvoicePdf } from '../../emails/handlers/invoice.handler';
4
5
  function getStripe(env) {
5
6
  const config = getShopConfig(env);
6
7
  return new Stripe(config.stripeSecretKey, {
7
8
  apiVersion: '2025-02-24.acacia'
8
9
  });
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
+ };
10
30
  /**
11
31
  * POST /api/shop/webhook
12
32
  * Handle Stripe webhook events
@@ -61,48 +81,34 @@ async function handleCheckoutSessionCompleted(c, stripe, session) {
61
81
  const lineItems = await stripe.checkout.sessions.listLineItems(session.id, {
62
82
  expand: ['data.price.product']
63
83
  });
64
- // Get invoice template
65
- const db = c.env.DB;
66
- if (!db) {
67
- console.error('Database not configured');
68
- return;
69
- }
70
- // Fetch invoice template from database
71
- const templateResult = await db
72
- .prepare("SELECT * FROM templates WHERE type = 'invoice'")
73
- .first();
84
+ // Get invoice template and branding from GitHub
74
85
  let template;
75
- if (templateResult) {
76
- template = {
77
- id: templateResult.id,
78
- config: JSON.parse(templateResult.config),
79
- logo: templateResult.logo,
80
- createdAt: templateResult.created_at,
81
- updatedAt: templateResult.updated_at
82
- };
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
+ }
83
106
  }
84
- else {
85
- // Use default template
107
+ catch (error) {
108
+ console.error('Failed to fetch settings from GitHub:', error);
86
109
  template = {
87
110
  id: 'invoice',
88
- config: {
89
- primaryColor: '#1f2937',
90
- accentColor: '#3b82f6',
91
- fontFamily: 'helvetica',
92
- logoPosition: 'left',
93
- showLogo: false,
94
- companyName: 'Your Company',
95
- companyAddress: '123 Main Street, City, Country',
96
- companyEmail: 'contact@company.com',
97
- companyPhone: '+1 234 567 890',
98
- footerText: 'Thank you for your business!',
99
- currencySymbol: '€',
100
- currencyPosition: 'before',
101
- showTaxBreakdown: true,
102
- taxLabel: 'VAT',
103
- taxRate: 19,
104
- dueDays: 14
105
- },
111
+ config: DEFAULT_INVOICE_CONFIG,
106
112
  createdAt: Date.now(),
107
113
  updatedAt: Date.now()
108
114
  };
@@ -139,8 +145,12 @@ async function handleCheckoutSessionCompleted(c, stripe, session) {
139
145
  currency: session.currency?.toUpperCase() || 'EUR',
140
146
  paymentMethod: session.payment_method_types?.[0] || 'card'
141
147
  };
142
- // Generate PDF
143
- const pdfBytes = await generateInvoicePdf(template, invoiceData);
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);
144
154
  // Send email with PDF attachment
145
155
  await sendInvoiceEmail(c, customerEmail, invoiceData, pdfBytes);
146
156
  console.log(`Invoice sent to ${customerEmail} for session ${session.id}`);
@@ -170,9 +180,10 @@ function formatAddress(address) {
170
180
  * Send invoice email with PDF attachment
171
181
  */
172
182
  async function sendInvoiceEmail(c, to, invoiceData, pdfBytes) {
173
- const apiKey = c.env.HG_NEWSLETTER_EMAIL_API_KEY;
174
- const fromEmail = c.env.HG_NEWSLETTER_FROM_EMAIL;
175
- const fromName = c.env.HG_NEWSLETTER_FROM_NAME || 'Shop';
183
+ const emailsConfig = getEmailsConfig(c.env);
184
+ const apiKey = emailsConfig.plunkSecretKey;
185
+ const fromEmail = emailsConfig.fromEmail;
186
+ const fromName = emailsConfig.fromName || 'Shop';
176
187
  if (!apiKey || !fromEmail) {
177
188
  console.error('Email not configured');
178
189
  return;
@@ -185,34 +196,29 @@ async function sendInvoiceEmail(c, to, invoiceData, pdfBytes) {
185
196
  <h1 style="color: #1f2937;">Thank you for your purchase!</h1>
186
197
  <p style="color: #4b5563;">Dear ${invoiceData.customerName},</p>
187
198
  <p style="color: #4b5563;">Thank you for your order. Please find your invoice attached to this email.</p>
188
-
199
+
189
200
  <div style="background-color: #f3f4f6; padding: 20px; border-radius: 8px; margin: 20px 0;">
190
201
  <p style="margin: 0; color: #1f2937;"><strong>Invoice Number:</strong> ${invoiceData.invoiceNumber}</p>
191
- <p style="margin: 8px 0 0; color: #1f2937;"><strong>Date:</strong> ${invoiceData.invoiceDate}</p>
192
- <p style="margin: 8px 0 0; color: #1f2937;"><strong>Total:</strong> ${invoiceData.currency} ${invoiceData.total.toFixed(2)}</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>
193
204
  </div>
194
-
205
+
195
206
  <p style="color: #4b5563;">If you have any questions about your order, please don't hesitate to contact us.</p>
196
-
207
+
197
208
  <p style="color: #9ca3af; font-size: 12px; margin-top: 40px;">
198
209
  This is an automated email. Please do not reply directly to this message.
199
210
  </p>
200
211
  </div>
201
212
  `;
213
+ // Send via Plunk API
202
214
  const payload = {
203
- from: `${fromName} <${fromEmail}>`,
204
215
  to,
205
216
  subject: `Your Invoice ${invoiceData.invoiceNumber}`,
206
- html,
207
- attachments: [
208
- {
209
- content: pdfBase64,
210
- filename: `invoice-${invoiceData.invoiceNumber}.pdf`,
211
- type: 'application/pdf'
212
- }
213
- ]
217
+ body: html,
218
+ from: fromEmail,
219
+ name: fromName,
214
220
  };
215
- const response = await fetch('https://api.resend.com/emails', {
221
+ const response = await fetch('https://api.useplunk.com/v1/send', {
216
222
  method: 'POST',
217
223
  headers: {
218
224
  Authorization: `Bearer ${apiKey}`,