feat: add due date reminders functionality with cron jobs and test scripts
This commit is contained in:
72
test-due-date-reminders.mjs
Normal file
72
test-due-date-reminders.mjs
Normal file
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Test script to simulate due date reminders
|
||||
* Creates a test project due in 3 days and runs the reminder script
|
||||
*/
|
||||
|
||||
import db from "./src/lib/db.js";
|
||||
import { addDays, format } from "date-fns";
|
||||
|
||||
async function createTestProject() {
|
||||
try {
|
||||
console.log("🧪 Creating test project due in 3 days...");
|
||||
|
||||
// Create a test contract first
|
||||
const contractResult = db.prepare(`
|
||||
INSERT INTO contracts (contract_number, contract_name, customer, date_signed)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`).run('TEST-001', 'Test Contract', 'Test Customer', new Date().toISOString());
|
||||
|
||||
const contractId = contractResult.lastInsertRowid;
|
||||
|
||||
// Create a test project due in 3 days
|
||||
const dueDate = addDays(new Date(), 3);
|
||||
const projectResult = db.prepare(`
|
||||
INSERT INTO projects (
|
||||
contract_id, project_name, project_number, address,
|
||||
finish_date, project_status
|
||||
) VALUES (?, ?, ?, ?, ?, ?)
|
||||
`).run(
|
||||
contractId,
|
||||
'Test Project - Due Soon',
|
||||
'1/TEST-001',
|
||||
'Test Address 123',
|
||||
dueDate.toISOString(),
|
||||
'in_progress_design'
|
||||
);
|
||||
|
||||
console.log(`✅ Created test project due on ${format(dueDate, 'yyyy-MM-dd')}`);
|
||||
console.log("🔄 Running due date reminders script...");
|
||||
|
||||
// Run the reminders script
|
||||
const { execSync } = await import('child_process');
|
||||
execSync('node send-due-date-reminders.mjs', { stdio: 'inherit' });
|
||||
|
||||
// Check if notifications were created
|
||||
const notifications = db.prepare(`
|
||||
SELECT * FROM notifications
|
||||
WHERE type = 'due_date_reminder'
|
||||
ORDER BY created_at DESC
|
||||
LIMIT 5
|
||||
`).all();
|
||||
|
||||
console.log(`📢 Found ${notifications.length} due date reminder notifications:`);
|
||||
notifications.forEach(notif => {
|
||||
console.log(` - ${notif.title}: ${notif.message.substring(0, 100)}...`);
|
||||
});
|
||||
|
||||
// Clean up test data
|
||||
console.log("🧹 Cleaning up test data...");
|
||||
db.prepare('DELETE FROM projects WHERE project_name = ?').run('Test Project - Due Soon');
|
||||
db.prepare('DELETE FROM contracts WHERE contract_number = ?').run('TEST-001');
|
||||
db.prepare('DELETE FROM notifications WHERE type = ? AND title LIKE ?').run('due_date_reminder', 'Projekt kończy się za%');
|
||||
|
||||
console.log("✅ Test completed successfully!");
|
||||
|
||||
} catch (error) {
|
||||
console.error("❌ Test failed:", error);
|
||||
}
|
||||
}
|
||||
|
||||
createTestProject();
|
||||
Reference in New Issue
Block a user