From 260b051926e32ae918bd42a35ef88842f1ba211f Mon Sep 17 00:00:00 2001 From: Erick Macedo Pinto Date: Thu, 21 Oct 2021 01:44:45 -0300 Subject: [PATCH] Add module to get the supplies by expiration date --- lib/inmana/supplies/get_by_expiration.ex | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 lib/inmana/supplies/get_by_expiration.ex diff --git a/lib/inmana/supplies/get_by_expiration.ex b/lib/inmana/supplies/get_by_expiration.ex new file mode 100644 index 0000000..dd5a527 --- /dev/null +++ b/lib/inmana/supplies/get_by_expiration.ex @@ -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