diff --git a/lib/store/inventory.ex b/lib/store/inventory.ex index 8ebb9fb..69f8c61 100644 --- a/lib/store/inventory.ex +++ b/lib/store/inventory.ex @@ -344,7 +344,7 @@ defmodule Store.Inventory do alias Store.Accounts.User def purchase(%User{} = user, %Product{} = product, product_params) do - order = get_user_draft_order(user) + order = get_order_draft_by_id(user.id, preloads: [:user, :products]) case order do %Order{} -> @@ -355,13 +355,6 @@ defmodule Store.Inventory do end end - defp get_user_draft_order(user) do - Order - |> where(user_id: ^user.id, status: :draft) - |> Repo.one() - |> Repo.preload([:user, :products]) - end - defp handle_existing_order(order, product, product_params) do quantity = String.to_integer(product_params["quantity"]) @@ -606,8 +599,7 @@ defmodule Store.Inventory do def get_order_draft_by_id(user_id, opts) when is_list(opts) do Order - |> where(user_id: ^user_id) - |> where(status: :draft) + |> where(user_id: ^user_id, status: :draft) |> Repo.one() |> apply_filters(opts) end