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 fields
41 [
42 { var: "Account Status", value: account_status },
43 { var: "Phone Number", value: tel || "Not Registered" },
44 { var: "Balance", value: "$%.4f" % balance },
45 next_renewal
46 ].compact
47 end
48end
49
50class AdminInfo
51 value_semantics do
52 jid ProxiedJID, coerce: ProxiedJID.method(:new)
53 customer_id String
54 info CustomerInfo
55 api API
56 end
57
58 def self.for(customer, plan, expires_at)
59 EMPromise.all([
60 CustomerInfo.for(customer, plan, expires_at),
61 customer.api
62 ]).then do |info, api_value|
63 new(
64 jid: customer.jid,
65 customer_id: customer.customer_id,
66 info: info, api: api_value
67 )
68 end
69 end
70
71 def plan_fields
72 [
73 { var: "Plan", value: info.plan.plan_name || "No Plan" },
74 { var: "Currency", value: (info.plan.currency || "No Currency").to_s }
75 ]
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 *plan_fields,
84 { var: "API", value: api.to_s }
85 ]
86 end
87end