This commit is contained in:
Ondřej 2024-08-09 16:20:30 +02:00
parent 8b17c987d8
commit 3c7fc6a92e

View file

@ -128,8 +128,12 @@ defmodule ChoreTracker.Chores do
end end
@doc """ @doc """
Calculate next assignee for the chore. Calculate next assignee for the chore according to the following algorithm:
Assigns the person who last did the task the longest time ago.
- 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. Returns `nil` if chore has no assignees.
""" """
def next_chore_assignee(%Chore{} = chore) do 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) %ChoreLog{inserted_at: inserted_at} -> DateTime.to_unix(inserted_at)
end end
{time, length(assignee.assigned_chores)} {time, -length(assignee.assigned_chores)}
end, end,
:desc :asc
) )
|> List.first() |> List.first()
|> dbg
end end
end end