diff --git a/src/controllers/tasks.controller.ts b/src/controllers/tasks.controller.ts index 045bf36..6130d10 100644 --- a/src/controllers/tasks.controller.ts +++ b/src/controllers/tasks.controller.ts @@ -52,7 +52,8 @@ export const getAll = async (req: Request, res: Response, next: NextFunction) => export const getDueToday = async (req: Request, res: Response, next: NextFunction) => { try { const { id: u_id } = res.locals.user; - const dueTodayTasks = await Tasks.getDueToday(u_id); + const sort = req.query.sort?.toString(); + const dueTodayTasks = await Tasks.getDueToday(u_id, sort); res.json({ message: 'Tasks returned successfully.', tasks: dueTodayTasks diff --git a/src/models/tasks.model.ts b/src/models/tasks.model.ts index 6920b8f..1202c62 100644 --- a/src/models/tasks.model.ts +++ b/src/models/tasks.model.ts @@ -87,7 +87,7 @@ class Tasks { } /** Gets all the tasks assigned to the passed user id and has due date today. */ - static async getDueToday(u_id: number): Promise { + static async getDueToday(u_id: number, sort?: string): Promise { const { startDate, endDate } = getDates(); const tasks: Task[] = await prisma.task.findMany({ where: { @@ -96,6 +96,11 @@ class Tasks { gte: startDate, lte: endDate } + }, + orderBy: { + t_due_date: sort === 'due' ? 'asc' : undefined, + t_created_at: sort === 'created' ? 'desc' : undefined, + t_priority: sort === 'priority' || sort === undefined ? 'desc' : undefined // Sort by priority date by default } }); return tasks;