Skip to content

Commit

Permalink
Added sort option to get due today
Browse files Browse the repository at this point in the history
  • Loading branch information
ab-elhaddad committed Oct 12, 2023
1 parent b1aa196 commit b929943
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/controllers/tasks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/models/tasks.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Task[]> {
static async getDueToday(u_id: number, sort?: string): Promise<Task[]> {
const { startDate, endDate } = getDates();
const tasks: Task[] = await prisma.task.findMany({
where: {
Expand All @@ -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;
Expand Down

0 comments on commit b929943

Please sign in to comment.