-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add module to get the supplies by expiration date
- Loading branch information
Showing
1 changed file
with
20 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |