@contentgrowth/content-emailing 0.7.6 → 0.7.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.
@@ -1,8 +1,64 @@
1
1
  // src/backend/routes/index.js
2
- import { Hono as Hono4 } from "hono";
2
+ import { Hono as Hono5 } from "hono";
3
3
 
4
- // src/backend/routes/templates.js
4
+ // src/backend/routes/settings.js
5
5
  import { Hono } from "hono";
6
+ function createSettingsRoutes(config = {}) {
7
+ const app = new Hono();
8
+ const tableName = config.tableName || "system_settings";
9
+ const keyPrefix = config.keyPrefix || "system_email.";
10
+ const getSettings = async (db) => {
11
+ try {
12
+ const { results } = await db.prepare(`SELECT * FROM ${tableName} WHERE key LIKE ?`).bind(`${keyPrefix}%`).all();
13
+ const settings = {};
14
+ for (const row of results) {
15
+ const cleanKey = row.key.slice(keyPrefix.length);
16
+ settings[cleanKey] = row.value;
17
+ }
18
+ return settings;
19
+ } catch (e) {
20
+ console.warn(`[Settings] Failed to fetch from ${tableName}:`, e.message);
21
+ return {};
22
+ }
23
+ };
24
+ app.get("/", async (c) => {
25
+ try {
26
+ const settings = await getSettings(c.env.DB);
27
+ return c.json(settings);
28
+ } catch (error) {
29
+ console.error("Failed to fetch settings:", error);
30
+ return c.json({ error: error.message }, 500);
31
+ }
32
+ });
33
+ app.post("/", async (c) => {
34
+ try {
35
+ const body = await c.req.json();
36
+ const db = c.env.DB;
37
+ const updates = Object.entries(body).map(([k, v]) => ({
38
+ key: `${keyPrefix}${k}`,
39
+ value: v
40
+ }));
41
+ if (updates.length === 0) return c.json({ success: true });
42
+ const stmt = db.prepare(`
43
+ INSERT INTO ${tableName} (key, value, updated_at)
44
+ VALUES (?, ?, strftime('%s', 'now'))
45
+ ON CONFLICT(key) DO UPDATE SET
46
+ value = excluded.value,
47
+ updated_at = excluded.updated_at
48
+ `);
49
+ const batch = updates.map((u) => stmt.bind(u.key, u.value === void 0 || u.value === null ? "" : String(u.value)));
50
+ await db.batch(batch);
51
+ return c.json({ success: true });
52
+ } catch (error) {
53
+ console.error("Failed to save settings:", error);
54
+ return c.json({ error: error.message }, 500);
55
+ }
56
+ });
57
+ return app;
58
+ }
59
+
60
+ // src/backend/routes/templates.js
61
+ import { Hono as Hono2 } from "hono";
6
62
 
7
63
  // src/backend/EmailService.js
8
64
  import { marked } from "marked";
@@ -483,6 +539,9 @@ var EmailService = class {
483
539
  // Updater function to save settings to backend
484
540
  // Signature: async (profile, tenantId, settings) => void
485
541
  settingsUpdater: config.settingsUpdater || null,
542
+ // Settings configuration
543
+ settingsTableName: config.settingsTableName || "system_settings",
544
+ settingsKeyPrefix: config.settingsKeyPrefix || "system_email.",
486
545
  // Branding configuration for email templates
487
546
  branding: {
488
547
  brandName: config.branding?.brandName || "Your App",
@@ -531,6 +590,22 @@ var EmailService = class {
531
590
  console.warn("[EmailService] settingsLoader failed:", e);
532
591
  }
533
592
  }
593
+ if (!settings && profile === "system" && this.db) {
594
+ try {
595
+ const tableName = this.config.settingsTableName;
596
+ const prefix = this.config.settingsKeyPrefix;
597
+ const { results } = await this.db.prepare(`SELECT * FROM ${tableName} WHERE key LIKE ?`).bind(`${prefix}%`).all();
598
+ if (results && results.length > 0) {
599
+ settings = {};
600
+ for (const row of results) {
601
+ const cleanKey = row.key.slice(prefix.length);
602
+ settings[cleanKey] = row.value;
603
+ }
604
+ }
605
+ } catch (e) {
606
+ console.warn("[EmailService] Failed to load settings from DB:", e.message);
607
+ }
608
+ }
534
609
  if (settings) {
535
610
  if (this.cache && this.cache.putSettings) {
536
611
  try {
@@ -1062,7 +1137,7 @@ var EmailService = class {
1062
1137
 
1063
1138
  // src/backend/routes/templates.js
1064
1139
  function createTemplateRoutes(config = {}) {
1065
- const app = new Hono();
1140
+ const app = new Hono2();
1066
1141
  app.get("/", async (c) => {
1067
1142
  const emailService = new EmailService(c.env, config);
1068
1143
  try {
@@ -1142,7 +1217,7 @@ function createTemplateRoutes(config = {}) {
1142
1217
  }
1143
1218
 
1144
1219
  // src/backend/routes/tracking.js
1145
- import { Hono as Hono2 } from "hono";
1220
+ import { Hono as Hono3 } from "hono";
1146
1221
  var TRACKING_PIXEL = new Uint8Array([
1147
1222
  71,
1148
1223
  73,
@@ -1188,7 +1263,7 @@ var TRACKING_PIXEL = new Uint8Array([
1188
1263
  59
1189
1264
  ]);
1190
1265
  function createTrackingRoutes(env, config = {}) {
1191
- const app = new Hono2();
1266
+ const app = new Hono3();
1192
1267
  const db = env.DB;
1193
1268
  const tablePrefix = config.emailTablePrefix || config.tableNamePrefix || "system_email_";
1194
1269
  app.get("/track/open/:token", async (c) => {
@@ -1317,9 +1392,9 @@ function createTrackingRoutes(env, config = {}) {
1317
1392
  }
1318
1393
 
1319
1394
  // src/backend/routes/logs.js
1320
- import { Hono as Hono3 } from "hono";
1395
+ import { Hono as Hono4 } from "hono";
1321
1396
  function createLogRoutes(config = {}) {
1322
- const app = new Hono3();
1397
+ const app = new Hono4();
1323
1398
  app.get("/", async (c) => {
1324
1399
  const limit = Math.min(parseInt(c.req.query("limit") || "50"), 100);
1325
1400
  const offset = parseInt(c.req.query("offset") || "0");
@@ -1351,14 +1426,20 @@ function createLogRoutes(config = {}) {
1351
1426
 
1352
1427
  // src/backend/routes/index.js
1353
1428
  function createEmailRoutes(config = {}, cacheProvider = null) {
1354
- const app = new Hono4();
1429
+ const app = new Hono5();
1355
1430
  app.route("/templates", createTemplateRoutes(config, cacheProvider));
1356
1431
  app.route("/logs", createLogRoutes(config));
1432
+ app.route("/settings", createSettingsRoutes({
1433
+ ...config,
1434
+ tableName: config.settingsTableName || "system_settings",
1435
+ keyPrefix: config.settingsKeyPrefix || "system_email."
1436
+ }));
1357
1437
  return app;
1358
1438
  }
1359
1439
  export {
1360
1440
  createEmailRoutes,
1361
1441
  createLogRoutes,
1442
+ createSettingsRoutes,
1362
1443
  createTemplateRoutes,
1363
1444
  createTrackingRoutes
1364
1445
  };