Skip to content

Commit

Permalink
add check for parameters on filter charges + fix db
Browse files Browse the repository at this point in the history
  • Loading branch information
annvelents committed Dec 20, 2024
1 parent 7b7a6da commit a451edb
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def call
filter ||= charge.filters.new

filter.invoice_display_name = filter_param[:invoice_display_name]
filter.properties = filter_param[:properties]
filter.properties = Charges::FilterChargeModelPropertiesService.call(
charge:,
properties: filter_param[:properties]
).properties
if filter.save! && touch && !filter.changed?
# NOTE: Make sure update_at is touched even if not changed to keep the right order
filter.touch # rubocop:disable Rails/SkipsModelValidations
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

class FixChargePropertiesWithDoubleValues < ActiveRecord::Migration[7.1]
def up
pairs = [['amount', 'graduated_ranges'], ['amount', 'graduated_percentage_ranges'], ['amount', 'volume_ranges']]
pairs.each do |pair|
ChargeFilter.includes(:charge).where("properties ?& array[:keys]", keys: pair).find_each do |cf|
fix_charge_filter_properties(cf)
end
Charge.where("properties ?& array[:keys]", keys: pair).find_each do |charge|
fix_charge_properties(charge)
end
end
end

def down
end

private

def fix_charge_filter_properties(charge_filter)
result = Charges::FilterChargeModelPropertiesService.call(
charge: charge_filter.charge, properties: charge_filter.properties
).raise_if_error!
charge_filter.properties = result.properties
charge.save!
end

def fix_charge_properties(charge)
result = Charges::FilterChargeModelPropertiesService.call(
charge:, properties: charge.properties
).raise_if_error!
charge.properties = result.properties
charge.save!
end
end
2 changes: 1 addition & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion spec/factories/charges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
factory :graduated_charge do
charge_model { 'graduated' }
properties do
{graduated_ranges: []}
{graduated_ranges: [
{from_value: 0, to_value: 10, per_unit_amount: '0', flat_amount: '200'},
{from_value: 11, to_value: nil, per_unit_amount: '0', flat_amount: '300'}
]}
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,45 @@
expect(filter2.values.count).to eq(3)
expect(filter2.values.pluck(:values).flatten).to match_array(%w[domestic visa debit])
end

context 'when filters properties contain not relevant values' do
let(:charge) { create(:graduated_charge) }
let(:filters_params) do
[
{
values: {
card_location_filter.key => ['domestic'],
scheme_filter.key => ['visa']
},
invoice_display_name: 'Visa domestic card payment',
properties: {amount: '10', graduated_ranges: [{from_value: 0, to_value: nil, per_unit_amount: '0', flat_amount: '200'}]}
},
{
values: {
card_location_filter.key => ['domestic'],
scheme_filter.key => ['visa'],
card_type_filter.key => ['debit']
},
invoice_display_name: 'Visa debit domestic card payment',
properties: {amount: '20', graduated_ranges: [{from_value: 0, to_value: nil, per_unit_amount: '0', flat_amount: '200'}]}
}
]
end

it 'removes the not relevant values from the properties' do
expect { service }.to change(ChargeFilter, :count).by(2)

filter1 = charge.filters.find_by(invoice_display_name: 'Visa domestic card payment')
expect(filter1.properties).to eq('graduated_ranges' => [
{'from_value' => 0, 'to_value' => nil, 'per_unit_amount' => '0', 'flat_amount' => '200'}
])

filter2 = charge.filters.find_by(invoice_display_name: 'Visa debit domestic card payment')
expect(filter2.properties).to eq('graduated_ranges' => [
{'from_value' => 0, 'to_value' => nil, 'per_unit_amount' => '0', 'flat_amount' => '200'}
])
end
end
end

context 'with existing filters' do
Expand Down

0 comments on commit a451edb

Please sign in to comment.