Separate artist badge awarding logic to new module

This commit is contained in:
Liam 2024-06-22 17:16:44 -04:00
parent f29fdae22f
commit 9b9a30621e
4 changed files with 87 additions and 26 deletions

View file

@ -9,8 +9,7 @@ defmodule Philomena.ArtistLinks do
alias Philomena.ArtistLinks.ArtistLink alias Philomena.ArtistLinks.ArtistLink
alias Philomena.ArtistLinks.AutomaticVerifier alias Philomena.ArtistLinks.AutomaticVerifier
alias Philomena.Badges.Badge alias Philomena.ArtistLinks.BadgeAwarder
alias Philomena.Badges.Award
alias Philomena.Tags.Tag alias Philomena.Tags.Tag
@doc """ @doc """
@ -77,32 +76,32 @@ defmodule Philomena.ArtistLinks do
|> Repo.update() |> Repo.update()
end end
def verify_artist_link(%ArtistLink{} = artist_link, user) do @doc """
artist_link_changeset = Transitions an artist_link to the verified state.
artist_link
|> ArtistLink.verify_changeset(user) ## Examples
iex> verify_artist_link(artist_link, verifying_user)
{:ok, %ArtistLink{}}
iex> verify_artist_link(artist_link, verifying_user)
:error
"""
def verify_artist_link(%ArtistLink{} = artist_link, verifying_user) do
artist_link_changeset = ArtistLink.verify_changeset(artist_link, verifying_user)
Multi.new() Multi.new()
|> Multi.update(:artist_link, artist_link_changeset) |> Multi.update(:artist_link, artist_link_changeset)
|> Multi.run(:add_award, fn repo, _changes -> |> Multi.run(:add_award, fn _repo, _changes -> BadgeAwarder.award_badge(artist_link) end)
now = DateTime.utc_now() |> DateTime.truncate(:second)
with badge when not is_nil(badge) <- repo.get_by(limit(Badge, 1), title: "Artist"),
nil <- repo.get_by(limit(Award, 1), badge_id: badge.id, user_id: artist_link.user_id) do
%Award{
badge_id: badge.id,
user_id: artist_link.user_id,
awarded_by_id: user.id,
awarded_on: now
}
|> Award.changeset(%{})
|> repo.insert()
else
_ ->
{:ok, nil}
end
end)
|> Repo.transaction() |> Repo.transaction()
|> case do
{:ok, %{artist_link: artist_link}} ->
{:ok, artist_link}
{:error, _operation, _value, _changes} ->
:error
end
end end
def reject_artist_link(%ArtistLink{} = artist_link) do def reject_artist_link(%ArtistLink{} = artist_link) do

View file

@ -0,0 +1,28 @@
defmodule Philomena.ArtistLinks.BadgeAwarder do
@moduledoc """
Handles awarding a badge to the user of an associated artist link.
"""
alias Philomena.Badges
@badge_title "Artist"
@doc """
Awards a badge to an artist with a verified link.
If the badge with the title `"Artist"` does not exist, no award will be created.
If the user already has an award with that badge title, no award will be created.
Returns `{:ok, award}`, `{:ok, nil}`, or `{:error, changeset}`. The return value is
suitable for use as the return value to an `Ecto.Multi.run/3` callback.
"""
def award_badge(artist_link) do
with badge when not is_nil(badge) <- Badges.get_badge_by_title(@badge_title),
award when is_nil(award) <- Badges.get_badge_award_for(badge, artist_link.user) do
Badges.create_badge_award(artist_link.user, artist_link.user)
else
_ ->
{:ok, nil}
end
end
end

View file

@ -38,6 +38,22 @@ defmodule Philomena.Badges do
""" """
def get_badge!(id), do: Repo.get!(Badge, id) def get_badge!(id), do: Repo.get!(Badge, id)
@doc """
Gets a single badge by its title.
Returns nil if the Badge does not exist.
## Examples
iex> get_badge_by_title("Artist")
%Badge{}
iex> get_badge_by_title("Nonexistent")
nil
"""
def get_badge_by_title(title), do: Repo.get_by(Badge, title: title)
@doc """ @doc """
Creates a badge. Creates a badge.
@ -162,6 +178,24 @@ defmodule Philomena.Badges do
""" """
def get_badge_award!(id), do: Repo.get!(Award, id) def get_badge_award!(id), do: Repo.get!(Award, id)
@doc """
Gets a the badge_award with the given badge type belonging to the user.
Raises nil if the Badge award does not exist.
## Examples
iex> get_badge_award_for(badge, user)
%Award{}
iex> get_badge_award_for(badge, user)
nil
"""
def get_badge_award_for(badge, user) do
Repo.get_by(Award, badge_id: badge.id, user_id: user.id)
end
@doc """ @doc """
Creates a badge_award. Creates a badge_award.

View file

@ -13,12 +13,12 @@ defmodule PhilomenaWeb.Admin.ArtistLink.VerificationController do
preload: [:user] preload: [:user]
def create(conn, _params) do def create(conn, _params) do
{:ok, result} = {:ok, artist_link} =
ArtistLinks.verify_artist_link(conn.assigns.artist_link, conn.assigns.current_user) ArtistLinks.verify_artist_link(conn.assigns.artist_link, conn.assigns.current_user)
conn conn
|> put_flash(:info, "Artist link successfully verified.") |> put_flash(:info, "Artist link successfully verified.")
|> moderation_log(details: &log_details/2, data: result.artist_link) |> moderation_log(details: &log_details/2, data: artist_link)
|> redirect(to: ~p"/admin/artist_links") |> redirect(to: ~p"/admin/artist_links")
end end