lets create another tab on the admin dashboard called "leads" where you
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
|
||||
'use server';
|
||||
|
||||
import db from '@/lib/db';
|
||||
|
||||
export type Lead = {
|
||||
id: number;
|
||||
name: string;
|
||||
email: string;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
export async function getLeads(): Promise<Lead[]> {
|
||||
try {
|
||||
const stmt = db.prepare(
|
||||
'SELECT id, name, email, createdAt FROM leads ORDER BY createdAt DESC'
|
||||
);
|
||||
const leads = stmt.all() as Lead[];
|
||||
return leads;
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch leads:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -109,6 +109,16 @@ async function createEstimatePdf(data: InputType): Promise<Buffer> {
|
||||
return Buffer.from(pdfBytes);
|
||||
}
|
||||
|
||||
async function saveLead(name: string, email: string) {
|
||||
try {
|
||||
const stmt = db.prepare('INSERT INTO leads (name, email) VALUES (?, ?)');
|
||||
stmt.run(name, email);
|
||||
console.log(`Lead saved: ${name}, ${email}`);
|
||||
} catch (error) {
|
||||
console.error('Failed to save lead:', error);
|
||||
// We don't want to block email sending if lead saving fails, so we just log the error.
|
||||
}
|
||||
}
|
||||
|
||||
export async function sendEstimateEmail(data: InputType): Promise<{ success: boolean, message?: string }> {
|
||||
const validation = inputSchema.safeParse(data);
|
||||
@@ -148,6 +158,10 @@ export async function sendEstimateEmail(data: InputType): Promise<{ success: boo
|
||||
};
|
||||
|
||||
await transporter.sendMail(mailOptions);
|
||||
|
||||
// Save lead after email is sent successfully
|
||||
await saveLead(validation.data.name, validation.data.email);
|
||||
|
||||
return { success: true };
|
||||
|
||||
} catch (error: any) {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
|
||||
import Database from 'better-sqlite3';
|
||||
|
||||
// Use a file-based database in development
|
||||
@@ -28,5 +29,14 @@ db.exec(`
|
||||
)
|
||||
`);
|
||||
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS leads (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
createdAt DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`);
|
||||
|
||||
|
||||
export default db;
|
||||
|
||||
Reference in New Issue
Block a user