Skip to content
Closed
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
18 changes: 18 additions & 0 deletions lib/bike_brigade/delivery.ex
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,24 @@ defmodule BikeBrigade.Delivery do
|> Repo.preload([:program, :tasks])
end

@doc """
Returns campaigns whose delivery window ended within the given time range.

## Parameters
- `from_datetime` - Start of the time window (inclusive)
- `to_datetime` - End of the time window (inclusive)
"""
def list_campaigns_ended_between(from_utc_datetime, to_utc_datetime) do
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the params should be from_datetime and to_datetime like in the docs above

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the changes in the PR: #486

query =
from c in Campaign,
where:
c.delivery_end >= ^from_utc_datetime and
c.delivery_end <= ^to_utc_datetime,
select: c

Repo.all(query)
end

@doc """
Fetches how many open vs filled tasks there are (optionally, by week)
and groups them by campaign ID.
Expand Down
34 changes: 34 additions & 0 deletions test/bike_brigade/delivery_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -345,5 +345,39 @@ defmodule BikeBrigade.DeliveryTest do
end
end

describe "list_campaigns_ended_between/2" do
setup do
now = get_utc_now()

campaign =
fixture(:campaign, %{
delivery_start: NaiveDateTime.add(now, -7, :hour),
delivery_end: NaiveDateTime.add(now, -1, :hour)
})

%{campaign: campaign, now: now}
end

test "returns a campaign available in the given window", %{
campaign: campaign,
now: now
} do
from_datetime = NaiveDateTime.add(now, -75, :minute)
to_datetime = NaiveDateTime.add(now, -60, :minute)

[ended_campaign] = Delivery.list_campaigns_ended_between(from_datetime, to_datetime)
assert campaign.id == ended_campaign.id
end

test "returns no campaign in the given window", %{now: now} do
from_datetime = NaiveDateTime.add(now, -45, :minute)
to_datetime = NaiveDateTime.add(now, -30, :minute)

assert [] == Delivery.list_campaigns_ended_between(from_datetime, to_datetime)
end
end

def item_name(%Task{task_items: [%{item: %{name: item_name}}]}), do: item_name

defp get_utc_now(), do: NaiveDateTime.utc_now()
end
Loading