Skip to content

Commit

Permalink
post review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
annvelents committed Nov 12, 2024
1 parent 456aac3 commit ba28df7
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 23 deletions.
4 changes: 2 additions & 2 deletions app/jobs/charges/update_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Charges
class UpdateJob < ApplicationJob
queue_as 'default'

def perform(charge:, params:, options:)
Charges::UpdateService.call(charge:, params:, options:).raise_if_error!
def perform(charge:, params:, cascade_options:)
Charges::UpdateService.call(charge:, params:, cascade_options:).raise_if_error!
end
end
end
15 changes: 9 additions & 6 deletions app/services/charge_filters/create_or_update_batch_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

module ChargeFilters
class CreateOrUpdateBatchService < BaseService
def initialize(charge:, filters_params:, options: {})
def initialize(charge:, filters_params:, cascade_options: {})
@charge = charge
@filters_params = filters_params
@options = options
@cascade_updates = options[:cascade]
@parent_filters_attributes = options[:parent_filters] || []
@parent_filters = ChargeFilter.with_discarded.where(id: parent_filters_attributes.map { |f| f['id'] })
@cascade_updates = cascade_options[:cascade]
@parent_filters_attributes = cascade_options[:parent_filters] || []
@parent_filters = if parent_filters_attributes.blank?
ChargeFilter.none
else
ChargeFilter.with_discarded.where(id: parent_filters_attributes.map { |f| f['id'] })
end

super
end
Expand Down Expand Up @@ -87,7 +90,7 @@ def call

private

attr_reader :charge, :filters_params, :cascade_updates, :options, :parent_filters, :parent_filters_attributes
attr_reader :charge, :filters_params, :cascade_updates, :parent_filters, :parent_filters_attributes

def filters
@filters ||= charge.filters.includes(values: :billable_metric_filter)
Expand Down
12 changes: 6 additions & 6 deletions app/services/charges/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

module Charges
class UpdateService < BaseService
def initialize(charge:, params:, options: {})
def initialize(charge:, params:, cascade_options: {})
@charge = charge
@params = params
@options = options
@cascade = options[:cascade]
@cascade_options = cascade_options
@cascade = cascade_options[:cascade]

super
end
Expand All @@ -19,7 +19,7 @@ def call
charge.charge_model = params[:charge_model] unless plan.attached_to_subscriptions?
charge.invoice_display_name = params[:invoice_display_name] unless cascade

if !cascade || options[:equal_properties]
if !cascade || cascade_options[:equal_properties]
properties = params.delete(:properties).presence || Charges::BuildDefaultPropertiesService.call(
params[:charge_model]
)
Expand All @@ -33,7 +33,7 @@ def call
ChargeFilters::CreateOrUpdateBatchService.call(
charge:,
filters_params: filters.map(&:with_indifferent_access),
options:
cascade_options:
).raise_if_error!
end

Expand Down Expand Up @@ -69,7 +69,7 @@ def call

private

attr_reader :charge, :params, :options, :cascade
attr_reader :charge, :params, :cascade_options, :cascade

delegate :plan, to: :charge
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/plans/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def cascade_charge_update(charge, payload_charge)
Charges::UpdateJob.perform_later(
charge: child_charge,
params: payload_charge,
options: {
cascade_options: {
cascade: true,
parent_filters: charge.filters.map(&:attributes),
equal_properties: charge.equal_properties?(child_charge)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
require 'rails_helper'

RSpec.describe ChargeFilters::CreateOrUpdateBatchService do
subject(:service) { described_class.call(charge:, filters_params:, options:) }
subject(:service) { described_class.call(charge:, filters_params:, cascade_options:) }

let(:charge) { create(:standard_charge) }
let(:filters_params) { {} }
let(:options) { {} }
let(:cascade_options) { {} }

let(:card_location_filter) do
create(
Expand Down Expand Up @@ -80,7 +80,7 @@
values: [card_location_filter.values.first]
)
end
let(:options) do
let(:cascade_options) do
{
cascade: true,
parent_filters: charge_parent.filters.map(&:attributes)
Expand Down Expand Up @@ -275,7 +275,7 @@
)
]
end
let(:options) do
let(:cascade_options) do
{
cascade: true,
parent_filters: charge_parent.filters.map(&:attributes)
Expand Down
8 changes: 4 additions & 4 deletions spec/services/charges/update_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
require 'rails_helper'

RSpec.describe Charges::UpdateService, type: :service do
subject(:update_service) { described_class.new(charge:, params:, options:) }
subject(:update_service) { described_class.new(charge:, params:, cascade_options:) }

let(:membership) { create(:membership) }
let(:organization) { membership.organization }
let(:plan) { create(:plan, organization:) }
let(:options) do
let(:cascade_options) do
{
cascade: false
}
Expand Down Expand Up @@ -103,7 +103,7 @@
end

context 'when cascade is true' do
let(:options) do
let(:cascade_options) do
{
cascade: true,
parent_filters: [],
Expand All @@ -129,7 +129,7 @@
end

context 'with charge properties already overridden' do
let(:options) do
let(:cascade_options) do
{
cascade: true,
parent_filters: [],
Expand Down

0 comments on commit ba28df7

Please sign in to comment.