@contentgrowth/content-emailing 0.7.5 → 0.7.6
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/backend/index.cjs +85 -42
- package/dist/backend/index.cjs.map +1 -1
- package/dist/backend/index.js +85 -42
- package/dist/backend/index.js.map +1 -1
- package/dist/backend/routes/index.cjs +87 -42
- package/dist/backend/routes/index.cjs.map +1 -1
- package/dist/backend/routes/index.d.cts +10 -6
- package/dist/backend/routes/index.d.ts +10 -6
- package/dist/backend/routes/index.js +86 -42
- package/dist/backend/routes/index.js.map +1 -1
- package/dist/index.cjs +85 -42
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +85 -42
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/backend/routes/index.js
|
|
2
|
-
import { Hono as
|
|
2
|
+
import { Hono as Hono4 } from "hono";
|
|
3
3
|
|
|
4
4
|
// src/backend/routes/templates.js
|
|
5
5
|
import { Hono } from "hono";
|
|
@@ -1061,71 +1061,81 @@ var EmailService = class {
|
|
|
1061
1061
|
};
|
|
1062
1062
|
|
|
1063
1063
|
// src/backend/routes/templates.js
|
|
1064
|
-
function createTemplateRoutes(
|
|
1064
|
+
function createTemplateRoutes(config = {}) {
|
|
1065
1065
|
const app = new Hono();
|
|
1066
|
-
|
|
1067
|
-
|
|
1066
|
+
app.get("/", async (c) => {
|
|
1067
|
+
const emailService = new EmailService(c.env, config);
|
|
1068
1068
|
try {
|
|
1069
1069
|
const templates = await emailService.getAllTemplates();
|
|
1070
|
-
return c.json(
|
|
1070
|
+
return c.json(templates);
|
|
1071
1071
|
} catch (err) {
|
|
1072
|
-
return c.json({
|
|
1072
|
+
return c.json({ error: err.message }, 500);
|
|
1073
1073
|
}
|
|
1074
1074
|
});
|
|
1075
|
-
app.get("
|
|
1075
|
+
app.get("/:id", async (c) => {
|
|
1076
|
+
const emailService = new EmailService(c.env, config);
|
|
1076
1077
|
try {
|
|
1077
1078
|
const template = await emailService.getTemplate(c.req.param("id"));
|
|
1078
|
-
if (!template) return c.json({
|
|
1079
|
-
return c.json(
|
|
1079
|
+
if (!template) return c.json({ error: "Template not found" }, 404);
|
|
1080
|
+
return c.json(template);
|
|
1080
1081
|
} catch (err) {
|
|
1081
|
-
return c.json({
|
|
1082
|
+
return c.json({ error: err.message }, 500);
|
|
1082
1083
|
}
|
|
1083
1084
|
});
|
|
1084
|
-
app.post("/
|
|
1085
|
+
app.post("/", async (c) => {
|
|
1086
|
+
const emailService = new EmailService(c.env, config);
|
|
1085
1087
|
try {
|
|
1086
1088
|
const data = await c.req.json();
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
+
let userId = "admin";
|
|
1090
|
+
const user = c.get("dbUser") || c.get("user");
|
|
1091
|
+
if (user && user.id) userId = user.id;
|
|
1092
|
+
if (!data.template_id || !data.subject_template) {
|
|
1093
|
+
return c.json({ error: "Missing required fields (template_id, subject_template)" }, 400);
|
|
1094
|
+
}
|
|
1095
|
+
const result = await emailService.saveTemplate(data, userId);
|
|
1096
|
+
return c.json({ success: true, message: "Template saved", result });
|
|
1089
1097
|
} catch (err) {
|
|
1090
|
-
return c.json({
|
|
1098
|
+
return c.json({ error: err.message }, 500);
|
|
1091
1099
|
}
|
|
1092
1100
|
});
|
|
1093
|
-
app.
|
|
1101
|
+
app.post("/send-test", async (c) => {
|
|
1102
|
+
const emailService = new EmailService(c.env, config);
|
|
1103
|
+
try {
|
|
1104
|
+
const { template_id, to, variables } = await c.req.json();
|
|
1105
|
+
let tid = template_id;
|
|
1106
|
+
if (!tid || !to) {
|
|
1107
|
+
return c.json({ error: "Missing required fields (template_id, to)" }, 400);
|
|
1108
|
+
}
|
|
1109
|
+
const user = c.get("dbUser") || c.get("user");
|
|
1110
|
+
const recipientUserId = user?.id || null;
|
|
1111
|
+
const result = await emailService.sendViaTemplate(tid, variables || {}, {
|
|
1112
|
+
to,
|
|
1113
|
+
profile: "test",
|
|
1114
|
+
recipientUserId
|
|
1115
|
+
});
|
|
1116
|
+
return c.json({ success: true, result });
|
|
1117
|
+
} catch (err) {
|
|
1118
|
+
return c.json({ error: `Failed to send test email: ${err.message}` }, 500);
|
|
1119
|
+
}
|
|
1120
|
+
});
|
|
1121
|
+
app.delete("/:id", async (c) => {
|
|
1122
|
+
const emailService = new EmailService(c.env, config);
|
|
1094
1123
|
try {
|
|
1095
1124
|
await emailService.deleteTemplate(c.req.param("id"));
|
|
1096
1125
|
return c.json({ success: true, message: "Template deleted" });
|
|
1097
1126
|
} catch (err) {
|
|
1098
|
-
return c.json({
|
|
1127
|
+
return c.json({ error: err.message }, 500);
|
|
1099
1128
|
}
|
|
1100
1129
|
});
|
|
1101
|
-
app.post("
|
|
1130
|
+
app.post("/:id/preview", async (c) => {
|
|
1131
|
+
const emailService = new EmailService(c.env, config);
|
|
1102
1132
|
try {
|
|
1103
1133
|
const id = c.req.param("id");
|
|
1104
1134
|
const data = await c.req.json();
|
|
1105
1135
|
const result = await emailService.renderTemplate(id, data);
|
|
1106
1136
|
return c.json({ success: true, preview: result });
|
|
1107
1137
|
} catch (err) {
|
|
1108
|
-
return c.json({
|
|
1109
|
-
}
|
|
1110
|
-
});
|
|
1111
|
-
app.post("/templates/:id/test", async (c) => {
|
|
1112
|
-
try {
|
|
1113
|
-
const id = c.req.param("id");
|
|
1114
|
-
const { to, data } = await c.req.json();
|
|
1115
|
-
const { subject, html, plainText } = await emailService.renderTemplate(id, data);
|
|
1116
|
-
const result = await emailService.sendEmail({
|
|
1117
|
-
to,
|
|
1118
|
-
subject: `[TEST] ${subject}`,
|
|
1119
|
-
html,
|
|
1120
|
-
text: plainText
|
|
1121
|
-
});
|
|
1122
|
-
if (result.success) {
|
|
1123
|
-
return c.json({ success: true, message: "Test email sent" });
|
|
1124
|
-
} else {
|
|
1125
|
-
return c.json({ success: false, error: result.error }, 500);
|
|
1126
|
-
}
|
|
1127
|
-
} catch (err) {
|
|
1128
|
-
return c.json({ success: false, error: err.message }, 500);
|
|
1138
|
+
return c.json({ error: err.message }, 500);
|
|
1129
1139
|
}
|
|
1130
1140
|
});
|
|
1131
1141
|
return app;
|
|
@@ -1306,15 +1316,49 @@ function createTrackingRoutes(env, config = {}) {
|
|
|
1306
1316
|
return app;
|
|
1307
1317
|
}
|
|
1308
1318
|
|
|
1309
|
-
// src/backend/routes/
|
|
1310
|
-
|
|
1319
|
+
// src/backend/routes/logs.js
|
|
1320
|
+
import { Hono as Hono3 } from "hono";
|
|
1321
|
+
function createLogRoutes(config = {}) {
|
|
1311
1322
|
const app = new Hono3();
|
|
1312
|
-
app.
|
|
1313
|
-
|
|
1323
|
+
app.get("/", async (c) => {
|
|
1324
|
+
const limit = Math.min(parseInt(c.req.query("limit") || "50"), 100);
|
|
1325
|
+
const offset = parseInt(c.req.query("offset") || "0");
|
|
1326
|
+
const env = c.env;
|
|
1327
|
+
try {
|
|
1328
|
+
const table = `${config.emailTablePrefix || "system_email_"}logs`;
|
|
1329
|
+
const { results } = await env.DB.prepare(`
|
|
1330
|
+
SELECT * FROM ${table}
|
|
1331
|
+
ORDER BY created_at DESC
|
|
1332
|
+
LIMIT ? OFFSET ?
|
|
1333
|
+
`).bind(limit, offset).all();
|
|
1334
|
+
const countResult = await env.DB.prepare(`SELECT COUNT(*) as exact_count FROM ${table}`).first();
|
|
1335
|
+
return c.json({
|
|
1336
|
+
logs: results.map((row) => ({
|
|
1337
|
+
...row,
|
|
1338
|
+
metadata: row.metadata ? JSON.parse(row.metadata) : null
|
|
1339
|
+
})),
|
|
1340
|
+
total: countResult.exact_count,
|
|
1341
|
+
limit,
|
|
1342
|
+
offset
|
|
1343
|
+
});
|
|
1344
|
+
} catch (error) {
|
|
1345
|
+
console.error("Failed to fetch logs:", error);
|
|
1346
|
+
return c.json({ error: "Failed to fetch logs" }, 500);
|
|
1347
|
+
}
|
|
1348
|
+
});
|
|
1349
|
+
return app;
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1352
|
+
// src/backend/routes/index.js
|
|
1353
|
+
function createEmailRoutes(config = {}, cacheProvider = null) {
|
|
1354
|
+
const app = new Hono4();
|
|
1355
|
+
app.route("/templates", createTemplateRoutes(config, cacheProvider));
|
|
1356
|
+
app.route("/logs", createLogRoutes(config));
|
|
1314
1357
|
return app;
|
|
1315
1358
|
}
|
|
1316
1359
|
export {
|
|
1317
1360
|
createEmailRoutes,
|
|
1361
|
+
createLogRoutes,
|
|
1318
1362
|
createTemplateRoutes,
|
|
1319
1363
|
createTrackingRoutes
|
|
1320
1364
|
};
|