Async barcode scan batch import for 2+ new records

When barcode scan results contain at least 2 new records,
enqueue individual Oban jobs instead of importing synchronously.
Wishlisted/collected/not_found results still process synchronously.
This commit is contained in:
Claudio Ortolina
2026-04-04 10:20:50 +01:00
parent 74f514afc1
commit b9be62a500
7 changed files with 389 additions and 13 deletions
@@ -402,23 +402,32 @@ defmodule MusicLibraryWeb.Components.BarcodeScanner do
def handle_event("import_releases", _params, socket) do
current_time = DateTime.utc_now()
scan_results = socket.assigns.scan_results
socket =
case BarcodeScan.import_results(socket.assigns.scan_results, current_time) do
[] ->
put_toast(socket, :info, gettext("Records imported successfully"))
if BarcodeScan.should_import_async?(scan_results) do
{:ok, sync_errors, async_count} =
BarcodeScan.import_results_async(scan_results, current_time)
errors ->
errors_summary =
Enum.map_join(errors, "\n", fn {number, reason} ->
"#{number}: #{ErrorMessages.friendly_message(reason)}"
end)
put_toast(
socket,
:error,
gettext("Some records could not be imported: %{summary}", summary: errors_summary)
socket
|> maybe_toast_errors(sync_errors)
|> put_toast(
:info,
ngettext(
"Importing %{count} record in the background...",
"Importing %{count} records in the background...",
async_count,
count: async_count
)
)
else
case BarcodeScan.import_results(scan_results, current_time) do
[] ->
put_toast(socket, :info, gettext("Records imported successfully"))
errors ->
maybe_toast_errors(socket, errors)
end
end
qs = %{order: :purchase}
@@ -429,6 +438,21 @@ defmodule MusicLibraryWeb.Components.BarcodeScanner do
|> push_patch(to: ~p"/collection?#{qs}")}
end
defp maybe_toast_errors(socket, []), do: socket
defp maybe_toast_errors(socket, errors) do
errors_summary =
Enum.map_join(errors, "\n", fn {number, reason} ->
"#{number}: #{ErrorMessages.friendly_message(reason)}"
end)
put_toast(
socket,
:error,
gettext("Some records could not be imported: %{summary}", summary: errors_summary)
)
end
defp release_format_label(release) do
release
|> MusicBrainz.ReleaseSearchResult.format()