From ed1315c4f3680511d8a474dbd18549d02cd2c9d0 Mon Sep 17 00:00:00 2001 From: Lovro Colic Date: Wed, 21 Feb 2024 07:19:59 +0100 Subject: [PATCH 1/2] fix edit subscription attached to parent plan --- app/services/subscriptions/update_service.rb | 23 +++++++++++++++---- .../subscriptions/update_service_spec.rb | 21 +++++++++++++++-- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/app/services/subscriptions/update_service.rb b/app/services/subscriptions/update_service.rb index ce384b1757e..555a616b8da 100644 --- a/app/services/subscriptions/update_service.rb +++ b/app/services/subscriptions/update_service.rb @@ -24,11 +24,10 @@ def call subscription.ending_at = params[:ending_at] if params.key?(:ending_at) if params.key?(:plan_overrides) - plan_result = Plans::UpdateService.call( - plan: subscription.plan, - params: params[:plan_overrides].to_h.with_indifferent_access, - ) + plan_result = handle_plan_override return plan_result unless plan_result.success? + + subscription.plan = plan_result.plan end if subscription.starting_in_the_future? && params.key?(:subscription_at) @@ -61,6 +60,22 @@ def process_subscription_at_change(subscription) BillSubscriptionJob.perform_later([subscription], Time.current.to_i) end + def handle_plan_override + current_plan = subscription.plan + + if current_plan.parent_id + Plans::UpdateService.call( + plan: current_plan, + params: params[:plan_overrides].to_h.with_indifferent_access, + ) + else + Plans::OverrideService.call( + plan: current_plan, + params: params[:plan_overrides].to_h.with_indifferent_access + ) + end + end + def valid?(args) Subscriptions::ValidateService.new(result, **args).valid? end diff --git a/spec/services/subscriptions/update_service_spec.rb b/spec/services/subscriptions/update_service_spec.rb index bf7fd5406a7..81b49cf5ff6 100644 --- a/spec/services/subscriptions/update_service_spec.rb +++ b/spec/services/subscriptions/update_service_spec.rb @@ -109,6 +109,8 @@ end context 'when plan_overrides' do + let(:plan) { create(:plan, organization: membership.organization) } + let(:subscription) { create(:subscription, plan:, subscription_at: Time.current - 1.year) } let(:params) do { plan_overrides: { @@ -119,8 +121,23 @@ around { |test| lago_premium!(&test) } - it 'updates the plan accordingly' do - expect { update_service.call }.to change { subscription.plan.reload.name }.to('new name') + it 'creates the new plan accordingly' do + update_service.call + + expect(subscription.plan.reload.name).to eq('new name') + expect(subscription.plan_id).not_to eq(plan.id) + end + + context 'with overriden plan' do + let(:parent_plan) { create(:plan, organization: membership.organization) } + let(:plan) { create(:plan, organization: membership.organization, parent_id: parent_plan.id) } + + it 'updates the plan accordingly' do + update_service.call + + expect(subscription.plan.reload.name).to eq('new name') + expect(subscription.plan_id).to eq(plan.id) + end end end From 0803e2fcdde84ae2ddabce2c3ccd707d478610a4 Mon Sep 17 00:00:00 2001 From: Lovro Colic Date: Wed, 21 Feb 2024 07:25:17 +0100 Subject: [PATCH 2/2] fix linter issues --- app/services/subscriptions/update_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/subscriptions/update_service.rb b/app/services/subscriptions/update_service.rb index 555a616b8da..55296b135bf 100644 --- a/app/services/subscriptions/update_service.rb +++ b/app/services/subscriptions/update_service.rb @@ -71,7 +71,7 @@ def handle_plan_override else Plans::OverrideService.call( plan: current_plan, - params: params[:plan_overrides].to_h.with_indifferent_access + params: params[:plan_overrides].to_h.with_indifferent_access, ) end end