customer_info.rb

  1# frozen_string_literal: true
  2
  3require "bigdecimal"
  4require "value_semantics/monkey_patched"
  5require_relative "proxied_jid"
  6require_relative "customer_plan"
  7
  8class CustomerInfo
  9	value_semantics do
 10		plan CustomerPlan
 11		auto_top_up_amount Integer
 12		tel Either(String, nil)
 13		balance BigDecimal
 14		expires_at Either(Time, nil)
 15		cnam Either(String, nil)
 16	end
 17
 18	def self.for(customer, plan, expires_at)
 19		plan.auto_top_up_amount.then do |auto_top_up_amount|
 20			new(
 21				plan: plan,
 22				auto_top_up_amount: auto_top_up_amount,
 23				tel: customer.registered? ? customer.registered?.phone : nil,
 24				balance: customer.balance,
 25				expires_at: expires_at,
 26				cnam: customer.tndetails&.dig(:features, :lidb, :subscriber_information)
 27			)
 28		end
 29	end
 30
 31	def account_status
 32		if plan.plan_name.nil?
 33			"Transitional"
 34		elsif plan.active?
 35			"Active"
 36		else
 37			"Expired"
 38		end
 39	end
 40
 41	def next_renewal
 42		return unless expires_at
 43
 44		{ var: "Next renewal", value: expires_at.strftime("%Y-%m-%d") }
 45	end
 46
 47	def monthly_amount
 48		return unless plan.monthly_price
 49
 50		{ var: "Renewal", value: "$%.4f / month" % plan.monthly_price }
 51	end
 52
 53	def auto_top_up
 54		{
 55			label: "Auto Top-up"
 56		}.merge(
 57			if auto_top_up_amount.positive?
 58				{ value: auto_top_up_amount.to_s, var: "auto_top_up_amount" }
 59			else
 60				{ value: "No" }
 61			end
 62		)
 63	end
 64
 65	def fields
 66		[
 67			{ var: "Account Status", value: account_status },
 68			{ var: "Phone Number", value: tel || "Not Registered" },
 69			({ var: "lidb_name", label: "CNAM", value: cnam } if cnam),
 70			{ var: "Balance", value: "$%.4f" % balance },
 71			monthly_amount,
 72			next_renewal,
 73			auto_top_up,
 74			{ var: "Currency", value: (plan.currency || "No Currency").to_s }
 75		].compact
 76	end
 77end
 78
 79class AdminInfo
 80	value_semantics do
 81		jid ProxiedJID, coerce: ProxiedJID.method(:new)
 82		customer_id String
 83		info CustomerInfo
 84		api API
 85	end
 86
 87	def self.for(customer, plan, expires_at)
 88		EMPromise.all([
 89			CustomerInfo.for(customer, plan, expires_at),
 90			customer.api
 91		]).then do |info, api_value|
 92			new(
 93				jid: customer.jid,
 94				customer_id: customer.customer_id,
 95				info: info, api: api_value
 96			)
 97		end
 98	end
 99
100	def fields
101		info.fields + [
102			{ var: "JID", value: jid.unproxied.to_s },
103			{ var: "Cheo JID", value: jid.to_s },
104			{ var: "Customer ID", value: customer_id },
105			{ var: "Plan", value: info.plan.plan_name || "No Plan" },
106			{ var: "API", value: api.to_s }
107		]
108	end
109end