# frozen_string_literal: true
require "value_semantics/monkey_patched"
require_relative "proxied_jid"
require_relative "customer_plan"
class CustomerInfo
value_semantics do
plan CustomerPlan
tel Either(String, nil)
balance BigDecimal
expires_at Either(Time, nil)
end
def self.for(customer, plan, expires_at)
customer.registered?.then do |registration|
new(
plan: plan,
tel: registration ? registration.phone : nil,
balance: customer.balance,
expires_at: expires_at
)
end
end
def account_status
if plan.plan_name.nil?
"Transitional"
elsif plan.active?
"Active"
else
"Expired"
end
end
def next_renewal
{ var: "Next renewal", value: expires_at.strftime("%Y-%m-%d") } if expires_at
end
def fields
[
{ var: "Account Status", value: account_status },
{ var: "Phone Number", value: tel || "Not Registered" },
{ var: "Balance", value: "$%.4f" % balance },
next_renewal
].compact
end
end
class AdminInfo
value_semantics do
jid ProxiedJID, coerce: ProxiedJID.method(:new)
customer_id String
info CustomerInfo
api API
end
def self.for(customer, plan, expires_at)
EMPromise.all([
CustomerInfo.for(customer, plan, expires_at),
customer.api
]).then do |info, api_value|
new(
jid: customer.jid,
customer_id: customer.customer_id,
info: info, api: api_value
)
end
end
def plan_fields
[
{ var: "Plan", value: info.plan.plan_name || "No Plan" },
{ var: "Currency", value: (info.plan.currency || "No Currency").to_s }
]
end
def fields
info.fields + [
{ var: "JID", value: jid.unproxied.to_s },
{ var: "Cheo JID", value: jid.to_s },
{ var: "Customer ID", value: customer_id },
*plan_fields,
{ var: "API", value: api.to_s }
]
end
end