import * as noteService from '../services/note.service.js'; import { logNoteCreated, logNoteUpdated, logNoteDeleted } from '../services/audit.service.js'; /** * Get all notes * GET /api/notes?search=query&companyId=xxx&projectId=xxx&todoId=xxx&contactId=xxx */ export const getAllNotes = async (req, res, next) => { try { const { search, companyId, projectId, todoId, contactId } = req.query; const filters = { searchTerm: search, companyId, projectId, todoId, contactId, }; const notes = await noteService.getAllNotes(filters); res.status(200).json({ success: true, count: notes.length, data: notes, }); } catch (error) { next(error); } }; /** * Get note by ID * GET /api/notes/:noteId */ export const getNoteById = async (req, res, next) => { try { const { noteId } = req.params; const note = await noteService.getNoteById(noteId); res.status(200).json({ success: true, data: note, }); } catch (error) { next(error); } }; /** * Create new note * POST /api/notes * Body: { title, content, companyId, projectId, todoId, contactId } */ export const createNote = async (req, res, next) => { try { const userId = req.userId; const data = req.body; const note = await noteService.createNote(userId, data); // Log audit event await logNoteCreated(userId, note.id, note.content, req.ip, req.headers['user-agent']); res.status(201).json({ success: true, data: note, message: 'Poznámka bola vytvorená', }); } catch (error) { next(error); } }; /** * Update note * PATCH /api/notes/:noteId * Body: { title, content, companyId, projectId, todoId, contactId } */ export const updateNote = async (req, res, next) => { try { const userId = req.userId; const { noteId } = req.params; const data = req.body; // Get old note for audit const oldNote = await noteService.getNoteById(noteId); const note = await noteService.updateNote(noteId, data); // Log audit event await logNoteUpdated(userId, noteId, oldNote.content, note.content, req.ip, req.headers['user-agent']); res.status(200).json({ success: true, data: note, message: 'Poznámka bola aktualizovaná', }); } catch (error) { next(error); } }; /** * Delete note * DELETE /api/notes/:noteId */ export const deleteNote = async (req, res, next) => { try { const userId = req.userId; const { noteId } = req.params; // Get note for audit before deletion const note = await noteService.getNoteById(noteId); const result = await noteService.deleteNote(noteId); // Log audit event await logNoteDeleted(userId, noteId, note.content, req.ip, req.headers['user-agent']); res.status(200).json({ success: true, message: result.message, }); } catch (error) { next(error); } }; /** * Get upcoming reminders for current user * GET /api/notes/my-reminders */ export const getMyReminders = async (req, res, next) => { try { const userId = req.userId; const reminders = await noteService.getUpcomingRemindersForUser(userId); res.status(200).json({ success: true, count: reminders.length, data: reminders, }); } catch (error) { next(error); } }; /** * Mark reminder as sent * POST /api/notes/:noteId/mark-reminder-sent */ export const markReminderSent = async (req, res, next) => { try { const { noteId } = req.params; const updated = await noteService.markReminderAsSent(noteId); res.status(200).json({ success: true, data: updated, message: 'Reminder označený ako odoslaný', }); } catch (error) { next(error); } }; /** * Search notes with company and project info * GET /api/notes/search?q=searchTerm */ export const searchNotes = async (req, res, next) => { try { const { q } = req.query; const results = await noteService.searchNotes(q); res.status(200).json({ success: true, count: results.length, data: results, }); } catch (error) { next(error); } };