Skip to content

Commit

Permalink
[patch] EHealth-249: refactored list filter by ids, added ids params …
Browse files Browse the repository at this point in the history
…for employees and divisions
  • Loading branch information
PavelVesnin committed Jul 18, 2017
1 parent 8970fac commit 03da4a9
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 41 deletions.
35 changes: 35 additions & 0 deletions lib/prm/comma_param_uuid.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule PRM.CommaParamsUUID do
@moduledoc false

@behaviour Ecto.Type

alias Ecto.UUID

def type, do: :string

def cast(string) when is_binary(string) do
string
|> String.split(",")
|> valid_uuid_params?()
|> case do
true -> {:ok, string}
false -> :error
end
end

def cast(_), do: :error

def load(string) when is_binary(string), do: {:ok, string}

def dump(string) when is_binary(string), do: {:ok, string}
def dump(_), do: :error

def valid_uuid_params?(comma_params) do
Enum.reduce_while(comma_params, true, fn (i, acc) ->
case UUID.cast(i) do
{:ok, _} -> {:cont, acc}
_ -> {:halt, false}
end
end)
end
end
1 change: 1 addition & 0 deletions lib/prm/divisions/division_search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule PRM.Divisions.DivisionSearch do
use Ecto.Schema

schema "division_search" do
field :ids, PRM.CommaParamsUUID
field :name, :string
field :type, :string
field :legal_entity_id, Ecto.UUID
Expand Down
6 changes: 6 additions & 0 deletions lib/prm/divisions/divisions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule PRM.Divisions do
use PRM.Search

import Ecto.{Query, Changeset}, warn: false
import PRM.Entities, only: [convert_comma_params_to_where_in_clause: 3]

alias PRM.Repo
alias PRM.Entities.LegalEntity
Expand All @@ -17,6 +18,10 @@ defmodule PRM.Divisions do
|> search(params, Division, Confex.get(:prm, :divisions_per_page))
end

def get_search_query(Division = entity, %{ids: _} = changes) do
super(entity, convert_comma_params_to_where_in_clause(changes, :ids, :id))
end

def get_search_query(Division = division, changes) do
params =
changes
Expand Down Expand Up @@ -126,6 +131,7 @@ defmodule PRM.Divisions do

