Skip to content

Commit

Permalink
Fix self invite as a facilitator
Browse files Browse the repository at this point in the history
  • Loading branch information
alxlion committed Jun 7, 2024
1 parent 0b315ac commit b54f9a4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
17 changes: 16 additions & 1 deletion lib/claper/events/activity_leader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Claper.Events.ActivityLeader do
field :delete, :boolean, virtual: true

field :user_id, :integer, virtual: true
field :user_email, :string, virtual: true

field :email, :string
belongs_to :event, Claper.Events.Event
Expand All @@ -21,12 +22,14 @@ defmodule Claper.Events.ActivityLeader do
|> cast(attrs, [
:email,
:event_id,
:delete
:delete,
:user_email
])
|> validate_required([:email])
|> validate_format(:email, ~r/^[^\s]+@[^\s]+$/, message: "must have the @ sign and no spaces")
|> validate_length(:email, min: 6, max: 160)
|> unique_constraint(:email)
|> validate_not_current_user_email
|> unsafe_validate_unique([:event_id, :email], Claper.Repo)
|> maybe_mark_for_deletion
end
Expand All @@ -40,4 +43,16 @@ defmodule Claper.Events.ActivityLeader do
changeset
end
end

defp validate_not_current_user_email(changeset) do
email = get_field(changeset, :email)
user_email = get_change(changeset, :user_email)
IO.inspect(user_email)

if email == user_email do
add_error(changeset, :email, "cannot be the same as the current user's email")
else
changeset
end
end
end
4 changes: 3 additions & 1 deletion lib/claper_web/live/event_live/event_form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ defmodule ClaperWeb.EventLive.EventFormComponent do
leaders =
existing_leaders
|> Enum.concat([
Events.change_activity_leader(%Events.ActivityLeader{temp_id: get_temp_id()})
Events.change_activity_leader(%Events.ActivityLeader{
temp_id: get_temp_id()
})
])

changeset =
Expand Down
2 changes: 2 additions & 0 deletions lib/claper_web/live/event_live/event_form_component.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@
key={:email}
name=""
/>
<%= hidden_input(l, :user_email, value: @current_user.email) %>
</div>

<label
Expand All @@ -364,6 +365,7 @@

<%= hidden_input(l, :temp_id) %>
<%= hidden_input(l, :event_id, value: @event.id) %>
<%= hidden_input(l, :user_email, value: @current_user.email) %>

<button
type="button"
Expand Down

0 comments on commit b54f9a4

Please sign in to comment.