Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore(charge_filters): add check on filter properties to be compliant with charge.charge_model #2989

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_filter.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
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
Loading