Skip to content

Commit

Permalink
Improving days_until_due match (#1008)
Browse files Browse the repository at this point in the history
* added support for dashes

* added log
  • Loading branch information
brennen-stripe authored Feb 15, 2023
1 parent 449668e commit 4edfe53
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
31 changes: 18 additions & 13 deletions lib/stripe-force/translate/order/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def self.remove_terminated_lines(original_phase_items)

sig { params(raw_days_until_due: T.any(String, Integer, Float)).returns(Integer) }
def self.transform_payment_terms_to_days_until_due(raw_days_until_due)
log.info "transforming payment terms to days_until_due", raw_days_until_due: raw_days_until_due
if raw_days_until_due.is_a?(Integer)
return raw_days_until_due
end
Expand All @@ -82,21 +83,25 @@ def self.transform_payment_terms_to_days_until_due(raw_days_until_due)
return raw_days_until_due.strip.to_i
end

# TODO it is possible for users to customize the options here, we may need to use regex extraction or something at some point
case raw_days_until_due
when "Net 15"
15
when "Net 30"
30
when "Net 45"
45
when "Net 60"
60
when "Net 90"
90
else
# Get the rest of the string after "Net " or "Net-"
# https://stackoverflow.com/questions/5006716/getting-the-text-that-follows-after-the-regex-match
raw_days = raw_days_until_due[/(?<=Net[- ]).*/, 0]

unless raw_days
raise StripeForce::Errors::RawUserError.new("unexpected days_until_due option #{raw_days_until_due}")
end

unless [15, 30, 45, 60, 90].include?(raw_days.to_i)
Integrations::ErrorContext.report_edge_case(
"recieved unexpected days_until_due option",
metadata: {
raw_days_until_due: raw_days_until_due,
raw_days: raw_days,
}
)
end

raw_days.to_i
end

# TODO this should move to the price helpers
Expand Down
5 changes: 5 additions & 0 deletions test/unit/test_order_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class OrderHelpersTest < Critic::UnitTest
it 'allows CPQ enum values' do
assert_equal(90, StripeForce::Translate::OrderHelpers.transform_payment_terms_to_days_until_due("Net 90"))
end

it 'allows CPQ enum values with dash' do
assert_equal(15, StripeForce::Translate::OrderHelpers.transform_payment_terms_to_days_until_due("Net-15"))
assert_equal(90, StripeForce::Translate::OrderHelpers.transform_payment_terms_to_days_until_due("Net-90"))
end
end
end
end

0 comments on commit 4edfe53

Please sign in to comment.