From 4e5226d213a268985e2a2101ecf64d18bb64fd12 Mon Sep 17 00:00:00 2001 From: fcarraUniSa Date: Tue, 23 Dec 2025 12:00:16 +0100 Subject: [PATCH] Update storage.ts --- services/storage.ts | 58 ++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 37 deletions(-) diff --git a/services/storage.ts b/services/storage.ts index 77df467..5335c52 100644 --- a/services/storage.ts +++ b/services/storage.ts @@ -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 => { - 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 => { - 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 => { - 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'); };