CSV export implemented, a few other fixes

This commit is contained in:
Adam Piontek 2021-03-23 11:54:49 -04:00
commit 56b72f8038
10 changed files with 245 additions and 21 deletions
lib/shift73k

View file

@ -21,7 +21,7 @@ defmodule Shift73k.Shifts do
Repo.all(Shift)
end
defp query_shifts_by_user(user_id) do
def query_shifts_by_user(user_id) do
from(s in Shift)
|> where([s], s.user_id == ^user_id)
end
@ -53,6 +53,25 @@ defmodule Shift73k.Shifts do
|> Repo.all()
end
def query_user_shift_dates(user_id) do
query_shifts_by_user(user_id)
|> select([s], s.date)
end
def get_min_user_shift_date(user_id) do
query_user_shift_dates(user_id)
|> order_by([s], asc: s.date)
|> limit([s], 1)
|> Repo.one()
end
def get_max_user_shift_date(user_id) do
query_user_shift_dates(user_id)
|> order_by([s], desc: s.date)
|> limit([s], 1)
|> Repo.one()
end
@doc """
Gets a single shift.
@ -146,4 +165,20 @@ defmodule Shift73k.Shifts do
def change_shift(%Shift{} = shift, attrs \\ %{}) do
Shift.changeset(shift, attrs)
end
###
# UTILS
def shift_length(%{time_end: time_end, time_start: time_start}) do
time_end
|> Time.diff(time_start)
|> Integer.floor_div(60)
|> shift_length()
end
def shift_length(len_min) when is_integer(len_min) and len_min >= 0, do: len_min
def shift_length(len_min) when is_integer(len_min) and len_min < 0, do: 1440 + len_min
def shift_length(time_end, time_start) do
shift_length(%{time_end: time_end, time_start: time_start})
end
end

View file

@ -2,6 +2,7 @@ defmodule Shift73k.Shifts.Templates.ShiftTemplate do
use Ecto.Schema
import Ecto.Changeset
alias Shift73k.Shifts
alias Shift73k.Shifts.Templates.ShiftTemplate
@app_vars Application.get_env(:shift73k, :app_global_vars, time_zone: "America/New_York")
@ -45,7 +46,7 @@ defmodule Shift73k.Shifts.Templates.ShiftTemplate do
|> validate_length(:subject, count: :codepoints, max: 280)
|> validate_length(:location, count: :codepoints, max: 280)
|> validate_change(:time_end, fn :time_end, time_end ->
shift_length = shift_length(time_end, time_start_from_attrs(attrs))
shift_length = Shifts.shift_length(time_end, time_start_from_attrs(attrs))
cond do
shift_length == 0 ->
@ -67,20 +68,6 @@ defmodule Shift73k.Shifts.Templates.ShiftTemplate do
defp time_start_from_attrs(%{time_start: time_start}), do: time_start
defp time_start_from_attrs(_), do: nil
def shift_length(%ShiftTemplate{time_end: time_end, time_start: time_start}) do
time_end
|> Time.diff(time_start)
|> Integer.floor_div(60)
|> shift_length()
end
def shift_length(len_min) when is_integer(len_min) and len_min >= 0, do: len_min
def shift_length(len_min) when is_integer(len_min) and len_min < 0, do: 1440 + len_min
def shift_length(time_end, time_start) do
shift_length(%ShiftTemplate{time_end: time_end, time_start: time_start})
end
# Get shift attrs from shift template
def attrs(%ShiftTemplate{} = shift_template) do
shift_template