|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
defmodule DecentralisedBookIndex.SyncTasks.SyncBooksTask do
|
|
|
|
|
alias DecentralisedBookIndex.Sync.ApiClients.FetchJson
|
|
|
|
|
alias DecentralisedBookIndex.Sync.ApiClients.FetchJsons
|
|
|
|
|
alias DecentralisedBookIndex.Sync.DataTransformers.BookTransformer
|
|
|
|
|
alias DecentralisedBookIndex.Sync.DataTransformers.BidsTransformer
|
|
|
|
|
alias DecentralisedBookIndex.Sync.BookSync
|
|
|
|
|
|
|
|
|
|
alias DecentralisedBookIndex.Metadata.DBIServer
|
|
|
|
@ -17,6 +19,9 @@ defmodule DecentralisedBookIndex.SyncTasks.SyncBooksTask do
|
|
|
|
|
def sync_author_chunk(json_chunk, server_id) do
|
|
|
|
|
for json <- json_chunk do
|
|
|
|
|
with {:ok, attrs} <- BookTransformer.from_json(json),
|
|
|
|
|
{:ok, author_roles} <- get_author_roles(json),
|
|
|
|
|
{:ok, bids} <- get_bids(json),
|
|
|
|
|
attrs <- merge_relationships_attrs(attrs, bids, author_roles),
|
|
|
|
|
:ok <- BookSync.create_update(attrs, server_id) do
|
|
|
|
|
:ok
|
|
|
|
|
else
|
|
|
|
@ -33,4 +38,46 @@ defmodule DecentralisedBookIndex.SyncTasks.SyncBooksTask do
|
|
|
|
|
sync_author_chunk(json_chunk, server.id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp merge_relationships_attrs(attrs, bids, author_roles) do
|
|
|
|
|
attrs
|
|
|
|
|
|> Map.put(:bids, bids)
|
|
|
|
|
|> Map.put(:author_roles, author_roles)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp get_author_roles_url(json) do
|
|
|
|
|
url = get_in(json, ["relationships", "author_roles", "links", "related"])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp get_author_roles(json) do
|
|
|
|
|
url = get_author_roles_url(json)
|
|
|
|
|
|
|
|
|
|
with {:ok, json} <- FetchJson.get(url),
|
|
|
|
|
{:ok, roles} <- BidsTransformer.from_json(json) do
|
|
|
|
|
{:ok, roles}
|
|
|
|
|
else
|
|
|
|
|
{:error, reason} ->
|
|
|
|
|
Logger.error("Pipeline error: #{inspect(reason)}")
|
|
|
|
|
|
|
|
|
|
{:error, reason}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp get_bids_url(json) do
|
|
|
|
|
url = get_in(json, ["relationships", "bids", "links", "related"])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp get_bids(json) do
|
|
|
|
|
url = json |> get_bids_url()
|
|
|
|
|
|
|
|
|
|
with {:ok, json} <- FetchJson.get(url),
|
|
|
|
|
{:ok, bids} <- BidsTransformer.from_json(json) do
|
|
|
|
|
{:ok, bids}
|
|
|
|
|
else
|
|
|
|
|
{:error, reason} ->
|
|
|
|
|
Logger.error("Pipeline error: #{inspect(reason)}")
|
|
|
|
|
|
|
|
|
|
{:error, reason}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|