Skip to content

Commit

Permalink
fix: transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ruioliveira02 committed Jan 24, 2025
1 parent 6912a97 commit 9c28ed5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
8 changes: 1 addition & 7 deletions lib/safira/activities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,7 @@ defmodule Safira.Activities do
|> Repo.transaction()
end

def create_enrolment(attrs) do
%Enrolment{}
|> Enrolment.changeset(attrs)
|> Repo.insert()
end

def delete_enrolment(%Enrolment{} = enrolment, attrs \\ %{}) do
def delete_enrolment(%Enrolment{} = enrolment) do
Repo.delete(enrolment)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ defmodule SafiraWeb.Backoffice.ScheduleLive.EnrolmentLive.FormComponent do
if valid_enrolments do
# For each enrolment, update or create it
Enum.each(enrolments, fn {_, _, _enrolment, form} ->
Activities.create_enrolment(form.params)
Activities.enrol(form.params.attendee_id, form.params.activity_id)
end)

{:noreply,
Expand Down
1 change: 1 addition & 0 deletions lib/safira_web/live/landing/components/schedule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ defmodule SafiraWeb.Landing.Components.Schedule do
:if={enrolments_enabled(@activity, @user_role, @enrolments)}
class="relative hover:underline cursor-pointer -mr-3 font-iregular text-lg text-accent sm:mr-1"
phx-click="enrol"
data-confirm={"#{gettext("You are enrolling for")} #{@activity.title}. #{gettext("This action cannot be undone. Are you sure?")}"}
phx-value-activity_id={@activity.id}
>
<%= gettext("Enrol") %>
Expand Down
11 changes: 10 additions & 1 deletion lib/safira_web/live/landing/home_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule SafiraWeb.Landing.HomeLive.Index do
|> assign(:has_sponsors?, Companies.get_companies_count() > 0)
|> assign(:has_schedule?, Activities.get_activities_count() > 0)
|> assign(:enrolments, get_enrolments(socket.assigns.current_user))
|> assign(:user_role, get_user_role(socket.assigns.current_user))
|> stream(:speakers, speakers |> Enum.shuffle())}
end

Expand All @@ -35,7 +36,7 @@ defmodule SafiraWeb.Landing.HomeLive.Index do
{:noreply,
socket
|> put_flash(:error, gettext("You must be logged in to enrol in activities"))
|> push_navigate(to: ~p"/users/log_in")}
|> redirect(to: ~p"/users/log_in?redirect=/")}
else
if socket.assigns.current_user.type == :attendee do
actual_enrol(activity_id, socket)
Expand Down Expand Up @@ -68,4 +69,12 @@ defmodule SafiraWeb.Landing.HomeLive.Index do
|> put_flash(:error, gettext("Something happened"))}
end
end

defp get_user_role(user) do
if is_nil(user) do
:attendee
else
user.type
end
end
end
2 changes: 1 addition & 1 deletion lib/safira_web/live/landing/home_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
url={~p"/"}
params={@params}
enrolments={@enrolments}
user_role={Map.get(@current_user, :type, :attendee)}
user_role={@user_role}
/>
</div>
<div class="xl:px-[15rem] md:px-[8rem] px-[2.5rem]">
Expand Down

0 comments on commit 9c28ed5

Please sign in to comment.