Update Book to sync only a new non-local data.
This commit is contained in:
@@ -74,6 +74,7 @@ defmodule DecentralisedBookIndex.Metadata do
|
||||
define :search_book_by_bid, action: :search_by_bid, args: [:type, :bid]
|
||||
define :update_book, action: :update
|
||||
define :assign_book_cover_image, args: [:cover_image_url], action: :assign_cover_image
|
||||
define :assign_book_dbi_server, args: [:dbi_server_id], action: :assign_dbi_server_id
|
||||
define :destroy_book, action: :destroy
|
||||
end
|
||||
|
||||
|
||||
@@ -40,7 +40,8 @@ defmodule DecentralisedBookIndex.Metadata.Book do
|
||||
:published,
|
||||
:publisher_id,
|
||||
:cover_image_url,
|
||||
:book_editions_registry_id
|
||||
:book_editions_registry_id,
|
||||
:dbi_server_id
|
||||
]
|
||||
|
||||
argument :bids, {:array, :map}
|
||||
@@ -276,6 +277,10 @@ defmodule DecentralisedBookIndex.Metadata.Book do
|
||||
update :assign_cover_image do
|
||||
accept [:cover_image_url]
|
||||
end
|
||||
|
||||
update :assign_dbi_server_id do
|
||||
accept [:dbi_server_id]
|
||||
end
|
||||
end
|
||||
|
||||
policies do
|
||||
|
||||
@@ -8,15 +8,18 @@ defmodule DecentralisedBookIndex.Sync.BookSync do
|
||||
|
||||
case Metadata.get_book_by_id(attrs.id) do
|
||||
{:ok, book} ->
|
||||
attrs =
|
||||
attrs
|
||||
|> Map.delete(:id)
|
||||
|> update_registry(registry_info)
|
||||
|> Map.put(:dbi_server_id, server_id)
|
||||
|
||||
book
|
||||
|> Ash.Changeset.for_update(:sync, attrs)
|
||||
|> Ash.update!(authorize?: false)
|
||||
if not is_local(book) and is_new(attrs, book) do
|
||||
attrs =
|
||||
attrs
|
||||
|> Map.delete(:id)
|
||||
|> update_registry(registry_info)
|
||||
|> Map.put_new(:dbi_server_id, server_id)
|
||||
|
||||
book
|
||||
|> Ash.Changeset.for_update(:sync, attrs)
|
||||
|> Ash.update!(authorize?: false)
|
||||
end
|
||||
|
||||
:ok
|
||||
|
||||
@@ -24,7 +27,7 @@ defmodule DecentralisedBookIndex.Sync.BookSync do
|
||||
attrs =
|
||||
attrs
|
||||
|> update_registry(registry_info)
|
||||
|> Map.put(:dbi_server_id, server_id)
|
||||
|> Map.put_new(:dbi_server_id, server_id)
|
||||
|
||||
Book
|
||||
|> Ash.Changeset.for_create(:sync_create, attrs)
|
||||
@@ -58,4 +61,17 @@ defmodule DecentralisedBookIndex.Sync.BookSync do
|
||||
|> Map.delete(:book_editions_registry_id)
|
||||
end
|
||||
end
|
||||
|
||||
defp is_local(book) do
|
||||
is_nil(book.dbi_server_id)
|
||||
end
|
||||
|
||||
defp is_new(new_book, book) do
|
||||
with {:ok, datetime, _offset} <- DateTime.from_iso8601(new_book[:updated_at]) do
|
||||
DateTime.after?(datetime, book.updated_at)
|
||||
else
|
||||
{:error, reason} ->
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user