Move Participant role from integer to enum

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2019-02-07 16:37:40 +01:00
parent 7b83682b26
commit 4bac5a07bd
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
9 changed files with 89 additions and 22 deletions

View File

@ -21,7 +21,7 @@
<div v-else> <div v-else>
<span v-for="participant in event.participants" :key="participant.actor.uuid"> <span v-for="participant in event.participants" :key="participant.actor.uuid">
{{ participant.actor.preferredUsername }} {{ participant.actor.preferredUsername }}
<span v-if="participant.role === 4">(organizer)</span>, <span v-if="participant.role === ParticipantRole.CREATOR">(organizer)</span>,
<!-- <translate <!-- <translate
:translate-params="{name: participant.actor.preferredUsername}" :translate-params="{name: participant.actor.preferredUsername}"
>&nbsp;%{name} is in,</translate>--> >&nbsp;%{name} is in,</translate>-->
@ -33,12 +33,18 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { IEvent } from "@/types/event.model"; import {IEvent, ParticipantRole} from "@/types/event.model";
import { Component, Prop, Vue } from "vue-property-decorator"; import { Component, Prop, Vue } from "vue-property-decorator";
@Component @Component
export default class EventCard extends Vue { export default class EventCard extends Vue {
@Prop({ required: true }) event!: IEvent; @Prop({ required: true }) event!: IEvent;
@Prop({ default: false }) hideDetails!: boolean; @Prop({ default: false }) hideDetails!: boolean;
data() {
return {
ParticipantRole: ParticipantRole
}
}
} }
</script> </script>

View File

@ -20,7 +20,11 @@ export enum EventJoinOptions {
} }
export enum ParticipantRole { export enum ParticipantRole {
NOT_APPROVED = 'not_approved',
PARTICIPANT = 'participant',
MODERATOR = 'moderator',
ADMINSTRATOR = 'administrator',
CREATOR = 'creator'
} }
export interface ICategory { export interface ICategory {

View File

@ -62,7 +62,7 @@
<a v-if="!actorIsParticipant()" @click="joinEvent" class="button"> <a v-if="!actorIsParticipant()" @click="joinEvent" class="button">
<translate>Join</translate> <translate>Join</translate>
</a> </a>
<a v-if="actorIsParticipant()" @click="leaveEvent" color="button">Leave</a> <a v-if="actorIsParticipant()" @click="leaveEvent" class="button">Leave</a>
</div> </div>
<h2 class="subtitle">Details</h2> <h2 class="subtitle">Details</h2>
<p v-if="event.description"> <p v-if="event.description">

View File

@ -279,7 +279,7 @@ defmodule Mobilizon.Events do
%Participant{} %Participant{}
|> Participant.changeset(%{ |> Participant.changeset(%{
actor_id: event.organizer_actor_id, actor_id: event.organizer_actor_id,
role: 4, role: :creator,
event_id: event.id event_id: event.id
}) })
|> Repo.insert() do |> Repo.insert() do
@ -561,6 +561,8 @@ defmodule Mobilizon.Events do
@doc """ @doc """
Returns the list of participants for an event. Returns the list of participants for an event.
Default behaviour is to not return :not_approved participants
## Examples ## Examples
iex> list_participants_for_event(someuuid) iex> list_participants_for_event(someuuid)
@ -573,7 +575,7 @@ defmodule Mobilizon.Events do
p in Participant, p in Participant,
join: e in Event, join: e in Event,
on: p.event_id == e.id, on: p.event_id == e.id,
where: e.uuid == ^uuid, where: e.uuid == ^uuid and p.role != ^:not_approved,
preload: [:actor] preload: [:actor]
) )
|> paginate(page, limit) |> paginate(page, limit)
@ -668,7 +670,7 @@ defmodule Mobilizon.Events do
""" """
@spec list_requests_for_actor(Actor.t()) :: list(Participant.t()) @spec list_requests_for_actor(Actor.t()) :: list(Participant.t())
def list_requests_for_actor(%Actor{id: actor_id}) do 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 end
alias Mobilizon.Events.Session alias Mobilizon.Events.Session

View File

