From b8a7ac9a489b4a1e83881f35630f8352d814e0e6 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 23 Mar 2025 12:33:41 +0200 Subject: [PATCH] Update the sync task for Author to link the record with the server. --- .../metadata/author.ex | 5 ++++- .../sync/sync/author_sync.ex | 6 +++++- .../sync/sync_tasks/sync_authors_task.ex | 21 +++++++++++++------ .../sync/sync/author_sync_test.exs | 12 +++++++++-- .../sync_tasks/sync_authors_task_test.exs | 6 +++--- 5 files changed, 37 insertions(+), 13 deletions(-) diff --git a/lib/decentralised_book_index/metadata/author.ex b/lib/decentralised_book_index/metadata/author.ex index b037344..bcd121c 100644 --- a/lib/decentralised_book_index/metadata/author.ex +++ b/lib/decentralised_book_index/metadata/author.ex @@ -63,9 +63,12 @@ defmodule DecentralisedBookIndex.Metadata.Author do end 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, _ -> + 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) if registry_id == nil do diff --git a/lib/decentralised_book_index/sync/sync/author_sync.ex b/lib/decentralised_book_index/sync/sync/author_sync.ex index 6bb6c70..bfdd9ef 100644 --- a/lib/decentralised_book_index/sync/sync/author_sync.ex +++ b/lib/decentralised_book_index/sync/sync/author_sync.ex @@ -1,7 +1,7 @@ defmodule DecentralisedBookIndex.Sync.AuthorSync do 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 {:ok, author} -> author_attrs = @@ -15,6 +15,10 @@ defmodule DecentralisedBookIndex.Sync.AuthorSync do :ok {:error, %Ash.Error.Query.NotFound{}} -> + author_attrs = + author_attrs + |> Map.put(:dbi_server_id, server_id) + DecentralisedBookIndex.Metadata.Author |> Ash.Changeset.for_create(:sync, author_attrs) |> Ash.create!() diff --git a/lib/decentralised_book_index/sync/sync_tasks/sync_authors_task.ex b/lib/decentralised_book_index/sync/sync_tasks/sync_authors_task.ex index 5375611..243b1b5 100644 --- a/lib/decentralised_book_index/sync/sync_tasks/sync_authors_task.ex +++ b/lib/decentralised_book_index/sync/sync_tasks/sync_authors_task.ex @@ -3,16 +3,19 @@ defmodule DecentralisedBookIndex.SyncTasks.SyncAuthorsTask do alias DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformer alias DecentralisedBookIndex.Sync.AuthorSync - def sync(endpoint) do - url = "#{endpoint}/api/v1/json/author" - FetchJsons.get(url, &sync_author_chunk/1) - endpoint + alias DecentralisedBookIndex.Metadata.DBIServer + + def sync(%DBIServer{} = server) do + url = "#{server.url}/api/v1/json/author" + FetchJsons.get(url, sync_author_closure(server)) + + server end - def sync_author_chunk(json_chunk) do + def sync_author_chunk(json_chunk, server_id) do for json <- json_chunk do with {:ok, attrs} <- AuthorTransformer.from_json(json), - :ok <- AuthorSync.create_update(attrs) do + :ok <- AuthorSync.create_update(attrs, server_id) do :ok else {:error, reason} -> @@ -22,4 +25,10 @@ defmodule DecentralisedBookIndex.SyncTasks.SyncAuthorsTask do [] end + + def sync_author_closure(server) do + fn json_chunk -> + sync_author_chunk(json_chunk, server.id) + end + end end diff --git a/test/decentralised_book_index/sync/sync/author_sync_test.exs b/test/decentralised_book_index/sync/sync/author_sync_test.exs index 6f7ba5d..eb70d55 100644 --- a/test/decentralised_book_index/sync/sync/author_sync_test.exs +++ b/test/decentralised_book_index/sync/sync/author_sync_test.exs @@ -5,8 +5,13 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorSyncTest do alias DecentralisedBookIndex.Metadata.Author alias DecentralisedBookIndex.Metadata + alias DecentralisedBookIndex.TestEndpoints + @test_server_endpoint TestEndpoints.test_api_endpoint() + describe "sync author transformations" do test "a new author will be created" do + server = generate(dbi_server(url: @test_server_endpoint)) + author = %{ id: "889a323e-d104-4b5d-b276-dad5a9b1da9d", name: "Author", @@ -14,15 +19,18 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorSyncTest do 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 author.id == saved_author.id assert author.name == saved_author.name assert nil != saved_author.author_alias_registry_id + assert server.id == saved_author.dbi_server_id end test "update an existing author" do + server = generate(dbi_server(url: @test_server_endpoint)) + {:ok, author} = Metadata.create_author("Author", "An description") author_attrs = %{ @@ -32,7 +40,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorSyncTest do 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 author.id == saved_author.id diff --git a/test/decentralised_book_index/sync/sync_tasks/sync_authors_task_test.exs b/test/decentralised_book_index/sync/sync_tasks/sync_authors_task_test.exs index b1b959a..20413a1 100644 --- a/test/decentralised_book_index/sync/sync_tasks/sync_authors_task_test.exs +++ b/test/decentralised_book_index/sync/sync_tasks/sync_authors_task_test.exs @@ -9,12 +9,12 @@ defmodule DecentralisedBookIndex.SyncTasks.SyncAuthorsTaskTest do describe "sync authors tasks" 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("Author2", "An description") - endpoint = @test_server_endpoint - - assert endpoint = SyncAuthorsTask.sync(endpoint) + assert server = SyncAuthorsTask.sync(server) end end end