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

Commit 76c20a5

Browse files
committed
Merge branch 'enhance-create' into dev
2 parents dd45e58 + e13900f commit 76c20a5

File tree

11 files changed

+207
-42
lines changed

11 files changed

+207
-42
lines changed

lib/helper/error_code.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ defmodule Helper.ErrorCode do
2323
def ecode(:already_exsit), do: @default_base + 7
2424
def ecode(:update_fails), do: @default_base + 8
2525
def ecode(:delete_fails), do: @default_base + 9
26+
def ecode(:create_fails), do: @default_base + 10
2627
# throttle
2728
def ecode(:throttle_inverval), do: @throttle_base + 1
2829
def ecode(:throttle_hour), do: @throttle_base + 2

lib/mastani_server/cms/delegates/article_curd.ex

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
defmodule MastaniServer.CMS.Delegate.ArticleCURD do
2+
@moduledoc """
3+
CURD operation on post/job/video ...
4+
"""
25
import Ecto.Query, warn: false
36
import MastaniServer.CMS.Utils.Matcher
47
import Helper.Utils, only: [done: 1]
8+
import Helper.ErrorCode
59
import ShortMaps
610

711
alias MastaniServer.Accounts.User
8-
alias MastaniServer.{Repo, CMS, Statistics}
9-
alias MastaniServer.CMS.Delegate.ArticleOperation
12+
alias MastaniServer.{CMS, Repo, Statistics}
13+
14+
alias CMS.Delegate.ArticleOperation
1015
alias Helper.{ORM, QueryBuilder}
1116

12-
alias CMS.{Author, Community}
17+
alias CMS.{Author, Community, Tag}
18+
alias Ecto.Multi
1319

