diff --git a/lib/shift73k/accounts/user.ex b/lib/shift73k/accounts/user.ex index d9b8b838..9f92c860 100644 --- a/lib/shift73k/accounts/user.ex +++ b/lib/shift73k/accounts/user.ex @@ -2,8 +2,8 @@ defmodule Shift73k.Accounts.User do use Ecto.Schema import Ecto.Changeset import EctoEnum + import Shift73k.Util.Dt, only: [weekdays: 0] - alias Shift73k.EctoEnums.WeekdayEnum alias Shift73k.Shifts.Templates.ShiftTemplate alias Shift73k.Shifts.Shift @@ -29,7 +29,7 @@ defmodule Shift73k.Accounts.User do field(:confirmed_at, :naive_datetime) field(:role, RolesEnum, default: :user) - field(:week_start_at, WeekdayEnum, default: 1) + field(:week_start_at, Ecto.Enum, values: weekdays(), default: :monday) has_many(:shift_templates, ShiftTemplate) belongs_to(:fave_shift_template, ShiftTemplate) diff --git a/lib/shift73k/ecto_enums.ex b/lib/shift73k/ecto_enums.ex deleted file mode 100644 index 18e08674..00000000 --- a/lib/shift73k/ecto_enums.ex +++ /dev/null @@ -1,8 +0,0 @@ -defmodule Shift73k.EctoEnums do - import EctoEnum - - @weekdays [:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday] - |> Enum.with_index(1) - - defenum(WeekdayEnum, @weekdays) -end diff --git a/lib/shift73k/util/dt.ex b/lib/shift73k/util/dt.ex index 6f5dfe46..8838b47c 100644 --- a/lib/shift73k/util/dt.ex +++ b/lib/shift73k/util/dt.ex @@ -2,5 +2,9 @@ defmodule Shift73k.Util.Dt do @app_vars Application.get_env(:shift73k, :app_global_vars, time_zone: "America/New_York") @app_time_zone @app_vars[:time_zone] + @weekdays [:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday] + def app_time_zone, do: @app_time_zone + + def weekdays, do: @weekdays end diff --git a/lib/shift73k_web/live/user/settings/week_start.ex b/lib/shift73k_web/live/user/settings/week_start.ex index 7759cd74..756aa7ea 100644 --- a/lib/shift73k_web/live/user/settings/week_start.ex +++ b/lib/shift73k_web/live/user/settings/week_start.ex @@ -1,7 +1,7 @@ defmodule Shift73kWeb.UserLive.Settings.WeekStart do use Shift73kWeb, :live_component + import Shift73k.Util.Dt, only: [weekdays: 0] - alias Shift73k.EctoEnums.WeekdayEnum alias Shift73k.Accounts @impl true @@ -14,13 +14,12 @@ defmodule Shift73kWeb.UserLive.Settings.WeekStart do end def week_start_options do - {week_start_at, _} = WeekdayEnum.__enum_map__() |> hd() - week_start = Date.beginning_of_week(Date.utc_today(), week_start_at) + week_start = Date.beginning_of_week(Date.utc_today(), hd(weekdays())) week_start |> Date.range(Date.add(week_start, 6)) |> Enum.map(&Calendar.strftime(&1, "%A")) - |> Enum.zip(Keyword.keys(WeekdayEnum.__enum_map__())) + |> Enum.zip(weekdays()) end @impl true diff --git a/priv/repo/migrations/20200913000515_create_users_auth_tables.exs b/priv/repo/migrations/20200913000515_create_users_auth_tables.exs index 7dbac93d..e57e7d7d 100644 --- a/priv/repo/migrations/20200913000515_create_users_auth_tables.exs +++ b/priv/repo/migrations/20200913000515_create_users_auth_tables.exs @@ -8,7 +8,7 @@ defmodule Shift73k.Repo.Migrations.CreateUsersAuthTables do add(:id, :binary_id, primary_key: true) add(:email, :citext, null: false) add(:hashed_password, :string, null: false) - add(:week_start_at, :integer, null: false) + add(:week_start_at, :string, null: false) add(:confirmed_at, :naive_datetime) timestamps() end diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index d99cbe5e..4a9c2de9 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -55,6 +55,10 @@ extra_mock_users = ~s([ {"email":"kat@73k.us","password":"katkatA1","role":"manager","inserted_at":"2018-12-14T01:06:01Z","confirmed_at":true} ]) +# for random week_start_at values +[head | tail] = Shift73k.Util.Dt.weekdays() +week_starts = [head | Enum.drop(tail, 4)] + mock_users = extra_mock_users |> Jason.decode!() @@ -66,7 +70,7 @@ mock_users = email: e["email"], role: String.to_existing_atom(e["role"]), hashed_password: Bcrypt.hash_pwd_salt(e["password"]), - week_start_at: :rand.uniform(2), + week_start_at: Enum.at(week_starts, Enum.random(0..2)), inserted_at: add_dt, updated_at: add_dt, confirmed_at: (e["confirmed_at"] && NaiveDateTime.add(add_dt, 300, :second)) || nil