8000 fix(huddl): check a new huddl's start in its own time zone by mwoods79 · Pull Request #686 · huddlz-hq/huddlz · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -6685,7 +6685,7 @@ body .form-row-inline {
flex-direction: row;
flex-wrap: wrap;
gap: 14px;
align-items: flex-end;
align-items: flex-start;
}

body .form-row-inline > div { display: flex; flex-direction: column; gap: 6px; }
Expand Down
4 changes: 1 addition & 3 deletions lib/huddlz/communities/huddl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ defmodule Huddlz.Communities.Huddl do
end

validate one_of(:lifecycle_state, [:draft, :published])
validate Huddlz.Communities.Huddl.Validations.FutureDateValidation

validate present(:frequency) do
where argument_equals(:is_recurring, true)
Expand All @@ -301,6 +300,7 @@ defmodule Huddlz.Communities.Huddl do
change Huddlz.Communities.Huddl.Changes.DefaultTimeZoneFromGroup
change Huddlz.Communities.Huddl.Changes.ApplySavedLocation
change Huddlz.Communities.Huddl.Changes.CalculateDateTimeFromInputs
validate Huddlz.Communities.Huddl.Validations.FutureStart

validate Huddlz.Communities.Huddl.Validations.RecurrenceBoundary do
where argument_equals(:is_recurring, true)
Expand Down Expand Up @@ -947,8 +947,6 @@ defmodule Huddlz.Communities.Huddl do
publish :complete, [:id]
end

# changes section removed - validation is handled by FutureDateValidation module

validations do
validate string_length(:title, min: 3, max: 200) do
message "Must be between 3 and 200 characters"
Expand Down
17 changes: 3 additions & 14 deletions lib/huddlz/communities/huddl/changes/copy_from_huddl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,11 @@ defmodule Huddlz.Communities.Huddl.Changes.CopyFromHuddl do

# A copy is scheduled either by date (start time and duration default to
# the source's) or by a start timestamp (the end defaults to the source's
# length). Either way it must start in the future.
# length). Either way the action's FutureStart validation requires it to
# start in the future.
defp put_schedule(changeset, source) do
if supplied?(changeset, :starts_at) do
changeset
|> default_ends_at(source)
|> Ash.Changeset.before_action(&require_future_start/1)
default_ends_at(changeset, source)
else
changeset
|> require_date()
Expand All @@ -150,16 +149,6 @@ defmodule Huddlz.Communities.Huddl.Changes.CopyFromHuddl do
end
end

defp require_future_start(changeset) do
starts_at = Ash.Changeset.get_attribute(changeset, :starts_at)

if DateTime.after?(starts_at, DateTime.utc_now()) do
changeset
else
Ash.Changeset.add_error(changeset, field: :starts_at, message: "must be in the future")
end
end