@ -1,3 +1,13 @@
import EctoEnum
defenum(Mobilizon.Events.ParticipantRoleEnum, :participant_role_type, [
:not_approved,
:participant,
:moderator,
:administrator,
:creator
])
defmodule Mobilizon.Events.Participant do defmodule Mobilizon.Events.Participant do
@moduledoc """ @moduledoc """
Represents a participant, an actor participating to an event Represents a participant, an actor participating to an event
@ -9,8 +19,7 @@ defmodule Mobilizon.Events.Participant do
@primary_key false @primary_key false
schema "participants" do schema "participants" do
# 0 : not_approved, 1 : participant, 2 : moderator, 3 : administrator, 4 : creator field(:role, Mobilizon.Events.ParticipantRoleEnum, default: :participant)
field(:role, :integer, default: 0)
belongs_to(:event, Event, primary_key: true) belongs_to(:event, Event, primary_key: true)
belongs_to(:actor, Actor, primary_key: true) belongs_to(:actor, Actor, primary_key: true)
@ -20,7 +29,7 @@ defmodule Mobilizon.Events.Participant do
@doc false @doc false
def changeset(%Participant{} = participant, attrs) do def changeset(%Participant{} = participant, attrs) do
participant participant
|> cast(attrs, [:role, :event_id, :actor_id]) |> Ecto.Changeset.cast(attrs, [:role, :event_id, :actor_id])
|> validate_required([:role, :event_id, :actor_id]) |> validate_required([:role, :event_id, :actor_id])
end end
end end

View File

@ -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

View File

@ -306,9 +306,9 @@ defmodule Mobilizon.EventsTest do
alias Mobilizon.Events.{Participant, Event} alias Mobilizon.Events.{Participant, Event}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
@valid_attrs %{role: 42} @valid_attrs %{role: :creator}
@update_attrs %{role: 43} @update_attrs %{role: :moderator}
@invalid_attrs %{role: nil} @invalid_attrs %{role: :no_such_role}
setup do setup do
actor = insert(:actor) actor = insert(:actor)
@ -341,7 +341,7 @@ defmodule Mobilizon.EventsTest do
with {:ok, %Participant{} = participant} <- Events.create_participant(valid_attrs) do with {:ok, %Participant{} = participant} <- Events.create_participant(valid_attrs) do
assert participant.event_id == event.id assert participant.event_id == event.id
assert participant.actor_id == actor.id assert participant.actor_id == actor.id
assert participant.role == 42 assert participant.role == :creator
else else
err -> err ->
flunk("Failed to create a participant #{inspect(err)}") flunk("Failed to create a participant #{inspect(err)}")
@ -357,7 +357,7 @@ defmodule Mobilizon.EventsTest do
} do } do
with {:ok, %Participant{} = participant} <- with {:ok, %Participant{} = participant} <-
Events.update_participant(participant, @update_attrs) do Events.update_participant(participant, @update_attrs) do
assert participant.role == 43 assert participant.role == :moderator
else else
err -> err ->
flunk("Failed to update a participant #{inspect(err)}") flunk("Failed to update a participant #{inspect(err)}")

View File

@ -89,13 +89,17 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
assert json_response(res, 200)["data"]["participants"] == [ assert json_response(res, 200)["data"]["participants"] == [
%{ %{
"actor" => %{"preferredUsername" => context.actor.preferred_username}, "actor" => %{"preferredUsername" => context.actor.preferred_username},
"role" => 4 "role" => "creator"
} }
] ]
# Adding a participant # Adding two participants
actor2 = insert(:actor) 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 = res =
context.conn context.conn
@ -104,11 +108,11 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
assert json_response(res, 200)["data"]["participants"] == [ assert json_response(res, 200)["data"]["participants"] == [
%{ %{
"actor" => %{"preferredUsername" => context.actor.preferred_username}, "actor" => %{"preferredUsername" => context.actor.preferred_username},
"role" => 4 "role" => "creator"
}, },
%{ %{
"actor" => %{"preferredUsername" => participant.actor.preferred_username}, "actor" => %{"preferredUsername" => participant2.actor.preferred_username},
"role" => 0 "role" => "participant"
} }
] ]
end end

View File

@ -117,7 +117,7 @@ defmodule Mobilizon.Factory do
%Mobilizon.Events.Participant{ %Mobilizon.Events.Participant{
event: build(:event), event: build(:event),
actor: build(:actor), actor: build(:actor),
role: 0 role: :creator
} }
end end