Skip to content

Commit

Permalink
Clarify query, organize code
Browse files Browse the repository at this point in the history
  • Loading branch information
nshoes committed Jan 6, 2025
1 parent 3686179 commit aa3bffa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 45 deletions.
23 changes: 12 additions & 11 deletions lib/nerves_hub/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1156,19 +1156,20 @@ defmodule NervesHub.Devices do
update_device_with_audit(device, params, user, description)
end

@spec move_many_to_deployment([integer()], integer()) :: {integer(), nil}
@spec move_many_to_deployment([integer()], integer()) ::
{:ok, %{updated: non_neg_integer(), ignored: non_neg_integer()}}
def move_many_to_deployment(device_ids, deployment_id) do
firmware =
Firmware
|> join(:inner, [f], d in Deployment, on: d.id == ^deployment_id)
|> limit(1)
|> Repo.one()
%{firmware: firmware} =
Deployment |> where(id: ^deployment_id) |> preload(:firmware) |> Repo.one()

Device
|> where([d], d.id in ^device_ids)
|> where([d], d.firmware_metadata["platform"] == ^firmware.platform)
|> where([d], d.firmware_metadata["architecture"] == ^firmware.architecture)
|> Repo.update_all(set: [deployment_id: deployment_id])
{devices_updated_count, _} =
Device
|> where([d], d.id in ^device_ids)
|> where([d], d.firmware_metadata["platform"] == ^firmware.platform)
|> where([d], d.firmware_metadata["architecture"] == ^firmware.architecture)
|> Repo.update_all(set: [deployment_id: deployment_id])

{:ok, %{updated: devices_updated_count, ignored: length(device_ids) - devices_updated_count}}
end

@spec move_many([Device.t()], Product.t(), User.t()) :: %{
Expand Down
75 changes: 41 additions & 34 deletions lib/nerves_hub_web/live/devices/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -313,46 +313,16 @@ defmodule NervesHubWeb.Live.Devices.Index do
}
} = socket
) do
{devices_updated_count, _} =
{:ok, %{updated: updated, ignored: ignored}} =
Devices.move_many_to_deployment(selected_devices, target_deployment.id)

devices_not_updated_count = length(selected_devices) - devices_updated_count

maybe_pluralize =
&if &1 == 1 do
&2
else
&2 <> "s"
end

socket =
socket
|> assign(:target_deployment, nil)
|> assign_display_devices()
|> then(fn socket ->
case [devices_updated_count, devices_not_updated_count] do
[updated_count, 0] ->
put_flash(
socket,
:info,
"#{updated_count} #{maybe_pluralize.(updated_count, "device")} added to deployment #{target_deployment.name}"
)

[0, _not_updated_count] ->
put_flash(
socket,
:info,
"No devices selected could be added to deployment #{target_deployment.name} because of mismatched firmware"
)

[updated_count, not_updated_count] ->
put_flash(
socket,
:info,
"#{updated_count} #{maybe_pluralize.(updated_count, "device")} added to deployment #{target_deployment.name}. #{not_updated_count} #{maybe_pluralize.(not_updated_count, "device")} could not be added to deployment because of mismatched firmware"
)
end
end)
|> then(
&update_flash_moving_devices_deployment(&1, updated, ignored, target_deployment.name)
)

{:noreply, socket}
end
Expand Down Expand Up @@ -641,4 +611,41 @@ defmodule NervesHubWeb.Live.Devices.Index do
js
|> JS.hide(transition: "fade-out", to: "##{id}")
end

defp update_flash_moving_devices_deployment(
socket,
updated_count,
ignored_count,
deployment_name
) do
maybe_pluralize =
&if &1 == 1 do
&2
else
&2 <> "s"
end

case [updated_count, ignored_count] do
[updated_count, 0] ->
put_flash(
socket,
:info,
"#{updated_count} #{maybe_pluralize.(updated_count, "device")} added to deployment #{deployment_name}"
)

[0, _not_updated_count] ->
put_flash(
socket,
:info,
"No devices selected could be added to deployment #{deployment_name} because of mismatched firmware"
)

[updated_count, not_updated_count] ->
put_flash(
socket,
:info,
"#{updated_count} #{maybe_pluralize.(updated_count, "device")} added to deployment #{deployment_name}. #{not_updated_count} #{maybe_pluralize.(not_updated_count, "device")} could not be added to deployment because of mismatched firmware"
)
end
end
end

0 comments on commit aa3bffa

Please sign in to comment.