defp require_date(changeset) do
if is_nil(Ash.Changeset.get_argument(changeset, :date)) do
Ash.Changeset.add_error(changeset,
Expand Down
6 changes: 5 additions & 1 deletion lib/huddlz/communities/huddl/recurrence_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,17 @@ defmodule Huddlz.Communities.Huddl.RecurrenceHelper do
context = Keyword.get(opts, :context, %{})
metadata = Map.put(context[:paper_trail_metadata] || %{}, :automatic?, true)

# Instances follow the series wherever it lands, including into the past
# when an organizer edits an earlier huddl, so they skip FutureStart.
create_opts = Keyword.put(opts, :context, Map.put(context, :allow_past_start?, true))

instance =
Huddl
|> Ash.Changeset.new()
|> Ash.Changeset.for_create(
:create,
instance_attrs(source, starts_at, ends_at, template),
opts
create_opts
)
|> Ash.Changeset.set_context(%{paper_trail_metadata: metadata})
# Preserve series authorship after SetCreatorToActor runs, while the
Expand Down
44 changes: 0 additions & 44 deletions lib/huddlz/communities/huddl/validations/future_date_validation.ex

This file was deleted.

38 changes: 38 additions & 0 deletions lib/huddlz/communities/huddl/validations/future_start.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule Huddlz.Communities.Huddl.Validations.FutureStart do
@moduledoc """
Requires a new huddl, including a copy, to start after now.

Runs once the start has been resolved in the huddl time zone, so a huddl
later today counts as future wherever it meets. A day before today flags
the date; a start time already passed today flags the start time. A huddl
given a start timestamp instead of a date flags that timestamp.

Code that records a huddl rather than scheduling one, such as a series
generating its own instances or the dev seeds' history, sets the private
`allow_past_start?` context to skip this check.
"""

use Ash.Resource.Validation

@impl true
def validate(changeset, _opts, _context) do
starts_at = Ash.Changeset.get_attribute(changeset, :starts_at)

cond do
changeset.context[:allow_past_start?] -> :ok
is_nil(starts_at) or DateTime.after?(starts_at, DateTime.utc_now()) -> :ok
date = Ash.Changeset.get_argument(changeset, :date) -> past_schedule(changeset, date)
true -> past(:starts_at)
end
end

defp past_schedule(changeset, date) do
time_zone = Ash.Changeset.get_attribute(changeset, :time_zone)

if Date.before?(date, DateTime.to_date(DateTime.now!(time_zone))),
do: past(:date),
else: past(:start_time)
end

defp past(field), do: {:error, field: field, message: "must be in the future"}
end
12 changes: 9 additions & 3 deletions priv/repo/dev_seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,9 @@ if Enum.empty?(existing_groups) do
group_location_id: book_location.id,
group_id: book_group.id
},
actor: bob
actor: bob,
# Seeded history and in-progress huddlz start before now.
context: %{allow_past_start?: true}
)
|> Ash.create()

Expand Down Expand Up @@ -383,7 +385,9 @@ if Enum.empty?(existing_groups) do
group_location_id: hiking_location.id,
group_id: hiking_group.id
},
actor: carol
actor: carol,
# Seeded history and in-progress huddlz start before now.
context: %{allow_past_start?: true}
)
|> Ash.create()

Expand Down Expand Up @@ -415,7 +419,9 @@ if Enum.empty?(existing_groups) do
virtual_link: "https://zoom.us/j/live#{i}",
group_id: phoenix_group.id
},
actor: alice
actor: alice,
# Seeded history and in-progress huddlz start before now.
context: %{allow_past_start?: true}
)
|> Ash.create()

Expand Down
7 changes: 7 additions & 0 deletions test/features/copy_huddl.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ Feature: Copy a huddl
Then the group has a new upcoming "Hands-on with Ash Framework"
And the past huddl is unchanged

Scenario: Copying onto a time later than now in the huddl's time zone
Given I organize a group with a past huddl "Hands-on with Ash Framework"
When I visit that huddl and choose to copy it
And I set it to start an hour from now
And I schedule the huddl
Then the group has a new huddl starting an hour from now

Scenario: Copying from the organizer list
Given I organize a group with a past huddl "Hands-on with Ash Framework"
When I copy it from the past huddlz on the organizer page
Expand Down
7 changes: 7 additions & 0 deletions test/features/location_based_time_zones.feature
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ Feature: Location-based time zones
Then I should see "does not exist in America/New_York because of daylight saving time"
And the huddl "Spring Forward Huddl" should not exist

Scenario: A huddl that already started today is refused in its own time zone
Given my group "Tokyo Neighbors" is based in "Asia/Tokyo"
When I try to schedule a virtual huddl that started earlier today
Then the "Start time" field says "must be in the future"
And the "Date" field has no error
And the huddl "Earlier Today Huddl" should not exist

Scenario: Calendar groups by the browser zone while showing the huddl zone
Given I am attending a Denver huddl at 11:30 PM on July 15, 2030
When I view Calendar from a browser in "America/New_York"
Expand Down
26 changes: 26 additions & 0 deletions test/features/schedule_huddl_api.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@async @database @conn @schedule_huddl_api
Feature: Schedule a huddl through the API
As an organizer using an API client
I want a huddl that has already started to be refused
So that the huddlz I schedule are ones people can still attend

Scenario Outline: A huddl cannot start in the past
Given I organize a group as an API client
When I schedule a virtual huddl through "<api>" starting an hour ago
Then the huddl is refused because "starts_at" "must be in the future"
And my group has no huddlz

