Update storage.ts

This commit is contained in:
fcarraUniSa
2025-12-23 12:00:16 +01:00
committed by GitHub
parent 947c5d3952
commit 4e5226d213

View File

@@ -1,6 +1,6 @@
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, '');
};
@@ -40,8 +40,6 @@ export const generateN8nCode = (template: EmailTemplate): string => {
const hasVars = template.variables.length > 0;
return `// Nodo Code n8n - Popolatore Template
// 1. Assicurati che il nodo precedente (SQL) restituisca 'full_html' e 'subject'.
for (const item of items) {
const templateHtml = item.json.full_html;
const templateSubject = item.json.subject;
@@ -64,50 +62,36 @@ ${hasVars ? varsMap : ' // Nessuna variabile rilevata'}
item.json.processed_html = finalHtml;
item.json.processed_subject = finalSubject;
}
return items;`;
};
// Async API calls
export const getTemplates = async (): Promise<EmailTemplate[]> => {
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 [];
const response = await fetch('/api/templates');
if (!response.ok) {
const errorBody = await response.text();
throw new Error(`Errore API (${response.status}): ${errorBody || response.statusText}`);
}
return await response.json();
};
export const saveTemplate = async (template: EmailTemplate): Promise<void> => {
try {
const response = await fetch('/api/templates', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(template),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.details || errorData.error || 'Salvataggio fallito');
}
} catch (e) {
console.error("Fallito il salvataggio del template", e);
throw e;
const response = await fetch('/api/templates', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(template),
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.details || errorData.error || `Salvataggio fallito con stato ${response.status}`);
}
};
export const deleteTemplate = async (id: string): Promise<void> => {
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;
}
const response = await fetch(`/api/templates/${id}`, {
method: 'DELETE',
});
if (!response.ok) throw new Error('Eliminazione fallita');
};