customer_info.rb

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