diff --git a/src/app/admin/settings/email-templates/page.tsx b/src/app/admin/settings/email-templates/page.tsx index d245024..dac9f83 100644 --- a/src/app/admin/settings/email-templates/page.tsx +++ b/src/app/admin/settings/email-templates/page.tsx @@ -56,21 +56,12 @@ export default function EmailTemplatesPage() { setIsLoading(true); try { const fetchedTemplate = await getEmailTemplate(); - if (fetchedTemplate) { - setTemplate(fetchedTemplate); - setOriginalTemplate(fetchedTemplate); - } else { - toast({ - variant: 'destructive', - title: 'Template Not Found', - description: 'No email template found in the database. You can create one by saving a new template.', - }); - setIsEditing(true); // Default to editing if no template exists - } + setTemplate(fetchedTemplate); + setOriginalTemplate(fetchedTemplate); } catch (error: any) { toast({ variant: 'destructive', - title: 'Error', + title: 'Error Loading Template', description: error.message || 'Failed to load email template.', }); } finally { diff --git a/src/lib/actions/email.ts b/src/lib/actions/email.ts index 2b7fc64..674a4e4 100644 --- a/src/lib/actions/email.ts +++ b/src/lib/actions/email.ts @@ -10,27 +10,77 @@ const emailTemplateSchema = z.object({ body: z.string().min(1, 'Body is required.'), }); +const defaultSubject = "Your EstimateFlow Project Estimate is Ready!"; +const defaultBody = ` + + + + + + Your Project Estimate + + + + + + + + + + + + +
+

EstimateFlow

+
+

Hello, [User Name],

+

+ Thank you for using EstimateFlow. We've prepared a rough estimate for your project based on your selections. +

+ + [EstimateDetails] + +

+ Please note that this is a preliminary estimate. For a more detailed quote and to discuss your project further, please don't hesitate to contact us. +

+
+ Contact Us +
+
+

Best regards,
The EstimateFlow Team

+

© ${new Date().getFullYear()} EstimateFlow. All rights reserved.

+
+ +`; + + /** * Gets the email template from the database. + * If no template is found, it creates and returns a default one. */ -export async function getEmailTemplate(): Promise<{ subject: string; body: string } | null> { +export async function getEmailTemplate(): Promise<{ subject: string; body: string }> { try { const stmt = db.prepare('SELECT subject, body FROM email_templates WHERE id = ?'); - // We assume there is only one template with id = 1 - const template = stmt.get(1) as { subject: string; body: string } | undefined; + let template = stmt.get(1) as { subject: string; body: string } | undefined; + if (!template) { - return null; + console.log('No template found, creating a default one.'); + const insertStmt = db.prepare('INSERT INTO email_templates (id, subject, body) VALUES (?, ?, ?)'); + insertStmt.run(1, defaultSubject, defaultBody); + template = { subject: defaultSubject, body: defaultBody }; + revalidatePath('/admin/settings/email-templates'); } + return template; } catch (error) { - console.error('Failed to get email template:', error); + console.error('Failed to get or create email template:', error); // Propagate the error to be handled by the caller - throw new Error('Database error while fetching email template.'); + throw new Error('Database error while fetching or creating email template.'); } } /** - * Updates or creates the email template in the database. + * Updates the email template in the database. */ export async function updateEmailTemplate(data: { subject: string; body: string }): Promise<{ success: boolean; message: string }> { const validation = emailTemplateSchema.safeParse(data); @@ -42,17 +92,11 @@ export async function updateEmailTemplate(data: { subject: string; body: string const { subject, body } = validation.data; try { - const checkStmt = db.prepare('SELECT id FROM email_templates WHERE id = 1'); - const existing = checkStmt.get(); + const stmt = db.prepare('UPDATE email_templates SET subject = ?, body = ? WHERE id = ?'); + const result = stmt.run(subject, body, 1); - if (existing) { - // Update existing template - const stmt = db.prepare('UPDATE email_templates SET subject = ?, body = ? WHERE id = ?'); - stmt.run(subject, body, 1); - } else { - // Insert new template - const stmt = db.prepare('INSERT INTO email_templates (id, subject, body) VALUES (?, ?, ?)'); - stmt.run(1, subject, body); + if (result.changes === 0) { + return { success: false, message: 'Could not find the template to update. Please try reloading the page.' }; } revalidatePath('/admin/settings/email-templates');