destroy action based on filters

I currently have a read action and I pass the result to the default destroy action, is it possible to have a destroy action which includes the read part? It seems like destroy only works when passing along the objects So basically I want to destroy the results of
read :replannable_routes_by_date do
prepare build(load: [stops: [orders: [:parcels]]])

argument :depot_id, :uuid, allow_nil?: false
filter expr(depot_id == ^arg(:depot_id))

argument :date, :date, allow_nil?: false
prepare fn %{arguments: %{date: date}} = query, _ ->
{start_utc, end_utc} = TimezoneHelpers.date_range_for_timezone(date)

Ash.Query.filter(
query,
expr(projected_departure_time >= ^start_utc and projected_departure_time <= ^end_utc)
)
end


filter expr(replan_locked == false)
end
read :replannable_routes_by_date do
prepare build(load: [stops: [orders: [:parcels]]])

argument :depot_id, :uuid, allow_nil?: false
filter expr(depot_id == ^arg(:depot_id))

argument :date, :date, allow_nil?: false
prepare fn %{arguments: %{date: date}} = query, _ ->
{start_utc, end_utc} = TimezoneHelpers.date_range_for_timezone(date)

Ash.Query.filter(
query,
expr(projected_departure_time >= ^start_utc and projected_departure_time <= ^end_utc)
)
end


filter expr(replan_locked == false)
end
Solution:
okay so you want to call it via a code interface. you can add the option require_reference?: false to the code interface, and then provide your own filters etc. in the action. (make sure to specify filters or it will delete everything)
Jump to solution
5 Replies
sevenseacat
sevenseacat2d ago
how are you calling the destroy action?
jeroen11dijk
jeroen11dijkOP2d ago
def delete_replannable_routes(depot_id, date, user) do
case Route.replannable_routes_by_date(depot_id, date,
actor: user,
tenant: user.company_id
) do
{:ok, replan_routes} ->
case Route.delete(routes, [bulk_options: [notify?: true, return_errors?: true]] ++ opts) do
%{status: :success, error_count: 0} ->
route_ids = Enum.map_join(routes, ", ", & &1.friendly_id)
message = "deleted routes: #{route_ids}"
{:ok, message}

%{error_count: error_count, errors: errors} when error_count > 0 ->
{:error, errors}
end

{:error, reason} ->
{:error, reason}
end
end
def delete_replannable_routes(depot_id, date, user) do
case Route.replannable_routes_by_date(depot_id, date,
actor: user,
tenant: user.company_id
) do
{:ok, replan_routes} ->
case Route.delete(routes, [bulk_options: [notify?: true, return_errors?: true]] ++ opts) do
%{status: :success, error_count: 0} ->
route_ids = Enum.map_join(routes, ", ", & &1.friendly_id)
message = "deleted routes: #{route_ids}"
{:ok, message}

%{error_count: error_count, errors: errors} when error_count > 0 ->
{:error, errors}
end

{:error, reason} ->
{:error, reason}
end
end
Currently doing this which works fine, I was just wondering if I could change the type of the action to a destroy, but then it seems to always require a route anyway
Solution
sevenseacat
sevenseacat2d ago
okay so you want to call it via a code interface. you can add the option require_reference?: false to the code interface, and then provide your own filters etc. in the action. (make sure to specify filters or it will delete everything)
ZachDaniel
ZachDaniel2d ago
You can also provide a query as the reference Route.delete(Ash.Query.filter(Route, ...))
jeroen11dijk
jeroen11dijkOP2d ago
Thanks that works!

Did you find this page helpful?