Update server.js

This commit is contained in:
fcarraUniSa
2025-12-15 10:45:12 +01:00
committed by GitHub
parent 89dc9d0509
commit 1e9ab4e803

View File

@@ -136,9 +136,26 @@ app.get('/api/templates', async (req, res) => {
// SAVE (Upsert) Template // SAVE (Upsert) Template
app.post('/api/templates', async (req, res) => { app.post('/api/templates', async (req, res) => {
const t = req.body; const t = req.body;
const fullHtml = `${t.header}${t.body}${t.footer}`; // Ensure default values to prevent undefined errors in Postgres
const templateKey = t.name.trim().toLowerCase().replace(/\s+/g, '_').replace(/[^a-z0-9_]/g, ''); const header = t.header || '';
const variablesJson = JSON.stringify(t.variables); const body = t.body || '';
const footer = t.footer || '';
const fullHtml = `${header}${body}${footer}`;
const templateKey = (t.name || '').trim().toLowerCase().replace(/\s+/g, '_').replace(/[^a-z0-9_]/g, '');
const variablesJson = JSON.stringify(t.variables || []);
const params = [
t.id,
templateKey,
t.name,
t.description || '',
t.subject || '',
header,
body,
footer,
fullHtml,
variablesJson
];
try { try {
if (DB_TYPE === 'postgres') { if (DB_TYPE === 'postgres') {
@@ -157,7 +174,7 @@ app.post('/api/templates', async (req, res) => {
required_variables = EXCLUDED.required_variables, required_variables = EXCLUDED.required_variables,
updated_at = NOW(); updated_at = NOW();
`; `;
await pool.query(query, [t.id, templateKey, t.name, t.description, t.subject, t.header, t.body, t.footer, fullHtml, variablesJson]); await pool.query(query, params);
} else { } else {
const query = ` const query = `
INSERT INTO email_templates (id, template_key, name, description, subject, header_html, body_html, footer_html, full_html, required_variables, updated_at) INSERT INTO email_templates (id, template_key, name, description, subject, header_html, body_html, footer_html, full_html, required_variables, updated_at)
@@ -174,7 +191,7 @@ app.post('/api/templates', async (req, res) => {
required_variables = VALUES(required_variables), required_variables = VALUES(required_variables),
updated_at = NOW(); updated_at = NOW();
`; `;
await pool.query(query, [t.id, templateKey, t.name, t.description, t.subject, t.header, t.body, t.footer, fullHtml, variablesJson]); await pool.query(query, params);
} }
res.json({ success: true }); res.json({ success: true });
} catch (err) { } catch (err) {