fix: corretto invio da mail (scrittura diretta nel db). feat: aggiunta gestione di gruppi interni per l'invio delle mail

This commit is contained in:
Gabriele Cimaschi
2026-07-09 22:36:46 +02:00
parent 725bdaeae4
commit 191f9a407b
11 changed files with 833 additions and 313 deletions
+66 -11
View File
@@ -145,7 +145,7 @@ const EmailCompose = (() => {
}
// ── Tag Input Helper ──────────────────────────────────────────────────────────
function makeTagInput(containerId, initialEmails = []) {
function makeTagInput(containerId, initialEmails = [], onFocus = null) {
const container = document.getElementById(containerId);
const tags = [...initialEmails];
@@ -168,6 +168,11 @@ const EmailCompose = (() => {
input.type = 'text';
input.placeholder = tags.length ? '' : 'email@esempio.com, premi Invio';
input.value = currentVal;
if (onFocus) {
input.addEventListener('focus', onFocus);
}
input.addEventListener('keydown', (e) => {
if ((e.key === 'Enter' || e.key === ',') && input.value.trim()) {
e.preventDefault();
@@ -221,6 +226,10 @@ const EmailCompose = (() => {
<label class="ec-label">CC</label>
<div class="ec-tags-input" id="ec-cc-container"></div>
</div>
<div class="ec-field">
<label class="ec-label">BCC (CCN)</label>
<div class="ec-tags-input" id="ec-bcc-container"></div>
</div>
<div class="ec-field">
<label class="ec-label">Oggetto</label>
<input type="text" id="ec-subject" class="form-input" style="margin-bottom:0;" placeholder="Oggetto email" />
@@ -233,10 +242,16 @@ const EmailCompose = (() => {
</select>
</div>
<div class="ec-field" style="flex:1;">
<label class="ec-label">Tieni helpdesk in copia</label>
<label class="ec-label">Gruppi di Indirizzi</label>
<select id="ec-groups-select" class="ec-select" style="width:100%; min-width:unset;">
<option value="">— Inserisci gruppo... —</option>
</select>
</div>
<div class="ec-field" style="flex:1;">
<label class="ec-label">Helpdesk BCC (genera nuovo ticket)</label>
<select id="ec-helpdesk-cc-select" class="ec-select" style="width:100%; min-width:unset;">
<option value="1"> (BCC automatico)</option>
<option value="0">No</option>
<option value="0">No (BCC automatico)</option>
<option value="1"></option>
</select>
</div>
</div>
@@ -310,6 +325,23 @@ const EmailCompose = (() => {
return '';
}
// ── Load Address Groups ────────────────────────────────────────────────────────
async function loadAddressGroups(agentId, selectEl) {
try {
const groups = await App.api(`/api/email/address-groups?agent_id=${agentId}`);
selectEl.innerHTML = '<option value="">— Inserisci gruppo... —</option>';
groups.forEach(g => {
const opt = document.createElement('option');
opt.value = g.id;
opt.textContent = g.name;
opt.dataset.emails = g.emails;
selectEl.appendChild(opt);
});
} catch (e) {
console.warn('[EmailCompose] Address groups load error:', e);
}
}
// ── Open ─────────────────────────────────────────────────────────────────────
async function open(options = {}) {
injectStyles();
@@ -323,11 +355,14 @@ const EmailCompose = (() => {
const overlay = buildModal();
document.body.appendChild(overlay);
// Init tag inputs
// Init tag inputs with focus tracking
const initialTo = options.initialTo || (options.customerEmail ? [options.customerEmail] : []);
const initialCc = options.initialCc || [];
const toTagsCtrl = makeTagInput('ec-to-container', initialTo);
const ccTagsCtrl = makeTagInput('ec-cc-container', initialCc);
let lastFocusedCtrl = null;
const toTagsCtrl = makeTagInput('ec-to-container', initialTo, () => { lastFocusedCtrl = toTagsCtrl; });
const ccTagsCtrl = makeTagInput('ec-cc-container', initialCc, () => { lastFocusedCtrl = ccTagsCtrl; });
const bccTagsCtrl = makeTagInput('ec-bcc-container', [], () => { lastFocusedCtrl = bccTagsCtrl; });
lastFocusedCtrl = toTagsCtrl;
// Subject
const subjectEl = document.getElementById('ec-subject');
@@ -335,10 +370,27 @@ const EmailCompose = (() => {
const title = options.ticketTitle || '';
subjectEl.value = tn ? `Re: [Ticket#${tn}] ${title}` : title;
// Signature select
// Signature and groups select
const sigSelect = document.getElementById('ec-signature-select');
const groupsSelect = document.getElementById('ec-groups-select');
const agentId = App.currentAgentId || 0;
const defaultSigHtml = await loadSignatures(agentId, sigSelect);
await loadAddressGroups(agentId, groupsSelect);
groupsSelect.addEventListener('change', () => {
const selectedOpt = groupsSelect.options[groupsSelect.selectedIndex];
if (!selectedOpt || !selectedOpt.value) return;
const emailsStr = selectedOpt.dataset.emails || '';
const emails = emailsStr.split(',').map(e => e.trim()).filter(Boolean);
if (lastFocusedCtrl) {
emails.forEach(email => {
lastFocusedCtrl.addTag(email);
});
}
groupsSelect.value = '';
});
// Quill editor
quillEditor = new Quill('#ec-quill-editor', {
@@ -405,7 +457,7 @@ const EmailCompose = (() => {
overlay.addEventListener('click', (e) => { if (e.target === overlay) close(); });
// Send
document.getElementById('ec-send').addEventListener('click', () => sendEmail(toTagsCtrl, ccTagsCtrl));
document.getElementById('ec-send').addEventListener('click', () => sendEmail(toTagsCtrl, ccTagsCtrl, bccTagsCtrl));
}
function processFiles(files) {
@@ -421,9 +473,10 @@ const EmailCompose = (() => {
}
// ── Send ─────────────────────────────────────────────────────────────────────
async function sendEmail(toCtrl, ccCtrl) {
async function sendEmail(toCtrl, ccCtrl, bccCtrl) {
const to = toCtrl.getTags();
const cc = ccCtrl.getTags();
const bcc = bccCtrl.getTags();
const subject = document.getElementById('ec-subject').value.trim();
const bodyHtml = quillEditor ? quillEditor.root.innerHTML : '';
@@ -438,10 +491,12 @@ const EmailCompose = (() => {
const agentId = App.currentAgentId || 0;
const payload = {
ticketId: currentOptions.ticketId,
to, cc, subject, bodyHtml,
to, cc, bcc, subject, bodyHtml,
attachments: attachmentsList,
agentId,
keepHelpdeskCopy: document.getElementById('ec-helpdesk-cc-select').value === '1',
inReplyTo: currentOptions.inReplyTo,
references: currentOptions.references,
};
const res = await App.api('/api/email/send', {