.rubocop.yml 🔗
@@ -40,6 +40,7 @@ Naming/MethodParameterName:
- id
- iq
- db
+ - to
Layout/IndentationStyle:
Enabled: false
Stephen Paul Weber created
Doesn't affect any current code paths, but should be correct in case we ever
start using it in the inbound case.
Also from does not have to be a formatted phone number, can be a string like
Anonymous, etc.
.rubocop.yml | 1 +
lib/call_attempt.rb | 21 +++++++++------------
lib/call_attempt_repo.rb | 30 ++++++++++++++++++++++++++----
web.rb | 11 +++++------
4 files changed, 41 insertions(+), 22 deletions(-)
@@ -40,6 +40,7 @@ Naming/MethodParameterName:
- id
- iq
- db
+ - to
Layout/IndentationStyle:
Enabled: false
@@ -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)
@@ -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
@@ -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"],