import db from "../db.js"; export function getNotesByProjectId(project_id) { return db .prepare( ` SELECT n.*, u.name as created_by_name, u.username as created_by_username FROM notes n LEFT JOIN users u ON n.created_by = u.id WHERE n.project_id = ? ORDER BY n.note_date DESC ` ) .all(project_id); } export function addNoteToProject(project_id, note, created_by = null, is_system = false) { db.prepare( ` INSERT INTO notes (project_id, note, created_by, is_system, note_date) VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP) ` ).run(project_id, note, created_by, is_system ? 1 : 0); } export function getNotesByTaskId(task_id) { return db .prepare( ` SELECT n.*, u.name as created_by_name, u.username as created_by_username FROM notes n LEFT JOIN users u ON n.created_by = u.id WHERE n.task_id = ? ORDER BY n.note_date DESC ` ) .all(task_id); } export function addNoteToTask( task_id, note, is_system = false, created_by = null ) { db.prepare( `INSERT INTO notes (task_id, note, is_system, created_by, note_date) VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)` ).run(task_id, note, is_system ? 1 : 0, created_by); } export function deleteNote(note_id) { db.prepare(`DELETE FROM notes WHERE note_id = ?`).run(note_id); } // Get all notes with user information (for admin/reporting purposes) export function getAllNotesWithUsers() { return db .prepare( ` SELECT n.*, u.name as created_by_name, u.username as created_by_username, p.project_name, COALESCE(pt.custom_task_name, t.name) as task_name FROM notes n LEFT JOIN users u ON n.created_by = u.id LEFT JOIN projects p ON n.project_id = p.project_id LEFT JOIN project_tasks pt ON n.task_id = pt.id LEFT JOIN tasks t ON pt.task_template_id = t.task_id ORDER BY n.note_date DESC ` ) .all(); } // Get notes created by a specific user export function getNotesByCreator(userId) { return db .prepare( ` SELECT n.*, u.name as created_by_name, u.username as created_by_username, p.project_name, COALESCE(pt.custom_task_name, t.name) as task_name FROM notes n LEFT JOIN users u ON n.created_by = u.id LEFT JOIN projects p ON n.project_id = p.project_id LEFT JOIN project_tasks pt ON n.task_id = pt.id LEFT JOIN tasks t ON pt.task_template_id = t.task_id WHERE n.created_by = ? ORDER BY n.note_date DESC ` ) .all(userId); }