fix bug when creating walk in bookings

This commit is contained in:
JasterV 2026-02-02 20:14:58 +01:00
parent 1398a2088d
commit d5115e8cd8
3 changed files with 29 additions and 6 deletions

View file

@ -243,11 +243,11 @@ defmodule SpazioSolazzo.BookingSystem.Booking do
change manage_relationship(:space_id, :space, type: :append_and_remove)
validate fn changeset, _ctx ->
start_datetime = Ash.Changeset.get_argument(changeset, :start_datetime)
end_datetime = Ash.Changeset.get_argument(changeset, :end_datetime)
now = DateTime.utc_now()
if start_datetime && DateTime.compare(start_datetime, now) == :lt do
{:error, field: :start_datetime, message: "cannot be in the past"}
if end_datetime && DateTime.compare(end_datetime, now) == :lt do
{:error, field: :end_datetime, message: "cannot be in the past"}
else
:ok
end

View file

@ -812,9 +812,9 @@ defmodule SpazioSolazzo.BookingSystem.BookingTest do
assert String.contains?(error_messages, "must be after start datetime")
end
test "rejects walk-in in the past", %{space: space} do
test "rejects walk-in with end time in the past", %{space: space} do
start_datetime = DateTime.utc_now() |> DateTime.add(-2, :hour)
end_datetime = DateTime.add(start_datetime, 1, :hour)
end_datetime = DateTime.utc_now() |> DateTime.add(-1, :hour)
assert {:error, error} =
BookingSystem.create_walk_in(
@ -831,6 +831,25 @@ defmodule SpazioSolazzo.BookingSystem.BookingTest do
assert String.contains?(error_messages, "cannot be in the past")
end
test "allows walk-in with start time in the past but end time in the future", %{space: space} do
start_datetime = DateTime.utc_now() |> DateTime.add(-1, :hour)
end_datetime = DateTime.utc_now() |> DateTime.add(2, :hour)
assert {:ok, booking} =
BookingSystem.create_walk_in(
space.id,
start_datetime,
end_datetime,
"Walk-in Customer",
"walkin@example.com",
nil,
nil
)
assert booking.state == :accepted
assert booking.customer_name == "Walk-in Customer"
end
test "rejects walk-in with invalid email", %{space: space} do
start_datetime = DateTime.utc_now() |> DateTime.add(1, :hour)
end_datetime = DateTime.add(start_datetime, 2, :hour)

View file

@ -66,7 +66,11 @@ defmodule SpazioSolazzo.BookingSystem.TimeSlotTemplateTest do
)
error_messages = Ash.Error.error_descriptions(error)
assert String.contains?(error_messages, "overlaps with existing time slot")
assert String.contains?(
error_messages,
"time slot overlaps with existing template for this space and day."
)
end
test "allows same time slot on different days", %{space: space} do