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

Commit 91bb5cc

Browse files
authored
refactor(create-article): remove old ids arg in GQ workflow (#406)
* refactor(create-article): remove old ids arg in GQ workflow * refactor(create-article): fix article_tags tests * refactor(create-article): fix article_tags tests edge case * refactor(create-article): fix article_tags tests edge case
1 parent d466c75 commit 91bb5cc

File tree

16 files changed

+118
-73
lines changed

16 files changed

+118
-73
lines changed

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
430430

431431
defp add_rich_text_attrs(attrs), do: attrs
432432

433-
# except Job, other article will just pass, should use set_article_tags function instead
434-
# defp exec_update_tags(_, _tags_ids), do: {:ok, :pass}
435-
436433
defp update_viewed_user_list(%{meta: nil} = article, user_id) do
437434
new_ids = Enum.uniq([user_id] ++ @default_article_meta.viewed_user_ids)
438435
meta = @default_article_meta |> Map.merge(%{viewed_user_ids: new_ids})

lib/groupher_server/cms/delegates/article_tag.ex

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
7272
end
7373

7474
# check if the tag to be set is in same community & thread
75-
defp is_article_tag_in_some_thread?(article_tags, filter) do
75+
defp is_article_tag_in_some_thread?(article_tag_ids, filter) do
7676
with {:ok, paged_article_tags} <- paged_article_tags(filter) do
7777
domain_tags_ids = Enum.map(paged_article_tags.entries, &to_string(&1.id))
78-
cur_tags_ids = Enum.map(article_tags, &to_string(&1.id))
78+
article_tag_ids = Enum.map(article_tag_ids, &to_string(&1))
7979

80-
Enum.all?(cur_tags_ids, &Enum.member?(domain_tags_ids, &1))
80+
Enum.all?(article_tag_ids, &Enum.member?(domain_tags_ids, &1))
8181
end
8282
end
8383

@@ -86,15 +86,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
8686
8787
used for create article with article_tags in args
8888
"""
89-
def set_article_tags(%Community{id: cid}, thread, article, %{article_tags: article_tags}) do
89+
def set_article_tags(%Community{id: cid}, thread, article, %{article_tags: article_tag_ids}) do
9090
check_filter = %{page: 1, size: 100, community_id: cid, thread: thread}
9191

92-
with true <- is_article_tag_in_some_thread?(article_tags, check_filter) do
93-
Enum.each(article_tags, fn article_tag ->
94-
set_article_tag(thread, article, article_tag.id)
95-
end)
96-
97-
{:ok, :pass}
92+
with true <- is_article_tag_in_some_thread?(article_tag_ids, check_filter) do
93+
Enum.each(article_tag_ids, &set_article_tag(thread, article, &1)) |> done
9894
else
9995
false -> raise_error(:invalid_domain_tag, "tag not in same community & thread")
10096
end

lib/groupher_server_web/schema/Helper/metrics.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ defmodule GroupherServerWeb.Schema.Helper.Metrics do
2727
pagination_fields()
2828
end
2929

30-
input_object :ids do
31-
field(:id, :id)
32-
end
33-
3430
input_object :common_paged_filter do
3531
pagination_args()
3632
field(:sort, :inserted_sort_enum, default_value: :desc_inserted)

lib/groupher_server_web/schema/cms/mutations/blog.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Blog do
1515
arg(:community_id, non_null(:id))
1616
arg(:link_addr, :string)
1717
arg(:thread, :thread, default_value: :blog)
18-
arg(:article_tags, list_of(:ids))
18+
arg(:article_tags, list_of(:id))
1919

2020
middleware(M.Authorize, :login)
2121
middleware(M.PublishThrottle)
@@ -34,7 +34,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Blog do
3434

3535
arg(:company, :string)
3636
arg(:company_link, :string)
37-
arg(:article_tags, list_of(:ids))
37+
arg(:article_tags, list_of(:id))
3838

3939
# ...
4040

lib/groupher_server_web/schema/cms/mutations/job.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Job do
2121
arg(:copy_right, :string)
2222

2323
arg(:thread, :thread, default_value: :job)
24-
arg(:article_tags, list_of(:ids))
24+
arg(:article_tags, list_of(:id))
2525

2626
middleware(M.Authorize, :login)
2727
middleware(M.PublishThrottle)
@@ -42,7 +42,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Job do
4242

4343
arg(:company, :string)
4444
arg(:company_link, :string)
45-
arg(:article_tags, list_of(:ids))
45+
arg(:article_tags, list_of(:id))
4646

4747
# ...
4848

lib/groupher_server_web/schema/cms/mutations/post.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Post do
1717
arg(:copy_right, :string)
1818
arg(:community_id, non_null(:id))
1919
arg(:thread, :thread, default_value: :post)
20-
arg(:article_tags, list_of(:ids))
20+
arg(:article_tags, list_of(:id))
2121

2222
middleware(M.Authorize, :login)
2323
# middleware(M.PublishThrottle)
@@ -34,7 +34,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Post do
3434
arg(:digest, :string)
3535
arg(:copy_right, :string)
3636
arg(:link_addr, :string)
37-
arg(:article_tags, list_of(:ids))
37+
arg(:article_tags, list_of(:id))
3838

3939
middleware(M.Authorize, :login)
4040
middleware(M.PassportLoader, source: :post)

lib/groupher_server_web/schema/cms/mutations/repo.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Repo do
3131

3232
arg(:community_id, non_null(:id))
3333
arg(:thread, :thread, default_value: :repo)
34-
arg(:article_tags, list_of(:ids))
34+
arg(:article_tags, list_of(:id))
3535

3636
middleware(M.Authorize, :login)
3737
middleware(M.PublishThrottle)

test/groupher_server/cms/article_tags/blog_tag_test.exs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ defmodule GroupherServer.Test.CMS.ArticleTag.BlogTag do
1212
article_tag_attrs = mock_attrs(:article_tag)
1313
article_tag_attrs2 = mock_attrs(:article_tag)
1414

15-
post_attrs = mock_attrs(:blog)
15+
blog_attrs = mock_attrs(:blog)
1616

17-
{:ok, ~m(user community blog post_attrs article_tag_attrs article_tag_attrs2)a}
17+
{:ok, ~m(user community blog blog_attrs article_tag_attrs article_tag_attrs2)a}
1818
end
1919

2020
describe "[blog tag CURD]" do
@@ -80,30 +80,28 @@ defmodule GroupherServer.Test.CMS.ArticleTag.BlogTag do
8080

8181
describe "[create/update blog with tags]" do
8282
test "can create blog with exsited article tags",
83-
~m(community user post_attrs article_tag_attrs article_tag_attrs2)a do
83+
~m(community user blog_attrs article_tag_attrs article_tag_attrs2)a do
8484
{:ok, article_tag} = CMS.create_article_tag(community, :blog, article_tag_attrs, user)
8585
{:ok, article_tag2} = CMS.create_article_tag(community, :blog, article_tag_attrs2, user)
8686

87-
post_with_tags =
88-
Map.merge(post_attrs, %{article_tags: [%{id: article_tag.id}, %{id: article_tag2.id}]})
87+
blog_with_tags = Map.merge(blog_attrs, %{article_tags: [article_tag.id, article_tag2.id]})
8988

90-
{:ok, created} = CMS.create_article(community, :blog, post_with_tags, user)
89+
{:ok, created} = CMS.create_article(community, :blog, blog_with_tags, user)
9190
{:ok, blog} = ORM.find(Blog, created.id, preload: :article_tags)
9291

9392
assert exist_in?(article_tag, blog.article_tags)
9493
assert exist_in?(article_tag2, blog.article_tags)
9594
end
9695

9796
test "can not create blog with other community's article tags",
98-
~m(community user post_attrs article_tag_attrs article_tag_attrs2)a do
97+
~m(community user blog_attrs article_tag_attrs article_tag_attrs2)a do
9998
{:ok, community2} = db_insert(:community)
10099
{:ok, article_tag} = CMS.create_article_tag(community, :blog, article_tag_attrs, user)
101100
{:ok, article_tag2} = CMS.create_article_tag(community2, :blog, article_tag_attrs2, user)
102101

103-
post_with_tags =
104-
Map.merge(post_attrs, %{article_tags: [%{id: article_tag.id}, %{id: article_tag2.id}]})
102+
blog_with_tags = Map.merge(blog_attrs, %{article_tags: [article_tag.id, article_tag2.id]})
105103

106-
{:error, reason} = CMS.create_article(community, :blog, post_with_tags, user)
104+
{:error, reason} = CMS.create_article(community, :blog, blog_with_tags, user)
107105
is_error?(reason, :invalid_domain_tag)
108106
end
109107
end

test/groupher_server/cms/article_tags/job_tag_test.exs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ defmodule GroupherServer.Test.CMS.ArticleTag.JobTag do
1212
article_tag_attrs = mock_attrs(:article_tag)
1313
article_tag_attrs2 = mock_attrs(:article_tag)
1414

15-
post_attrs = mock_attrs(:job)
15+
job_attrs = mock_attrs(:job)
1616

17-
{:ok, ~m(user community job post_attrs article_tag_attrs article_tag_attrs2)a}
17+
{:ok, ~m(user community job job_attrs article_tag_attrs article_tag_attrs2)a}
1818
end
1919

2020
describe "[job tag CURD]" do
@@ -80,30 +80,28 @@ defmodule GroupherServer.Test.CMS.ArticleTag.JobTag do
8080

8181
describe "[create/update job with tags]" do
8282
test "can create job with exsited article tags",
83-
~m(community user post_attrs article_tag_attrs article_tag_attrs2)a do
83+
~m(community user job_attrs article_tag_attrs article_tag_attrs2)a do
8484
{:ok, article_tag} = CMS.create_article_tag(community, :job, article_tag_attrs, user)
8585
{:ok, article_tag2} = CMS.create_article_tag(community, :job, article_tag_attrs2, user)
8686

87-
post_with_tags =
88-
Map.merge(post_attrs, %{article_tags: [%{id: article_tag.id}, %{id: article_tag2.id}]})
87+
job_with_tags = Map.merge(job_attrs, %{article_tags: [article_tag.id, article_tag2.id]})
8988

90-
{:ok, created} = CMS.create_article(community, :job, post_with_tags, user)
89+
{:ok, created} = CMS.create_article(community, :job, job_with_tags, user)
9190
{:ok, job} = ORM.find(Job, created.id, preload: :article_tags)
9291

9392
assert exist_in?(article_tag, job.article_tags)
9493
assert exist_in?(article_tag2, job.article_tags)
9594
end
9695

9796
test "can not create job with other community's article tags",
98-
~m(community user post_attrs article_tag_attrs article_tag_attrs2)a do
97+
~m(community user job_attrs article_tag_attrs article_tag_attrs2)a do
9998
{:ok, community2} = db_insert(:community)
10099
{:ok, article_tag} = CMS.create_article_tag(community, :job, article_tag_attrs, user)
101100
{:ok, article_tag2} = CMS.create_article_tag(community2, :job, article_tag_attrs2, user)
102101

103-
post_with_tags =
104-
Map.merge(post_attrs, %{article_tags: [%{id: article_tag.id}, %{id: article_tag2.id}]})
102+
job_with_tags = Map.merge(job_attrs, %{article_tags: [article_tag.id, article_tag2.id]})
105103

106-
{:error, reason} = CMS.create_article(community, :job, post_with_tags, user)
104+
{:error, reason} = CMS.create_article(community, :job, job_with_tags, user)
107105
is_error?(reason, :invalid_domain_tag)
108106
end
109107
end

test/groupher_server/cms/article_tags/post_tag_test.exs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ defmodule GroupherServer.Test.CMS.ArticleTag.PostTag do
8484
{:ok, article_tag} = CMS.create_article_tag(community, :post, article_tag_attrs, user)
8585
{:ok, article_tag2} = CMS.create_article_tag(community, :post, article_tag_attrs2, user)
8686

87-
post_with_tags =
88-
Map.merge(post_attrs, %{article_tags: [%{id: article_tag.id}, %{id: article_tag2.id}]})
87+
post_with_tags = Map.merge(post_attrs, %{article_tags: [article_tag.id, article_tag2.id]})
8988

9089
{:ok, created} = CMS.create_article(community, :post, post_with_tags, user)
9190
{:ok, post} = ORM.find(Post, created.id, preload: :article_tags)
@@ -100,8 +99,7 @@ defmodule GroupherServer.Test.CMS.ArticleTag.PostTag do
10099
{:ok, article_tag} = CMS.create_article_tag(community, :post, article_tag_attrs, user)
101100
{:ok, article_tag2} = CMS.create_article_tag(community2, :post, article_tag_attrs2, user)
102101

103-
post_with_tags =
104-
Map.merge(post_attrs, %{article_tags: [%{id: article_tag.id}, %{id: article_tag2.id}]})
102+
post_with_tags = Map.merge(post_attrs, %{article_tags: [article_tag.id, article_tag2.id]})
105103

106104
{:error, reason} = CMS.create_article(community, :post, post_with_tags, user)
107105
is_error?(reason, :invalid_domain_tag)

0 commit comments

Comments
 (0)