Update Publisher to sync only a new non-local data.

This commit is contained in:
2025-05-06 21:21:44 +03:00
parent 5d8e70d79d
commit 5ba0e4da06
4 changed files with 77 additions and 15 deletions
+1
View File
@@ -149,6 +149,7 @@ defmodule DecentralisedBookIndex.Metadata do
define :get_publisher_by_id, args: [:id], action: :by_id
define :search_publisher, action: :search, args: [:name]
define :update_publisher, action: :update
define :assign_publisher_dbi_server, args: [:dbi_server_id], action: :assign_dbi_server_id
define :destroy_publisher, action: :destroy
end
end
@@ -56,6 +56,10 @@ defmodule DecentralisedBookIndex.Metadata.Publisher do
accept [:name]
end
update :assign_dbi_server_id do
accept [:dbi_server_id]
end
update :sync do
accept [:name, :inserted_at, :updated_at, :dbi_server_id]
end
@@ -5,21 +5,23 @@ defmodule DecentralisedBookIndex.Sync.PublisherSync do
def create_update(attrs, server_id) do
case Metadata.get_publisher_by_id(attrs.id) do
{:ok, publisher} ->
attrs =
attrs
|> Map.delete(:id)
|> Map.put(:dbi_server_id, server_id)
if not is_local(publisher) and is_new(attrs, publisher) do
attrs =
attrs
|> Map.delete(:id)
|> Map.put_new(:dbi_server_id, server_id)
publisher
|> Ash.Changeset.for_update(:sync, attrs)
|> Ash.update!(authorize?: false)
publisher
|> Ash.Changeset.for_update(:sync, attrs)
|> Ash.update!(authorize?: false)
end
:ok
{:error, %Ash.Error.Query.NotFound{}} ->
attrs =
attrs
|> Map.put(:dbi_server_id, server_id)
|> Map.put_new(:dbi_server_id, server_id)
Publisher
|> Ash.Changeset.for_create(:sync_create, attrs)
@@ -28,4 +30,17 @@ defmodule DecentralisedBookIndex.Sync.PublisherSync do
:ok
end
end
defp is_local(publisher) do
is_nil(publisher.dbi_server_id)
end
defp is_new(new_publisher, publisher) do
with {:ok, datetime, _offset} <- DateTime.from_iso8601(new_publisher[:updated_at]) do
DateTime.after?(datetime, publisher.updated_at)
else
{:error, reason} ->
false
end
end
end