From 3c7fc6a92e5f1521171b989e304aea157d19d6aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20N=C3=BDvlt?= Date: Fri, 9 Aug 2024 16:20:30 +0200 Subject: [PATCH] WIP --- lib/chore_tracker/chores.ex | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/chore_tracker/chores.ex b/lib/chore_tracker/chores.ex index 767b631..98a67ec 100644 --- a/lib/chore_tracker/chores.ex +++ b/lib/chore_tracker/chores.ex @@ -128,8 +128,12 @@ defmodule ChoreTracker.Chores do end @doc """ - Calculate next assignee for the chore. - Assigns the person who last did the task the longest time ago. + Calculate next assignee for the chore according to the following algorithm: + + - Select the person who did the task the longest time ago (or never). + - If there are multiple persons who did the taks same time ago (or never), + select the one with least assigned chores. + Returns `nil` if chore has no assignees. """ def next_chore_assignee(%Chore{} = chore) do @@ -144,10 +148,11 @@ defmodule ChoreTracker.Chores do %ChoreLog{inserted_at: inserted_at} -> DateTime.to_unix(inserted_at) end - {time, length(assignee.assigned_chores)} + {time, -length(assignee.assigned_chores)} end, - :desc + :asc ) |> List.first() + |> dbg end end