@@ -8,7 +8,7 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
88
99 alias MastaniServer.Accounts
1010 alias Helper . { Guardian , ORM , QueryBuilder , RadarSearch }
11- alias MastaniServer.Accounts . { Achievement , GithubUser , User }
11+ alias MastaniServer.Accounts . { Achievement , GithubUser , User , Social }
1212 alias MastaniServer . { CMS , Repo }
1313
1414 alias Ecto.Multi
@@ -23,25 +23,11 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
2323 user
2424 |> Ecto.Changeset . change ( attrs )
2525
26- changeset =
27- cond do
28- Map . has_key? ( attrs , :education_backgrounds ) ->
29- changeset
30- |> Ecto.Changeset . put_embed ( :education_backgrounds , attrs . education_backgrounds )
31-
32- Map . has_key? ( attrs , :work_backgrounds ) ->
33- changeset
34- |> Ecto.Changeset . put_embed ( :work_backgrounds , attrs . work_backgrounds )
35-
36- Map . has_key? ( attrs , :other_embeds ) ->
37- changeset
38- |> Ecto.Changeset . put_embed ( :other_embeds , attrs . other_embeds )
39-
40- true ->
41- changeset
42- end
43-
44- changeset |> User . update_changeset ( attrs ) |> Repo . update ( )
26+ changeset
27+ |> update_social_ifneed ( user , attrs )
28+ |> embed_background_ifneed ( changeset )
29+ |> User . update_changeset ( attrs )
30+ |> Repo . update ( )
4531 end
4632
4733 @ doc """
@@ -153,6 +139,9 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
153139 |> Multi . run ( :create_profile , fn _ , % { create_user: user } ->
154140 create_profile ( user , github_profile , :github )
155141 end )
142+ |> Multi . run ( :update_profile_social , fn _ , % { create_user: user } ->
143+ update_profile_social ( user , github_profile , :github )
144+ end )
156145 |> Multi . run ( :init_achievement , fn _ , % { create_user: user } ->
157146 Achievement |> ORM . upsert_by ( [ user_id: user . id ] , % { user_id: user . id } )
158147 end )
@@ -171,6 +160,9 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
171160 defp register_github_result ( { :error , :create_profile , _result , _steps } ) ,
172161 do: { :error , "Accounts create_profile internal error" }
173162
163+ defp register_github_result ( { :error , :update_profile_social , _result , _steps } ) ,
164+ do: { :error , "Accounts update_profile_social error" }
165+
174166 defp gen_token ( % User { } = user ) do
175167 with { :ok , token , _info } <- Guardian . jwt_encode ( user ) do
176168 { :ok , % { token: token , user: user } }
@@ -181,7 +173,6 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
181173 attrs = % {
182174 login: String . downcase ( profile [ "login" ] ) ,
183175 nickname: profile [ "login" ] ,
184- github: "https://github.com/#{ profile [ "login" ] } " ,
185176 avatar: profile [ "avatar_url" ] ,
186177 bio: profile [ "bio" ] ,
187178 location: profile [ "location" ] ,
@@ -203,6 +194,14 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
203194 Repo . insert ( changeset )
204195 end
205196
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+
206205 defp create_profile ( user , github_profile , :github ) do
207206 # attrs = github_user |> Map.merge(%{github_id: github_user.id, user_id: 1}) |> Map.delete(:id)
208207 attrs =
@@ -215,4 +214,31 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
215214 |> GithubUser . changeset ( attrs )
216215 |> Repo . insert ( )
217216 end
217+
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
224+ Social |> ORM . upsert_by ( [ user_id: user . id ] , attrs )
225+ changeset
226+ end
227+
228+ defp update_social_ifneed ( changeset , _user , _attrs ) , do: changeset
229+
230+ defp embed_background_ifneed ( % Ecto.Changeset { } = changeset , attrs ) do
231+ cond do
232+ Map . has_key? ( attrs , :education_backgrounds ) ->
233+ changeset
234+ |> Ecto.Changeset . put_embed ( :education_backgrounds , attrs . education_backgrounds )
235+
236+ Map . has_key? ( attrs , :work_backgrounds ) ->
237+ changeset
238+ |> Ecto.Changeset . put_embed ( :work_backgrounds , attrs . work_backgrounds )
239+
240+ true ->
241+ changeset
242+ end
243+ end
218244end
0 commit comments