Skip to content

Commit

Permalink
Merge pull request #77 from otakulan/feature/tgh/extend-reservations
Browse files Browse the repository at this point in the history
Allow reservation time to be extended
  • Loading branch information
starcraft66 authored Feb 18, 2024
2 parents dd22cf4 + 8700fc8 commit d91efb3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
60 changes: 59 additions & 1 deletion lib/lanpartyseating/logic/reservation_logic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
)

Expand All @@ -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,
Expand Down
20 changes: 19 additions & 1 deletion lib/lanpartyseating_web/components/cancellation_modal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,29 @@ defmodule CancellationModalComponent do
"%H:%M"
) %> *REPLACE WITH COUNTDOWN*</b>
</p>
<p class="py-4">Enter a reason for canceling the reservation</p>
<form method="dialog">
<button class="btn btn-sm btn-circle btn-ghost absolute right-2 top-2">✕</button>
</form>
<p class="py-4">Enter an amount of minutes to extend the reservation by</p>
<form phx-submit="extend_reservation">
<input type="hidden" name="station_id" value={"#{@station.id}"}>
<input type="hidden" name="station_number" value={"#{@station.station_number}"}>
<input type="text" placeholder="Minutes to add" value="5" class="w-full max-w-xs input input-bordered" name="minutes_increment"/>
<div class="modal-action">
<button
class="btn btn-success"
x-on:click={"$refs.station_modal_#{@station.station_number}.close()"}
type="submit">
Add time to reservation
</button>
</div>
</form>
<p class="py-4">Enter a reason for canceling the reservation</p>
<form phx-submit="cancel_station">
<input type="hidden" name="station_id" value={"#{@station.id}"}>
<input type="hidden" name="station_number" value={"#{@station.station_number}"}>
Expand Down
13 changes: 13 additions & 0 deletions lib/lanpartyseating_web/live/cancellation_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit d91efb3

Please sign in to comment.