Examples:
| api |
| JSON:API |
| GraphQL |

Scenario Outline: A huddl can start later today
Given I organize a group as an API client
When I schedule a virtual huddl through "<api>" starting an hour from now
Then my group has that huddl

Examples:
| api |
| JSON:API |
| GraphQL |
25 changes: 25 additions & 0 deletions test/features/step_definitions/copy_huddl_steps.exs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ defmodule CopyHuddlSteps do
Map.update!(context, :session, &click_button(&1, "Schedule huddl"))
end

# West of UTC, the start read as UTC lands hours before now, which is how
# a huddl later today used to be refused.
step "I set it to start an hour from now", context do
starts_at =
DateTime.utc_now()
|> DateTime.add(1, :hour)
|> DateTime.truncate(:second)
|> Map.put(:second, 0)

local = DateTime.shift_zone!(starts_at, context.source.time_zone)

session =
context.session
|> fill_in("Date", with: Date.to_iso8601(DateTime.to_date(local)))
|> fill_in("Start time", with: Calendar.strftime(local, "%H:%M"))

Map.merge(context, %{session: session, chosen_start: starts_at})
end

step "the group has a new huddl starting an hour from now", context do
[copy] = copies(context)
assert DateTime.compare(copy.starts_at, context.chosen_start) == :eq
Map.put(context, :copy, copy)
end

step "I leave the form without saving", context do
Map.update!(context, :session, &click_link(&1, "Cancel"))
end
Expand Down
57 changes: 57 additions & 0 deletions test/features/step_definitions/location_based_time_zones_steps.exs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,42 @@ defmodule LocationBasedTimeZonesSteps do
Map.merge(context, %{session: session, conn: session})
end

# East of UTC, the start read as UTC lands hours after now, which is how a
# huddl that already started used to be accepted. An hour ago, but never
# before midnight, so the date stays today in the group's time zone.
step "I try to schedule a virtual huddl that started earlier today", context do
group = List.first(context.groups)
now = DateTime.now!(group.time_zone)
an_hour_ago = DateTime.add(now, -1, :hour)

start_time =
if DateTime.to_date(an_hour_ago) == DateTime.to_date(now),
do: Calendar.strftime(an_hour_ago, "%H:%M"),
else: "00:00"

session =
(context[:session] || context[:conn])
|> visit("/groups/#{group.slug}/huddlz/new")
|> choose("Virtual")
|> fill_in("Title", with: "Earlier Today Huddl", exact: false)
|> fill_in("Date", with: Date.to_iso8601(DateTime.to_date(now)), exact: false)
|> fill_in("Start time", with: start_time, exact: false)
|> fill_in("Online link", with: "https://example.com/earlier", exact: false)
|> click_button("Schedule huddl")

Map.merge(context, %{session: session, conn: session})
end

step "the {string} field says {string}", %{args: [label, message]} = context do
assert message in field_errors(context.session, label)
context
end

step "the {string} field has no error", %{args: [label]} = context do
assert field_errors(context.session, label) == []
context
end

step "the huddl {string} should not exist", %{args: [title]} = context do
huddl =
Huddlz.Communities.Huddl
Expand Down Expand Up @@ -840,4 +876,25 @@ defmodule LocationBasedTimeZonesSteps do
mountain_location: mountain_location
})
end

# The errors announced with a field: the alerts among its accessible
# description.
defp field_errors(session, label) do
doc = session.view |> Phoenix.LiveViewTest.render() |> LazyHTML.from_fragment()

field_label =
doc
|> LazyHTML.query("label")
|> Enum.find(&(&1 |> LazyHTML.text() |> String.trim() == label))

assert field_label, "no #{label} field on the page"
[id] = LazyHTML.attribute(field_label, "for")

doc
|> LazyHTML.query("[id='#{id}']")
|> LazyHTML.attribute("aria-describedby")
|> Enum.flat_map(&String.split/1)
|> Enum.flat_map(&(doc |> LazyHTML.query("[id='#{&1}'][role='alert']") |> Enum.to_list()))
|> Enum.map(&(&1 |> LazyHTML.text() |> String.trim()))
end
end
Loading
Loading
0