const { Settings } = require('../models'); const mailService = require('../services/mail.service'); const telegramService = require('../services/telegram.service'); // Get all settings exports.getAllSettings = async (req, res, next) => { try { const settings = await Settings.findAll(); // Hide sensitive values const sanitized = settings.map(s => ({ ...s.toJSON(), value: s.is_encrypted ? '********' : s.value, })); res.json({ success: true, data: sanitized, }); } catch (error) { next(error); } }; // Update Gmail settings exports.updateGmailSettings = async (req, res, next) => { try { const { gmail_user, gmail_app_password, gmail_from_name } = req.body; if (gmail_user) { await Settings.upsert({ key: 'gmail_user', value: gmail_user.trim(), is_encrypted: false, description: 'Gmail email address', }); } if (gmail_app_password) { // Remove all spaces from App Password (Google gives it with spaces) const cleanPassword = gmail_app_password.replace(/\s+/g, ''); await Settings.upsert({ key: 'gmail_password', value: cleanPassword, is_encrypted: true, description: 'Gmail App Password', }); } if (gmail_from_name) { await Settings.upsert({ key: 'gmail_from_name', value: gmail_from_name, is_encrypted: false, description: 'Sender name for emails', }); } res.json({ success: true, message: 'Gmail settings updated successfully', }); } catch (error) { next(error); } }; // Update Telegram settings exports.updateTelegramSettings = async (req, res, next) => { try { const { telegram_bot_token, telegram_chat_id } = req.body; if (telegram_bot_token) { await Settings.upsert({ key: 'telegram_bot_token', value: telegram_bot_token, is_encrypted: true, description: 'Telegram Bot Token', }); } if (telegram_chat_id) { await Settings.upsert({ key: 'telegram_chat_id', value: telegram_chat_id, is_encrypted: false, description: 'Telegram Chat ID', }); } res.json({ success: true, message: 'Telegram settings updated successfully', }); } catch (error) { next(error); } }; // Test Gmail connection exports.testGmail = async (req, res, next) => { try { const result = await mailService.testConnection(); res.json(result); } catch (error) { res.status(500).json({ success: false, error: error.message, }); } }; // Test Telegram connection exports.testTelegram = async (req, res, next) => { try { const result = await telegramService.sendTestMessage(); res.json(result); } catch (error) { res.status(500).json({ success: false, error: error.message, }); } }; module.exports = exports;