2025-11-10 17:00:40 +03:00
|
|
|
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 {
|
2025-11-10 19:24:49 +03:00
|
|
|
const { gmail_user, gmail_app_password, gmail_from_name } = req.body;
|
2025-11-10 17:00:40 +03:00
|
|
|
|
|
|
|
|
if (gmail_user) {
|
|
|
|
|
await Settings.upsert({
|
|
|
|
|
key: 'gmail_user',
|
2025-11-10 19:27:20 +03:00
|
|
|
value: gmail_user.trim(),
|
2025-11-10 17:00:40 +03:00
|
|
|
is_encrypted: false,
|
|
|
|
|
description: 'Gmail email address',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-10 19:24:49 +03:00
|
|
|
if (gmail_app_password) {
|
2025-11-10 19:27:20 +03:00
|
|
|
// Remove all spaces from App Password (Google gives it with spaces)
|
|
|
|
|
const cleanPassword = gmail_app_password.replace(/\s+/g, '');
|
2025-11-10 17:00:40 +03:00
|
|
|
await Settings.upsert({
|
|
|
|
|
key: 'gmail_password',
|
2025-11-10 19:27:20 +03:00
|
|
|
value: cleanPassword,
|
2025-11-10 17:00:40 +03:00
|
|
|
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;
|
|
|
|
|
|