diff --git a/doc/development/internal_api/index.md b/doc/development/internal_api/index.md index 5fceb9013da388c72b940ef102db282a93203f97..7873b61b30ca5e7b3ef4f26900c1613414bc2ca0 100644 --- a/doc/development/internal_api/index.md +++ b/doc/development/internal_api/index.md @@ -857,9 +857,9 @@ PUT /namespaces/:id/subscription_add_on_purchase/:add_on_name | Attribute | Type | Required | Description | |:------------|:--------|:---------|:------------| -| `quantity` | integer | yes | Amount of units in the subscription add-on purchase (Example: Number of seats for a code suggestions add-on) | +| `quantity` | integer | no | Amount of units in the subscription add-on purchase (Example: Number of seats for a code suggestions add-on) | | `expires_on` | date | yes | Expiration date of the subscription add-on purchase | -| `purchase_xid` | string | yes | Identifier for the subscription add-on purchase (Example: Subscription name for a code suggestions add-on) | +| `purchase_xid` | string | no | Identifier for the subscription add-on purchase (Example: Subscription name for a code suggestions add-on) | Example request: diff --git a/ee/app/services/gitlab_subscriptions/add_on_purchases/update_service.rb b/ee/app/services/gitlab_subscriptions/add_on_purchases/update_service.rb index 2adfeebca8924c3e7f4753241d9416b0b9230201..7495c98b662cfbd84696058c7fdd36d6193fb4b4 100644 --- a/ee/app/services/gitlab_subscriptions/add_on_purchases/update_service.rb +++ b/ee/app/services/gitlab_subscriptions/add_on_purchases/update_service.rb @@ -23,11 +23,13 @@ def add_on_purchase # rubocop: enable CodeReuse/ActiveRecord def update_add_on_purchase - add_on_purchase.update( + attributes = { quantity: quantity, expires_on: expires_on, purchase_xid: purchase_xid - ) + }.compact + + add_on_purchase.update(attributes) end def error_response diff --git a/ee/lib/api/gitlab_subscriptions/add_on_purchases.rb b/ee/lib/api/gitlab_subscriptions/add_on_purchases.rb index d4e303b1b48b9225fbd2ffc1db933635155cffdf..ed010f5176a643fe576c376481a0852f020fdb25 100644 --- a/ee/lib/api/gitlab_subscriptions/add_on_purchases.rb +++ b/ee/lib/api/gitlab_subscriptions/add_on_purchases.rb @@ -16,14 +16,6 @@ class AddOnPurchases < ::API::Base end resource :namespaces, requirements: ::API::API::NAMESPACE_OR_PROJECT_REQUIREMENTS do - helpers do - params :purchased_subscription_add_on_attributes do - requires :quantity, type: Integer, desc: 'The quantity of the purchase' - requires :expires_on, type: Date, desc: 'The date when purchase expires on' - requires :purchase_xid, type: String, desc: 'The purchase identifier (example: the subscription name)' - end - end - desc 'Create an add-on purchase for the namespace' do detail 'Creates a subscription add-on record for the given namespaces and add-on' success ::EE::API::Entities::GitlabSubscriptions::AddOnPurchase @@ -34,7 +26,9 @@ class AddOnPurchases < ::API::Base ] end params do - use :purchased_subscription_add_on_attributes + requires :quantity, type: Integer, desc: 'The quantity of the purchase' + requires :expires_on, type: Date, desc: 'The date when purchase expires on' + requires :purchase_xid, type: String, desc: 'The purchase identifier (example: the subscription name)' end post ":id/subscription_add_on_purchase/:add_on_name" do result = ::GitlabSubscriptions::AddOnPurchases::CreateService.new( @@ -78,7 +72,9 @@ class AddOnPurchases < ::API::Base ] end params do - use :purchased_subscription_add_on_attributes + requires :expires_on, type: Date, desc: 'The date when purchase expires on' + optional :quantity, type: Integer, desc: 'The quantity of the purchase' + optional :purchase_xid, type: String, desc: 'The purchase identifier (example: the subscription name)' end put ":id/subscription_add_on_purchase/:add_on_name" do result = ::GitlabSubscriptions::AddOnPurchases::UpdateService.new( diff --git a/ee/spec/requests/api/gitlab_subscriptions/add_on_purchases_spec.rb b/ee/spec/requests/api/gitlab_subscriptions/add_on_purchases_spec.rb index 7440c2c7d4dba6f4f72f2ac5352c125d3212aa79..f208a5eb0d3070d3fabf1b9a0473d41775940760 100644 --- a/ee/spec/requests/api/gitlab_subscriptions/add_on_purchases_spec.rb +++ b/ee/spec/requests/api/gitlab_subscriptions/add_on_purchases_spec.rb @@ -234,7 +234,7 @@ context 'when the add-on purchase exists' do let_it_be(:expires_on) { Date.current + 6.months } - let_it_be(:add_on_purchase) do + let_it_be_with_reload(:add_on_purchase) do create( :gitlab_subscription_add_on_purchase, namespace: namespace, @@ -245,7 +245,7 @@ ) end - it 'creates a new add-on purchase' do + it 'updates the found add-on purchase' do expect do put_add_on_purchase add_on_purchase.reload @@ -263,6 +263,28 @@ ) end + context 'with only required params' do + let(:params) { { expires_on: (Date.current + 1.year).to_s } } + + it 'updates the add-on purchase' do + expect do + put_add_on_purchase + add_on_purchase.reload + end.to change { add_on_purchase.expires_on }.from(expires_on).to(params[:expires_on].to_date) + .and not_change { add_on_purchase.quantity } + + expect(response).to have_gitlab_http_status(:success) + expect(json_response).to eq( + 'namespace_id' => namespace_id, + 'namespace_name' => namespace.name, + 'add_on' => add_on.name.titleize, + 'quantity' => add_on_purchase.quantity, + 'expires_on' => params[:expires_on], + 'purchase_xid' => add_on_purchase.purchase_xid + ) + end + end + context 'when the add-on purchase cannot be saved' do let(:params) { super().merge(quantity: 0) }