diff options
author | Logan Hunt <logan.hunt@usu.edu> | 2023-02-01 11:58:57 -0700 |
---|---|---|
committer | Logan Hunt <logan.hunt@usu.edu> | 2023-02-01 11:58:57 -0700 |
commit | 324d041d5c5cbcdf0083dcd802144a57443789f6 (patch) | |
tree | 2b61b9f55a062a3c8e241a1d923bf01fa7cf5cf5 /lib/chessh/web | |
parent | 4edaae9343680181c4ca67feb971b51b0783c628 (diff) | |
download | chessh-324d041d5c5cbcdf0083dcd802144a57443789f6.tar.gz chessh-324d041d5c5cbcdf0083dcd802144a57443789f6.zip |
Fix upsert on discord id conflict
Diffstat (limited to 'lib/chessh/web')
-rw-r--r-- | lib/chessh/web/web.ex | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/lib/chessh/web/web.ex b/lib/chessh/web/web.ex index 2b465b2..8cddd32 100644 --- a/lib/chessh/web/web.ex +++ b/lib/chessh/web/web.ex @@ -270,19 +270,22 @@ defmodule Chessh.Web.Endpoint do %{"username" => username, "discriminator" => discriminator, "id" => discord_id} = Jason.decode!(String.Chars.to_string(user_details)) - %Player{id: id} = - Repo.insert!( - %Player{discord_id: discord_id, username: username <> "#" <> discriminator}, - on_conflict: [set: [discord_id: discord_id]], - conflict_target: :discord_id - ) + case Repo.get_by(Player, discord_id: discord_id) do + nil -> %Player{discord_id: discord_id} + player -> player + end + |> Player.discord_changeset(%{ + username: username <> "#" <> discriminator, + discord_id: discord_id + }) + |> Repo.insert_or_update() {200, %{ success: true, jwt: Token.generate_and_sign!(%{ - "uid" => id + "uid" => Repo.get_by(Player, discord_id: discord_id).id }) }} |