Skip to content

Commit

Permalink
Validate entries at changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRodrigues10 committed Aug 12, 2023
1 parent d724579 commit 2ed6035
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
17 changes: 17 additions & 0 deletions lib/atomic/activities/enrollment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Atomic.Activities.Enrollment do
use Atomic.Schema

alias Atomic.Accounts.User
alias Atomic.Activities
alias Atomic.Activities.Session

schema "enrollments" do
Expand All @@ -22,5 +23,21 @@ defmodule Atomic.Activities.Enrollment do
enrollment
|> cast(attrs, [:session_id, :user_id, :present])
|> validate_required([:session_id, :user_id])
|> validate_max_entries()
end

@doc """
Validates if the maximum number of enrollments has been reached.
"""
def validate_max_entries(changeset) do
sesion_id = get_field(changeset, :session_id)
session = Activities.get_session!(sesion_id)
enrolled = Activities.get_total_enrolled(session.id)

if session.maximum_entries <= enrolled do
add_error(changeset, :session_id, "Maximum number of enrollments reached.")
else
changeset
end
end
end
29 changes: 10 additions & 19 deletions lib/atomic_web/live/activity_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,17 @@ defmodule AtomicWeb.ActivityLive.Show do

@impl true
def handle_event("enroll", _payload, socket) do
session_id = socket.assigns.id
current_user = socket.assigns.current_user
case Activities.create_enrollment(socket.assigns.id, socket.assigns.current_user) do
{:ok, _enrollment} ->
{:noreply,
socket
|> put_flash(:success, "Enrolled successufully!")
|> set_enrolled(socket.assigns.id, socket.assigns.current_user)}

if Activities.verify_maximum_enrollments?(session_id) do
case Activities.create_enrollment(session_id, current_user) do
{:ok, _enrollment} ->
{:noreply,
socket
|> put_flash(:success, "Enrolled successufully!")
|> set_enrolled(session_id, current_user)}

{:error, _error} ->
{:noreply,
socket
|> put_flash(:error, "Unable to enroll")}
end
else
{:noreply,
socket
|> put_flash(:error, "Maximum number of entries reached")}
{:error, changeset} ->
{:noreply,
socket
|> put_flash(:error, changeset.errors[:session_id] |> elem(0))}
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/atomic/activities_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ defmodule Atomic.ActivitiesTest do

test "update_enrollment/2 with valid data updates the enrollment" do
enrollment = insert(:enrollment)
update_attrs = %{}
update_attrs = %{present: true}

assert {:ok, %Enrollment{}} = Activities.update_enrollment(enrollment, update_attrs)
end
Expand Down
2 changes: 1 addition & 1 deletion test/support/factories/organizations_factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Atomic.Factories.OrganizationFactory do

defmacro __using__(_opts) do
quote do
@roles ~w(follower member admin owner)a
@roles ~w(member admin owner)a

def organization_factory do
%Organization{
Expand Down

0 comments on commit 2ed6035

Please sign in to comment.