Skip to content

Commit

Permalink
Merge with main
Browse files Browse the repository at this point in the history
  • Loading branch information
ruilopesm committed Aug 17, 2023
2 parents 3e8e44d + 95d9099 commit 8d7b105
Show file tree
Hide file tree
Showing 21 changed files with 94 additions and 48 deletions.
18 changes: 18 additions & 0 deletions lib/atomic/activities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ defmodule Atomic.Activities do
|> Enum.map(& &1.organization_id)
end

@doc """
Verifies the maximum number of enrollments for an activity session.
## Examples
iex> verify_maximum_enrollments(session_id)
true
iex> verify_maximum_enrollments(session_id)
false
"""
def verify_maximum_enrollments?(session_id) do
session = get_session!(session_id)
total_enrolled = get_total_enrolled(session_id)

session.maximum_entries > total_enrolled
end

@doc """
Verifies if an user is enrolled in an activity session.
Expand Down
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
1 change: 1 addition & 0 deletions lib/atomic/activities/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ defmodule Atomic.Activities.Session do
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
|> validate_location()
|> check_constraint(:minimum_entries, name: :minimum_entries_lower_than_maximum_entries)
|> maybe_mark_for_deletion()
|> maybe_put_departments(attrs)
|> maybe_put_speakers(attrs)
Expand Down
2 changes: 1 addition & 1 deletion lib/atomic/organizations/organization.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Atomic.Organizations.Organization do
alias Atomic.Organizations.Card
alias Atomic.Organizations.Department
alias Atomic.Organizations.Membership
alias Atomic.Partnerships.Partner
alias Atomic.Organizations.Partner
alias Atomic.Uploaders

@required_fields ~w(name description)a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Atomic.Partnerships.Partner do
defmodule Atomic.Organizations.Partner do
@moduledoc """
A partnership.
"""
Expand Down
2 changes: 1 addition & 1 deletion lib/atomic/partnerships.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Atomic.Partnerships do
import Ecto.Query, warn: false
alias Atomic.Repo

alias Atomic.Partnerships.Partner
alias Atomic.Organizations.Partner

@doc """
Returns the list of partnerships.
Expand Down
2 changes: 1 addition & 1 deletion lib/atomic/uploaders/image.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Atomic.Uploaders.Image do
"""
use Waffle.Definition
use Waffle.Ecto.Definition
alias Atomic.Partnerships.Partner
alias Atomic.Organizations.Partner

@versions [:original, :medium, :thumb]
@extension_whitelist ~w(.jpg .jpeg .gif .png)
Expand Down
2 changes: 1 addition & 1 deletion lib/atomic/uploaders/partner_image.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Atomic.Uploaders.PartnerImage do
use Waffle.Definition
use Waffle.Ecto.Definition

alias Atomic.Partnerships.Partner
alias Atomic.Organizations.Partner

@versions [:original, :medium, :thumb]
@extension_whitelist ~w(.jpg .jpeg .png)
Expand Down
2 changes: 1 addition & 1 deletion lib/atomic_web/components/calendar/month.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ defmodule AtomicWeb.Components.CalendarMonth do
<%= for session <- get_date_sessions(@sessions, @date) do %>
<li>
<%= if session.activity do %>
<%= live_patch to: Routes.activity_show_path(AtomicWeb.Endpoint, :show, session.activity, organization), class: "group flex" do %>
<%= live_patch to: Routes.activity_show_path(AtomicWeb.Endpoint, :show, organization, session.id), class: "group flex" do %>
<p class="text-zinc-600 group-hover:text-zinc-800 flex-auto truncate font-medium">
<%= session.activity.title %>
<%!-- <%= if Gettext.get_locale() == "en" do
Expand Down
14 changes: 10 additions & 4 deletions lib/atomic_web/live/activity_live/index.html.heex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="inline-block min-w-full align-middle">
<div x-data="{ open: true }" class="inline-block min-w-full align-middle">
<div class="bg-white">
<div class="pt-4 px-4">
<div class="flex items-center justify-between">
Expand Down Expand Up @@ -66,9 +66,15 @@
</div>
</div>
<div class="flex flex-col-reverse lg:flex-row-reverse lg:items-center lg:justify-between">
<div class="mt-2 flex items-center text-sm text-zinc-500 lg:mt-0">
<Heroicons.Solid.bell class="flex-shrink-0 mr-1.5 w-5 h-5 text-zinc-400" /> Closing
</div>
<%= if Activities.verify_maximum_enrollments?(session.id) do %>
<div class="mt-2 flex items-center text-sm text-zinc-500 lg:mt-0">
<Heroicons.Solid.bell class="flex-shrink-0 mr-1.5 w-5 h-5 text-zinc-400" /> Open
</div>
<% else %>
<div class="mt-2 flex items-center text-sm text-zinc-500 lg:mt-0">
<Heroicons.Solid.bell class="flex-shrink-0 mr-1.5 w-5 h-5 text-zinc-400" /> Closed
</div>
<% end %>
<div class="flex flex-row space-x-2">
<%= for speaker <- session.speakers do %>
<div class="mt-2 flex items-center">
Expand Down
18 changes: 8 additions & 10 deletions lib/atomic_web/live/activity_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ defmodule AtomicWeb.ActivityLive.Show do
|> assign(:breadcrumb_entries, entries)
|> assign(:current_page, :activities)
|> assign(:session, %{session | enrolled: Activities.get_total_enrolled(id)})
|> assign(:max_enrolled?, Activities.verify_maximum_enrollments?(session.id))
|> assign(:activity, activity)}
else
raise MismatchError
Expand All @@ -54,21 +55,18 @@ 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(session_id, current_user) do
case Activities.create_enrollment(socket.assigns.id, socket.assigns.current_user) do
{:ok, _enrollment} ->
{:noreply,
socket
|> put_flash(:success, "Enrolled successufully!")
|> set_enrolled(session_id, current_user)}
|> set_enrolled(socket.assigns.id, socket.assigns.current_user)}

{:error, _error} ->
{:noreply,
socket
|> put_flash(:error, "Unable to enroll")
|> set_enrolled(session_id, current_user)}
{:error, changeset} ->
case is_nil(changeset.errors[:session_id]) do
true -> {:noreply, socket |> put_flash(:error, "Unable to enroll")}
_ -> {:noreply, socket |> put_flash(:error, changeset.errors[:session_id] |> elem(0))}
end
end
end

Expand Down
28 changes: 15 additions & 13 deletions lib/atomic_web/live/activity_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,21 @@
</button>
</dd>
<% else %>
<div class="flex flex-col">
<dt class="text-sm font-medium text-zinc-500">
<%= gettext("You are not enrolled in this activity") %>
</dt>
<button
phx-click="enroll"
phx-disable-with={gettext("Enrolling...")}
phx-throttle="0"
class="w-fit mt-1 rounded-md bg-orange-500 py-2 px-3.5 text-sm font-medium text-white shadow-sm hover:bg-orange-600 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
>
<%= gettext("Enroll") %>
</button>
</div>
<%= if @max_enrolled? do %>
<div class="flex flex-col">
<dt class="text-sm font-medium text-zinc-500">
<%= gettext("You are not enrolled in this activity") %>
</dt>
<button
phx-click="enroll"
phx-disable-with={gettext("Enrolling...")}
phx-throttle="0"
class="w-fit mt-1 rounded-md bg-orange-500 py-2 px-3.5 text-sm font-medium text-white shadow-sm hover:bg-orange-600 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
>
<%= gettext("Enroll") %>
</button>
</div>
<% end %>
<% end %>
</div>
</dl>
Expand Down
2 changes: 1 addition & 1 deletion lib/atomic_web/live/partner_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule AtomicWeb.PartnerLive.Index do

alias Atomic.Organizations
alias Atomic.Partnerships
alias Atomic.Partnerships.Partner
alias Atomic.Organizations.Partner

@impl true
def mount(%{"organization_id" => organization_id}, _session, socket) do
Expand Down
4 changes: 4 additions & 0 deletions priv/repo/migrations/20221023200102_create_sessions.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ defmodule Atomic.Repo.Migrations.CreateSessions do
end

create index(:sessions, [:activity_id])

create constraint(:sessions, :minimum_entries_lower_than_maximum_entries,
check: "minimum_entries < maximum_entries"
)
end
end
3 changes: 2 additions & 1 deletion priv/repo/seeds/accounts.exs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ defmodule Atomic.Repo.Seeds.Accounts do
"password" => "password1234",
"role" => role,
"course_id" => Enum.random(courses).id,
"default_organization_id" => Enum.random(organizations).id
"default_organization_id" => Enum.random(organizations).id,
"confirmed_at" => DateTime.utc_now()
}

case Accounts.register_user(user) do
Expand Down
15 changes: 7 additions & 8 deletions priv/repo/seeds/activities.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ defmodule Atomic.Repo.Seeds.Activities do
end

def seed_activities() do
location = %{
name: "Departamento de Informática da Universidade do Minho",
url: "https://web.di.uminho.pt"
}

case Repo.all(Activity) do
[] ->
location = %{
name: "Departamento de Informática da Universidade do Minho",
url: "https://web.di.uminho.pt"
}

Activity.changeset(
%Activity{},
%{
Expand Down Expand Up @@ -173,7 +173,7 @@ defmodule Atomic.Repo.Seeds.Activities do
activity_sessions: [
%{
minimum_entries: 0,
maximum_entries: 10,
maximum_entries: Enum.random(10..50),
enrolled: 0,
start: DateTime.from_naive!(~N[2023-04-06 15:00:00], "Etc/UTC"),
finish: DateTime.from_naive!(~N[2023-04-06 17:00:00], "Etc/UTC"),
Expand All @@ -190,10 +190,9 @@ defmodule Atomic.Repo.Seeds.Activities do
end

def seed_activities_departments() do
department = Repo.get_by(Department, name: "Merchandise and Partnerships")

case Repo.all(SessionDepartment) do
[] ->
department = Repo.get_by(Department, name: "Merchandise and Partnerships")
activities = Repo.all(Activity)

for activity <- activities do
Expand Down
2 changes: 1 addition & 1 deletion priv/repo/seeds/partners.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Atomic.Repo.Seeds.Partners do
alias Atomic.Partnerships.Partner
alias Atomic.Organizations.Partner
alias Atomic.Organizations.Organization
alias Atomic.Repo

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/atomic/partnerships_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Atomic.PartnershipsTest do

describe "partnerships" do
alias Atomic.OrganizationsFixtures
alias Atomic.Partnerships.Partner
alias Atomic.Organizations.Partner
import Atomic.PartnershipsFixtures

@invalid_attrs %{description: nil, name: nil}
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
2 changes: 1 addition & 1 deletion test/support/fixtures/activities_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule Atomic.ActivitiesFixtures do
attrs
|> Enum.into(%{
maximum_entries: 42,
minimum_entries: 42,
minimum_entries: 0,
finish: ~N[2022-10-22 20:00:00],
start: ~N[2022-10-22 20:00:00]
})
Expand Down

0 comments on commit 8d7b105

Please sign in to comment.