Skip to content

Commit

Permalink
Merge pull request #17 from groupher/faq-sections
Browse files Browse the repository at this point in the history
feat(dashboard): faq sections
  • Loading branch information
mydearxym authored Aug 4, 2023
2 parents 00ccaef + deeb23c commit a89515c
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/groupher_server/cms/delegates/Seeds/posts.ex
Original file line number Diff line number Diff line change
@@ -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
"""
Expand All @@ -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})
Expand Down
7 changes: 5 additions & 2 deletions lib/groupher_server/cms/models/community_dashboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
%{
Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions lib/groupher_server/cms/models/embeds/dashboard_faq.ex
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions lib/groupher_server/cms/models/metrics/dashboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/groupher_server_web/resolvers/cms_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/groupher_server_web/schema/cms/cms_metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do
value(:header_links)
value(:footer_links)
value(:social_links)
value(:faqs)
## ...
end

Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions lib/groupher_server_web/schema/cms/cms_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions lib/groupher_server_web/schema/cms/mutations/dashboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/helper/orm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions priv/mock/post_seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
@@ -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
65 changes: 65 additions & 0 deletions test/groupher_server/cms/community/community_dashboard_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,71 @@ defmodule GroupherServer.Test.Community.CommunityDashboard do
assert third.group_index == 3
end

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

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)

Expand Down
48 changes: 47 additions & 1 deletion test/groupher_server_web/mutation/cms/dashboard_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down Expand Up @@ -382,5 +381,52 @@ 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
}
}
}
}
"""
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

0 comments on commit a89515c

Please sign in to comment.