const express = require('express'); const nodemailer = require('nodemailer'); const { body, validationResult } = require('express-validator'); const { db, logActivity } = require('../database/db'); const { adminAuth } = require('../middleware/auth'); const router = express.Router(); // Get email configuration router.get('/config', adminAuth, async (req, res) => { try { const config = await db('email_configs').first(); if (!config) { return res.json({ smtp_host: '', smtp_port: 587, smtp_secure: false, smtp_user: '', smtp_pass: '', // Don't send actual password from_email: '', from_name: '' }); } // Don't send the actual password res.json({ ...config, smtp_pass: config.smtp_pass ? '********' : '' }); } catch (error) { console.error('Email config fetch error:', error); res.status(500).json({ error: 'Failed to fetch email configuration' }); } }); // Update email configuration router.post('/config', [ adminAuth, body('smtp_host').notEmpty().withMessage('SMTP host is required'), body('smtp_port').isInt({ min: 1, max: 65535 }).withMessage('Invalid port number'), body('from_email').isEmail().withMessage('Invalid from email address') ], async (req, res) => { try { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({ errors: errors.array() }); } const { smtp_host, smtp_port, smtp_secure, smtp_user, smtp_pass, from_email, from_name } = req.body; // Check if config exists const existingConfig = await db('email_configs').first(); const configData = { smtp_host, smtp_port: parseInt(smtp_port), smtp_secure: smtp_secure || false, smtp_user: smtp_user || '', from_email, from_name: from_name || 'Photo Sharing', updated_at: new Date() }; // Only update password if provided and not masked if (smtp_pass && smtp_pass !== '********') { configData.smtp_pass = smtp_pass; } if (existingConfig) { await db('email_configs') .where('id', existingConfig.id) .update(configData); } else { await db('email_configs').insert(configData); } // Log activity await logActivity('email_config_updated', { smtp_host, from_email }, null, { type: 'admin', id: req.user.id, name: req.user.username } ); res.json({ message: 'Email configuration updated successfully' }); } catch (error) { console.error('Email config update error:', error); res.status(500).json({ error: 'Failed to update email configuration' }); } }); // Test email configuration router.post('/test', adminAuth, async (req, res) => { try { const { test_email } = req.body; if (!test_email) { return res.status(400).json({ error: 'Test email address is required' }); } // Get email config const config = await db('email_configs').first(); if (!config) { return res.status(400).json({ error: 'Email configuration not found. Please configure SMTP settings first.' }); } // Create transporter const transporter = nodemailer.createTransport({ host: config.smtp_host, port: config.smtp_port, secure: config.smtp_secure, auth: config.smtp_user ? { user: config.smtp_user, pass: config.smtp_pass } : undefined }); // Send test email await transporter.sendMail({ from: `${config.from_name} <${config.from_email}>`, to: test_email, subject: 'Test Email - Photo Sharing Platform', html: `
This is a test email from your Photo Sharing platform.
If you're seeing this, your email configuration is working correctly.
Sent from: ${config.from_email}
SMTP Host: ${config.smtp_host}
Time: ${new Date().toISOString()}