diff --git a/lib/lanpartyseating/logic/reservation_logic.ex b/lib/lanpartyseating/logic/reservation_logic.ex index 03ece6b..30a8407 100644 --- a/lib/lanpartyseating/logic/reservation_logic.ex +++ b/lib/lanpartyseating/logic/reservation_logic.ex @@ -43,7 +43,8 @@ defmodule Lanpartyseating.ReservationLogic do "new_reservation", %{ station_number: station_number, - # reservation: updated + start_date: updated.start_date |> DateTime.to_iso8601(), + end_date: updated.end_date |> DateTime.to_iso8601(), } ) @@ -66,6 +67,63 @@ defmodule Lanpartyseating.ReservationLogic do end end + def extend_reservation(_id, minutes) when minutes <= 0 do + {:error, "Reservations can only be extended by a positive non-zero amount of minutes"} + end + + def extend_reservation(id, minutes) do + existing_reservation = + from(r in Reservation, + where: r.station_id == ^id, + where: is_nil(r.deleted_at), + join: s in assoc(r, :station), + preload: [station: s] + ) + |> Repo.one() + + new_end_date = DateTime.add(existing_reservation.end_date, minutes, :minute) + updated_reservation = + Ecto.Changeset.change(existing_reservation, + end_date: new_end_date + ) + + reservation = with {:ok, reservation} <- Repo.update(updated_reservation) do + # Terminate the reservation expiration task with the old end date + GenServer.cast(:"expire_reservation_#{reservation.id}", :terminate) + + # Start a new reservation expiration task with the new end date + DynamicSupervisor.start_child( + Lanpartyseating.ExpirationTaskSupervisor, + {Lanpartyseating.Tasks.ExpireReservation, {new_end_date, reservation.id}} + ) + + Endpoint.broadcast!( + "desktop:all", + "extend_reservation", + %{ + station_number: reservation.station.station_number, + start_date: reservation.start_date |> DateTime.to_iso8601(), + end_date: reservation.end_date |> DateTime.to_iso8601(), + } + ) + + {:ok, stations} = StationLogic.get_all_stations() + + Phoenix.PubSub.broadcast( + PubSub, + "station_update", + {:stations, stations} + ) + + reservation + else + {:error, err} -> + {:error, {:reservation_failed, err}} + end + + {:ok, reservation} + end + def cancel_reservation(id, reason) do cancelled = from(r in Reservation, diff --git a/lib/lanpartyseating_web/components/cancellation_modal.ex b/lib/lanpartyseating_web/components/cancellation_modal.ex index 8c83fa5..0879678 100644 --- a/lib/lanpartyseating_web/components/cancellation_modal.ex +++ b/lib/lanpartyseating_web/components/cancellation_modal.ex @@ -74,11 +74,29 @@ defmodule CancellationModalComponent do "%H:%M" ) %> *REPLACE WITH COUNTDOWN*
-Enter a reason for canceling the reservation
+Enter an amount of minutes to extend the reservation by
+ + + +Enter a reason for canceling the reservation
+