From 4bac5a07bde575b040c236717b22228998103aa2 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 7 Feb 2019 16:37:40 +0100 Subject: [PATCH] Move Participant role from integer to enum Signed-off-by: Thomas Citharel --- js/src/components/Event/EventCard.vue | 10 ++++- js/src/types/event.model.ts | 6 ++- js/src/views/Event/Event.vue | 2 +- lib/mobilizon/events/events.ex | 8 ++-- lib/mobilizon/events/participant.ex | 15 +++++-- ...07134142_move_participant_role_to_enum.exs | 42 +++++++++++++++++++ test/mobilizon/events/events_test.exs | 10 ++--- .../resolvers/event_resolver_test.exs | 16 ++++--- test/support/factory.ex | 2 +- 9 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 priv/repo/migrations/20190207134142_move_participant_role_to_enum.exs diff --git a/js/src/components/Event/EventCard.vue b/js/src/components/Event/EventCard.vue index 04244a6f0..cb6a18f31 100644 --- a/js/src/components/Event/EventCard.vue +++ b/js/src/components/Event/EventCard.vue @@ -21,7 +21,7 @@
{{ participant.actor.preferredUsername }} - (organizer), + (organizer), @@ -33,12 +33,18 @@ diff --git a/js/src/types/event.model.ts b/js/src/types/event.model.ts index 011cd41dd..e985748f4 100644 --- a/js/src/types/event.model.ts +++ b/js/src/types/event.model.ts @@ -20,7 +20,11 @@ export enum EventJoinOptions { } export enum ParticipantRole { - + NOT_APPROVED = 'not_approved', + PARTICIPANT = 'participant', + MODERATOR = 'moderator', + ADMINSTRATOR = 'administrator', + CREATOR = 'creator' } export interface ICategory { diff --git a/js/src/views/Event/Event.vue b/js/src/views/Event/Event.vue index 9863afee3..d2854ae02 100644 --- a/js/src/views/Event/Event.vue +++ b/js/src/views/Event/Event.vue @@ -62,7 +62,7 @@ Join - Leave + Leave

Details

diff --git a/lib/mobilizon/events/events.ex b/lib/mobilizon/events/events.ex index 798eab90c..6cfb79c3c 100644 --- a/lib/mobilizon/events/events.ex +++ b/lib/mobilizon/events/events.ex @@ -279,7 +279,7 @@ defmodule Mobilizon.Events do %Participant{} |> Participant.changeset(%{ actor_id: event.organizer_actor_id, - role: 4, + role: :creator, event_id: event.id }) |> Repo.insert() do @@ -561,6 +561,8 @@ defmodule Mobilizon.Events do @doc """ Returns the list of participants for an event. + Default behaviour is to not return :not_approved participants + ## Examples iex> list_participants_for_event(someuuid) @@ -573,7 +575,7 @@ defmodule Mobilizon.Events do p in Participant, join: e in Event, on: p.event_id == e.id, - where: e.uuid == ^uuid, + where: e.uuid == ^uuid and p.role != ^:not_approved, preload: [:actor] ) |> paginate(page, limit) @@ -668,7 +670,7 @@ defmodule Mobilizon.Events do """ @spec list_requests_for_actor(Actor.t()) :: list(Participant.t()) def list_requests_for_actor(%Actor{id: actor_id}) do - Repo.all(from(p in Participant, where: p.actor_id == ^actor_id and p.approved == false)) + Repo.all(from(p in Participant, where: p.actor_id == ^actor_id and p.role == ^:not_approved)) end alias Mobilizon.Events.Session diff --git a/lib/mobilizon/events/participant.ex b/lib/mobilizon/events/participant.ex index 9b7506f00..276891996 100644 --- a/lib/mobilizon/events/participant.ex +++ b/lib/mobilizon/events/participant.ex @@ -1,3 +1,13 @@ +import EctoEnum + +defenum(Mobilizon.Events.ParticipantRoleEnum, :participant_role_type, [ + :not_approved, + :participant, + :moderator, + :administrator, + :creator +]) + defmodule Mobilizon.Events.Participant do @moduledoc """ Represents a participant, an actor participating to an event @@ -9,8 +19,7 @@ defmodule Mobilizon.Events.Participant do @primary_key false schema "participants" do - # 0 : not_approved, 1 : participant, 2 : moderator, 3 : administrator, 4 : creator - field(:role, :integer, default: 0) + field(:role, Mobilizon.Events.ParticipantRoleEnum, default: :participant) belongs_to(:event, Event, primary_key: true) belongs_to(:actor, Actor, primary_key: true) @@ -20,7 +29,7 @@ defmodule Mobilizon.Events.Participant do @doc false def changeset(%Participant{} = participant, attrs) do participant - |> cast(attrs, [:role, :event_id, :actor_id]) + |> Ecto.Changeset.cast(attrs, [:role, :event_id, :actor_id]) |> validate_required([:role, :event_id, :actor_id]) end end diff --git a/priv/repo/migrations/20190207134142_move_participant_role_to_enum.exs b/priv/repo/migrations/20190207134142_move_participant_role_to_enum.exs new file mode 100644 index 000000000..de371cdf5 --- /dev/null +++ b/priv/repo/migrations/20190207134142_move_participant_role_to_enum.exs @@ -0,0 +1,42 @@ +defmodule Mobilizon.Repo.Migrations.MoveParticipantRoleToEnum do + use Ecto.Migration + alias Mobilizon.Events.ParticipantRoleEnum + + def up do + ParticipantRoleEnum.create_type() + + alter table(:participants) do + add(:role_tmp, ParticipantRoleEnum.type(), default: "participant") + end + + execute "UPDATE participants set role_tmp = 'not_approved' where role = 0" + execute "UPDATE participants set role_tmp = 'participant' where role = 1" + execute "UPDATE participants set role_tmp = 'moderator' where role = 2" + execute "UPDATE participants set role_tmp = 'administrator' where role = 3" + execute "UPDATE participants set role_tmp = 'creator' where role = 4" + + alter table(:participants) do + remove(:role) + end + rename table(:participants), :role_tmp, to: :role + end + + def down do + alter table(:participants) do + add(:role_tmp, :integer, default: 1) + end + + execute "UPDATE participants set role_tmp = 0 where role = 'not_approved'" + execute "UPDATE participants set role_tmp = 1 where role = 'participant'" + execute "UPDATE participants set role_tmp = 2 where role = 'moderator'" + execute "UPDATE participants set role_tmp = 3 where role = 'administrator'" + execute "UPDATE participants set role_tmp = 4 where role = 'creator'" + + alter table(:participants) do + remove(:role) + end + ParticipantRoleEnum.drop_type() + + rename table(:participants), :role_tmp, to: :role + end +end diff --git a/test/mobilizon/events/events_test.exs b/test/mobilizon/events/events_test.exs index 26202f671..d326da56e 100644 --- a/test/mobilizon/events/events_test.exs +++ b/test/mobilizon/events/events_test.exs @@ -306,9 +306,9 @@ defmodule Mobilizon.EventsTest do alias Mobilizon.Events.{Participant, Event} alias Mobilizon.Actors.Actor - @valid_attrs %{role: 42} - @update_attrs %{role: 43} - @invalid_attrs %{role: nil} + @valid_attrs %{role: :creator} + @update_attrs %{role: :moderator} + @invalid_attrs %{role: :no_such_role} setup do actor = insert(:actor) @@ -341,7 +341,7 @@ defmodule Mobilizon.EventsTest do with {:ok, %Participant{} = participant} <- Events.create_participant(valid_attrs) do assert participant.event_id == event.id assert participant.actor_id == actor.id - assert participant.role == 42 + assert participant.role == :creator else err -> flunk("Failed to create a participant #{inspect(err)}") @@ -357,7 +357,7 @@ defmodule Mobilizon.EventsTest do } do with {:ok, %Participant{} = participant} <- Events.update_participant(participant, @update_attrs) do - assert participant.role == 43 + assert participant.role == :moderator else err -> flunk("Failed to update a participant #{inspect(err)}") diff --git a/test/mobilizon_web/resolvers/event_resolver_test.exs b/test/mobilizon_web/resolvers/event_resolver_test.exs index 44120eece..01a775fee 100644 --- a/test/mobilizon_web/resolvers/event_resolver_test.exs +++ b/test/mobilizon_web/resolvers/event_resolver_test.exs @@ -89,13 +89,17 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do assert json_response(res, 200)["data"]["participants"] == [ %{ "actor" => %{"preferredUsername" => context.actor.preferred_username}, - "role" => 4 + "role" => "creator" } ] - # Adding a participant + # Adding two participants actor2 = insert(:actor) - participant = insert(:participant, event: event, actor: actor2) + actor3 = insert(:actor) + # This one won't get listed (as not approved) + participant = insert(:participant, event: event, actor: actor2, role: :not_approved) + # This one will (as a participant) + participant2 = insert(:participant, event: event, actor: actor3, role: :participant) res = context.conn @@ -104,11 +108,11 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do assert json_response(res, 200)["data"]["participants"] == [ %{ "actor" => %{"preferredUsername" => context.actor.preferred_username}, - "role" => 4 + "role" => "creator" }, %{ - "actor" => %{"preferredUsername" => participant.actor.preferred_username}, - "role" => 0 + "actor" => %{"preferredUsername" => participant2.actor.preferred_username}, + "role" => "participant" } ] end diff --git a/test/support/factory.ex b/test/support/factory.ex index 06b3a7e8f..74537fff9 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -117,7 +117,7 @@ defmodule Mobilizon.Factory do %Mobilizon.Events.Participant{ event: build(:event), actor: build(:actor), - role: 0 + role: :creator } end