From 324d041d5c5cbcdf0083dcd802144a57443789f6 Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Wed, 1 Feb 2023 11:58:57 -0700 Subject: Fix upsert on discord id conflict --- lib/chessh/web/web.ex | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'lib/chessh/web') 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 }) }} -- cgit v1.2.3-70-g09d2