From 6853002fdc224214f55b3eec7876a5ab8ddef14d Mon Sep 17 00:00:00 2001 From: Tristan Gosselin-Hane Date: Wed, 24 Jan 2024 01:26:24 -0500 Subject: [PATCH 1/3] Send reservation start and end times to desktop client --- lib/lanpartyseating/logic/reservation_logic.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/lanpartyseating/logic/reservation_logic.ex b/lib/lanpartyseating/logic/reservation_logic.ex index 03ece6b..4e829d7 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(), } ) From 0a4cd323a3d37e26ecc312ee3f2ca4b8dc4b63f5 Mon Sep 17 00:00:00 2001 From: Tristan Gosselin-Hane Date: Wed, 24 Jan 2024 01:28:31 -0500 Subject: [PATCH 2/3] Add api to extend reservation time --- .../logic/reservation_logic.ex | 57 +++++++++++++++++++ .../live/cancellation_live.ex | 13 +++++ 2 files changed, 70 insertions(+) diff --git a/lib/lanpartyseating/logic/reservation_logic.ex b/lib/lanpartyseating/logic/reservation_logic.ex index 4e829d7..30a8407 100644 --- a/lib/lanpartyseating/logic/reservation_logic.ex +++ b/lib/lanpartyseating/logic/reservation_logic.ex @@ -67,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/live/cancellation_live.ex b/lib/lanpartyseating_web/live/cancellation_live.ex index aa41162..135f804 100644 --- a/lib/lanpartyseating_web/live/cancellation_live.ex +++ b/lib/lanpartyseating_web/live/cancellation_live.ex @@ -51,6 +51,19 @@ defmodule LanpartyseatingWeb.CancellationLive do {:noreply, socket} end + def handle_event( + "extend_reservation", + %{"station_id" => id, "station_number" => _station_number, "minutes_increment" => minutes}, + socket + ) do + {:ok, _} = ReservationLogic.extend_reservation( + String.to_integer(id), + String.to_integer(minutes) + ) + + {:noreply, socket} + end + def handle_event( "cancel_station", %{"station_id" => id, "station_number" => _station_number, "cancel_reason" => reason}, From 8700fc87a0e1b43d8c12881ffa319cda4961a75d Mon Sep 17 00:00:00 2001 From: Tristan Gosselin-Hane Date: Wed, 24 Jan 2024 01:28:50 -0500 Subject: [PATCH 3/3] Add UI to cancellation modal to extend reservation duration --- .../components/cancellation_modal.ex | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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

+