Accounts context can now set and clear a user's favorite shift template

This commit is contained in:
Adam Piontek 2021-03-11 15:41:30 -05:00
parent ea0f8b86cd
commit 19b9bbfb0d
3 changed files with 28 additions and 9 deletions

View file

@ -7,6 +7,7 @@ defmodule Shift73k.Accounts do
alias Shift73k.Repo
alias Shift73k.Accounts.{User, UserToken, UserNotifier}
alias Shift73kWeb.UserAuth
alias Shift73k.ShiftTemplates.ShiftTemplate
## Database getters
@ -162,13 +163,6 @@ defmodule Shift73k.Accounts do
User.update_changeset(user, attrs, hash_password: true)
end
# @doc """
# Returns an `%Ecto.Changeset{}` for tracking singer_name updates.
# """
# def change_singer_name_update(%User{} = user, attrs \\ %{}) do
# User.update_singer_name_changeset(user, attrs)
# end
@doc """
Updates the user given with attributes given
"""
@ -449,4 +443,29 @@ defmodule Shift73k.Accounts do
"""
def delete_user(nil), do: {:error, false}
def delete_user(%User{} = user), do: Repo.delete(user)
## Favorite Shift Template
@doc """
Sets a shift template as a user's favorite
"""
def set_user_fave_shift_template(%User{id: user_id}, %ShiftTemplate{
id: shift_template_id,
user_id: user_id
}) do
User
|> where(id: ^user_id)
|> Repo.update_all(set: [fave_shift_template_id: shift_template_id])
end
def set_user_fave_shift_template(_, _), do: {0, nil}
@doc """
Clears a user's favorite shift template
"""
def unset_user_fave_shift_template(%User{id: user_id}) do
User
|> where(id: ^user_id)
|> Repo.update_all(set: [fave_shift_template_id: nil])
end
end

View file

@ -29,7 +29,7 @@ defmodule Shift73k.Accounts.User do
field(:role, RolesEnum, default: :user)
has_many(:shift_templates, ShiftTemplate)
belongs_to(:default_shift_template, ShiftTemplate)
belongs_to(:fave_shift_template, ShiftTemplate)
timestamps()
end

View file

@ -3,7 +3,7 @@ defmodule Shift73k.Repo.Migrations.AddUserDefaultShiftColumn do
def change do
alter table(:users) do
add(:default_shift_template_id, references(:shift_templates, type: :binary_id, on_delete: :delete_all))
add(:fave_shift_template_id, references(:shift_templates, type: :binary_id, on_delete: :delete_all))
end
end
end