From 59796e14e1989040fab1dc65e4141b20547a7b99 Mon Sep 17 00:00:00 2001 From: Francesco Noacco Date: Thu, 22 Jun 2023 14:55:27 +0200 Subject: [PATCH] feat(update_campaigns): add update api add api to allow updates of existing update_campaigns. it is possible to edit - UpdateCampaign.name - UpdateCampaign.rollout_mechanism, but only with update inputs of the same type - PushRollout.max_in_progress_updates, but only with an higher value - all other PushRollout properties Signed-off-by: Francesco Noacco --- backend/lib/edgehog/update_campaigns.ex | 17 ++++++++++++ .../edgehog_web/resolvers/update_campaigns.ex | 10 +++++++ .../schema/update_campaigns_types.ex | 26 +++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/backend/lib/edgehog/update_campaigns.ex b/backend/lib/edgehog/update_campaigns.ex index 732830aaf..2779279c9 100644 --- a/backend/lib/edgehog/update_campaigns.ex +++ b/backend/lib/edgehog/update_campaigns.ex @@ -462,6 +462,23 @@ defmodule Edgehog.UpdateCampaigns do end end + @doc """ + Updates an update_campaign. + + ## Examples + + iex> update_update_campaign(update_campaign, %{field: new_value}) + {:ok, %UpdateCamapaign{}} + + iex> update_update_campaign(update_campaign, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_update_campaign(%UpdateCampaign{} = update_campaign, attrs) do + UpdateCampaign.changeset(update_campaign, attrs) + |> Repo.update() + end + @doc """ Preloads the default associations for a Target or a list of Targets """ diff --git a/backend/lib/edgehog_web/resolvers/update_campaigns.ex b/backend/lib/edgehog_web/resolvers/update_campaigns.ex index a58c69806..1e5dbf428 100644 --- a/backend/lib/edgehog_web/resolvers/update_campaigns.ex +++ b/backend/lib/edgehog_web/resolvers/update_campaigns.ex @@ -22,6 +22,7 @@ defmodule EdgehogWeb.Resolvers.UpdateCampaigns do alias Edgehog.BaseImages alias Edgehog.UpdateCampaigns alias Edgehog.UpdateCampaigns.UpdateChannel + alias Edgehog.UpdateCampaigns.UpdateCampaign import Absinthe.Resolution.Helpers, only: [batch: 3] def find_target(args, _resolution) do @@ -107,6 +108,15 @@ defmodule EdgehogWeb.Resolvers.UpdateCampaigns do end end + def update_update_campaign(args, _resolution) do + with {:ok, update_campaign} <- + UpdateCampaigns.fetch_update_campaign(args.update_campaign_id), + {:ok, updated_update_campaign} <- + UpdateCampaigns.update_update_campaign(update_campaign, args) do + {:ok, %{update_campaign: updated_update_campaign}} + end + end + # This moves the type tag from the outer key to the inner map, which adapts the behaviour # offered by GraphQL to the one required by PolymorphicEmbed in the changeset defp tag_rollout_mechanism(%{push: push_rollout_mechanism}) do diff --git a/backend/lib/edgehog_web/schema/update_campaigns_types.ex b/backend/lib/edgehog_web/schema/update_campaigns_types.ex index b3d478b46..0021ff9cf 100644 --- a/backend/lib/edgehog_web/schema/update_campaigns_types.ex +++ b/backend/lib/edgehog_web/schema/update_campaigns_types.ex @@ -373,5 +373,31 @@ defmodule EdgehogWeb.Schema.UpdateCampaignsTypes do resolve &Resolvers.UpdateCampaigns.create_update_campaign/2 end + + @desc "Updates an existing update campaign." + payload field :update_update_campaign do + input do + @desc "The ID of the update campaign to be updated" + field :update_campaign_id, non_null(:id) + + @desc "The name of the Update Campaign." + field :name, :string + + @desc "The Rollout Mechanism of the Update Campaign, with its properties" + field :rollout_mechanism, :rollout_mechanism_input + end + + output do + @desc "The updated Update Campaign." + field :update_campaign, non_null(:update_campaign) + end + + middleware Absinthe.Relay.Node.ParseIDs, + # base_image_id: :base_image, + # update_channel_id: :update_channel, + update_campaign_id: :update_campaign + + resolve &Resolvers.UpdateCampaigns.update_update_campaign/2 + end end end