From 57f55b42acdb3ab7e68851daa2b07465573ef399 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Wed, 2 Aug 2023 22:38:45 +0800 Subject: [PATCH 1/2] feat(dashboard): add faq update --- .../cms/delegates/Seeds/posts.ex | 4 +- .../cms/models/community_dashboard.ex | 7 +- .../cms/models/embeds/dashboard_faq.ex | 30 +++++++++ .../cms/models/metrics/dashboard.ex | 8 +++ .../resolvers/cms_resolver.ex | 2 +- .../schema/cms/cms_metrics.ex | 5 ++ .../schema/cms/cms_types.ex | 2 + .../schema/cms/mutations/dashboard.ex | 15 +++++ lib/helper/orm.ex | 2 +- priv/mock/post_seeds.exs | 7 +- .../20230802135742_add_faqs_for_dashboard.exs | 9 +++ .../community/community_dashboard_test.exs | 67 +++++++++++++++++++ .../mutation/cms/dashboard_test.exs | 49 +++++++++++++- 13 files changed, 196 insertions(+), 11 deletions(-) create mode 100644 lib/groupher_server/cms/models/embeds/dashboard_faq.ex create mode 100644 priv/repo/migrations/20230802135742_add_faqs_for_dashboard.exs diff --git a/lib/groupher_server/cms/delegates/Seeds/posts.ex b/lib/groupher_server/cms/delegates/Seeds/posts.ex index cc4e5fb..51470ad 100644 --- a/lib/groupher_server/cms/delegates/Seeds/posts.ex +++ b/lib/groupher_server/cms/delegates/Seeds/posts.ex @@ -1,4 +1,4 @@ -defmodule GroupherServer.CMS.Delegate.Seeds.Posts do +defmodule GroupherServer.CMS.Delegate.Seeds.Articles do @moduledoc """ seeds data for init, should be called ONLY in new database, like migration """ @@ -19,7 +19,7 @@ defmodule GroupherServer.CMS.Delegate.Seeds.Posts do seed communities pragraming languages """ # type: city, pl, framework, ... - def seed_posts(community_slug, thread) do + def seed_articles(community_slug, thread) do with {:ok, community} <- ORM.find_by(Community, slug: community_slug), {:ok, user} <- ORM.find(User, 1) do attrs = mock_attrs(thread, %{community_id: community.id}) diff --git a/lib/groupher_server/cms/models/community_dashboard.ex b/lib/groupher_server/cms/models/community_dashboard.ex index 20c7a61..4157c3e 100644 --- a/lib/groupher_server/cms/models/community_dashboard.ex +++ b/lib/groupher_server/cms/models/community_dashboard.ex @@ -16,7 +16,7 @@ defmodule GroupherServer.CMS.Model.CommunityDashboard do } @required_fields ~w(community_id)a - @optional_fields ~w(base_info seo layout enable rss header_links footer_links social_links)a + @optional_fields ~w(base_info seo layout enable rss header_links footer_links social_links faqs)a def default() do %{ @@ -28,7 +28,8 @@ defmodule GroupherServer.CMS.Model.CommunityDashboard do name_alias: Embeds.DashboardNameAlias.default(), header_links: Embeds.DashboardHeaderLink.default(), footer_links: Embeds.DashboardFooterLink.default(), - social_links: Embeds.DashboardSocialLink.default() + social_links: Embeds.DashboardSocialLink.default(), + faqs: Embeds.DashboardFAQ.default() } end @@ -43,6 +44,7 @@ defmodule GroupherServer.CMS.Model.CommunityDashboard do embeds_many(:header_links, Embeds.DashboardHeaderLink, on_replace: :delete) embeds_many(:footer_links, Embeds.DashboardFooterLink, on_replace: :delete) embeds_many(:social_links, Embeds.DashboardSocialLink, on_replace: :delete) + embeds_many(:faqs, Embeds.DashboardFAQ, on_replace: :delete) # posts_block_list ... timestamps(type: :utc_datetime) @@ -61,6 +63,7 @@ defmodule GroupherServer.CMS.Model.CommunityDashboard do |> cast_embed(:header_links, with: &Embeds.DashboardHeaderLink.changeset/2) |> cast_embed(:footer_links, with: &Embeds.DashboardFooterLink.changeset/2) |> cast_embed(:social_links, with: &Embeds.DashboardSocialLink.changeset/2) + |> cast_embed(:faqs, with: &Embeds.DashboardFAQ.changeset/2) end @doc false diff --git a/lib/groupher_server/cms/models/embeds/dashboard_faq.ex b/lib/groupher_server/cms/models/embeds/dashboard_faq.ex new file mode 100644 index 0000000..dfb406d --- /dev/null +++ b/lib/groupher_server/cms/models/embeds/dashboard_faq.ex @@ -0,0 +1,30 @@ +defmodule GroupherServer.CMS.Model.Embeds.DashboardFAQ do + @moduledoc """ + general article comment meta info + """ + use Ecto.Schema + use Accessible + + import Ecto.Changeset + + import GroupherServerWeb.Schema.Helper.Fields, + only: [dashboard_cast_fields: 1, dashboard_default: 1, dashboard_fields: 1] + + @optional_fields dashboard_cast_fields(:faq_section) + + @doc "for test usage" + def default() do + [ + dashboard_default(:faq_section) + ] + end + + embedded_schema do + dashboard_fields(:faq_section) + end + + def changeset(struct, params) do + struct + |> cast(params, @optional_fields) + end +end diff --git a/lib/groupher_server/cms/models/metrics/dashboard.ex b/lib/groupher_server/cms/models/metrics/dashboard.ex index 90c283f..3d2c811 100644 --- a/lib/groupher_server/cms/models/metrics/dashboard.ex +++ b/lib/groupher_server/cms/models/metrics/dashboard.ex @@ -134,4 +134,12 @@ defmodule GroupherServer.CMS.Model.Metrics.Dashboard do [:link, :string, ""] ] end + + def macro_schema(:faq_section) do + [ + [:title, :string, ""], + [:body, :string, ""], + [:index, :integer, 0], + ] + end end diff --git a/lib/groupher_server_web/resolvers/cms_resolver.ex b/lib/groupher_server_web/resolvers/cms_resolver.ex index 9194a2e..d364de0 100644 --- a/lib/groupher_server_web/resolvers/cms_resolver.ex +++ b/lib/groupher_server_web/resolvers/cms_resolver.ex @@ -44,7 +44,7 @@ defmodule GroupherServerWeb.Resolvers.CMS do def update_dashboard(_root, %{dashboard_section: key, community: community} = args, _info) do dashboard_args = - case key in [:header_links, :footer_links, :name_alias, :social_links] do + case key in [:header_links, :footer_links, :name_alias, :social_links, :faqs] do true -> Map.drop(args, [:community, :dashboard_section]) |> Map.get(key) false -> Map.drop(args, [:community, :dashboard_section]) end diff --git a/lib/groupher_server_web/schema/cms/cms_metrics.ex b/lib/groupher_server_web/schema/cms/cms_metrics.ex index ac67cc8..8875340 100644 --- a/lib/groupher_server_web/schema/cms/cms_metrics.ex +++ b/lib/groupher_server_web/schema/cms/cms_metrics.ex @@ -49,6 +49,7 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do value(:header_links) value(:footer_links) value(:social_links) + value(:faqs) ## ... end @@ -301,4 +302,8 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do input_object :dashboard_social_link_map do dashboard_gq_fields(:social_link) end + + input_object :dashboard_faq_map do + dashboard_gq_fields(:faq_section) + end end diff --git a/lib/groupher_server_web/schema/cms/cms_types.ex b/lib/groupher_server_web/schema/cms/cms_types.ex index 6b45a38..24e3919 100644 --- a/lib/groupher_server_web/schema/cms/cms_types.ex +++ b/lib/groupher_server_web/schema/cms/cms_types.ex @@ -139,6 +139,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do object(:dasbboard_name_alias, do: dashboard_gq_fields(:name_alias)) object(:dasbboard_link, do: dashboard_gq_fields(:header_link)) object(:dasbboard_social_link, do: dashboard_gq_fields(:social_link)) + object(:dasbboard_faq_section, do: dashboard_gq_fields(:faq_section)) object :dashboard do field(:seo, :dasbboard_seo) @@ -150,6 +151,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do field(:header_links, list_of(:dasbboard_link)) field(:footer_links, list_of(:dasbboard_link)) field(:social_links, list_of(:dasbboard_social_link)) + field(:faqs, list_of(:dasbboard_faq_section)) end object :community do diff --git a/lib/groupher_server_web/schema/cms/mutations/dashboard.ex b/lib/groupher_server_web/schema/cms/mutations/dashboard.ex index 3cf0ae6..a2d11db 100644 --- a/lib/groupher_server_web/schema/cms/mutations/dashboard.ex +++ b/lib/groupher_server_web/schema/cms/mutations/dashboard.ex @@ -142,5 +142,20 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Dashboard do # middleware(M.PublishThrottle, interval: 3, hour_limit: 15, day_limit: 30) resolve(&R.CMS.update_dashboard/3) end + + @desc "update faqs in dashboard" + field :update_dashboard_faqs, :community do + arg(:community, non_null(:string)) + arg(:dashboard_section, :dashboard_section, default_value: :faqs) + + arg(:faqs, list_of(:dashboard_faq_map)) + + middleware(M.Authorize, :login) + # middleware(M.Passport, claim: "cms->community.update") + + # middleware(M.PublishThrottle) + # middleware(M.PublishThrottle, interval: 3, hour_limit: 15, day_limit: 30) + resolve(&R.CMS.update_dashboard/3) + end end end diff --git a/lib/helper/orm.ex b/lib/helper/orm.ex index ce80e23..fab14b2 100644 --- a/lib/helper/orm.ex +++ b/lib/helper/orm.ex @@ -323,7 +323,7 @@ defmodule Helper.ORM do end def update_dashboard(%CommunityDashboard{} = community_dashboard, field, args) - when field in [:header_links, :footer_links, :name_alias, :social_links] do + when field in [:header_links, :footer_links, :name_alias, :social_links, :faqs] do community_dashboard |> Ecto.Changeset.change(%{}) |> Ecto.Changeset.put_embed(field, args) diff --git a/priv/mock/post_seeds.exs b/priv/mock/post_seeds.exs index 341538b..6c91996 100644 --- a/priv/mock/post_seeds.exs +++ b/priv/mock/post_seeds.exs @@ -3,12 +3,11 @@ alias GroupherServer.CMS.Delegate.Seeds alias GroupherServer.CMS alias CMS.Constant -Enum.reduce(1..5, [], fn _, _ -> - {:ok, post} = Seeds.Posts.seed_posts("home", :post) +Enum.reduce(1..10, [], fn _, _ -> + # {:ok, post} = Seeds.Articles.seed_articles("home", :post) # {:ok, _} = CMS.set_post_cat(post, Constant.article_cat().feature) # {:ok, _} = CMS.set_post_state(post, Constant.article_state().todo) - {:ok, _} = CMS.set_post_cat(post, Constant.article_cat().bug) - {:ok, _} = CMS.set_post_state(post, Constant.article_state().done) + {:ok, doc} = Seeds.Articles.seed_articles("home", :doc) end) diff --git a/priv/repo/migrations/20230802135742_add_faqs_for_dashboard.exs b/priv/repo/migrations/20230802135742_add_faqs_for_dashboard.exs new file mode 100644 index 0000000..3386897 --- /dev/null +++ b/priv/repo/migrations/20230802135742_add_faqs_for_dashboard.exs @@ -0,0 +1,9 @@ +defmodule GroupherServer.Repo.Migrations.AddFaqsForDashboard do + use Ecto.Migration + + def change do + alter table(:community_dashboards) do + add(:faqs, {:array, :map}) + end + end +end diff --git a/test/groupher_server/cms/community/community_dashboard_test.exs b/test/groupher_server/cms/community/community_dashboard_test.exs index f24af37..752bbff 100644 --- a/test/groupher_server/cms/community/community_dashboard_test.exs +++ b/test/groupher_server/cms/community/community_dashboard_test.exs @@ -326,6 +326,73 @@ defmodule GroupherServer.Test.Community.CommunityDashboard do assert third.group_index == 3 end + @tag :wip + test "can update faqs in community dashboard", ~m(community_attrs)a do + {:ok, community} = CMS.create_community(community_attrs) + + {:ok, _} = + CMS.update_dashboard(community.slug, :faqs, [ + %{ + title: "xx is yy ?", + index: 0, + body: "this is body" + } + ]) + + {:ok, find_community} = ORM.find(Community, community.id, preload: :dashboard) + + first = find_community.dashboard.faqs |> Enum.at(0) + + assert first.title == "xx is yy ?" + assert first.body == "this is body" + end + + @tag :wip + test "should overwirte all faqs in community dashboard everytime", + ~m(community_attrs)a do + {:ok, community} = CMS.create_community(community_attrs) + + {:ok, _} = + CMS.update_dashboard(community.slug, :faqs, [ + %{ + title: "xx is yy ?", + index: 0, + body: "this is body" + }, + %{ + title: "xx is yy 2 ?", + index: 1, + body: "this is body 2" + } + ]) + + {:ok, find_community} = ORM.find(Community, community.id, preload: :dashboard) + + assert find_community.dashboard.faqs |> length == 2 + + first = find_community.dashboard.faqs |> Enum.at(0) + second = find_community.dashboard.faqs |> Enum.at(1) + + assert first.title == "xx is yy ?" + assert second.title == "xx is yy 2 ?" + + {:ok, _} = + CMS.update_dashboard(community.slug, :faqs, [ + %{ + title: "xx is zz ?", + index: 0, + body: "this is body" + } + ]) + + {:ok, find_community} = ORM.find(Community, community.id, preload: :dashboard) + assert find_community.dashboard.faqs |> length == 1 + + third = find_community.dashboard.faqs |> Enum.at(0) + assert third.title == "xx is zz ?" + assert third.body == "this is body" + end + test "can update social links in community dashboard", ~m(community_attrs)a do {:ok, community} = CMS.create_community(community_attrs) diff --git a/test/groupher_server_web/mutation/cms/dashboard_test.exs b/test/groupher_server_web/mutation/cms/dashboard_test.exs index de523d0..3d8d6fa 100644 --- a/test/groupher_server_web/mutation/cms/dashboard_test.exs +++ b/test/groupher_server_web/mutation/cms/dashboard_test.exs @@ -351,7 +351,6 @@ defmodule GroupherServer.Test.Mutation.CMS.Dashboard do } } """ - test "update community dashboard social links info", ~m(community)a do rule_conn = simu_conn(:user, cms: %{"community.update" => true}) @@ -382,5 +381,53 @@ defmodule GroupherServer.Test.Mutation.CMS.Dashboard do assert link.type == "twitter" assert link.link == "link" end + + @update_faqs_query """ + mutation($community: String!, $faqs: [dashboardFaqMap]) { + updateDashboardFaqs(community: $community, faqs: $faqs) { + id + title + dashboard { + faqs { + title + body + index + } + } + } + } + """ + @tag :wip + test "update community dashboard faqs info", ~m(community)a do + rule_conn = simu_conn(:user, cms: %{"community.update" => true}) + + variables = %{ + community: community.slug, + faqs: [ + %{ + title: "title", + body: "body", + index: 0 + } + ] + } + + updated = + rule_conn + |> mutation_result( + @update_faqs_query, + variables, + "updateDashboardFaqs" + ) + + assert updated["dashboard"]["faqs"] |> List.first() |> Map.get("title") == "title" + + {:ok, found} = Community |> ORM.find(updated["id"], preload: :dashboard) + + faq = found.dashboard.faqs |> Enum.at(0) + + assert faq.title == "title" + assert faq.body == "body" + end end end From deeb23c071353ce722a9e5c55dbae8d18c13a2d7 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Wed, 2 Aug 2023 22:45:10 +0800 Subject: [PATCH 2/2] feat(dashboard): fmt --- lib/groupher_server/cms/models/metrics/dashboard.ex | 2 +- test/groupher_server/cms/community/community_dashboard_test.exs | 2 -- test/groupher_server_web/mutation/cms/dashboard_test.exs | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/groupher_server/cms/models/metrics/dashboard.ex b/lib/groupher_server/cms/models/metrics/dashboard.ex index 3d2c811..196a07b 100644 --- a/lib/groupher_server/cms/models/metrics/dashboard.ex +++ b/lib/groupher_server/cms/models/metrics/dashboard.ex @@ -139,7 +139,7 @@ defmodule GroupherServer.CMS.Model.Metrics.Dashboard do [ [:title, :string, ""], [:body, :string, ""], - [:index, :integer, 0], + [:index, :integer, 0] ] end end diff --git a/test/groupher_server/cms/community/community_dashboard_test.exs b/test/groupher_server/cms/community/community_dashboard_test.exs index 752bbff..a7e2401 100644 --- a/test/groupher_server/cms/community/community_dashboard_test.exs +++ b/test/groupher_server/cms/community/community_dashboard_test.exs @@ -326,7 +326,6 @@ defmodule GroupherServer.Test.Community.CommunityDashboard do assert third.group_index == 3 end - @tag :wip test "can update faqs in community dashboard", ~m(community_attrs)a do {:ok, community} = CMS.create_community(community_attrs) @@ -347,7 +346,6 @@ defmodule GroupherServer.Test.Community.CommunityDashboard do assert first.body == "this is body" end - @tag :wip test "should overwirte all faqs in community dashboard everytime", ~m(community_attrs)a do {:ok, community} = CMS.create_community(community_attrs) diff --git a/test/groupher_server_web/mutation/cms/dashboard_test.exs b/test/groupher_server_web/mutation/cms/dashboard_test.exs index 3d8d6fa..bc5a43a 100644 --- a/test/groupher_server_web/mutation/cms/dashboard_test.exs +++ b/test/groupher_server_web/mutation/cms/dashboard_test.exs @@ -397,7 +397,6 @@ defmodule GroupherServer.Test.Mutation.CMS.Dashboard do } } """ - @tag :wip test "update community dashboard faqs info", ~m(community)a do rule_conn = simu_conn(:user, cms: %{"community.update" => true})