Update server.js
This commit is contained in:
14
server.js
14
server.js
@@ -19,7 +19,10 @@ app.use(express.json());
|
|||||||
app.use(express.static(path.join(__dirname, 'dist')));
|
app.use(express.static(path.join(__dirname, 'dist')));
|
||||||
|
|
||||||
// DB Configuration
|
// DB Configuration
|
||||||
const DB_TYPE = process.env.DB_TYPE || 'mysql'; // 'mysql' or 'postgres'
|
// Normalize DB_TYPE: allow 'postgres', 'postgresql', 'Postgres', etc.
|
||||||
|
const rawDbType = (process.env.DB_TYPE || 'mysql').toLowerCase().trim();
|
||||||
|
const DB_TYPE = (rawDbType === 'postgres' || rawDbType === 'postgresql') ? 'postgres' : 'mysql';
|
||||||
|
|
||||||
const dbConfig = {
|
const dbConfig = {
|
||||||
host: process.env.DB_HOST,
|
host: process.env.DB_HOST,
|
||||||
user: process.env.DB_USER,
|
user: process.env.DB_USER,
|
||||||
@@ -30,7 +33,7 @@ const dbConfig = {
|
|||||||
|
|
||||||
// Debug Log: Print config to console (masking password)
|
// Debug Log: Print config to console (masking password)
|
||||||
console.log('--- Database Configuration ---');
|
console.log('--- Database Configuration ---');
|
||||||
console.log(`Type: ${DB_TYPE}`);
|
console.log(`Type: ${DB_TYPE} (Input: ${process.env.DB_TYPE || 'default'})`);
|
||||||
console.log(`Host: ${dbConfig.host}`);
|
console.log(`Host: ${dbConfig.host}`);
|
||||||
console.log(`User: ${dbConfig.user}`);
|
console.log(`User: ${dbConfig.user}`);
|
||||||
console.log(`Database: ${dbConfig.database}`);
|
console.log(`Database: ${dbConfig.database}`);
|
||||||
@@ -52,6 +55,7 @@ const initDB = async () => {
|
|||||||
pool = new Pool(dbConfig);
|
pool = new Pool(dbConfig);
|
||||||
|
|
||||||
// Create table for Postgres
|
// Create table for Postgres
|
||||||
|
// Using JSONB for variables
|
||||||
await pool.query(`
|
await pool.query(`
|
||||||
CREATE TABLE IF NOT EXISTS email_templates (
|
CREATE TABLE IF NOT EXISTS email_templates (
|
||||||
id VARCHAR(255) PRIMARY KEY,
|
id VARCHAR(255) PRIMARY KEY,
|
||||||
@@ -159,9 +163,10 @@ app.post('/api/templates', async (req, res) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (DB_TYPE === 'postgres') {
|
if (DB_TYPE === 'postgres') {
|
||||||
|
// Postgres requires explicit casting for JSONB parameters when passed as strings ($10::jsonb)
|
||||||
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)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, NOW())
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10::jsonb, NOW())
|
||||||
ON CONFLICT (id) DO UPDATE SET
|
ON CONFLICT (id) DO UPDATE SET
|
||||||
template_key = EXCLUDED.template_key,
|
template_key = EXCLUDED.template_key,
|
||||||
name = EXCLUDED.name,
|
name = EXCLUDED.name,
|
||||||
@@ -195,7 +200,8 @@ app.post('/api/templates', async (req, res) => {
|
|||||||
}
|
}
|
||||||
res.json({ success: true });
|
res.json({ success: true });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error("Save Template Error:", err);
|
||||||
|
// Return specific error details to help debugging on the client
|
||||||
res.status(500).json({ error: 'Failed to save template', details: err.message });
|
res.status(500).json({ error: 'Failed to save template', details: err.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user