213 lines
5.1 KiB
JavaScript
213 lines
5.1 KiB
JavaScript
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);
|
||
}
|
||
};
|
||
|
||
// Update System settings (domain, etc.)
|
||
exports.updateSystemSettings = async (req, res, next) => {
|
||
try {
|
||
const { base_url, frontend_url, cors_enabled } = req.body;
|
||
|
||
if (base_url !== undefined) {
|
||
if (base_url) {
|
||
// Remove trailing slash if exists
|
||
const cleanUrl = base_url.trim().replace(/\/$/, '');
|
||
|
||
// Basic URL validation
|
||
try {
|
||
new URL(cleanUrl);
|
||
} catch (e) {
|
||
return res.status(400).json({
|
||
success: false,
|
||
error: 'Geçersiz Base URL formatı. Örnek: https://yourdomain.com',
|
||
});
|
||
}
|
||
|
||
await Settings.upsert({
|
||
key: 'base_url',
|
||
value: cleanUrl,
|
||
is_encrypted: false,
|
||
description: 'Base URL for tracking links (backend)',
|
||
});
|
||
|
||
// Update process.env for immediate use
|
||
process.env.BASE_URL = cleanUrl;
|
||
} else {
|
||
await Settings.destroy({ where: { key: 'base_url' } });
|
||
}
|
||
}
|
||
|
||
if (frontend_url !== undefined) {
|
||
if (frontend_url) {
|
||
// Remove trailing slash if exists
|
||
const cleanUrl = frontend_url.trim().replace(/\/$/, '');
|
||
|
||
// Basic URL validation
|
||
try {
|
||
new URL(cleanUrl);
|
||
} catch (e) {
|
||
return res.status(400).json({
|
||
success: false,
|
||
error: 'Geçersiz Frontend URL formatı. Örnek: https://panel.yourdomain.com',
|
||
});
|
||
}
|
||
|
||
await Settings.upsert({
|
||
key: 'frontend_url',
|
||
value: cleanUrl,
|
||
is_encrypted: false,
|
||
description: 'Frontend URL (for CORS)',
|
||
});
|
||
} else {
|
||
await Settings.destroy({ where: { key: 'frontend_url' } });
|
||
}
|
||
}
|
||
|
||
if (cors_enabled !== undefined) {
|
||
await Settings.upsert({
|
||
key: 'cors_enabled',
|
||
value: cors_enabled ? 'true' : 'false',
|
||
is_encrypted: false,
|
||
description: 'Enable CORS for separate domains',
|
||
});
|
||
}
|
||
|
||
// Update CORS configuration if available
|
||
if (req.app && req.app.updateCorsSettings) {
|
||
await req.app.updateCorsSettings();
|
||
}
|
||
|
||
res.json({
|
||
success: true,
|
||
message: 'Sistem ayarları güncellendi. CORS ayarları uygulandı.',
|
||
});
|
||
} 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;
|
||
|