Update the sync task for Author to link the record with the server.
continuous-integration/drone/push Build is passing Details

dev
KKlochko 3 months ago
parent e5e05320cf
commit b8a7ac9a48

@ -63,9 +63,12 @@ defmodule DecentralisedBookIndex.Metadata.Author do
end end
create :sync do create :sync do
accept [:id, :name, :description, :author_alias_registry_id] accept [:id, :name, :description, :dbi_server_id, :author_alias_registry_id]
change fn changeset, _ -> change fn changeset, _ ->
server_id = Ash.Changeset.get_attribute(changeset, :dbi_server)
Ash.Changeset.force_change_attribute(changeset, :author_alias_registry_id, server_id)
registry_id = Ash.Changeset.get_attribute(changeset, :author_alias_registry_id) registry_id = Ash.Changeset.get_attribute(changeset, :author_alias_registry_id)
if registry_id == nil do if registry_id == nil do

@ -1,7 +1,7 @@
defmodule DecentralisedBookIndex.Sync.AuthorSync do defmodule DecentralisedBookIndex.Sync.AuthorSync do
alias DecentralisedBookIndex.Metadata.Author alias DecentralisedBookIndex.Metadata.Author
def create_update(author_attrs) do def create_update(author_attrs, server_id) do
case DecentralisedBookIndex.Metadata.get_author_by_id(author_attrs.id) do case DecentralisedBookIndex.Metadata.get_author_by_id(author_attrs.id) do
{:ok, author} -> {:ok, author} ->
author_attrs = author_attrs =
@ -15,6 +15,10 @@ defmodule DecentralisedBookIndex.Sync.AuthorSync do
:ok :ok
{:error, %Ash.Error.Query.NotFound{}} -> {:error, %Ash.Error.Query.NotFound{}} ->
author_attrs =
author_attrs
|> Map.put(:dbi_server_id, server_id)
DecentralisedBookIndex.Metadata.Author DecentralisedBookIndex.Metadata.Author
|> Ash.Changeset.for_create(:sync, author_attrs) |> Ash.Changeset.for_create(:sync, author_attrs)
|> Ash.create!() |> Ash.create!()

@ -3,16 +3,19 @@ defmodule DecentralisedBookIndex.SyncTasks.SyncAuthorsTask do
alias DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformer alias DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformer
alias DecentralisedBookIndex.Sync.AuthorSync alias DecentralisedBookIndex.Sync.AuthorSync
def sync(endpoint) do alias DecentralisedBookIndex.Metadata.DBIServer
url = "#{endpoint}/api/v1/json/author"
FetchJsons.get(url, &sync_author_chunk/1) def sync(%DBIServer{} = server) do
endpoint url = "#{server.url}/api/v1/json/author"
FetchJsons.get(url, sync_author_closure(server))
server
end end
def sync_author_chunk(json_chunk) do def sync_author_chunk(json_chunk, server_id) do
for json <- json_chunk do for json <- json_chunk do
with {:ok, attrs} <- AuthorTransformer.from_json(json), with {:ok, attrs} <- AuthorTransformer.from_json(json),
:ok <- AuthorSync.create_update(attrs) do :ok <- AuthorSync.create_update(attrs, server_id) do
:ok :ok
else else
{:error, reason} -> {:error, reason} ->
@ -22,4 +25,10 @@ defmodule DecentralisedBookIndex.SyncTasks.SyncAuthorsTask do
[] []
end end
def sync_author_closure(server) do
fn json_chunk ->
sync_author_chunk(json_chunk, server.id)
end
end
end end

@ -5,8 +5,13 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorSyncTest do
alias DecentralisedBookIndex.Metadata.Author alias DecentralisedBookIndex.Metadata.Author
alias DecentralisedBookIndex.Metadata alias DecentralisedBookIndex.Metadata
alias DecentralisedBookIndex.TestEndpoints
@test_server_endpoint TestEndpoints.test_api_endpoint()
describe "sync author transformations" do describe "sync author transformations" do
test "a new author will be created" do test "a new author will be created" do
server = generate(dbi_server(url: @test_server_endpoint))
author = %{ author = %{
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d", id: "889a323e-d104-4b5d-b276-dad5a9b1da9d",
name: "Author", name: "Author",
@ -14,15 +19,18 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorSyncTest do
author_alias_registry_id: nil author_alias_registry_id: nil
} }
assert :ok = AuthorSync.create_update(author) assert :ok = AuthorSync.create_update(author, server.id)
assert {:ok, saved_author} = Metadata.get_author_by_id(author.id) assert {:ok, saved_author} = Metadata.get_author_by_id(author.id)
assert author.id == saved_author.id assert author.id == saved_author.id
assert author.name == saved_author.name assert author.name == saved_author.name
assert nil != saved_author.author_alias_registry_id assert nil != saved_author.author_alias_registry_id
assert server.id == saved_author.dbi_server_id
end end
test "update an existing author" do test "update an existing author" do
server = generate(dbi_server(url: @test_server_endpoint))
{:ok, author} = Metadata.create_author("Author", "An description") {:ok, author} = Metadata.create_author("Author", "An description")
author_attrs = %{ author_attrs = %{
@ -32,7 +40,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorSyncTest do
author_alias_registry_id: nil author_alias_registry_id: nil
} }
assert :ok = AuthorSync.create_update(author_attrs) assert :ok = AuthorSync.create_update(author_attrs, server.id)
assert {:ok, saved_author} = Metadata.get_author_by_id(author.id) assert {:ok, saved_author} = Metadata.get_author_by_id(author.id)
assert author.id == saved_author.id assert author.id == saved_author.id

@ -9,12 +9,12 @@ defmodule DecentralisedBookIndex.SyncTasks.SyncAuthorsTaskTest do
describe "sync authors tasks" do describe "sync authors tasks" do
test "sync authors" do test "sync authors" do
server = generate(dbi_server(url: @test_server_endpoint))
{:ok, author} = Metadata.create_author("Author", "An description") {:ok, author} = Metadata.create_author("Author", "An description")
{:ok, author} = Metadata.create_author("Author2", "An description") {:ok, author} = Metadata.create_author("Author2", "An description")
endpoint = @test_server_endpoint assert server = SyncAuthorsTask.sync(server)
assert endpoint = SyncAuthorsTask.sync(endpoint)
end end
end end
end end

Loading…
Cancel
Save