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