Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit a261f47

Browse files
committed
refactor(profile): rm unnecessary social fields & emhance update
1 parent fdae9d6 commit a261f47

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

lib/mastani_server/accounts/delegates/profile.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,15 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
221221
end
222222

223223
defp update_social_ifneed(changeset, %User{} = user, %{social: attrs}) do
224-
Social |> ORM.upsert_by([user_id: user.id], attrs)
225-
Ecto.Changeset.put_change(changeset, :social, nil)
224+
case ORM.find_by(Social, user_id: user.id) do
225+
{:ok, _} ->
226+
ORM.update_by(Social, [user_id: user.id], attrs)
227+
Ecto.Changeset.put_change(changeset, :social, nil)
228+
229+
{:error, _} ->
230+
ORM.create(Social, attrs)
231+
changeset
232+
end
226233
end
227234

228235
defp update_social_ifneed(changeset, _user, _attrs), do: changeset

lib/mastani_server/accounts/user.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule MastaniServer.Accounts.User do
44

55
use Ecto.Schema
66

7-
import MastaniServerWeb.Schema.Utils.Helper
7+
# import MastaniServerWeb.Schema.Utils.Helper
88
import Ecto.Changeset
99

1010
alias MastaniServer.Accounts.{
@@ -44,7 +44,6 @@ defmodule MastaniServer.Accounts.User do
4444
embeds_many(:work_backgrounds, WorkBackground)
4545

4646
has_one(:social, Social)
47-
social_fields()
4847

4948
has_one(:achievement, Achievement)
5049
has_one(:github_profile, GithubUser)

lib/mastani_server_web/schema/account/account_types.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ defmodule MastaniServerWeb.Schema.Account.Types do
2727
field(:geo_city, :string)
2828

2929
field(:views, :integer)
30-
31-
social_fields()
3230
field(:social, :social_map, resolve: dataloader(Accounts, :social))
3331

3432
field(:inserted_at, :datetime)

test/mastani_server/accounts/accounts_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ defmodule MastaniServer.Test.Accounts do
3434
{:ok, user} = db_insert(:user)
3535

3636
attrs = %{
37+
location: "new name",
3738
social: %{
3839
github: "github addr",
3940
weibo: "weibo addr"
@@ -42,6 +43,8 @@ defmodule MastaniServer.Test.Accounts do
4243

4344
{:ok, updated} = Accounts.update_profile(%User{id: user.id}, attrs)
4445

46+
assert updated.location == "new name"
47+
4548
assert updated.social.github == attrs.social.github
4649
assert updated.social.weibo == attrs.social.weibo
4750
end

test/mastani_server_web/mutation/accounts/account_test.exs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ defmodule MastaniServer.Test.Mutation.Account.Basic do
3131
nickname
3232
social {
3333
zhihu
34+
github
35+
twitter
3436
}
3537
education_backgrounds {
3638
school
@@ -48,17 +50,24 @@ defmodule MastaniServer.Test.Mutation.Account.Basic do
4850

4951
variables = %{
5052
profile: %{
51-
nickname: "new nickname"
53+
nickname: "new nickname",
54+
bio: "everyday is the opportunity you don't get back, so live life to the fullest",
55+
location: "china |> chengDu (成都).",
56+
sex: "dude"
5257
},
5358
social: %{
54-
zhihu: "xieyiming-75"
59+
zhihu: "xieyiming-75",
60+
github: "mydearxym",
61+
twitter: "fe2"
5562
}
5663
}
5764

5865
updated = ownd_conn |> mutation_result(@update_query, variables, "updateProfile")
5966

6067
assert updated["nickname"] == "new nickname"
6168
assert updated["social"]["zhihu"] == variables.social.zhihu
69+
assert updated["social"]["github"] == variables.social.github
70+
assert updated["social"]["twitter"] == variables.social.twitter
6271
end
6372

6473
test "user can update it's own backgrounds", ~m(user)a do

0 commit comments

Comments
 (0)