Files
estimation-flow/scripts/seed.ts
T

113 lines
4.0 KiB
TypeScript

import Database from 'better-sqlite3';
const db = new Database('local.db');
function seed() {
console.log('Seeding database...');
// Create settings table if it doesn't exist
db.exec(`
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT
)
`);
// Create email_templates table
db.exec(`
CREATE TABLE IF NOT EXISTS email_templates (
id INTEGER PRIMARY KEY,
subject TEXT,
body TEXT
)
`);
// Check if the hourly_rate setting already exists
const settingStmt = db.prepare('SELECT * FROM settings WHERE key = ?');
const hourlyRateSetting = settingStmt.get('hourly_rate');
if (!hourlyRateSetting) {
// Insert the default hourly rate
const insertSetting = db.prepare(
"INSERT INTO settings (key, value) VALUES (?, ?)"
);
insertSetting.run("hourly_rate", "100");
console.log('Default hourly rate set.');
} else {
console.log('Hourly rate setting already exists.');
}
// Check if default email template exists
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 = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Your Project Estimate</title>
</head>
<body style="font-family: Arial, sans-serif; margin: 0; padding: 20px; background-color: #f4f4f4; color: #333;">
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="max-width: 600px; margin: auto; background-color: #ffffff; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
<tr>
<td style="padding: 20px; text-align: center; background-color: #4A90E2; color: #ffffff; border-top-left-radius: 8px; border-top-right-radius: 8px;">
<h1 style="margin: 0; font-size: 24px;">EstimateFlow</h1>
</td>
</tr>
<tr>
<td style="padding: 30px 20px;">
<h2 style="font-size: 20px; color: #333;">Hello, [User Name],</h2>
<p style="font-size: 16px; line-height: 1.6;">
Thank you for using EstimateFlow. We've prepared a rough estimate for your project based on your selections.
</p>
[EstimateDetails]
<p style="font-size: 16px; line-height: 1.6; margin-top: 20px;">
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.
</p>
<div style="text-align: center; margin-top: 30px;">
<a href="#" style="background-color: #4A90E2; color: #ffffff; padding: 12px 25px; text-decoration: none; border-radius: 5px; font-size: 16px;">Contact Us</a>
</div>
</td>
</tr>
<tr>
<td style="padding: 20px; text-align: center; font-size: 12px; color: #777; border-top: 1px solid #eaeaea;">
<p>Best regards,<br>The EstimateFlow Team</p>
<p>&copy; ${new Date().getFullYear()} EstimateFlow. All rights reserved.</p>
</td>
</tr>
</table>
</body>
</html>`;
if (!defaultTemplate) {
const insertTemplate = db.prepare(
"INSERT INTO email_templates (id, subject, body) VALUES (?, ?, ?)"
);
insertTemplate.run(1, defaultSubject, defaultBody);
console.log('Default email template created.');
} else {
// 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.');
}
console.log('Seeding complete.');
}
try {
seed();
} catch (e) {
console.error('Seeding failed:');
console.error(e);
process.exit(1);
} finally {
db.close();
}