1420
@doc """
1521
get paged post / job ...
@@ -66,15 +72,62 @@ defmodule MastaniServer.CMS.Delegate.ArticleCURD do
6672
def create_content(%Community{id: community_id}, thread, attrs, %User{id: user_id}) do
6773
with {:ok, author} <- ensure_author_exists(%User{id: user_id}),
6874
{:ok, action} <- match_action(thread, :community),
69-
{:ok, community} <- ORM.find(Community, community_id),
70-
{:ok, content} <-
71-
action.target
72-
|> struct()
73-
|> action.target.changeset(attrs)
74-
|> Ecto.Changeset.put_change(:author_id, author.id)
75-
|> Repo.insert() do
76-
Statistics.log_publish_action(%User{id: user_id})
77-
ArticleOperation.set_community(community, thread, content.id)
75+
{:ok, community} <- ORM.find(Community, community_id) do
76+
Multi.new()
77+
|> Multi.run(:add_content_author, fn _ ->
78+
action.target
79+
|> struct()
80+
|> action.target.changeset(attrs)
81+
|> Ecto.Changeset.put_change(:author_id, author.id)
82+
|> Repo.insert()
83+
end)
84+
|> Multi.run(:set_community, fn %{add_content_author: content} ->
85+
ArticleOperation.set_community(community, thread, content.id)
86+
end)
87+
|> Multi.run(:set_tag, fn %{add_content_author: content} ->
88+
case attrs |> Map.has_key?(:tags) do
89+
true -> set_tags(community, thread, content.id, attrs.tags)
90+
false -> {:ok, "pass"}
91+
end
92+
end)
93+
|> Multi.run(:log_action, fn _ ->
94+
Statistics.log_publish_action(%User{id: user_id})
95+
end)
96+
|> Repo.transaction()
97+
|> create_content_result()
98+
end
99+
end
100+
101+
defp create_content_result({:ok, %{add_content_author: result}}), do: {:ok, result}
102+
103+
defp create_content_result({:error, :add_content_author, _result, _steps}) do
104+
{:error, [message: "assign author", code: ecode(:create_fails)]}
105+
end
106+
107+
defp create_content_result({:error, :set_community, _result, _steps}) do
108+
{:error, [message: "set community", code: ecode(:create_fails)]}
109+
end
110+
111+
defp create_content_result({:error, :set_tag, result, _steps}) do
112+
{:error, result}
113+
end
114+
115+
defp create_content_result({:error, :log_action, result, _steps}) do
116+
{:error, [message: "log action", code: ecode(:create_fails)]}
117+
end
118+
119+
# if empty just pass
120+
defp set_tags(community, thread, content_id, []), do: {:ok, "pass"}
121+
122+
defp set_tags(community, thread, content_id, tags) do
123+
try do
124+
Enum.each(tags, fn tag ->
125+
{:ok, _} = ArticleOperation.set_tag(community, thread, %Tag{id: tag.id}, content_id)
126+
end)
127+
128+
{:ok, "psss"}
129+
rescue
130+
_ -> {:error, [message: "set tag", code: ecode(:create_fails)]}
78131
end
79132
end
80133

lib/mastani_server/cms/delegates/article_operation.ex

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,22 @@ defmodule MastaniServer.CMS.Delegate.ArticleOperation do
5353
with {:ok, action} <- match_action(thread, :tag),
5454
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
5555
{:ok, tag} <- ORM.find(action.reactor, tag_id) do
56-
case tag_in_community_thread?(%Community{id: communitId}, thread, tag) do
57-
true ->
58-
content
59-
|> Ecto.Changeset.change()
60-
|> Ecto.Changeset.put_assoc(:tags, content.tags ++ [tag])
61-
|> Repo.update()
56+
content
57+
|> Ecto.Changeset.change()
58+
|> Ecto.Changeset.put_assoc(:tags, content.tags ++ [tag])
59+
|> Repo.update()
60+
61+
# NOTE: this should be control by Middleware
62+
# case tag_in_community_thread?(%Community{id: communitId}, thread, tag) do
63+
# true ->
64+
# content
65+
# |> Ecto.Changeset.change()
66+
# |> Ecto.Changeset.put_assoc(:tags, content.tags ++ [tag])
67+
# |> Repo.update()
6268

63-
_ ->
64-
{:error, message: "Tag,Community,Thread not match", code: ecode(:custom)}
65-
end
69+
# _ ->
70+
# {:error, message: "Tag,Community,Thread not match", code: ecode(:custom)}
71+
# end
6672
end
6773
end
6874

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ defmodule MastaniServerWeb.Schema.CMS.Mutations.Job do
1919
arg(:link_source, :string)
2020

2121
arg(:thread, :cms_thread, default_value: :job)
22+
arg(:tags, list_of(:ids))
2223

2324
middleware(M.Authorize, :login)
2425
resolve(&R.CMS.create_content/3)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ defmodule MastaniServerWeb.Schema.CMS.Mutations.Post do
1414
arg(:link_addr, :string)
1515
arg(:community_id, non_null(:id))
1616
arg(:thread, :cms_thread, default_value: :post)
17+
arg(:tags, list_of(:ids))
1718

1819
middleware(M.Authorize, :login)
1920
middleware(M.PublishThrottle)

lib/mastani_server_web/schema/utils/common_types.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
defmodule MastaniServerWeb.Schema.Utils.CommonTypes do
2+
@moduledoc """
3+
common types might be used in all context
4+
"""
25
import MastaniServerWeb.Schema.Utils.Helper
36

47
use Absinthe.Schema.Notation
@@ -13,6 +16,10 @@ defmodule MastaniServerWeb.Schema.Utils.CommonTypes do
1316
field(:done, :boolean)
1417
end
1518

19+
input_object :ids do
20+
field(:id, :id)
21+
end
22+
1623
input_object :common_paged_filter do
1724
pagination_args()
1825
end

test/mastani_server/accounts/accounts_test.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ defmodule MastaniServer.Test.Accounts do
8989
describe "[github login]" do
9090
alias Accounts.{GithubUser, User}
9191

92-
@tag :wip
9392
test "register a valid github user with non-exist in db" do
9493
assert {:error, _} =
9594
ORM.find_by(GithubUser, github_id: to_string(@valid_github_profile["id"]))

test/mastani_server/cms/job_test.exs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ defmodule MastaniServer.Test.Job do
2424
assert found.title == job.title
2525
end
2626

27+
test "can create job with exsited tags", ~m(user community job_attrs)a do
28+
{:ok, tag1} = db_insert(:tag)
29+
{:ok, tag2} = db_insert(:tag)
30+
31+
job_with_tags = Map.merge(job_attrs, %{tags: [%{id: tag1.id}, %{id: tag2.id}]})
32+
33+
{:ok, created} = CMS.create_content(community, :job, job_with_tags, user)
34+
{:ok, found} = ORM.find(CMS.Job, created.id, preload: :tags)
35+
36+
assert found.tags |> Enum.any?(&(&1.id == tag1.id))
37+
assert found.tags |> Enum.any?(&(&1.id == tag2.id))
38+
end
39+
40+
test "create job with invalid tags fails", ~m(user community job_attrs)a do
41+
{:ok, tag1} = db_insert(:tag)
42+
{:ok, tag2} = db_insert(:tag)
43+
44+
job_with_tags =
45+
Map.merge(job_attrs, %{tags: [%{id: tag1.id}, %{id: tag2.id}, %{id: non_exsit_id()}]})
46+
47+
{:error, _} = CMS.create_content(community, :job, job_with_tags, user)
48+
{:error, _} = ORM.find_by(CMS.Job, %{title: job_attrs.title})
49+
end
50+
2751
test "create job with an exsit community fails", ~m(user)a do
2852
invalid_attrs = mock_attrs(:job, %{community_id: non_exsit_id()})
2953
ivalid_community = %Community{id: non_exsit_id()}

test/mastani_server/cms/post_test.exs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ defmodule MastaniServer.Test.Post do
2525
assert post.title == post_attrs.title
2626
end
2727

28+
test "can create post with exsited tags", ~m(user community post_attrs)a do
29+
{:ok, tag1} = db_insert(:tag)
30+
{:ok, tag2} = db_insert(:tag)
31+
32+
post_with_tags = Map.merge(post_attrs, %{tags: [%{id: tag1.id}, %{id: tag2.id}]})
33+
34+
{:ok, created} = CMS.create_content(community, :post, post_with_tags, user)
35+
{:ok, found} = ORM.find(CMS.Post, created.id, preload: :tags)
36+
37+
assert found.tags |> Enum.any?(&(&1.id == tag1.id))
38+
assert found.tags |> Enum.any?(&(&1.id == tag2.id))
39+
end
40+
41+
test "create post with invalid tags fails", ~m(user community post_attrs)a do
42+
{:ok, tag1} = db_insert(:tag)
43+
{:ok, tag2} = db_insert(:tag)
44+
45+
post_with_tags =
46+
Map.merge(post_attrs, %{tags: [%{id: tag1.id}, %{id: tag2.id}, %{id: non_exsit_id()}]})
47+
48+
{:error, _} = CMS.create_content(community, :post, post_with_tags, user)
49+
{:error, _} = ORM.find_by(CMS.Post, %{title: post_attrs.title})
50+
end
51+
2852
test "add user to cms authors, if the user is not exsit in cms authors",
2953
~m(user community post_attrs)a do
3054
assert {:error, _} = ORM.find_by(Author, user_id: user.id)

test/mastani_server_web/mutation/cms/job_test.exs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ defmodule MastaniServer.Test.Mutation.Job do
2525
$communityId: ID!,
2626
$company: String!,
2727
$companyLogo: String!
28-
$location: String!
28+
$location: String!,
29+
$tags: [Ids]
2930
){
3031
createJob(
3132
title: $title,
@@ -35,7 +36,8 @@ defmodule MastaniServer.Test.Mutation.Job do
3536
communityId: $communityId,
3637
company: $company,
3738
companyLogo: $companyLogo,
38-
location: $location
39+
location: $location,
40+
tags: $tags
3941
) {
4042
id
4143
title
@@ -64,6 +66,29 @@ defmodule MastaniServer.Test.Mutation.Job do
6466
assert created["id"] == to_string(found.id)
6567
end
6668

69+
test "can create job with tags" do
70+
{:ok, user} = db_insert(:user)
71+
user_conn = simu_conn(:user, user)
72+
73+
{:ok, community} = db_insert(:community)
74+
{:ok, tag1} = db_insert(:tag)
75+
{:ok, tag2} = db_insert(:tag)
76+
77+
job_attr = mock_attrs(:job)
78+
79+
variables =
80+
job_attr
81+
|> Map.merge(%{communityId: community.id})
82+
|> Map.merge(%{companyLogo: job_attr.company_logo})
83+
|> Map.merge(%{tags: [%{id: tag1.id}, %{id: tag2.id}]})
84+
85+
created = user_conn |> mutation_result(@create_job_query, variables, "createJob")
86+
{:ok, job} = ORM.find(CMS.Job, created["id"], preload: :tags)
87+
88+
assert job.tags |> Enum.any?(&(&1.id == tag1.id))
89+
assert job.tags |> Enum.any?(&(&1.id == tag2.id))
90+
end
91+
6792
@query """
6893
mutation($id: ID!, $title: String, $body: String){
6994
updateJob(id: $id, title: $title, body: $body) {
@@ -183,16 +208,17 @@ defmodule MastaniServer.Test.Mutation.Job do
183208
# assert tag.id in assoc_tags
184209
end
185210

186-
test "auth user set a invalid tag to job fails", ~m(job)a do
187-
{:ok, community} = db_insert(:community)
188-
{:ok, tag} = db_insert(:tag, %{thread: "job"})
211+
# TODO: should fix in auth layer
212+
# test "auth user set a other community's tag to job fails", ~m(job)a do
213+
# {:ok, community} = db_insert(:community)
214+
# {:ok, tag} = db_insert(:tag, %{thread: "job"})
189215

190-
passport_rules = %{community.title => %{"job.tag.set" => true}}
191-
rule_conn = simu_conn(:user, cms: passport_rules)
216+
# passport_rules = %{community.title => %{"job.tag.set" => true}}
217+
# rule_conn = simu_conn(:user, cms: passport_rules)
192218

193-
variables = %{thread: "JOB", id: job.id, tagId: tag.id, communityId: community.id}
194-
assert rule_conn |> mutation_get_error?(@set_tag_query, variables, ecode(:custom))
195-
end
219+
# variables = %{thread: "JOB", id: job.id, tagId: tag.id, communityId: community.id}
220+
# assert rule_conn |> mutation_get_error?(@set_tag_query, variables, ecode(:custom))
221+
# end
196222

197223
test "can set multi tag to a job", ~m(job)a do
198224
{:ok, community} = db_insert(:community)

0 commit comments

Comments
 (0)