This commit is contained in:
parent
b429a4dbb6
commit
e7d63b9b0a
5 changed files with 93 additions and 18 deletions
|
|
@ -10,10 +10,10 @@ defmodule MvWeb.MemberExportController do
|
|||
require Ash.Query
|
||||
import Ash.Expr
|
||||
|
||||
alias Mv.Membership.Member
|
||||
alias Mv.Membership.CustomField
|
||||
alias Mv.Membership.MembersCSV
|
||||
alias Mv.Authorization.Actor
|
||||
alias Mv.Membership.CustomField
|
||||
alias Mv.Membership.Member
|
||||
alias Mv.Membership.MembersCSV
|
||||
|
||||
@member_fields_allowlist Mv.Constants.member_fields() |> Enum.map(&Atom.to_string/1)
|
||||
@custom_field_prefix Mv.Constants.custom_field_prefix()
|
||||
|
|
@ -100,7 +100,9 @@ defmodule MvWeb.MemberExportController do
|
|||
|
||||
defp filter_valid_uuids(id_list) when is_list(id_list) do
|
||||
id_list
|
||||
|> Enum.filter(fn id -> is_binary(id) and match?({:ok, _}, Ecto.UUID.cast(id)) end)
|
||||
|> Enum.filter(fn id ->
|
||||
is_binary(id) and match?({:ok, _}, Ecto.UUID.cast(id))
|
||||
end)
|
||||
|> Enum.uniq()
|
||||
end
|
||||
|
||||
|
|
@ -130,14 +132,18 @@ defmodule MvWeb.MemberExportController do
|
|||
|> Ash.Query.filter(expr(id in ^custom_field_ids))
|
||||
|> Ash.Query.select([:id, :name, :value_type])
|
||||
|
||||
case Ash.read(query, actor: actor) do
|
||||
{:ok, custom_fields} ->
|
||||
by_id = build_custom_fields_by_id(custom_field_ids, custom_fields)
|
||||
{:ok, by_id}
|
||||
query
|
||||
|> Ash.read(actor: actor)
|
||||
|> handle_custom_fields_read_result(custom_field_ids)
|
||||
end
|
||||
|
||||
{:error, %Ash.Error.Forbidden{}} ->
|
||||
{:error, :forbidden}
|
||||
end
|
||||
defp handle_custom_fields_read_result({:ok, custom_fields}, custom_field_ids) do
|
||||
by_id = build_custom_fields_by_id(custom_field_ids, custom_fields)
|
||||
{:ok, by_id}
|
||||
end
|
||||
|
||||
defp handle_custom_fields_read_result({:error, %Ash.Error.Forbidden{}}, _custom_field_ids) do
|
||||
{:error, :forbidden}
|
||||
end
|
||||
|
||||
defp build_custom_fields_by_id(custom_field_ids, custom_fields) do
|
||||
|
|
|
|||
|
|
@ -642,24 +642,48 @@ defmodule MvWeb.ImportExportLive do
|
|||
# Start async task to process chunk in production
|
||||
# Use start_child for fire-and-forget: no monitor, no Task messages
|
||||
# We only use our own send/2 messages for communication
|
||||
Task.Supervisor.start_child(Mv.TaskSupervisor, fn ->
|
||||
# Set locale in task process for translations
|
||||
Gettext.put_locale(MvWeb.Gettext, locale)
|
||||
|
||||
process_chunk_with_error_handling(
|
||||
Task.Supervisor.start_child(
|
||||
Mv.TaskSupervisor,
|
||||
build_chunk_processing_task(
|
||||
chunk,
|
||||
import_state.column_map,
|
||||
import_state.custom_field_map,
|
||||
opts,
|
||||
live_view_pid,
|
||||
idx
|
||||
idx,
|
||||
locale
|
||||
)
|
||||
end)
|
||||
)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
# Builds the task function for processing a chunk asynchronously.
|
||||
defp build_chunk_processing_task(
|
||||
chunk,
|
||||
column_map,
|
||||
custom_field_map,
|
||||
opts,
|
||||
live_view_pid,
|
||||
idx,
|
||||
locale
|
||||
) do
|
||||
fn ->
|
||||
# Set locale in task process for translations
|
||||
Gettext.put_locale(MvWeb.Gettext, locale)
|
||||
|
||||
process_chunk_with_error_handling(
|
||||
chunk,
|
||||
column_map,
|
||||
custom_field_map,
|
||||
opts,
|
||||
live_view_pid,
|
||||
idx
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
# Handles chunk processing result from async task and schedules the next chunk.
|
||||
@spec handle_chunk_result(
|
||||
Phoenix.LiveView.Socket.t(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue