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 aea0a41 + 95d9099 commit 36fc6ee
Show file tree
Hide file tree
Showing 32 changed files with 104 additions and 67 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
1 change: 0 additions & 1 deletion lib/atomic/activities/activity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ defmodule Atomic.Activities.Activity do
timestamps()
end

@doc false
def changeset(activity, attrs) do
activity
|> cast(attrs, @required_fields ++ @optional_fields)
Expand Down
18 changes: 17 additions & 1 deletion 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

@required_fields ~w(session_id user_id)a
Expand All @@ -19,10 +20,25 @@ defmodule Atomic.Activities.Enrollment do
timestamps()
end

@doc false
def changeset(enrollment, attrs) do
enrollment
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_maximum_entries()
|> validate_required(@required_fields)
end

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

if session.maximum_entries <= enrolled do
add_error(changeset, :session_id, Gettext.gettext("maximum number of enrollments reached"))
else
changeset
end
end
end
2 changes: 1 addition & 1 deletion lib/atomic/activities/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ defmodule Atomic.Activities.Session do
timestamps()
end

@doc false
def changeset(session, attrs) do
session
|> cast(attrs, @required_fields ++ @optional_fields)
|> cast_embed(:location, with: &Location.changeset/2)
|> cast_attachments(attrs, [:session_image])
|> validate_required(@required_fields)
|> 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
1 change: 0 additions & 1 deletion lib/atomic/activities/speaker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ defmodule Atomic.Activities.Speaker do
timestamps()
end

@doc false
def changeset(speaker, attrs) do
speaker
|> cast(attrs, @required_fields)
Expand Down
1 change: 0 additions & 1 deletion lib/atomic/events/enrollment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ defmodule Atomic.Events.Enrollment do
timestamps()
end

@doc false
def changeset(enrollment, attrs) do
enrollment
|> cast(attrs, @required_fields ++ @optional_fields)
Expand Down
1 change: 0 additions & 1 deletion lib/atomic/events/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ defmodule Atomic.Events.Event do
timestamps()
end

@doc false
def changeset(events, attrs) do
events
|> cast(attrs, @required_fields ++ @optional_fields)
Expand Down
1 change: 0 additions & 1 deletion lib/atomic/events/event_organization.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ defmodule Atomic.Events.EventOrganization do
timestamps()
end

@doc false
def changeset(event_organization, attrs) do
event_organization
|> cast(attrs, @required_fields)
Expand Down
1 change: 0 additions & 1 deletion lib/atomic/news/new.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ defmodule Atomic.News.New do
timestamps()
end

@doc false
def changeset(new, attrs) do
new
|> cast(attrs, @required_fields)
Expand Down
1 change: 0 additions & 1 deletion lib/atomic/organizations/board.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ defmodule Atomic.Organizations.Board do
timestamps()
end

@doc false
def changeset(board, attrs) do
board
|> cast(attrs, @required_fields)
Expand Down
1 change: 0 additions & 1 deletion lib/atomic/organizations/board_departments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ defmodule Atomic.Organizations.BoardDepartments do
timestamps()
end

@doc false
def changeset(board_departments, attrs) do
board_departments
|> cast(attrs, @required_fields)
Expand Down
1 change: 0 additions & 1 deletion lib/atomic/organizations/department.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ defmodule Atomic.Organizations.Department do
timestamps()
end

@doc false
def changeset(department, attrs) do
department
|> cast(attrs, @required_fields ++ @optional_fields)
Expand Down
1 change: 0 additions & 1 deletion lib/atomic/organizations/membership.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ defmodule Atomic.Organizations.Membership do
timestamps()
end

@doc false
def changeset(organization, attrs) do
organization
|> cast(attrs, @required_fields ++ @optional_fields)
Expand Down
3 changes: 1 addition & 2 deletions lib/atomic/organizations/organization.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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 Expand Up @@ -51,7 +51,6 @@ defmodule Atomic.Organizations.Organization do
timestamps()
end

@doc false
def changeset(organization, attrs) do
organization
|> cast(attrs, @required_fields ++ @optional_fields)
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 Expand Up @@ -31,12 +31,16 @@ defmodule Atomic.Partnerships.Partner do
timestamps()
end

@doc false
def changeset(partner, attrs) do
partner
|> cast(attrs, @required_fields ++ @optional_fields)
|> cast_attachments(attrs, [:image])
|> validate_required(@required_fields)
|> unique_constraint(:name)
end

def image_changeset(partner, attrs) do
partner
|> cast_attachments(attrs, [:image])
end
end
1 change: 0 additions & 1 deletion lib/atomic/organizations/users_organizations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ defmodule Atomic.Organizations.UserOrganization do
timestamps()
end

@doc false
def changeset(user_organization, attrs) do
user_organization
|> cast(attrs, @required_fields)
Expand Down
2 changes: 1 addition & 1 deletion lib/atomic/partnerships.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Atomic.Partnerships do
"""
use Atomic.Context

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 @@ -2,8 +2,8 @@ defmodule AtomicWeb.PartnerLive.Index do
use AtomicWeb, :live_view

alias Atomic.Organizations
alias Atomic.Organizations.Partner
alias Atomic.Partnerships
alias Atomic.Partnerships.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 @@ -19,5 +19,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
7 changes: 4 additions & 3 deletions priv/repo/seeds/accounts.exs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,16 @@ 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
{:error, changeset} ->
Mix.shell().error(Kernel.inspect(changeset.errors))

{:ok, _} ->
:ok
{:ok, changeset} ->
Repo.update!(Accounts.User.confirm_changeset(changeset))
end
end
end
Expand Down
Loading

0 comments on commit 36fc6ee

Please sign in to comment.