# frozen_string_literal: true
require "bigdecimal"
require "forwardable"
require "relative_time"
require "value_semantics/monkey_patched"
require_relative "proxied_jid"
require_relative "customer_plan"
require_relative "form_template"
class PlanInfo
extend Forwardable
def_delegators :plan, :expires_at, :auto_top_up_amount
def self.for(plan)
return EMPromise.resolve(NoPlan.new) unless plan&.plan_name
plan.activation_date.then do |adate|
new(plan: plan, start_date: adate)
end
end
class NoPlan
def template
FormTemplate.new("")
end
def admin_template
FormTemplate.new("")
end
def status
"Transitional"
end
end
value_semantics do
plan CustomerPlan
start_date Time
end
def template
FormTemplate.for("plan_info", plan_info: self)
end
def admin_template
FormTemplate.for("admin_plan_info", plan_info: self)
end
def monthly_price
"$%.4f / month" % plan.monthly_price
end
def relative_start_date
RelativeTime.in_words(start_date)
end
def formatted_start_date
start_date.strftime("%Y-%m-%d %H:%M:%S")
end
def currency
(plan.currency || "No Currency").to_s
end
def status
plan.active? ? "Active" : "Expired"
end
end
class CustomerInfo
value_semantics do
plan_info Either(PlanInfo, PlanInfo::NoPlan)
tel Either(String, nil)
balance BigDecimal
cnam Either(String, nil)
end
def self.for(customer, plan)
PlanInfo.for(plan).then do |plan_info|
new(
plan_info: plan_info,
tel: customer.registered? ? customer.registered?.phone : nil,
balance: customer.balance,
cnam: customer.tndetails.dig(:features, :lidb, :subscriber_information)
)
end
end
def form
FormTemplate.render("customer_info", info: self)
end
end
class AdminInfo
value_semantics do
jid ProxiedJID, coerce: ProxiedJID.method(:new)
customer_id String
fwd Either(CustomerFwd, nil)
info CustomerInfo
api API
end
def self.for(customer, plan)
EMPromise.all([
CustomerInfo.for(customer, plan),
customer.api
]).then do |info, api_value|
new(
jid: customer.jid,
customer_id: customer.customer_id,
fwd: customer.fwd, info: info, api: api_value
)
end
end
def form
FormTemplate.render("admin_info", admin_info: self)
end
def tel_link
[
"https://dashboard.bandwidth.com/portal/r/a",
CONFIG[:creds][:account],
"numbers/details",
info.tel.gsub(/\A\+1/, "")
].join("/")
end
end