@@ -23,7 +23,6 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
2323 user
2424 |> Ecto.Changeset . change ( attrs )
2525
26- # TODO: update social if need
2726 changeset
2827 |> update_social_ifneed ( user , attrs )
2928 |> embed_background_ifneed ( changeset )
@@ -140,6 +139,9 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
140139 |> Multi . run ( :create_profile , fn _ , % { create_user: user } ->
141140 create_profile ( user , github_profile , :github )
142141 end )
142+ |> Multi . run ( :update_profile_social , fn _ , % { create_user: user } ->
143+ update_profile_social ( user , github_profile , :github )
144+ end )
143145 |> Multi . run ( :init_achievement , fn _ , % { create_user: user } ->
144146 Achievement |> ORM . upsert_by ( [ user_id: user . id ] , % { user_id: user . id } )
145147 end )
@@ -158,6 +160,9 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
158160 defp register_github_result ( { :error , :create_profile , _result , _steps } ) ,
159161 do: { :error , "Accounts create_profile internal error" }
160162
163+ defp register_github_result ( { :error , :update_profile_social , _result , _steps } ) ,
164+ do: { :error , "Accounts update_profile_social error" }
165+
161166 defp gen_token ( % User { } = user ) do
162167 with { :ok , token , _info } <- Guardian . jwt_encode ( user ) do
163168 { :ok , % { token: token , user: user } }
@@ -168,7 +173,6 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
168173 attrs = % {
169174 login: String . downcase ( profile [ "login" ] ) ,
170175 nickname: profile [ "login" ] ,
171- github: "https://github.com/#{ profile [ "login" ] } " ,
172176 avatar: profile [ "avatar_url" ] ,
173177 bio: profile [ "bio" ] ,
174178 location: profile [ "location" ] ,
@@ -190,6 +194,14 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
190194 Repo . insert ( changeset )
191195 end
192196
197+ def update_profile_social ( user , profile , :github ) do
198+ update_social_ifneed ( user , % {
199+ social: % {
200+ github: "https://github.com/#{ profile [ "login" ] } "
201+ }
202+ } )
203+ end
204+
193205 defp create_profile ( user , github_profile , :github ) do
194206 # attrs = github_user |> Map.merge(%{github_id: github_user.id, user_id: 1}) |> Map.delete(:id)
195207 attrs =
@@ -203,7 +215,12 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
203215 |> Repo . insert ( )
204216 end
205217
206- defp update_social_ifneed ( changeset , user , % { social: attrs } ) do
218+ defp update_social_ifneed ( % User { } = user , % { social: attrs } ) do
219+ attrs = Map . merge ( % { user_id: user . id } , attrs )
220+ Social |> ORM . upsert_by ( [ user_id: user . id ] , attrs )
221+ end
222+
223+ defp update_social_ifneed ( changeset , % User { } = user , % { social: attrs } ) do
207224 Social |> ORM . upsert_by ( [ user_id: user . id ] , attrs )
208225 changeset
209226 end
0 commit comments