Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small refactoring #277

Open
StefanHoutzager opened this issue Oct 17, 2018 · 0 comments
Open

small refactoring #277

StefanHoutzager opened this issue Oct 17, 2018 · 0 comments

Comments

@StefanHoutzager
Copy link

StefanHoutzager commented Oct 17, 2018

I just for fun refactored a couple of functions in avia/apps/admin_app/lib/admin_app/order/order.ex.
The get_rummage function contains a risky String.to_atom call (see https://hexdocs.pm/elixir/String.html#to_atom/1). I removed it. The rest of the changes could speak for itself (feel free to ask if you have questions). There is one order_list function now. I have no elixir editor from where I work, so maybe there are errors in the code.

  def order_list(order_state, sort_order) do
    orders =
      get_rummage(sort_order)
      |> query_orders(order_state)
      |> load_orders()
	  
    case order_state do
      "complete" ->  orders 
      _          ->  orders |> Enum.filter(fn order -> filter_order?(order.packages, order_state) end)
    end  
  end
  
  defp filter_order?(packages, "pending"), do: 
    Enum.member?(packages, "processing")
 
  defp filter_order?(packages, "unshipped"), do: 
    Enum.member?(packages, "ready")
	
  defp filter_order?(packages, "shipped"), do: 
    Enum.any?(packages, fn package ->
      package.state == "shipped" || package.state == "delivered"
    end)
  
  defp get_rummage(sort_order) do
    case sort_order do
      nil ->
        %{}

      "asc" ->
        %{
          sort: %{field: :inserted_at, order: :asc}
        }
      "desc" ->
        %{
          sort: %{field: :inserted_at, order: :desc}
        }		
    end
  end  
  
  defp query_orders(rummage, order_state) do
    {queryable, _rummage} = Order.rummage(rummage)

    case order_state do
      complete" -> from(p in queryable, where: p.state == "complete")
      _         -> from(p in queryable, where: p.state == "confirmed") 
    end
  end  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant