@hintergrund/worker 0.1.1 → 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.
- 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/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/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/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 +15 -0
- 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 +233 -0
- package/dist/api/shop/routes.d.ts.map +1 -1
- package/dist/api/shop/routes.js +9 -12
- 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 +9 -17
- 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 +39 -44
- 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 +207 -0
- package/dist/lib/plunk.d.ts.map +1 -0
- package/dist/lib/plunk.js +262 -0
- package/dist/middleware/auth.d.ts.map +1 -1
- package/dist/middleware/auth.js +2 -5
- package/package.json +32 -31
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contacts Handler
|
|
3
|
+
*
|
|
4
|
+
* Manages email contacts via Plunk API.
|
|
5
|
+
*/
|
|
6
|
+
import { Context } from 'hono';
|
|
7
|
+
/**
|
|
8
|
+
* GET /api/email/admin/contacts
|
|
9
|
+
* Get all contacts from Plunk
|
|
10
|
+
*/
|
|
11
|
+
export declare function getContacts(c: Context): Promise<Response>;
|
|
12
|
+
/**
|
|
13
|
+
* POST /api/email/admin/contacts
|
|
14
|
+
* Create a new contact in Plunk
|
|
15
|
+
*/
|
|
16
|
+
export declare function createContact(c: Context): Promise<Response>;
|
|
17
|
+
/**
|
|
18
|
+
* PATCH /api/email/admin/contacts/:id
|
|
19
|
+
* Update an existing contact in Plunk
|
|
20
|
+
*/
|
|
21
|
+
export declare function updateContact(c: Context): Promise<Response>;
|
|
22
|
+
/**
|
|
23
|
+
* DELETE /api/email/admin/contacts/:id
|
|
24
|
+
* Delete a contact from Plunk
|
|
25
|
+
*/
|
|
26
|
+
export declare function deleteContact(c: Context): Promise<Response>;
|
|
27
|
+
/**
|
|
28
|
+
* POST /api/email/admin/contacts/:id/subscribe
|
|
29
|
+
* Subscribe a contact
|
|
30
|
+
*/
|
|
31
|
+
export declare function subscribeContact(c: Context): Promise<Response>;
|
|
32
|
+
/**
|
|
33
|
+
* POST /api/email/admin/contacts/:id/unsubscribe
|
|
34
|
+
* Unsubscribe a contact
|
|
35
|
+
*/
|
|
36
|
+
export declare function unsubscribeContact(c: Context): Promise<Response>;
|
|
37
|
+
//# sourceMappingURL=contacts.handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contacts.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/emails/handlers/contacts.handler.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAG/B;;;GAGG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAgB/D;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CA2CjE;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAqCjE;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsBjE;AAED;;;GAGG;AACH,wBAAsB,gBAAgB,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsBpE;AAED;;;GAGG;AACH,wBAAsB,kBAAkB,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsBtE"}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contacts Handler
|
|
3
|
+
*
|
|
4
|
+
* Manages email contacts via Plunk API.
|
|
5
|
+
*/
|
|
6
|
+
import { createPlunkClient } from '../../../lib/plunk';
|
|
7
|
+
/**
|
|
8
|
+
* GET /api/email/admin/contacts
|
|
9
|
+
* Get all contacts from Plunk
|
|
10
|
+
*/
|
|
11
|
+
export async function getContacts(c) {
|
|
12
|
+
try {
|
|
13
|
+
const plunk = createPlunkClient(c.env);
|
|
14
|
+
const { contacts, count } = await plunk.getContacts();
|
|
15
|
+
return c.json({ contacts, count });
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
console.error('Get contacts error:', error);
|
|
19
|
+
return c.json({
|
|
20
|
+
success: false,
|
|
21
|
+
message: error instanceof Error ? error.message : 'Failed to fetch contacts',
|
|
22
|
+
}, 500);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* POST /api/email/admin/contacts
|
|
27
|
+
* Create a new contact in Plunk
|
|
28
|
+
*/
|
|
29
|
+
export async function createContact(c) {
|
|
30
|
+
try {
|
|
31
|
+
const body = await c.req.json();
|
|
32
|
+
if (!body.email) {
|
|
33
|
+
return c.json({ success: false, message: 'Email is required' }, 400);
|
|
34
|
+
}
|
|
35
|
+
// Validate email format
|
|
36
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
37
|
+
if (!emailRegex.test(body.email)) {
|
|
38
|
+
return c.json({ success: false, message: 'Invalid email format' }, 400);
|
|
39
|
+
}
|
|
40
|
+
const plunk = createPlunkClient(c.env);
|
|
41
|
+
const result = await plunk.createContact({
|
|
42
|
+
email: body.email.toLowerCase().trim(),
|
|
43
|
+
subscribed: body.subscribed ?? true, // Default to subscribed for admin-created contacts
|
|
44
|
+
data: body.data,
|
|
45
|
+
});
|
|
46
|
+
return c.json({
|
|
47
|
+
success: true,
|
|
48
|
+
contact: {
|
|
49
|
+
id: result.id,
|
|
50
|
+
email: result.email,
|
|
51
|
+
subscribed: result.subscribed,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
console.error('Create contact error:', error);
|
|
57
|
+
return c.json({
|
|
58
|
+
success: false,
|
|
59
|
+
message: error instanceof Error ? error.message : 'Failed to create contact',
|
|
60
|
+
}, 500);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* PATCH /api/email/admin/contacts/:id
|
|
65
|
+
* Update an existing contact in Plunk
|
|
66
|
+
*/
|
|
67
|
+
export async function updateContact(c) {
|
|
68
|
+
try {
|
|
69
|
+
const id = c.req.param('id');
|
|
70
|
+
if (!id) {
|
|
71
|
+
return c.json({ success: false, message: 'Contact ID is required' }, 400);
|
|
72
|
+
}
|
|
73
|
+
const body = await c.req.json();
|
|
74
|
+
const plunk = createPlunkClient(c.env);
|
|
75
|
+
const result = await plunk.updateContact(id, {
|
|
76
|
+
subscribed: body.subscribed,
|
|
77
|
+
data: body.data,
|
|
78
|
+
});
|
|
79
|
+
return c.json({
|
|
80
|
+
success: true,
|
|
81
|
+
contact: {
|
|
82
|
+
id: result.id,
|
|
83
|
+
email: result.email,
|
|
84
|
+
subscribed: result.subscribed,
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
console.error('Update contact error:', error);
|
|
90
|
+
return c.json({
|
|
91
|
+
success: false,
|
|
92
|
+
message: error instanceof Error ? error.message : 'Failed to update contact',
|
|
93
|
+
}, 500);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* DELETE /api/email/admin/contacts/:id
|
|
98
|
+
* Delete a contact from Plunk
|
|
99
|
+
*/
|
|
100
|
+
export async function deleteContact(c) {
|
|
101
|
+
try {
|
|
102
|
+
const id = c.req.param('id');
|
|
103
|
+
if (!id) {
|
|
104
|
+
return c.json({ success: false, message: 'Contact ID is required' }, 400);
|
|
105
|
+
}
|
|
106
|
+
const plunk = createPlunkClient(c.env);
|
|
107
|
+
await plunk.deleteContact(id);
|
|
108
|
+
return c.json({ success: true });
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
console.error('Delete contact error:', error);
|
|
112
|
+
return c.json({
|
|
113
|
+
success: false,
|
|
114
|
+
message: error instanceof Error ? error.message : 'Failed to delete contact',
|
|
115
|
+
}, 500);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* POST /api/email/admin/contacts/:id/subscribe
|
|
120
|
+
* Subscribe a contact
|
|
121
|
+
*/
|
|
122
|
+
export async function subscribeContact(c) {
|
|
123
|
+
try {
|
|
124
|
+
const id = c.req.param('id');
|
|
125
|
+
if (!id) {
|
|
126
|
+
return c.json({ success: false, message: 'Contact ID is required' }, 400);
|
|
127
|
+
}
|
|
128
|
+
const plunk = createPlunkClient(c.env);
|
|
129
|
+
await plunk.subscribeContact(id);
|
|
130
|
+
return c.json({ success: true });
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
console.error('Subscribe contact error:', error);
|
|
134
|
+
return c.json({
|
|
135
|
+
success: false,
|
|
136
|
+
message: error instanceof Error ? error.message : 'Failed to subscribe contact',
|
|
137
|
+
}, 500);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* POST /api/email/admin/contacts/:id/unsubscribe
|
|
142
|
+
* Unsubscribe a contact
|
|
143
|
+
*/
|
|
144
|
+
export async function unsubscribeContact(c) {
|
|
145
|
+
try {
|
|
146
|
+
const id = c.req.param('id');
|
|
147
|
+
if (!id) {
|
|
148
|
+
return c.json({ success: false, message: 'Contact ID is required' }, 400);
|
|
149
|
+
}
|
|
150
|
+
const plunk = createPlunkClient(c.env);
|
|
151
|
+
await plunk.unsubscribeContact(id);
|
|
152
|
+
return c.json({ success: true });
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
console.error('Unsubscribe contact error:', error);
|
|
156
|
+
return c.json({
|
|
157
|
+
success: false,
|
|
158
|
+
message: error instanceof Error ? error.message : 'Failed to unsubscribe contact',
|
|
159
|
+
}, 500);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Invoice Handler
|
|
3
|
+
*
|
|
4
|
+
* Manages invoice template configuration (stored in GitHub) and PDF generation.
|
|
5
|
+
* Uses global branding for logo when showLogo is enabled.
|
|
6
|
+
*/
|
|
7
|
+
import type { Context } from 'hono';
|
|
8
|
+
import type { InvoiceTemplate, InvoiceData } from '@hintergrund/shared';
|
|
9
|
+
/**
|
|
10
|
+
* GET /api/email/admin/invoice-template
|
|
11
|
+
* Fetch the invoice template configuration from GitHub
|
|
12
|
+
*/
|
|
13
|
+
export declare function getInvoiceTemplate(c: Context): Promise<Response>;
|
|
14
|
+
/**
|
|
15
|
+
* PUT /api/email/admin/invoice-template
|
|
16
|
+
* Save the invoice template configuration to GitHub
|
|
17
|
+
*/
|
|
18
|
+
export declare function saveInvoiceTemplate(c: Context): Promise<Response>;
|
|
19
|
+
/**
|
|
20
|
+
* Generate PDF invoice
|
|
21
|
+
* @param template Invoice template configuration
|
|
22
|
+
* @param data Invoice data
|
|
23
|
+
* @param branding Optional global branding for logo and company name
|
|
24
|
+
*/
|
|
25
|
+
export declare function generateInvoicePdf(template: InvoiceTemplate, data: InvoiceData, branding?: {
|
|
26
|
+
logo?: string;
|
|
27
|
+
companyName?: string;
|
|
28
|
+
primaryColor?: string;
|
|
29
|
+
}): Promise<Uint8Array>;
|
|
30
|
+
//# sourceMappingURL=invoice.handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invoice.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/emails/handlers/invoice.handler.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAiB,eAAe,EAAE,WAAW,EAAkB,MAAM,qBAAqB,CAAC;AA0CvG;;;GAGG;AACH,wBAAsB,kBAAkB,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAmBtE;AAED;;;GAGG;AACH,wBAAsB,mBAAmB,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CA2DvE;AAwBD;;;;;GAKG;AACH,wBAAsB,kBAAkB,CACvC,QAAQ,EAAE,eAAe,EACzB,IAAI,EAAE,WAAW,EACjB,QAAQ,CAAC,EAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,CAAA;CAAE,GACvE,OAAO,CAAC,UAAU,CAAC,CA0WrB"}
|
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Invoice Handler
|
|
3
|
+
*
|
|
4
|
+
* Manages invoice template configuration (stored in GitHub) and PDF generation.
|
|
5
|
+
* Uses global branding for logo when showLogo is enabled.
|
|
6
|
+
*/
|
|
7
|
+
import { PDFDocument, rgb, StandardFonts } from 'pdf-lib';
|
|
8
|
+
import { getGitHubConfig } from '../../../lib/config';
|
|
9
|
+
import { createGitHubClient } from '../../../lib/github';
|
|
10
|
+
// Default invoice configuration
|
|
11
|
+
const DEFAULT_INVOICE_CONFIG = {
|
|
12
|
+
showLogo: true,
|
|
13
|
+
primaryColor: '#1f2937',
|
|
14
|
+
accentColor: '#3b82f6',
|
|
15
|
+
logoPosition: 'left',
|
|
16
|
+
fontFamily: 'helvetica',
|
|
17
|
+
companyName: 'Your Company',
|
|
18
|
+
companyAddress: '123 Main Street, City, Country',
|
|
19
|
+
companyEmail: 'contact@company.com',
|
|
20
|
+
companyPhone: '+1 234 567 890',
|
|
21
|
+
currencySymbol: '$',
|
|
22
|
+
currencyPosition: 'before',
|
|
23
|
+
showTaxBreakdown: true,
|
|
24
|
+
taxLabel: 'Tax',
|
|
25
|
+
taxRate: 0,
|
|
26
|
+
dueDays: 14,
|
|
27
|
+
footerText: 'Thank you for your business!'
|
|
28
|
+
};
|
|
29
|
+
// Default invoice template
|
|
30
|
+
function getDefaultInvoiceTemplate() {
|
|
31
|
+
return {
|
|
32
|
+
id: 'invoice',
|
|
33
|
+
config: DEFAULT_INVOICE_CONFIG,
|
|
34
|
+
createdAt: Date.now(),
|
|
35
|
+
updatedAt: Date.now()
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Get the settings file path
|
|
40
|
+
*/
|
|
41
|
+
function getSettingsPath() {
|
|
42
|
+
return `emails/settings.json`;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* GET /api/email/admin/invoice-template
|
|
46
|
+
* Fetch the invoice template configuration from GitHub
|
|
47
|
+
*/
|
|
48
|
+
export async function getInvoiceTemplate(c) {
|
|
49
|
+
try {
|
|
50
|
+
const gitConfig = getGitHubConfig(c.env);
|
|
51
|
+
const github = createGitHubClient(gitConfig);
|
|
52
|
+
const settingsPath = getSettingsPath();
|
|
53
|
+
// Try to read settings from GitHub
|
|
54
|
+
const result = await github.readJsonFile(settingsPath);
|
|
55
|
+
if (!result || !result.data.invoice) {
|
|
56
|
+
// Return default template if file doesn't exist
|
|
57
|
+
return c.json({ success: true, template: getDefaultInvoiceTemplate() });
|
|
58
|
+
}
|
|
59
|
+
return c.json({ success: true, template: result.data.invoice });
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
console.error('Failed to fetch invoice template:', error);
|
|
63
|
+
return c.json({ success: true, template: getDefaultInvoiceTemplate() });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* PUT /api/email/admin/invoice-template
|
|
68
|
+
* Save the invoice template configuration to GitHub
|
|
69
|
+
*/
|
|
70
|
+
export async function saveInvoiceTemplate(c) {
|
|
71
|
+
try {
|
|
72
|
+
const body = await c.req.json();
|
|
73
|
+
const { config: invoiceConfig, commitSha } = body;
|
|
74
|
+
if (!invoiceConfig) {
|
|
75
|
+
return c.json({ success: false, error: 'Config is required' }, 400);
|
|
76
|
+
}
|
|
77
|
+
const gitConfig = getGitHubConfig(c.env);
|
|
78
|
+
const github = createGitHubClient(gitConfig);
|
|
79
|
+
const settingsPath = getSettingsPath();
|
|
80
|
+
const now = Date.now();
|
|
81
|
+
// Read existing settings
|
|
82
|
+
const existing = await github.readJsonFile(settingsPath);
|
|
83
|
+
// Check for conflicts if commitSha provided
|
|
84
|
+
if (commitSha) {
|
|
85
|
+
const currentSha = await github.getCommitSha();
|
|
86
|
+
if (commitSha !== currentSha) {
|
|
87
|
+
const currentSettings = await github.readJsonFile(settingsPath);
|
|
88
|
+
return c.json({
|
|
89
|
+
conflict: true,
|
|
90
|
+
currentCommitSha: currentSha,
|
|
91
|
+
serverTemplate: currentSettings?.data.invoice || getDefaultInvoiceTemplate(),
|
|
92
|
+
message: 'Settings have been modified. Please resolve conflicts.',
|
|
93
|
+
}, 409);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// Build updated invoice template
|
|
97
|
+
const template = {
|
|
98
|
+
id: 'invoice',
|
|
99
|
+
config: invoiceConfig,
|
|
100
|
+
createdAt: existing?.data.invoice?.createdAt || now,
|
|
101
|
+
updatedAt: now
|
|
102
|
+
};
|
|
103
|
+
// Merge with existing settings (preserve branding config)
|
|
104
|
+
const settings = {
|
|
105
|
+
branding: existing?.data.branding,
|
|
106
|
+
invoice: template,
|
|
107
|
+
};
|
|
108
|
+
// Write to GitHub
|
|
109
|
+
await github.writeJsonFile(settingsPath, settings, 'Update invoice template [skip ci]');
|
|
110
|
+
// Get new commit SHA
|
|
111
|
+
const newCommitSha = await github.getCommitSha();
|
|
112
|
+
return c.json({ success: true, template, commitSha: newCommitSha });
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
console.error('Failed to save invoice template:', error);
|
|
116
|
+
return c.json({ success: false, error: 'Failed to save invoice template' }, 500);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Helper to parse hex color to RGB values
|
|
121
|
+
*/
|
|
122
|
+
function hexToRgb(hex) {
|
|
123
|
+
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
|
124
|
+
return result
|
|
125
|
+
? {
|
|
126
|
+
r: parseInt(result[1], 16) / 255,
|
|
127
|
+
g: parseInt(result[2], 16) / 255,
|
|
128
|
+
b: parseInt(result[3], 16) / 255
|
|
129
|
+
}
|
|
130
|
+
: { r: 0, g: 0, b: 0 };
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Format currency amount
|
|
134
|
+
*/
|
|
135
|
+
function formatCurrency(amount, symbol, position) {
|
|
136
|
+
const formattedAmount = amount.toFixed(2);
|
|
137
|
+
return position === 'before' ? `${symbol}${formattedAmount}` : `${formattedAmount}${symbol}`;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Generate PDF invoice
|
|
141
|
+
* @param template Invoice template configuration
|
|
142
|
+
* @param data Invoice data
|
|
143
|
+
* @param branding Optional global branding for logo and company name
|
|
144
|
+
*/
|
|
145
|
+
export async function generateInvoicePdf(template, data, branding) {
|
|
146
|
+
const pdfDoc = await PDFDocument.create();
|
|
147
|
+
const page = pdfDoc.addPage([595.28, 841.89]); // A4 size in points
|
|
148
|
+
const { width, height } = page.getSize();
|
|
149
|
+
// Get fonts
|
|
150
|
+
const config = template.config;
|
|
151
|
+
let font;
|
|
152
|
+
let boldFont;
|
|
153
|
+
switch (config.fontFamily) {
|
|
154
|
+
case 'times':
|
|
155
|
+
font = await pdfDoc.embedFont(StandardFonts.TimesRoman);
|
|
156
|
+
boldFont = await pdfDoc.embedFont(StandardFonts.TimesRomanBold);
|
|
157
|
+
break;
|
|
158
|
+
case 'courier':
|
|
159
|
+
font = await pdfDoc.embedFont(StandardFonts.Courier);
|
|
160
|
+
boldFont = await pdfDoc.embedFont(StandardFonts.CourierBold);
|
|
161
|
+
break;
|
|
162
|
+
default:
|
|
163
|
+
font = await pdfDoc.embedFont(StandardFonts.Helvetica);
|
|
164
|
+
boldFont = await pdfDoc.embedFont(StandardFonts.HelveticaBold);
|
|
165
|
+
}
|
|
166
|
+
// Use primary color from branding if available, otherwise default
|
|
167
|
+
const primaryColor = hexToRgb(branding?.primaryColor || '#1f2937');
|
|
168
|
+
const accentColor = hexToRgb(branding?.primaryColor || '#3b82f6');
|
|
169
|
+
const margin = 50;
|
|
170
|
+
let yPos = height - margin;
|
|
171
|
+
// Logo (from global branding if available and enabled)
|
|
172
|
+
const logo = config.showLogo ? branding?.logo : undefined;
|
|
173
|
+
if (logo) {
|
|
174
|
+
try {
|
|
175
|
+
// Try to embed logo
|
|
176
|
+
const logoData = logo.split(',')[1]; // Remove data:image/... prefix
|
|
177
|
+
const logoBytes = Uint8Array.from(atob(logoData), (c) => c.charCodeAt(0));
|
|
178
|
+
let logoImage;
|
|
179
|
+
if (logo.includes('image/png')) {
|
|
180
|
+
logoImage = await pdfDoc.embedPng(logoBytes);
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
logoImage = await pdfDoc.embedJpg(logoBytes);
|
|
184
|
+
}
|
|
185
|
+
const logoMaxHeight = 50;
|
|
186
|
+
const logoMaxWidth = 150;
|
|
187
|
+
const scale = Math.min(logoMaxWidth / logoImage.width, logoMaxHeight / logoImage.height);
|
|
188
|
+
const logoWidth = logoImage.width * scale;
|
|
189
|
+
const logoHeight = logoImage.height * scale;
|
|
190
|
+
// Always left-align logo for invoices
|
|
191
|
+
const logoX = margin;
|
|
192
|
+
page.drawImage(logoImage, {
|
|
193
|
+
x: logoX,
|
|
194
|
+
y: yPos - logoHeight,
|
|
195
|
+
width: logoWidth,
|
|
196
|
+
height: logoHeight
|
|
197
|
+
});
|
|
198
|
+
yPos -= logoHeight + 20;
|
|
199
|
+
}
|
|
200
|
+
catch (e) {
|
|
201
|
+
console.error('Failed to embed logo:', e);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// Company info (right-aligned)
|
|
205
|
+
const companyName = branding?.companyName || 'Your Company';
|
|
206
|
+
const companyInfoX = width - margin;
|
|
207
|
+
page.drawText(companyName, {
|
|
208
|
+
x: companyInfoX - boldFont.widthOfTextAtSize(companyName, 12),
|
|
209
|
+
y: yPos,
|
|
210
|
+
size: 12,
|
|
211
|
+
font: boldFont,
|
|
212
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
213
|
+
});
|
|
214
|
+
yPos -= 15;
|
|
215
|
+
const addressLines = config.companyAddress.split(',').map((s) => s.trim());
|
|
216
|
+
for (const line of addressLines) {
|
|
217
|
+
page.drawText(line, {
|
|
218
|
+
x: companyInfoX - font.widthOfTextAtSize(line, 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.companyEmail) {
|
|
227
|
+
page.drawText(config.companyEmail, {
|
|
228
|
+
x: companyInfoX - font.widthOfTextAtSize(config.companyEmail, 10),
|
|
229
|
+
y: yPos,
|
|
230
|
+
size: 10,
|
|
231
|
+
font,
|
|
232
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
233
|
+
});
|
|
234
|
+
yPos -= 12;
|
|
235
|
+
}
|
|
236
|
+
if (config.companyPhone) {
|
|
237
|
+
page.drawText(config.companyPhone, {
|
|
238
|
+
x: companyInfoX - font.widthOfTextAtSize(config.companyPhone, 10),
|
|
239
|
+
y: yPos,
|
|
240
|
+
size: 10,
|
|
241
|
+
font,
|
|
242
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
243
|
+
});
|
|
244
|
+
yPos -= 12;
|
|
245
|
+
}
|
|
246
|
+
yPos -= 30;
|
|
247
|
+
// Invoice title
|
|
248
|
+
page.drawText('INVOICE', {
|
|
249
|
+
x: margin,
|
|
250
|
+
y: yPos,
|
|
251
|
+
size: 24,
|
|
252
|
+
font: boldFont,
|
|
253
|
+
color: rgb(accentColor.r, accentColor.g, accentColor.b)
|
|
254
|
+
});
|
|
255
|
+
yPos -= 30;
|
|
256
|
+
// Invoice details
|
|
257
|
+
page.drawText(`Invoice #: ${data.invoiceNumber}`, {
|
|
258
|
+
x: margin,
|
|
259
|
+
y: yPos,
|
|
260
|
+
size: 10,
|
|
261
|
+
font,
|
|
262
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
263
|
+
});
|
|
264
|
+
yPos -= 15;
|
|
265
|
+
page.drawText(`Date: ${data.invoiceDate}`, {
|
|
266
|
+
x: margin,
|
|
267
|
+
y: yPos,
|
|
268
|
+
size: 10,
|
|
269
|
+
font,
|
|
270
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
271
|
+
});
|
|
272
|
+
yPos -= 30;
|
|
273
|
+
// Bill To
|
|
274
|
+
page.drawText('Bill To:', {
|
|
275
|
+
x: margin,
|
|
276
|
+
y: yPos,
|
|
277
|
+
size: 10,
|
|
278
|
+
font: boldFont,
|
|
279
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
280
|
+
});
|
|
281
|
+
yPos -= 15;
|
|
282
|
+
page.drawText(data.customerName, {
|
|
283
|
+
x: margin,
|
|
284
|
+
y: yPos,
|
|
285
|
+
size: 10,
|
|
286
|
+
font,
|
|
287
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
288
|
+
});
|
|
289
|
+
yPos -= 12;
|
|
290
|
+
page.drawText(data.customerEmail, {
|
|
291
|
+
x: margin,
|
|
292
|
+
y: yPos,
|
|
293
|
+
size: 10,
|
|
294
|
+
font,
|
|
295
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
296
|
+
});
|
|
297
|
+
yPos -= 12;
|
|
298
|
+
if (data.customerAddress) {
|
|
299
|
+
page.drawText(data.customerAddress, {
|
|
300
|
+
x: margin,
|
|
301
|
+
y: yPos,
|
|
302
|
+
size: 10,
|
|
303
|
+
font,
|
|
304
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
305
|
+
});
|
|
306
|
+
yPos -= 12;
|
|
307
|
+
}
|
|
308
|
+
yPos -= 30;
|
|
309
|
+
// Table header
|
|
310
|
+
const tableHeaderHeight = 25;
|
|
311
|
+
const colDescription = margin;
|
|
312
|
+
const colQty = width - margin - 200;
|
|
313
|
+
const colPrice = width - margin - 120;
|
|
314
|
+
const colTotal = width - margin - 40;
|
|
315
|
+
// Header background
|
|
316
|
+
page.drawRectangle({
|
|
317
|
+
x: margin,
|
|
318
|
+
y: yPos - tableHeaderHeight,
|
|
319
|
+
width: width - 2 * margin,
|
|
320
|
+
height: tableHeaderHeight,
|
|
321
|
+
color: rgb(accentColor.r, accentColor.g, accentColor.b)
|
|
322
|
+
});
|
|
323
|
+
// Header text
|
|
324
|
+
page.drawText('Description', {
|
|
325
|
+
x: colDescription + 10,
|
|
326
|
+
y: yPos - 17,
|
|
327
|
+
size: 10,
|
|
328
|
+
font: boldFont,
|
|
329
|
+
color: rgb(1, 1, 1)
|
|
330
|
+
});
|
|
331
|
+
page.drawText('Qty', {
|
|
332
|
+
x: colQty,
|
|
333
|
+
y: yPos - 17,
|
|
334
|
+
size: 10,
|
|
335
|
+
font: boldFont,
|
|
336
|
+
color: rgb(1, 1, 1)
|
|
337
|
+
});
|
|
338
|
+
page.drawText('Price', {
|
|
339
|
+
x: colPrice,
|
|
340
|
+
y: yPos - 17,
|
|
341
|
+
size: 10,
|
|
342
|
+
font: boldFont,
|
|
343
|
+
color: rgb(1, 1, 1)
|
|
344
|
+
});
|
|
345
|
+
page.drawText('Total', {
|
|
346
|
+
x: colTotal - font.widthOfTextAtSize('Total', 10),
|
|
347
|
+
y: yPos - 17,
|
|
348
|
+
size: 10,
|
|
349
|
+
font: boldFont,
|
|
350
|
+
color: rgb(1, 1, 1)
|
|
351
|
+
});
|
|
352
|
+
yPos -= tableHeaderHeight + 15;
|
|
353
|
+
// Table rows
|
|
354
|
+
for (const item of data.items) {
|
|
355
|
+
page.drawText(item.description, {
|
|
356
|
+
x: colDescription + 10,
|
|
357
|
+
y: yPos,
|
|
358
|
+
size: 10,
|
|
359
|
+
font,
|
|
360
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
361
|
+
});
|
|
362
|
+
page.drawText(item.quantity.toString(), {
|
|
363
|
+
x: colQty,
|
|
364
|
+
y: yPos,
|
|
365
|
+
size: 10,
|
|
366
|
+
font,
|
|
367
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
368
|
+
});
|
|
369
|
+
const priceStr = formatCurrency(item.unitPrice, config.currencySymbol, config.currencyPosition);
|
|
370
|
+
page.drawText(priceStr, {
|
|
371
|
+
x: colPrice,
|
|
372
|
+
y: yPos,
|
|
373
|
+
size: 10,
|
|
374
|
+
font,
|
|
375
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
376
|
+
});
|
|
377
|
+
const totalStr = formatCurrency(item.total, config.currencySymbol, config.currencyPosition);
|
|
378
|
+
page.drawText(totalStr, {
|
|
379
|
+
x: colTotal - font.widthOfTextAtSize(totalStr, 10),
|
|
380
|
+
y: yPos,
|
|
381
|
+
size: 10,
|
|
382
|
+
font,
|
|
383
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
384
|
+
});
|
|
385
|
+
yPos -= 20;
|
|
386
|
+
}
|
|
387
|
+
// Line separator
|
|
388
|
+
yPos -= 10;
|
|
389
|
+
page.drawLine({
|
|
390
|
+
start: { x: margin, y: yPos },
|
|
391
|
+
end: { x: width - margin, y: yPos },
|
|
392
|
+
thickness: 1,
|
|
393
|
+
color: rgb(0.8, 0.8, 0.8)
|
|
394
|
+
});
|
|
395
|
+
yPos -= 20;
|
|
396
|
+
// Subtotal
|
|
397
|
+
if (config.showTaxBreakdown) {
|
|
398
|
+
const subtotalLabel = 'Subtotal:';
|
|
399
|
+
const subtotalValue = formatCurrency(data.subtotal, config.currencySymbol, config.currencyPosition);
|
|
400
|
+
page.drawText(subtotalLabel, {
|
|
401
|
+
x: colPrice - font.widthOfTextAtSize(subtotalLabel, 10),
|
|
402
|
+
y: yPos,
|
|
403
|
+
size: 10,
|
|
404
|
+
font,
|
|
405
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
406
|
+
});
|
|
407
|
+
page.drawText(subtotalValue, {
|
|
408
|
+
x: colTotal - font.widthOfTextAtSize(subtotalValue, 10),
|
|
409
|
+
y: yPos,
|
|
410
|
+
size: 10,
|
|
411
|
+
font,
|
|
412
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
413
|
+
});
|
|
414
|
+
yPos -= 15;
|
|
415
|
+
// Tax
|
|
416
|
+
if (data.tax !== undefined) {
|
|
417
|
+
const taxLabel = `${config.taxLabel} (${config.taxRate}%):`;
|
|
418
|
+
const taxValue = formatCurrency(data.tax, config.currencySymbol, config.currencyPosition);
|
|
419
|
+
page.drawText(taxLabel, {
|
|
420
|
+
x: colPrice - font.widthOfTextAtSize(taxLabel, 10),
|
|
421
|
+
y: yPos,
|
|
422
|
+
size: 10,
|
|
423
|
+
font,
|
|
424
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
425
|
+
});
|
|
426
|
+
page.drawText(taxValue, {
|
|
427
|
+
x: colTotal - font.widthOfTextAtSize(taxValue, 10),
|
|
428
|
+
y: yPos,
|
|
429
|
+
size: 10,
|
|
430
|
+
font,
|
|
431
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
432
|
+
});
|
|
433
|
+
yPos -= 15;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
// Total
|
|
437
|
+
const totalLabel = 'Total:';
|
|
438
|
+
const totalValue = formatCurrency(data.total, config.currencySymbol, config.currencyPosition);
|
|
439
|
+
page.drawText(totalLabel, {
|
|
440
|
+
x: colPrice - boldFont.widthOfTextAtSize(totalLabel, 12),
|
|
441
|
+
y: yPos,
|
|
442
|
+
size: 12,
|
|
443
|
+
font: boldFont,
|
|
444
|
+
color: rgb(primaryColor.r, primaryColor.g, primaryColor.b)
|
|
445
|
+
});
|
|
446
|
+
page.drawText(totalValue, {
|
|
447
|
+
x: colTotal - boldFont.widthOfTextAtSize(totalValue, 12),
|
|
448
|
+
y: yPos,
|
|
449
|
+
size: 12,
|
|
450
|
+
font: boldFont,
|
|
451
|
+
color: rgb(accentColor.r, accentColor.g, accentColor.b)
|
|
452
|
+
});
|
|
453
|
+
// Footer - Thank you message
|
|
454
|
+
const footerText = 'Thank you for your business!';
|
|
455
|
+
page.drawText(footerText, {
|
|
456
|
+
x: (width - font.widthOfTextAtSize(footerText, 10)) / 2,
|
|
457
|
+
y: margin,
|
|
458
|
+
size: 10,
|
|
459
|
+
font,
|
|
460
|
+
color: rgb(0.5, 0.5, 0.5)
|
|
461
|
+
});
|
|
462
|
+
return await pdfDoc.save();
|
|
463
|
+
}
|