Skip to content

Commit

Permalink
Add module to get the supplies by expiration date
Browse files Browse the repository at this point in the history
  • Loading branch information
erickmp07 committed Oct 21, 2021
1 parent dd23f2e commit 260b051
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/inmana/supplies/get_by_expiration.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Inmana.Supplies.GetByExpiration do
import Ecto.Query

alias Inmana.{Repo, Restaurant, Supply}

def call do
today = Date.utc_today()
beginning_of_week = Date.beginning_of_week(today, :sunday)
end_of_week = Date.end_of_week(today, :sunday)

query = from supply in Supply,
where:
supply.expiration_date >= ^beginning_of_week and supply.expiration_date <= ^end_of_week,
preload: [:restaurant]

query
|> Repo.all()
|> Enum.group_by(fn %Supply{restaurant: %Restaurant{email: email}} -> email end)
end
end

0 comments on commit 260b051

Please sign in to comment.