diff --git a/lib/decentralised_book_index/metadata/book.ex b/lib/decentralised_book_index/metadata/book.ex index 34f8543..990eea6 100644 --- a/lib/decentralised_book_index/metadata/book.ex +++ b/lib/decentralised_book_index/metadata/book.ex @@ -83,8 +83,10 @@ defmodule DecentralisedBookIndex.Metadata.Book do prepare fn query, context -> author = query.arguments.author + {:ok, author_ids} = Metadata.get_author_ids(author) + Metadata.Book - |> Ash.Query.filter(expr(exists(author_roles, author_id == ^author.id))) + |> Ash.Query.filter(expr(exists(author_roles, author_id in ^author_ids))) end end end diff --git a/test/decentralised_book_index/metadata/book_test.exs b/test/decentralised_book_index/metadata/book_test.exs index 02d35d6..bf13449 100644 --- a/test/decentralised_book_index/metadata/book_test.exs +++ b/test/decentralised_book_index/metadata/book_test.exs @@ -72,5 +72,39 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do assert {:ok, books} = Metadata.get_author_books(author) end + + test "get the list contains aliases' books" do + {:ok, author} = Metadata.create_author("Author", "An description") + {:ok, alias1} = + Metadata.add_author_to_related_alias_registry("Author2", "An description2", author.id) + {:ok, alias2} = + Metadata.add_author_to_related_alias_registry("Author3", "An description3", author.id) + + author_roles = [ + %{order: 1, author_id: author.id, role: ""} + ] + + {:ok, book} = Metadata.create_book("Book", "1234567890", "An description", author_roles) + + author_roles = [ + %{order: 1, author_id: alias1.id, role: ""} + ] + + {:ok, book2} = Metadata.create_book("Book2", "1234567890", "An description", author_roles) + + author_roles = [ + %{order: 1, author_id: alias2.id, role: ""} + ] + + {:ok, book3} = Metadata.create_book("Book3", "1234567890", "An description", author_roles) + + assert {:ok, books} = Metadata.get_author_books(author) + + book_ids = Enum.map(books, & &1.id) + + assert book.id in book_ids + assert book2.id in book_ids + assert book3.id in book_ids + end end end