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

View File

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

View File

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

View File

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

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

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.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)}")

View File

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

View File

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