diff --git a/scripts/seed.ts b/scripts/seed.ts
index 5acdab7..6a684c7 100644
--- a/scripts/seed.ts
+++ b/scripts/seed.ts
@@ -1,3 +1,4 @@
+
import Database from 'better-sqlite3';
const db = new Database('local.db');
@@ -41,25 +42,60 @@ function seed() {
const templateStmt = db.prepare('SELECT * FROM email_templates WHERE id = ?');
const defaultTemplate = templateStmt.get(1);
+ 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.
+
+
+`;
+
if (!defaultTemplate) {
const insertTemplate = db.prepare(
"INSERT INTO email_templates (id, subject, body) VALUES (?, ?, ?)"
);
- const defaultSubject = "Your Project Estimate is Ready!";
- const defaultBody = `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.
-
-Best regards,
-The EstimateFlow Team`;
insertTemplate.run(1, defaultSubject, defaultBody);
console.log('Default email template created.');
} else {
- console.log('Default email template already exists.');
+ // If you want to update the template on every seed, you can use an UPDATE query here
+ const updateTemplate = db.prepare("UPDATE email_templates SET subject = ?, body = ? WHERE id = ?");
+ updateTemplate.run(defaultSubject, defaultBody, 1);
+ console.log('Default email template updated.');
}
diff --git a/src/app/admin/settings/email-templates/page.tsx b/src/app/admin/settings/email-templates/page.tsx
index 002e6e3..d245024 100644
--- a/src/app/admin/settings/email-templates/page.tsx
+++ b/src/app/admin/settings/email-templates/page.tsx
@@ -9,6 +9,7 @@ import { getEmailTemplate, updateEmailTemplate } from '@/lib/actions/email';
import { Input } from '@/components/ui/input';
import { Textarea } from '@/components/ui/textarea';
import { Label } from '@/components/ui/label';
+import { Skeleton } from '@/components/ui/skeleton';
type EmailTemplate = {
subject: string;
@@ -16,26 +17,29 @@ type EmailTemplate = {
};
function EmailPreview({ template }: { template: EmailTemplate }) {
- const previewBody = template.body.replace(
- '[EstimateDetails]',
- `
);
}
@@ -56,7 +60,12 @@ export default function EmailTemplatesPage() {
setTemplate(fetchedTemplate);
setOriginalTemplate(fetchedTemplate);
} else {
- throw new Error('Could not find email template.');
+ 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
}
} catch (error: any) {
toast({
@@ -117,7 +126,7 @@ export default function EmailTemplatesPage() {
) : (
- Use placeholders like [User Name] and [EstimateDetails]. They will be replaced with actual values.
+ Use placeholders like [User Name] and [EstimateDetails]. They will be replaced with actual values. HTML is supported.
diff --git a/src/lib/actions/email.ts b/src/lib/actions/email.ts
index 0162cc1..2b7fc64 100644
--- a/src/lib/actions/email.ts
+++ b/src/lib/actions/email.ts
@@ -30,7 +30,7 @@ export async function getEmailTemplate(): Promise<{ subject: string; body: strin
}
/**
- * Updates the email template in the database.
+ * Updates or creates 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,12 +42,17 @@ export async function updateEmailTemplate(data: { subject: string; body: string
const { subject, body } = validation.data;
try {
- const stmt = db.prepare('UPDATE email_templates SET subject = ?, body = ? WHERE id = ?');
- // We assume we're updating the template with id = 1
- const info = stmt.run(subject, body, 1);
-
- if (info.changes === 0) {
- return { success: false, message: 'Could not find the template to update. No changes were made.' };
+ const checkStmt = db.prepare('SELECT id FROM email_templates WHERE id = 1');
+ const existing = checkStmt.get();
+
+ 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);
}
revalidatePath('/admin/settings/email-templates');