Update storage.ts
This commit is contained in:
@@ -1,22 +1,7 @@
|
|||||||
|
|
||||||
import { EmailTemplate } from '../types';
|
import { EmailTemplate } from '../types';
|
||||||
|
|
||||||
// Fallback for crypto.randomUUID in non-secure (HTTP) contexts
|
// Helper functions remain synchronous as they are utility functions
|
||||||
export const generateUUID = () => {
|
|
||||||
if (typeof crypto !== 'undefined' && crypto.randomUUID) {
|
|
||||||
try {
|
|
||||||
return crypto.randomUUID();
|
|
||||||
} catch (e) {
|
|
||||||
// Fallback if it exists but fails
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
||||||
const r = Math.random() * 16 | 0;
|
|
||||||
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
||||||
return v.toString(16);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export const generateTemplateKey = (name: string): string => {
|
export const generateTemplateKey = (name: string): string => {
|
||||||
return name.trim().toLowerCase().replace(/\s+/g, '_').replace(/[^a-z0-9_]/g, '');
|
return name.trim().toLowerCase().replace(/\s+/g, '_').replace(/[^a-z0-9_]/g, '');
|
||||||
};
|
};
|
||||||
@@ -29,15 +14,10 @@ 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}', '${name}', '${desc}', '${subject}', '${header}', '${body}', '${footer}', '${fullHtml}', '${vars}')
|
VALUES ('${template.id}', '${key}', '${template.name.replace(/'/g, "''")}', '${template.description?.replace(/'/g, "''") || ''}', '${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),
|
||||||
@@ -56,18 +36,24 @@ export const generateN8nCode = (template: EmailTemplate): string => {
|
|||||||
const hasVars = template.variables.length > 0;
|
const hasVars = template.variables.length > 0;
|
||||||
|
|
||||||
return `// Nodo Code n8n - Popolatore Template
|
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) {
|
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'}
|
${hasVars ? varsMap : ' // Nessuna variabile rilevata in questo template'}
|
||||||
};
|
};
|
||||||
|
|
||||||
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) {
|
||||||
@@ -75,48 +61,54 @@ ${hasVars ? varsMap : ' // Nessuna variabile rilevata'}
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Output del contenuto processato
|
||||||
item.json.processed_html = finalHtml;
|
item.json.processed_html = finalHtml;
|
||||||
item.json.processed_subject = finalSubject;
|
item.json.processed_subject = finalSubject;
|
||||||
}
|
}
|
||||||
|
|
||||||
return items;`;
|
return items;`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Async API calls to replace synchronous localStorage
|
||||||
export const getTemplates = async (): Promise<EmailTemplate[]> => {
|
export const getTemplates = async (): Promise<EmailTemplate[]> => {
|
||||||
console.log("Fetching templates from /api/templates...");
|
try {
|
||||||
const response = await fetch('/api/templates');
|
const response = await fetch('/api/templates');
|
||||||
if (!response.ok) {
|
if (!response.ok) throw new Error('Fallito il recupero');
|
||||||
const errorBody = await response.text();
|
return await response.json();
|
||||||
throw new Error(`Errore API (${response.status}): ${errorBody || response.statusText}`);
|
} catch (e) {
|
||||||
|
console.error("Fallito il caricamento dei template", e);
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
return await response.json();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const saveTemplate = async (template: EmailTemplate): Promise<void> => {
|
export const saveTemplate = async (template: EmailTemplate): Promise<void> => {
|
||||||
console.log("Saving template to /api/templates...", template.name);
|
try {
|
||||||
const response = await fetch('/api/templates', {
|
const response = await fetch('/api/templates', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify(template),
|
body: JSON.stringify(template),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
let errorMessage = `Errore HTTP ${response.status}`;
|
const error = await response.json();
|
||||||
try {
|
throw new Error(error.message || 'Salvataggio fallito');
|
||||||
const errorData = await response.json();
|
|
||||||
errorMessage = errorData.details || errorData.error || errorMessage;
|
|
||||||
} catch (e) {
|
|
||||||
const textError = await response.text();
|
|
||||||
if (textError) errorMessage = textError;
|
|
||||||
}
|
}
|
||||||
throw new Error(errorMessage);
|
} catch (e) {
|
||||||
|
console.error("Fallito il salvataggio del template", e);
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteTemplate = async (id: string): Promise<void> => {
|
export const deleteTemplate = async (id: string): Promise<void> => {
|
||||||
const response = await fetch(`/api/templates/${id}`, {
|
try {
|
||||||
method: 'DELETE',
|
const response = await fetch(`/api/templates/${id}`, {
|
||||||
});
|
method: 'DELETE',
|
||||||
if (!response.ok) throw new Error('Eliminazione fallita');
|
});
|
||||||
|
if (!response.ok) throw new Error('Eliminazione fallita');
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Fallita l'eliminazione del template", e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user