Update storage.ts

This commit is contained in:
fcarraUniSa
2025-12-23 10:35:29 +01:00
committed by GitHub
parent 68f3b1d6c0
commit 753d8c4ab1

View File

@@ -13,10 +13,15 @@ export const generateSQL = (template: EmailTemplate): string => {
const subject = template.subject.replace(/'/g, "''"); const subject = template.subject.replace(/'/g, "''");
const vars = JSON.stringify(template.variables).replace(/'/g, "''"); const vars = JSON.stringify(template.variables).replace(/'/g, "''");
const key = generateTemplateKey(template.name); const key = generateTemplateKey(template.name);
const name = template.name.replace(/'/g, "''");
const desc = (template.description || '').replace(/'/g, "''");
return `INSERT INTO email_templates (id, template_key, name, description, subject, header_html, body_html, footer_html, full_html, required_variables) 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}') VALUES ('${template.id}', '${key}', '${name}', '${desc}', '${subject}', '${header}', '${body}', '${footer}', '${fullHtml}', '${vars}')
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
template_key = VALUES(template_key),
name = VALUES(name),
description = VALUES(description),
subject = VALUES(subject), subject = VALUES(subject),
header_html = VALUES(header_html), header_html = VALUES(header_html),
body_html = VALUES(body_html), body_html = VALUES(body_html),
@@ -36,23 +41,19 @@ export const generateN8nCode = (template: EmailTemplate): string => {
return `// Nodo Code n8n - Popolatore Template return `// Nodo Code n8n - Popolatore Template
// 1. Assicurati che il nodo precedente (SQL) restituisca 'full_html' e 'subject'. // 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) { for (const item of items) {
const templateHtml = item.json.full_html; const templateHtml = item.json.full_html;
const templateSubject = item.json.subject; const templateSubject = item.json.subject;
// Definisci qui i tuoi dati dinamici
const replacements = { const replacements = {
${hasVars ? varsMap : ' // Nessuna variabile rilevata in questo template'} ${hasVars ? varsMap : ' // Nessuna variabile rilevata'}
}; };
let finalHtml = templateHtml; let finalHtml = templateHtml;
let finalSubject = templateSubject; let finalSubject = templateSubject;
// Esegui sostituzione
for (const [key, value] of Object.entries(replacements)) { for (const [key, value] of Object.entries(replacements)) {
// Sostituisce {{key}} globalmente nell'HTML e nell'Oggetto
const regex = new RegExp('{{' + key + '}}', 'g'); const regex = new RegExp('{{' + key + '}}', 'g');
finalHtml = finalHtml.replace(regex, value); finalHtml = finalHtml.replace(regex, value);
if (finalSubject) { if (finalSubject) {
@@ -60,7 +61,6 @@ ${hasVars ? varsMap : ' // Nessuna variabile rilevata in questo template'}
} }
} }
// Output del contenuto processato
item.json.processed_html = finalHtml; item.json.processed_html = finalHtml;
item.json.processed_subject = finalSubject; item.json.processed_subject = finalSubject;
} }
@@ -68,7 +68,7 @@ ${hasVars ? varsMap : ' // Nessuna variabile rilevata in questo template'}
return items;`; return items;`;
}; };
// Async API calls to replace synchronous localStorage // Async API calls
export const getTemplates = async (): Promise<EmailTemplate[]> => { export const getTemplates = async (): Promise<EmailTemplate[]> => {
try { try {
const response = await fetch('/api/templates'); const response = await fetch('/api/templates');
@@ -91,8 +91,8 @@ export const saveTemplate = async (template: EmailTemplate): Promise<void> => {
}); });
if (!response.ok) { if (!response.ok) {
const error = await response.json(); const errorData = await response.json();
throw new Error(error.message || 'Salvataggio fallito'); throw new Error(errorData.details || errorData.error || 'Salvataggio fallito');
} }
} catch (e) { } catch (e) {
console.error("Fallito il salvataggio del template", e); console.error("Fallito il salvataggio del template", e);