Update storage.ts
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
import { EmailTemplate } from '../types';
|
import { EmailTemplate } from '../types';
|
||||||
|
|
||||||
// Helper functions remain synchronous as they are utility functions
|
|
||||||
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, '');
|
||||||
};
|
};
|
||||||
@@ -40,8 +40,6 @@ 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'.
|
|
||||||
|
|
||||||
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;
|
||||||
@@ -64,50 +62,36 @@ ${hasVars ? varsMap : ' // Nessuna variabile rilevata'}
|
|||||||
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
|
|
||||||
export const getTemplates = async (): Promise<EmailTemplate[]> => {
|
export const getTemplates = async (): Promise<EmailTemplate[]> => {
|
||||||
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> => {
|
||||||
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) {
|
const errorData = await response.json().catch(() => ({}));
|
||||||
const errorData = await response.json();
|
throw new Error(errorData.details || errorData.error || `Salvataggio fallito con stato ${response.status}`);
|
||||||
throw new Error(errorData.details || errorData.error || 'Salvataggio fallito');
|
|
||||||
}
|
|
||||||
} 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> => {
|
||||||
try {
|
const response = await fetch(`/api/templates/${id}`, {
|
||||||
const response = await fetch(`/api/templates/${id}`, {
|
method: 'DELETE',
|
||||||
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