import { EmailTemplate } from '../types'; // Helper functions remain synchronous as they are utility functions export const generateTemplateKey = (name: string): string => { return name.trim().toLowerCase().replace(/\s+/g, '_').replace(/[^a-z0-9_]/g, ''); }; export const generateSQL = (template: EmailTemplate): string => { const fullHtml = `${template.header}${template.body}${template.footer}`.replace(/'/g, "''"); const header = template.header.replace(/'/g, "''"); const body = template.body.replace(/'/g, "''"); const footer = template.footer.replace(/'/g, "''"); const subject = template.subject.replace(/'/g, "''"); const vars = JSON.stringify(template.variables).replace(/'/g, "''"); const key = generateTemplateKey(template.name); return `INSERT INTO email_templates (id, template_key, name, description, subject, header_html, body_html, footer_html, full_html, required_variables) VALUES ('${template.id}', '${key}', '${template.name.replace(/'/g, "''")}', '${template.description?.replace(/'/g, "''") || ''}', '${subject}', '${header}', '${body}', '${footer}', '${fullHtml}', '${vars}') ON DUPLICATE KEY UPDATE subject = VALUES(subject), header_html = VALUES(header_html), body_html = VALUES(body_html), footer_html = VALUES(footer_html), full_html = VALUES(full_html), required_variables = VALUES(required_variables);`; }; export const generateSelectSQL = (template: EmailTemplate): string => { const key = generateTemplateKey(template.name); return `SELECT * FROM email_templates WHERE template_key = '${key}';`; }; export const generateN8nCode = (template: EmailTemplate): string => { const varsMap = template.variables.map(v => ` "${v}": "REPLACE_WITH_VALUE"`).join(',\n'); const hasVars = template.variables.length > 0; return `// Nodo Code n8n - Popolatore Template // 1. Assicurati che il nodo precedente (SQL) restituisca 'full_html' e 'subject'. // 2. Aggiusta il percorso (item.json.full_html) se l'output del tuo nodo SQL รจ diverso. for (const item of items) { const templateHtml = item.json.full_html; const templateSubject = item.json.subject; // Definisci qui i tuoi dati dinamici const replacements = { ${hasVars ? varsMap : ' // Nessuna variabile rilevata in questo template'} }; let finalHtml = templateHtml; let finalSubject = templateSubject; // Esegui sostituzione for (const [key, value] of Object.entries(replacements)) { // Sostituisce {{key}} globalmente nell'HTML e nell'Oggetto const regex = new RegExp('{{' + key + '}}', 'g'); finalHtml = finalHtml.replace(regex, value); if (finalSubject) { finalSubject = finalSubject.replace(regex, value); } } // Output del contenuto processato item.json.processed_html = finalHtml; item.json.processed_subject = finalSubject; } return items;`; }; // Async API calls to replace synchronous localStorage export const getTemplates = async (): Promise => { try { const response = await fetch('/api/templates'); if (!response.ok) throw new Error('Fallito il recupero'); return await response.json(); } catch (e) { console.error("Fallito il caricamento dei template", e); return []; } }; export const saveTemplate = async (template: EmailTemplate): Promise => { try { const response = await fetch('/api/templates', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(template), }); if (!response.ok) { const error = await response.json(); throw new Error(error.message || 'Salvataggio fallito'); } } catch (e) { console.error("Fallito il salvataggio del template", e); throw e; } }; export const deleteTemplate = async (id: string): Promise => { try { const response = await fetch(`/api/templates/${id}`, { method: 'DELETE', }); if (!response.ok) throw new Error('Eliminazione fallita'); } catch (e) { console.error("Fallita l'eliminazione del template", e); throw e; } };