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

This commit is contained in:
2025-03-23 12:33:41 +02:00
parent e5e05320cf
commit b8a7ac9a48
5 changed files with 37 additions and 13 deletions
@@ -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
@@ -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!()
@@ -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