defp division_changeset(%DivisionSearch{} = division, attrs) do
fields = ~W(
ids
name
legal_entity_id
type
Expand Down
1 change: 1 addition & 0 deletions lib/prm/employees/employee_search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule PRM.Employees.EmployeeSearch do
use Ecto.Schema

schema "employee_search" do
field :ids, PRM.CommaParamsUUID
field :party_id, Ecto.UUID
field :division_id, Ecto.UUID
field :legal_entity_id, Ecto.UUID
Expand Down
6 changes: 6 additions & 0 deletions lib/prm/employees/employees.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule PRM.Employees do
use PRM.Search

import Ecto.{Query, Changeset}, warn: false
import PRM.Entities, only: [convert_comma_params_to_where_in_clause: 3]

alias PRM.Repo
alias PRM.Employees.Employee
Expand Down Expand Up @@ -44,6 +45,10 @@ defmodule PRM.Employees do
|> preload_relations(params)
end

def get_search_query(Employee = entity, %{ids: _} = changes) do
super(entity, convert_comma_params_to_where_in_clause(changes, :ids, :id))
end

def get_search_query(Employee = entity, changes) do
params =
changes
Expand Down Expand Up @@ -117,6 +122,7 @@ defmodule PRM.Employees do

defp employee_changeset(%EmployeeSearch{} = employee, attrs) do
fields = ~W(
ids
party_id
legal_entity_id
division_id
Expand Down
18 changes: 8 additions & 10 deletions lib/prm/entities/entities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ defmodule PRM.Entities do
import Ecto.{Query, Changeset}, warn: false

alias PRM.Repo
alias PRM.Entities.Validators
alias PRM.Entities.LegalEntity
alias PRM.Entities.LegalEntitySearch

Expand All @@ -20,12 +19,7 @@ defmodule PRM.Entities do
end

def get_search_query(LegalEntity = entity, %{ids: ids} = changes) do
changes =
changes
|> Map.delete(:ids)
|> Map.put(:id, {String.split(ids, ","), :in})

super(entity, changes)
super(entity, convert_comma_params_to_where_in_clause(changes, :ids, :id))
end

def get_search_query(LegalEntity = entity, %{settlement_id: settlement_id} = changes) do
Expand All @@ -42,6 +36,12 @@ defmodule PRM.Entities do
end
def get_search_query(entity, changes), do: super(entity, changes)

def convert_comma_params_to_where_in_clause(changes, param_name, db_field) do
changes
|> Map.put(db_field, {String.split(changes[param_name], ","), :in})
|> Map.delete(param_name)
end

def get_legal_entity!(id) do
LegalEntity
|> Repo.get!(id)
Expand Down Expand Up @@ -90,9 +90,7 @@ defmodule PRM.Entities do
mis_verified
)

legal_entity
|> cast(attrs, fields)
|> Validators.validate_comma_params_uuid(:ids)
cast(legal_entity, attrs, fields)
end

defp legal_entity_changeset(%LegalEntity{} = legal_entity, attrs) do
Expand Down
2 changes: 1 addition & 1 deletion lib/prm/entities/legal_entity_search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule PRM.Entities.LegalEntitySearch do

@primary_key {:id, :binary_id, autogenerate: false}
schema "legal_entity_search" do
field :ids, :string
field :ids, PRM.CommaParamsUUID
field :is_active, :boolean
field :nhs_verified, :boolean
field :edrpou, :string
Expand Down
28 changes: 0 additions & 28 deletions lib/prm/entities/validators.ex

This file was deleted.

4 changes: 2 additions & 2 deletions test/support/simple_factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ defmodule PRM.SimpleFactory do
party_user
end

def employee(employee_type \\ "DOCTOR") do
def employee(employee_type \\ "DOCTOR", status \\ nil) do
%{id: party_id} = party()
%{id: division_id} = division()
%{id: legal_entity_id} = legal_entity()

attrs = %{
"is_active" => true,
"position" => "some position",
"status" => "some status",
"status" => status || "some status",
"employee_type" => employee_type,
"end_date" => ~D[2010-04-17],
"start_date" => ~D[2010-04-17],
Expand Down
18 changes: 18 additions & 0 deletions test/web/controllers/division_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ defmodule PRM.Web.DivisionControllerTest do
resp = json_response(conn, 200)["data"]
assert 0 == length(resp)
end

test "search divisions by ids and type", %{conn: conn} do
fixture(:legal_entity)
%{id: id} = division()
%{id: id_2} = division("clinic")
%{id: id_3} = division("clinic")
ids = [id, id_2, id_3, UUID.generate()]

conn = get conn, division_path(conn, :index, [ids: Enum.join(ids, ","), type: "clinic"])
resp = json_response(conn, 200)

assert Map.has_key?(resp, "paging")
assert 2 == length(resp["data"])
Enum.each(resp["data"], fn (%{"id" => l_id}) ->
assert l_id in [id_2, id_3]
end)
refute resp["paging"]["has_more"]
end
end

test "set divisions mountain group by settlement_id", %{conn: conn} do
Expand Down
17 changes: 17 additions & 0 deletions test/web/controllers/employee_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,23 @@ defmodule PRM.Web.EmployeeControllerTest do
assert Enum.at(resp, 0)["id"] == doctor.id
end

test "search employee by ids and status", %{conn: conn} do
%{id: id_1} = employee("DOCTOR", "ACTIVE")
%{id: id_2} = employee("DOCTOR", "ACTIVE")
%{id: id_3} = employee("DOCTOR", "NON_ACTIVE")
employee()
ids = [id_1, id_2, id_3]

conn = get conn, employee_path(conn, :index, [status: "ACTIVE", ids: Enum.join(ids, ",")])
resp = json_response(conn, 200)
assert Map.has_key?(resp, "paging")
assert 2 == length(resp["data"])
Enum.each(resp["data"], fn (%{"id" => e_id}) ->
assert e_id in [id_1, id_2]
end)
refute resp["paging"]["has_more"]
end

test "search employees by legal_entity_id", %{conn: conn} do
hr = "hr" |> employee() |> Repo.preload(:legal_entity)

Expand Down

0 comments on commit 03da4a9

Please sign in to comment.