From d8cf0f03d3dd3d68875346345985aa973ed2dfcc Mon Sep 17 00:00:00 2001 From: Christopher Vollick <0@psycoti.ca> Date: Thu, 31 Aug 2023 14:47:15 -0400 Subject: [PATCH] Toll Free Leads to Infinity If rate is zero, then minutes remaining becomes infinite. That's hard to express in json... So instead I just leave the limits out, which downstream can interpret as "unlimited". --- lib/call_attempt.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++- test/test_web.rb | 22 ++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/lib/call_attempt.rb b/lib/call_attempt.rb index 126654ea1ef97d4857d54844494974d46b69ed3d..645a656a550f041c7e6d699544f1297acb64f31c 100644 --- a/lib/call_attempt.rb +++ b/lib/call_attempt.rb @@ -20,7 +20,7 @@ class CallAttempt end def self.limits(customer, usage, credit, rate:, **) - return {} unless customer && usage && rate + return {} unless customer && usage && rate && rate.positive? can_use = customer.minute_limit.to_d + customer.monthly_overage_limit { @@ -70,6 +70,50 @@ class CallAttempt as_json.to_json(*args) end + class TollFree + CallAttempt.register do |rate:, customer:, **kwargs| + if rate&.zero? + new( + **kwargs + .merge(customer_id: customer.customer_id) + .slice(*value_semantics.attributes.map(&:name)) + ) + end + end + + value_semantics do + customer_id String + from String + to(/\A\+\d+\Z/) + call_id String + direction Either(:inbound, :outbound) + end + + def to_render + ["#{direction}/connect", { locals: to_h }] + end + + def to_s + "TollFree" + end + + def create_call(fwd, *args, &block) + fwd.create_call(*args, &block) + end + + def as_json(*) + { + from: from, + to: to, + customer_id: customer_id + }.compact + end + + def to_json(*args) + as_json.to_json(*args) + end + end + class Expired CallAttempt.register do |customer:, direction:, **| new(direction: direction) if customer.plan_name && !customer.active? diff --git a/test/test_web.rb b/test/test_web.rb index f7c7573c65ab2c04d65bf785b6a53e083a17e5cf..e334abf90ef4d3a73cbe6848da6c2bbacb0eafcd 100644 --- a/test/test_web.rb +++ b/test/test_web.rb @@ -112,6 +112,7 @@ class WebTest < Minitest::Test ["test_usd", "+15557654321", :outbound] => [{ "rate" => 0.01 }], ["test_usd", "+15557654321", :inbound] => [{ "rate" => 0.01 }], ["test_usd", "+14445556666", :inbound] => [{ "rate" => 0.01 }], + ["test_usd", "+18001234567", :outbound] => [{ "rate" => 0.00 }], ["customerid_limit"] => FakeDB::MultiResult.new( [{ "a" => 1000 }], [{ "settled_amount" => 15 }] @@ -337,6 +338,27 @@ class WebTest < Minitest::Test end em :test_outbound_atlimit_digits + def test_outbound_toll_free + post( + "/outbound/calls", + { + from: "ccustomerid", + to: "+18001234567", + callId: "acall" + }.to_json, + { "CONTENT_TYPE" => "application/json" } + ) + + assert last_response.ok? + assert_equal( + "" \ + "" \ + "+18001234567", + last_response.body + ) + end + em :test_outbound_toll_free + def test_inbound CustomerFwd::BANDWIDTH_VOICE.expect( :create_call,