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
@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