added weekdays enum and week_start_at for user records

This commit is contained in:
Adam Piontek 2021-03-12 11:15:22 -05:00
parent 4fb0e2df7b
commit 4031640e1d
4 changed files with 11 additions and 0 deletions

View file

@ -3,6 +3,7 @@ defmodule Shift73k.Accounts.User do
import Ecto.Changeset import Ecto.Changeset
import EctoEnum import EctoEnum
alias Shift73k.EctoEnums.WeekdayEnum
alias Shift73k.Shifts.Templates.ShiftTemplate alias Shift73k.Shifts.Templates.ShiftTemplate
alias Shift73k.Shifts.Shift alias Shift73k.Shifts.Shift
@ -28,6 +29,7 @@ defmodule Shift73k.Accounts.User do
field(:confirmed_at, :naive_datetime) field(:confirmed_at, :naive_datetime)
field(:role, RolesEnum, default: :user) field(:role, RolesEnum, default: :user)
field(:week_start_at, WeekdayEnum, default: 1)
has_many(:shift_templates, ShiftTemplate) has_many(:shift_templates, ShiftTemplate)
belongs_to(:fave_shift_template, ShiftTemplate) belongs_to(:fave_shift_template, ShiftTemplate)

View file

@ -0,0 +1,7 @@
defmodule Shift73k.EctoEnums do
import EctoEnum
@weekdays [:mon, :tue, :wed, :thu, :fri, :sat, :sun] |> Enum.with_index(1)
defenum(WeekdayEnum, @weekdays)
end

View file

@ -8,6 +8,7 @@ defmodule Shift73k.Repo.Migrations.CreateUsersAuthTables do
add(:id, :binary_id, primary_key: true) add(:id, :binary_id, primary_key: true)
add(:email, :citext, null: false) add(:email, :citext, null: false)
add(:hashed_password, :string, null: false) add(:hashed_password, :string, null: false)
add(:week_start_at, :integer, null: false)
add(:confirmed_at, :naive_datetime) add(:confirmed_at, :naive_datetime)
timestamps() timestamps()
end end

View file

@ -66,6 +66,7 @@ mock_users =
email: e["email"], email: e["email"],
role: String.to_existing_atom(e["role"]), role: String.to_existing_atom(e["role"]),
hashed_password: Bcrypt.hash_pwd_salt(e["password"]), hashed_password: Bcrypt.hash_pwd_salt(e["password"]),
week_start_at: :rand.uniform(2),
inserted_at: add_dt, inserted_at: add_dt,
updated_at: add_dt, updated_at: add_dt,
confirmed_at: (e["confirmed_at"] && NaiveDateTime.add(add_dt, 300, :second)) || nil confirmed_at: (e["confirmed_at"] && NaiveDateTime.add(add_dt, 300, :second)) || nil