const express = require('express'); const router = express.Router(); const { getDb } = require('../db'); router.get('/', (req, res) => { const db = getDb(); const automations = db.prepare('SELECT * FROM automations ORDER BY created_at DESC').all(); const templates = [ { id: 'client_welcome', name: 'Client Welcome', description: 'Send welcome email when new client is created', icon: 'mail' }, { id: 'task_reminder', name: 'Task Reminder', description: 'Send reminder when task is overdue', icon: 'bell' }, { id: 'invoice_followup', name: 'Invoice Follow-up', description: 'Send follow-up when invoice is overdue', icon: 'dollar-sign' }, { id: 'project_complete', name: 'Project Complete', description: 'Notify when project is completed', icon: 'check-circle' } ]; res.render('automations', { automations, templates }); }); router.post('/create', (req, res) => { const db = getDb(); const { name, trigger_type, actions } = req.body; db.prepare('INSERT INTO automations (name, trigger_type, actions, status, created_at) VALUES (?, ?, ?, "active", CURRENT_TIMESTAMP)').run(name, trigger_type, JSON.stringify(actions)); res.redirect('/automations'); }); router.post('/:id/toggle', (req, res) => { const db = getDb(); const automation = db.prepare('SELECT status FROM automations WHERE id = ?').get(req.params.id); const newStatus = automation.status === 'active' ? 'paused' : 'active'; db.prepare('UPDATE automations SET status = ? WHERE id = ?').run(newStatus, req.params.id); res.json({ success: true, status: newStatus }); }); module.exports = router;