From 646f976f76938dd320e0e6327104791dfa351813 Mon Sep 17 00:00:00 2001 From: nwittstruck Date: Thu, 28 Mar 2024 12:48:28 +0100 Subject: [PATCH] feature: allow deletion of all cards in a brainstorming #271 --- lib/mindwendel/brainstormings.ex | 7 +++ .../live/admin/brainstorming_live/edit.ex | 12 ++++ .../admin/brainstorming_live/edit.html.heex | 25 ++++++++ lib/mindwendel_web/router.ex | 1 + priv/gettext/de/LC_MESSAGES/default.po | 24 ++++++-- priv/gettext/default.pot | 24 ++++++-- priv/gettext/en/LC_MESSAGES/default.po | 24 ++++++-- test/mindwendel/brainstormings_test.exs | 58 +++++++++++++++++++ .../admin/brainstorming_live/edit_test.exs | 32 +++++++++- test/mindwendel_web/live/live_helpers_test.ex | 20 ------- 10 files changed, 194 insertions(+), 33 deletions(-) delete mode 100644 test/mindwendel_web/live/live_helpers_test.ex diff --git a/lib/mindwendel/brainstormings.ex b/lib/mindwendel/brainstormings.ex index cc393982..80567d80 100644 --- a/lib/mindwendel/brainstormings.ex +++ b/lib/mindwendel/brainstormings.ex @@ -386,6 +386,13 @@ defmodule Mindwendel.Brainstormings do end) end + def empty(%Brainstorming{} = brainstorming) do + # we only delete ideas - labels and users should be left intact: + Repo.delete_all(from idea in Idea, where: idea.brainstorming_id == ^brainstorming.id) + + broadcast({:ok, brainstorming}, :brainstorming_updated) + end + @doc """ Deletes all brainstormings, older than 30 days since creation diff --git a/lib/mindwendel_web/live/admin/brainstorming_live/edit.ex b/lib/mindwendel_web/live/admin/brainstorming_live/edit.ex index 51c0fb1e..71b756fc 100644 --- a/lib/mindwendel_web/live/admin/brainstorming_live/edit.ex +++ b/lib/mindwendel_web/live/admin/brainstorming_live/edit.ex @@ -154,6 +154,18 @@ defmodule MindwendelWeb.Admin.BrainstormingLive.Edit do end end + def handle_event("empty", %{"value" => brainstorming_admin_url_id}, socket) + when brainstorming_admin_url_id == socket.assigns.brainstorming.admin_url_id do + brainstorming = socket.assigns.brainstorming + + Brainstormings.empty(brainstorming) + + {:noreply, + redirect(socket, + to: Routes.brainstorming_show_path(socket, :show, brainstorming) + )} + end + defp cancel_changeset_timer(socket) do if socket.assigns[:reset_changeset_timer_ref], do: Process.cancel_timer(socket.assigns.reset_changeset_timer_ref) diff --git a/lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex b/lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex index 34d58572..024333a9 100644 --- a/lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex +++ b/lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex @@ -184,6 +184,31 @@ +
+
+

<%= gettext("Empty brainstorming") %>

+
+
+
+
+

+ <%= gettext( + "Attention: This will delete all ideas in this brainstorming. The brainstorming itself and labels will be left unchanged. This cant be undone." + ) %> +

+ +
+
+
+
+

<%= gettext("Delete Brainstorming") %>

