From 0b93a1814adcd3180de57c63e639cc37dd5c7650 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 29 Jul 2024 11:26:11 -0400 Subject: [PATCH] Fix wrong awarding user for automatic artist badge award --- lib/philomena/artist_links.ex | 2 +- lib/philomena/artist_links/badge_awarder.ex | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/philomena/artist_links.ex b/lib/philomena/artist_links.ex index 9a9de4a6..28f468a9 100644 --- a/lib/philomena/artist_links.ex +++ b/lib/philomena/artist_links.ex @@ -93,7 +93,7 @@ defmodule Philomena.ArtistLinks do Multi.new() |> Multi.update(:artist_link, artist_link_changeset) - |> Multi.run(:add_award, fn _repo, _changes -> BadgeAwarder.award_badge(artist_link) end) + |> Multi.run(:add_award, BadgeAwarder.award_callback(artist_link, verifying_user)) |> Repo.transaction() |> case do {:ok, %{artist_link: artist_link}} -> diff --git a/lib/philomena/artist_links/badge_awarder.ex b/lib/philomena/artist_links/badge_awarder.ex index ae231c74..275a5aee 100644 --- a/lib/philomena/artist_links/badge_awarder.ex +++ b/lib/philomena/artist_links/badge_awarder.ex @@ -16,13 +16,22 @@ defmodule Philomena.ArtistLinks.BadgeAwarder do 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 + def award_badge(artist_link, verifying_user) 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, %{badge_id: badge.id}) + Badges.create_badge_award(verifying_user, artist_link.user, %{badge_id: badge.id}) else _ -> {:ok, nil} end end + + @doc """ + Get a callback for issuing a badge award from within an `m:Ecto.Multi`. + """ + def award_callback(artist_link, verifying_user) do + fn _repo, _changes -> + award_badge(artist_link, verifying_user) + end + end end