feat: aggiunta ricerca spudorata. progress: ripreso con il tentativo di invio delle mail che non creino nuovi ticket
This commit is contained in:
1 parent
3650bf5790
commit
8d24759a15
18 files changed
+1661
-98
No files matched your search
+100
-19
@@ -193,6 +193,59 @@ router.delete('/address-groups/:id', (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// ─── SETTINGS ──────────────────────────────────────────────────────────────────
|
||||
|
||||
// GET /api/email/settings — Fetch agent sender preference
|
||||
router.get('/settings', async (req, res) => {
|
||||
try {
|
||||
const agentId = parseInt(req.query.agent_id || '1', 10);
|
||||
const row = db.prepare(`SELECT use_agent_sender FROM agent_settings WHERE agent_id = ?`).get(agentId);
|
||||
const useAgentSender = row ? Boolean(row.use_agent_sender) : false;
|
||||
|
||||
let agentEmail = '';
|
||||
const prefRes = await pool.query(
|
||||
`SELECT preferences_value FROM user_preferences WHERE user_id = $1 AND preferences_key = 'UserEmail'`,
|
||||
[agentId]
|
||||
);
|
||||
if (prefRes.rows.length > 0) {
|
||||
agentEmail = prefRes.rows[0].preferences_value || '';
|
||||
}
|
||||
|
||||
const envSender = process.env.AZURE_MAIL_SENDER || process.env.SMTP_FROM || process.env.SMTP_USER || '';
|
||||
|
||||
res.json({
|
||||
agent_id: agentId,
|
||||
use_agent_sender: useAgentSender,
|
||||
agent_email: agentEmail,
|
||||
env_sender: envSender
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('[Email] Error fetching settings:', err);
|
||||
res.status(500).json({ error: 'Errore durante il recupero delle impostazioni email: ' + err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/email/settings — Update agent sender preference
|
||||
router.post('/settings', async (req, res) => {
|
||||
try {
|
||||
const { agent_id, use_agent_sender } = req.body;
|
||||
const agentId = parseInt(agent_id || '1', 10);
|
||||
const val = use_agent_sender ? 1 : 0;
|
||||
|
||||
const existing = db.prepare(`SELECT agent_id FROM agent_settings WHERE agent_id = ?`).get(agentId);
|
||||
if (existing) {
|
||||
db.prepare(`UPDATE agent_settings SET use_agent_sender = ? WHERE agent_id = ?`).run(val, agentId);
|
||||
} else {
|
||||
db.prepare(`INSERT INTO agent_settings (agent_id, use_agent_sender) VALUES (?, ?)`).run(agentId, val);
|
||||
}
|
||||
|
||||
res.json({ success: true, use_agent_sender: Boolean(val) });
|
||||
} catch (err) {
|
||||
console.error('[Email] Error updating settings:', err);
|
||||
res.status(500).json({ error: 'Errore durante il salvataggio delle impostazioni email: ' + err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// ─── SEND EMAIL ───────────────────────────────────────────────────────────────
|
||||
|
||||
// POST /api/email/send — send an email for a ticket
|
||||
@@ -207,7 +260,6 @@ router.post('/send', async (req, res) => {
|
||||
attachments = [],
|
||||
inlineImages = [],
|
||||
agentId,
|
||||
agentName = 'Agente',
|
||||
keepHelpdeskCopy = true,
|
||||
inReplyTo,
|
||||
references,
|
||||
@@ -218,6 +270,39 @@ router.post('/send', async (req, res) => {
|
||||
if (!bodyHtml) return res.status(400).json({ error: 'Il corpo della email è obbligatorio' });
|
||||
|
||||
try {
|
||||
// Fetch agent email and login details
|
||||
let agentEmail = '';
|
||||
let agentName = '';
|
||||
|
||||
if (agentId) {
|
||||
const userRes = await pool.query(
|
||||
`SELECT login, first_name, last_name FROM users WHERE id = $1`,
|
||||
[agentId]
|
||||
);
|
||||
if (userRes.rows.length > 0) {
|
||||
const u = userRes.rows[0];
|
||||
agentName = (u.first_name && u.last_name) ? `${u.first_name} ${u.last_name}` : (u.login || '');
|
||||
}
|
||||
|
||||
const prefRes = await pool.query(
|
||||
`SELECT preferences_value FROM user_preferences WHERE user_id = $1 AND preferences_key = 'UserEmail'`,
|
||||
[agentId]
|
||||
);
|
||||
if (prefRes.rows.length > 0) {
|
||||
agentEmail = prefRes.rows[0].preferences_value || '';
|
||||
}
|
||||
}
|
||||
|
||||
// Check if agent sender override is enabled in agent_settings SQLite
|
||||
let customSender = null;
|
||||
if (agentId) {
|
||||
const settingRow = db.prepare(`SELECT use_agent_sender FROM agent_settings WHERE agent_id = ?`).get(parseInt(agentId, 10));
|
||||
const useAgentSender = settingRow ? settingRow.use_agent_sender === 1 : false;
|
||||
if (useAgentSender && agentEmail) {
|
||||
customSender = agentName ? `"${agentName}" <${agentEmail}>` : agentEmail;
|
||||
}
|
||||
}
|
||||
|
||||
// 1. Fetch ticket number and title for subject
|
||||
const ticketResult = await pool.query(
|
||||
`SELECT tn, title FROM ticket WHERE id = $1`,
|
||||
@@ -263,29 +348,25 @@ router.post('/send', async (req, res) => {
|
||||
const messageId = `<${Date.now()}.${Math.random().toString(36).substring(2)}@pharmaidea.com>`;
|
||||
|
||||
// 3. Send via configured mailer (Graph API or SMTP)
|
||||
const mailResult = await sendMail({ to, cc, bcc: bccList, subject, bodyHtml: processedBodyHtml, attachments, inlineImages: finalInlineImages, inReplyTo, references, messageId });
|
||||
const mailResult = await sendMail({
|
||||
from: customSender,
|
||||
to,
|
||||
cc,
|
||||
bcc: bccList,
|
||||
subject,
|
||||
bodyHtml: processedBodyHtml,
|
||||
attachments,
|
||||
inlineImages: finalInlineImages,
|
||||
inReplyTo,
|
||||
references,
|
||||
messageId
|
||||
});
|
||||
const finalMessageId = (mailResult && mailResult.internetMessageId) || messageId;
|
||||
|
||||
// 4. Log article in OTRS ticket via DB as a standard Email article
|
||||
try {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const agentLoginResult = agentId
|
||||
? await pool.query(`SELECT login FROM users WHERE id = $1`, [agentId])
|
||||
: null;
|
||||
const agentLogin = agentLoginResult?.rows[0]?.login || 'system';
|
||||
|
||||
// Query email from user_preferences for agent
|
||||
let agentEmail = '';
|
||||
if (agentId) {
|
||||
const prefRes = await pool.query(
|
||||
`SELECT preferences_value FROM user_preferences WHERE user_id = $1 AND preferences_key = 'UserEmail'`,
|
||||
[agentId]
|
||||
);
|
||||
if (prefRes.rows.length > 0) {
|
||||
agentEmail = prefRes.rows[0].preferences_value || '';
|
||||
}
|
||||
}
|
||||
const aFrom = agentEmail ? `"${agentLogin}" <${agentEmail}>` : `"${agentLogin}" <${process.env.AZURE_MAIL_SENDER || process.env.SMTP_FROM || 'helpdesk@example.com'}>`;
|
||||
const aFrom = customSender || (agentEmail ? `"${agentName}" <${agentEmail}>` : `"${agentName || 'system'}" <${process.env.AZURE_MAIL_SENDER || process.env.SMTP_FROM || 'helpdesk@example.com'}>`);
|
||||
|
||||
const toList = to.join(', ');
|
||||
|
||||
|
||||
Reference in new issue
Block a user