Add the sync task for Publisher.

This commit is contained in:
2025-04-02 10:56:55 +03:00
parent 6a6987bee6
commit 5ee34f76bc
7 changed files with 235 additions and 2 deletions
@@ -25,6 +25,10 @@ defmodule DecentralisedBookIndex.Metadata.Publisher do
accept [:name]
end
create :sync_create do
accept [:id, :name, :inserted_at, :updated_at, :dbi_server_id]
end
read :by_id do
argument :id, :uuid, allow_nil?: false
get? true
@@ -41,17 +45,24 @@ defmodule DecentralisedBookIndex.Metadata.Publisher do
pagination offset?: true, default_limit: 10
end
update :sync do
accept [:name, :inserted_at, :updated_at, :dbi_server_id]
end
end
attributes do
uuid_primary_key :id
uuid_primary_key :id, writable?: true
attribute :name, :string do
allow_nil? false
public? true
end
timestamps()
timestamps() do
writable? true
public? true
end
end
relationships do
@@ -0,0 +1,22 @@
defmodule DecentralisedBookIndex.Sync.DataTransformers.PublisherTransformer do
def from_json(json_body) do
attrs =
if Map.has_key?(json_body, "data") do
%{
id: get_in(json_body, ["data", "id"]),
name: get_in(json_body, ["data", "attributes", "name"]),
inserted_at: get_in(json_body, ["data", "attributes", "inserted_at"]),
updated_at: get_in(json_body, ["data", "attributes", "updated_at"]),
}
else
%{
id: get_in(json_body, ["id"]),
name: get_in(json_body, ["attributes", "name"]),
inserted_at: get_in(json_body, ["attributes", "inserted_at"]),
updated_at: get_in(json_body, ["attributes", "updated_at"]),
}
end
{:ok, attrs}
end
end
@@ -0,0 +1,29 @@
defmodule DecentralisedBookIndex.Sync.PublisherSync do
alias DecentralisedBookIndex.Metadata
alias DecentralisedBookIndex.Metadata.Publisher
def create_update(attrs, server_id) do
case Metadata.get_publisher_by_id(attrs.id) do
{:ok, publisher} ->
attrs =
attrs
|> Map.delete(:id)
publisher
|> Ash.Changeset.for_update(:sync, attrs)
|> Ash.update!()
:ok
{:error, %Ash.Error.Query.NotFound{}} ->
attrs =
attrs
|> Map.put(:dbi_server_id, server_id)
Publisher
|> Ash.Changeset.for_create(:sync_create, attrs)
|> Ash.create!()
:ok
end
end
end
@@ -0,0 +1,34 @@
defmodule DecentralisedBookIndex.SyncTasks.SyncPublishersTask do
alias DecentralisedBookIndex.Sync.ApiClients.FetchJsons
alias DecentralisedBookIndex.Sync.DataTransformers.PublisherTransformer
alias DecentralisedBookIndex.Sync.PublisherSync
alias DecentralisedBookIndex.Metadata.DBIServer
def sync(%DBIServer{} = server) do
url = "#{server.url}/api/v1/json/publishers"
FetchJsons.get(url, sync_closure(server))
server
end
def sync_chunk(json_chunk, server_id) do
for json <- json_chunk do
with {:ok, attrs} <- PublisherTransformer.from_json(json),
:ok <- PublisherSync.create_update(attrs, server_id) do
:ok
else
{:error, reason} ->
Logger.error("Pipeline error: #{inspect(reason)}")
end
end
[]
end
def sync_closure(server) do
fn json_chunk ->
sync_chunk(json_chunk, server.id)
end
end
end