diff --git a/.rubocop.yml b/.rubocop.yml index c76c911780686212200e06f1438ca587e7825db7..d0b95af5f9199af7fff5b3fa40e6d4e650f97c7e 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -40,6 +40,7 @@ Naming/MethodParameterName: - id - iq - db + - to Layout/IndentationStyle: Enabled: false diff --git a/lib/call_attempt.rb b/lib/call_attempt.rb index 9116caeea471876909f15ba0ddeab327ab9cc5ad..34b12805fe32eab0132292197e4c0fbc0abb6040 100644 --- a/lib/call_attempt.rb +++ b/lib/call_attempt.rb @@ -11,24 +11,21 @@ class CallAttempt "cad_beta_unlimited-v20210223" => 1.1 }.freeze - def self.for(customer, other_tel, rate, usage, direction:, **kwargs) + def self.for(customer, rate, usage, direction:, **kwargs) kwargs.merge!(direction: direction) included_credit = [customer.minute_limit.to_d - usage, 0].max if !rate || rate >= EXPENSIVE_ROUTE.fetch(customer.plan_name, 0.1) Unsupported.new(direction: direction) elsif included_credit + customer.balance < rate * 10 - NoBalance.for(customer, other_tel, rate, usage, **kwargs) + NoBalance.for(customer, rate, usage, **kwargs) else - for_ask_or_go(customer, other_tel, rate, usage, **kwargs) + for_ask_or_go(customer, rate, usage, **kwargs) end end - def self.for_ask_or_go(customer, otel, rate, usage, digits: nil, **kwargs) + def self.for_ask_or_go(customer, rate, usage, digits: nil, **kwargs) can_use = customer.minute_limit.to_d + customer.monthly_overage_limit - kwargs.merge!( - customer_id: customer.customer_id, - from: customer.registered?.phone, to: otel - ) + kwargs.merge!(customer_id: customer.customer_id) if digits != "1" && can_use - usage < rate * 10 AtLimit.new(**kwargs) else @@ -38,7 +35,7 @@ class CallAttempt value_semantics do customer_id String - from(/\A\+\d+\Z/) + from String to(/\A\+\d+\Z/) call_id String direction Either(:inbound, :outbound) @@ -93,12 +90,12 @@ class CallAttempt end class NoBalance - def self.for(customer, other_tel, rate, usage, direction:, **kwargs) + def self.for(customer, rate, usage, direction:, **kwargs) LowBalance.for(customer).then(&:notify!).then do |amount| if amount&.positive? CallAttempt.for( customer.with_balance(customer.balance + amount), - other_tel, rate, usage, direction: direction, **kwargs + rate, usage, direction: direction, **kwargs ) else NoBalance.new(balance: customer.balance, direction: direction) @@ -137,7 +134,7 @@ class CallAttempt class AtLimit value_semantics do customer_id String - from(/\A\+\d+\Z/) + from String to(/\A\+\d+\Z/) call_id String direction Either(:inbound, :outbound) diff --git a/lib/call_attempt_repo.rb b/lib/call_attempt_repo.rb index fb16917765059f03f0ae684b7be345eb970b7f46..52cc5f09c5907bb0864251ef5708c12626bc9ad5 100644 --- a/lib/call_attempt_repo.rb +++ b/lib/call_attempt_repo.rb @@ -10,19 +10,41 @@ class CallAttemptRepo db Anything(), default: LazyObject.new { DB } end - def find(customer, other_tel, direction: :outbound, **kwargs) + def find_outbound(customer, to, **kwargs) + find( + customer, + to, + direction: :outbound, + from: customer.registered?.phone, + to: to, + **kwargs + ) + end + + def find_inbound(customer, from, **kwargs) + find( + customer, + from, + direction: :inbound, + from: from, + to: customer.registered?.phone, + **kwargs + ) + end + +protected + + def find(customer, other_tel, direction:, **kwargs) EMPromise.all([ find_rate(customer.plan_name, other_tel, direction), find_usage(customer.customer_id) ]).then do |(rate, usage)| CallAttempt.for( - customer, other_tel, rate, usage, direction: direction, **kwargs + customer, rate, usage, direction: direction, **kwargs ) end end -protected - def find_usage(customer_id) promise = db.query_defer(<<~SQL, [customer_id]) SELECT COALESCE(SUM(charge), 0) AS a FROM cdr_with_charge diff --git a/web.rb b/web.rb index 2740b5217d0c1118fabc955d75fa15e45f2bf2fb..53f73ec65152d0309b6a7f441ab88aed184fd944 100644 --- a/web.rb +++ b/web.rb @@ -246,12 +246,11 @@ class Web < Roda r.post do customer_repo.find_by_tel(params["to"]).then do |customer| - call_attempt_repo.find( + call_attempt_repo.find_inbound( customer, params["from"], call_id: call_id, - digits: params["digits"], - direction: :inbound + digits: params["digits"] ).then { |ca| render(*ca.to_render) } end end @@ -263,9 +262,9 @@ class Web < Roda ).find_by_tel(params["to"]).then { |customer| EMPromise.all([ customer.fwd, - call_attempt_repo.find( + call_attempt_repo.find_inbound( customer, params["from"], - call_id: params["callId"], direction: :inbound + call_id: params["callId"] ) ]) }.then do |(fwd, ca)| @@ -302,7 +301,7 @@ class Web < Roda customer_repo( sgx_repo: Bwmsgsv2Repo.new ).find_by_format(from).then do |c| - call_attempt_repo.find( + call_attempt_repo.find_outbound( c, params["to"], call_id: params["callId"],