diff --git a/lib/mindwendel_web/router.ex b/lib/mindwendel_web/router.ex index d98f2596..105ee626 100644 --- a/lib/mindwendel_web/router.ex +++ b/lib/mindwendel_web/router.ex @@ -44,6 +44,7 @@ defmodule MindwendelWeb.Router do delete("/brainstormings/:id", BrainstormingController, :delete) get("/brainstormings/:id/export", BrainstormingController, :export) live("/brainstormings/:id/edit", BrainstormingLive.Edit, :edit) + live("/brainstormings/:id/empty", BrainstormingLive.Edit, :empty) end post("/brainstormings", BrainstormingController, :create) diff --git a/priv/gettext/de/LC_MESSAGES/default.po b/priv/gettext/de/LC_MESSAGES/default.po index 00eec758..e67baf29 100644 --- a/priv/gettext/de/LC_MESSAGES/default.po +++ b/priv/gettext/de/LC_MESSAGES/default.po @@ -41,7 +41,7 @@ msgstr "%{name} - Neue Idee" msgid "Are you sure you want to delete this idea?" msgstr "Möchtest du die Idee löschen?" -#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:195 +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:220 #, elixir-autogen, elixir-format msgid "Attention: This will delete the brainstorming with all belonging ideas and other associated records to it. This cant be undone" msgstr "Achtung: Hiermit löschst du das Brainstorming und alle dazugehörigen Ideen. Diese Aktion kann nicht rückgängig gemacht werden." @@ -67,12 +67,12 @@ msgstr "Erstelle ein Brainstorming." msgid "Create!" msgstr "Erstellen!" -#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:199 +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:224 #, elixir-autogen, elixir-format msgid "Delete" msgstr "Löschen" -#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:189 +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:214 #, elixir-autogen, elixir-format msgid "Delete Brainstorming" msgstr "Lösche Brainstorming" @@ -343,7 +343,7 @@ msgstr "Download als PNG" msgid "Download as svg" msgstr "Download als SVG" -#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:200 +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:225 #, elixir-autogen, elixir-format, fuzzy msgid "Brainstorming delete are you sure" msgstr "Bist du sicher, dass das Brainstorming gelöscht werden soll?" @@ -367,3 +367,19 @@ msgstr "" #, elixir-autogen, elixir-format, fuzzy msgid "Brainstorming will be deleted in %{days}" msgstr "Brainstorming wird gelöscht in " + +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:195 +#, elixir-autogen, elixir-format, fuzzy +msgid "Attention: This will delete all ideas in this brainstorming. The brainstorming itself and labels will be left unchanged. This cant be undone." +msgstr "Achtung: Hiermit löscht Du alle Ideen in diesem Brainstorming. Das Brainstorming und die Labels bleiben unverändert. Diese Aktion kann nicht rückgängig gemacht werden." + +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:203 +#, elixir-autogen, elixir-format, fuzzy +msgid "Brainstorming will be emptied - are you sure" +msgstr "Bist du sicher, dass das Brainstorming geleert werden soll?" + +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:189 +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:205 +#, elixir-autogen, elixir-format, fuzzy +msgid "Empty brainstorming" +msgstr "Leere das Brainstorming" diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 36d36f98..4cd12c16 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -40,7 +40,7 @@ msgstr "" msgid "Are you sure you want to delete this idea?" msgstr "" -#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:195 +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:220 #, elixir-autogen, elixir-format msgid "Attention: This will delete the brainstorming with all belonging ideas and other associated records to it. This cant be undone" msgstr "" @@ -66,12 +66,12 @@ msgstr "" msgid "Create!" msgstr "" -#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:199 +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:224 #, elixir-autogen, elixir-format msgid "Delete" msgstr "" -#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:189 +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:214 #, elixir-autogen, elixir-format msgid "Delete Brainstorming" msgstr "" @@ -342,7 +342,7 @@ msgstr "" msgid "Download as svg" msgstr "" -#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:200 +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:225 #, elixir-autogen, elixir-format msgid "Brainstorming delete are you sure" msgstr "" @@ -366,3 +366,19 @@ msgstr "" #, elixir-autogen, elixir-format msgid "Brainstorming will be deleted in %{days}" msgstr "" + +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:195 +#, elixir-autogen, elixir-format +msgid "Attention: This will delete all ideas in this brainstorming. The brainstorming itself and labels will be left unchanged. This cant be undone." +msgstr "" + +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:203 +#, elixir-autogen, elixir-format +msgid "Brainstorming will be emptied - are you sure" +msgstr "" + +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:189 +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:205 +#, elixir-autogen, elixir-format +msgid "Empty brainstorming" +msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index a65ad321..633cf5ed 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -41,7 +41,7 @@ msgstr "" msgid "Are you sure you want to delete this idea?" msgstr "" -#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:195 +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:220 #, elixir-autogen, elixir-format msgid "Attention: This will delete the brainstorming with all belonging ideas and other associated records to it. This cant be undone" msgstr "" @@ -67,12 +67,12 @@ msgstr "" msgid "Create!" msgstr "" -#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:199 +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:224 #, elixir-autogen, elixir-format msgid "Delete" msgstr "" -#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:189 +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:214 #, elixir-autogen, elixir-format msgid "Delete Brainstorming" msgstr "" @@ -343,7 +343,7 @@ msgstr "Download as png" msgid "Download as svg" msgstr "Download as svg" -#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:200 +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:225 #, elixir-autogen, elixir-format, fuzzy msgid "Brainstorming delete are you sure" msgstr "Are you sure that you want to delete this brainstorming?" @@ -367,3 +367,19 @@ msgstr "" #, elixir-autogen, elixir-format, fuzzy msgid "Brainstorming will be deleted in %{days}" msgstr "" + +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:195 +#, elixir-autogen, elixir-format, fuzzy +msgid "Attention: This will delete all ideas in this brainstorming. The brainstorming itself and labels will be left unchanged. This cant be undone." +msgstr "" + +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:203 +#, elixir-autogen, elixir-format, fuzzy +msgid "Brainstorming will be emptied - are you sure" +msgstr "Are you sure that you want to delete this brainstorming?" + +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:189 +#: lib/mindwendel_web/live/admin/brainstorming_live/edit.html.heex:205 +#, elixir-autogen, elixir-format, fuzzy +msgid "Empty brainstorming" +msgstr "" diff --git a/test/mindwendel/brainstormings_test.exs b/test/mindwendel/brainstormings_test.exs index 767bae7c..3121e214 100644 --- a/test/mindwendel/brainstormings_test.exs +++ b/test/mindwendel/brainstormings_test.exs @@ -1,4 +1,5 @@ defmodule Mindwendel.BrainstormingsTest do + alias Mindwendel.Brainstormings.IdeaIdeaLabel use Mindwendel.DataCase alias Mindwendel.Brainstormings.BrainstormingModeratingUser alias Mindwendel.Factory @@ -311,4 +312,61 @@ defmodule Mindwendel.BrainstormingsTest do assert Repo.exists?(from(b in Brainstorming, where: b.id == ^brainstorming.id)) end end + + describe "empty/1 brainstormings" do + test "empty/1 removes all ideas from a brainstorming", %{brainstorming: brainstorming} do + brainstorming = brainstorming |> Repo.preload([:ideas]) + assert Enum.count(brainstorming.ideas) == 1 + Brainstormings.empty(brainstorming) + # reload brainstorming: + brainstorming = Brainstormings.get_brainstorming!(brainstorming.id) + brainstorming = brainstorming |> Repo.preload([:ideas]) + assert Enum.count(brainstorming.ideas) == 0 + end + + test "empty/1 also clears likes and labels from ideas", %{brainstorming: brainstorming} do + idea = + Factory.insert!(:idea, + brainstorming: brainstorming, + inserted_at: ~N[2021-01-01 15:04:30] + ) + + like = Factory.insert!(:like, idea: idea) + + {:ok, idea} = + Brainstormings.add_idea_label_to_idea(idea, Enum.at(brainstorming.labels, 0)) + + idea = idea |> Repo.preload([:idea_labels]) + + Brainstormings.empty(brainstorming) + # reload brainstorming: + brainstorming = Brainstormings.get_brainstorming!(brainstorming.id) + + assert Enum.count(brainstorming.ideas) == 0 + assert Repo.get_by(Idea, id: idea.id) == nil + assert Repo.get_by(IdeaIdeaLabel, idea_id: idea.id) == nil + assert Repo.get_by(Like, id: like.id) == nil + end + + test "empty/1 does not removes all ideas from other brainstormings", %{ + brainstorming: brainstorming + } do + other_brainstorming = Factory.insert!(:brainstorming) + + Factory.insert!(:idea, + brainstorming: other_brainstorming + ) + + other_brainstorming = other_brainstorming |> Repo.preload([:ideas]) + + assert Enum.count(other_brainstorming.ideas) == 1 + Brainstormings.empty(brainstorming) + # reload brainstorming: + brainstorming = Brainstormings.get_brainstorming!(brainstorming.id) + brainstorming = brainstorming |> Repo.preload([:ideas]) + other_brainstorming = other_brainstorming |> Repo.preload([:ideas]) + assert Enum.count(brainstorming.ideas) == 0 + assert Enum.count(other_brainstorming.ideas) == 1 + end + end end diff --git a/test/mindwendel_web/live/admin/brainstorming_live/edit_test.exs b/test/mindwendel_web/live/admin/brainstorming_live/edit_test.exs index 354bd8b6..3293f2b6 100644 --- a/test/mindwendel_web/live/admin/brainstorming_live/edit_test.exs +++ b/test/mindwendel_web/live/admin/brainstorming_live/edit_test.exs @@ -4,8 +4,16 @@ defmodule MindwendelWeb.Admin.BrainstormingLive.EditTest do alias Mindwendel.Factory + alias Mindwendel.Brainstormings + setup do - %{brainstorming: Factory.insert!(:brainstorming)} + brainstorming = Factory.insert!(:brainstorming) + + %{ + brainstorming: brainstorming, + idea: + Factory.insert!(:idea, brainstorming: brainstorming, inserted_at: ~N[2021-01-01 15:04:30]) + } end test "connected mount", %{ @@ -130,6 +138,28 @@ defmodule MindwendelWeb.Admin.BrainstormingLive.EditTest do assert edit_live_view |> element("input#brainstorming_labels_4_name") |> has_element? end + describe "empty brainstorming" do + test "handles empty event to delete brainstorming content", %{ + conn: conn, + brainstorming: brainstorming + } do + {:ok, edit_live_view, _html} = + live(conn, Routes.admin_brainstorming_edit_path(conn, :edit, brainstorming.admin_url_id)) + + # reload brainstorming to check for changes: + brainstorming = Brainstormings.get_brainstorming!(brainstorming.id) + assert Enum.count(brainstorming.ideas) == 1 + + edit_live_view + |> element("button", "Empty") + |> render_click() + + # reload brainstorming to check for changes: + brainstorming = Brainstormings.get_brainstorming!(brainstorming.id) + assert Enum.count(brainstorming.ideas) == 0 + end + end + defp html_selector_remove_idea_label_button(idea_label) do "button[value=\"#{idea_label.id}\"]" end diff --git a/test/mindwendel_web/live/live_helpers_test.ex b/test/mindwendel_web/live/live_helpers_test.ex deleted file mode 100644 index 72ee6e35..00000000 --- a/test/mindwendel_web/live/live_helpers_test.ex +++ /dev/null @@ -1,20 +0,0 @@ -defmodule MindwendelWeb.LiveHelpersTest do - use MindwendelWeb.ConnCase - import Phoenix.LiveViewTest - - alias Mindwendel.Factory - - setup do - %{brainstorming: Factory.insert!(:brainstorming)} - end - - test "contains deletion date", %{ - conn: conn, - brainstorming: brainstorming - } do - {:ok, _show_live_view, html} = - live(conn, Routes.brainstorming_show_path(conn, :show, brainstorming)) - - assert html =~ "in 29 days" - end -end