diff --git a/src/controllers/note.controller.js b/src/controllers/note.controller.js index c6d9dc0..fbc5575 100644 --- a/src/controllers/note.controller.js +++ b/src/controllers/note.controller.js @@ -167,3 +167,23 @@ export const markReminderSent = async (req, res, next) => { 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); + } +}; diff --git a/src/routes/note.routes.js b/src/routes/note.routes.js index a4b2892..950d12a 100644 --- a/src/routes/note.routes.js +++ b/src/routes/note.routes.js @@ -18,6 +18,9 @@ router.use(authenticate); // Get all notes (admin only - returns all notes system-wide) router.get('/', requireAdmin, noteController.getAllNotes); +// Search notes with company/project info (must be before /:noteId to avoid route conflict) +router.get('/search', noteController.searchNotes); + // Get my reminders (must be before /:noteId to avoid route conflict) router.get('/my-reminders', noteController.getMyReminders); diff --git a/src/services/note.service.js b/src/services/note.service.js index a8fbeb4..1592ddd 100644 --- a/src/services/note.service.js +++ b/src/services/note.service.js @@ -332,3 +332,32 @@ export const getNotesByContactId = async (contactId) => { .where(eq(notes.contactId, contactId)) .orderBy(desc(notes.createdAt)); }; + +/** + * Search notes with company and project info + * Returns notes matching searchTerm with linked company/project names + */ +export const searchNotes = async (searchTerm) => { + if (!searchTerm || searchTerm.trim().length < 2) { + return []; + } + + const results = await db + .select({ + id: notes.id, + content: notes.content, + companyId: notes.companyId, + projectId: notes.projectId, + companyName: companies.name, + projectName: projects.name, + createdAt: notes.createdAt, + }) + .from(notes) + .leftJoin(companies, eq(notes.companyId, companies.id)) + .leftJoin(projects, eq(notes.projectId, projects.id)) + .where(ilike(notes.content, `%${searchTerm}%`)) + .orderBy(desc(notes.createdAt)) + .limit(20); + + return results; +};