Skip to content

Commit

Permalink
fix: Deleting with 'select all' was not including relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
staylorwr committed Dec 14, 2020
1 parent 919df35 commit 8945a1b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/ex_teal/resource/delete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,17 @@ defmodule ExTeal.Resource.Delete do
|> Serializer.render_errors(errors)
end

defp find_deleteable(resource, %Conn{params: %{"resources" => "all"}} = conn) do
defp find_deleteable(resource, %Conn{params: %{"resources" => "all"} = params}) do
resource.model()
|> Index.field_filters(conn.params, resource)
|> Index.filter_via_relationships(params)
|> Index.field_filters(params, resource)
|> Index.search(params, resource)
end

defp find_deleteable(resource, %Conn{params: %{"resources" => ids}} = conn) do
defp find_deleteable(resource, %Conn{params: %{"resources" => ids}}) do
ids = ids |> String.split(",") |> Enum.map(&String.to_integer/1)

resource.model()
|> Index.field_filters(conn.params, resource)
|> where([r], r.id in ^ids)
end
end
45 changes: 45 additions & 0 deletions test/ex_teal/resource/delete_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule ExTeal.Resource.DeleteTest do
use TestExTeal.ConnCase
alias ExTeal.Resource.Delete
alias TestExTeal.{Post, Repo}

defmodule FailingOnDeleteResource do
use ExTeal.Resource
Expand Down Expand Up @@ -60,6 +61,50 @@ defmodule ExTeal.Resource.DeleteTest do
assert response.status == 204
end

@tag manifest: TestExTeal.DefaultManifest
test "'all' filters based on filter fields" do
p = insert(:post, name: "Foo")
not_selected = insert(:post)
filters = [%{"field" => "name", "operator" => "=", "operand" => "Foo"}]
encoded_filters = filters |> Jason.encode!() |> :base64.encode()

conn =
build_conn(:delete, "/posts/", %{
"field_filters" => encoded_filters,
"resources" => "all",
"action" => "publish-post"
})
|> fetch_query_params()

response = Delete.call(TestExTeal.PostResource, conn)
assert response.status == 204
refute Repo.get(Post, p.id)
assert Repo.get(Post, not_selected.id)
end

@tag manifest: TestExTeal.DefaultManifest
test "filters based on relationships" do
u = insert(:user)
p = insert(:post, user: u)
not_related = insert(:post)

conn =
build_conn(:delete, "posts", %{
"via_resource" => "users",
"via_resource_id" => "#{u.id}",
"via_relationship" => "posts",
"relationship_type" => "hasMany",
"resources" => "all"
})
|> fetch_query_params()

response = Delete.call(TestExTeal.PostResource, conn)

assert response.status == 204
refute Repo.get(Post, p.id)
assert Repo.get(Post, not_related.id)
end

def prep_conn(method, path, params \\ %{}) do
params = Map.merge(params, %{"_format" => "json"})

Expand Down

0 comments on commit 8945a1b

Please sign in to comment.