port_in_order.rb

 1# frozen_string_literal: true
 2
 3class PortInOrder
 4	using FormToH
 5
 6	def self.parse(customer, form)
 7		params = form.to_h.slice(
 8			"BillingTelephoneNumber", "Subscriber", "WirelessInfo"
 9		)
10		# If the zip has anything that's not a digit, assume Canada
11		if params.dig("Subscriber", "ServiceAddress", "Zip") =~ /[^-\d\s]/
12			Canada.new(customer, params)
13		else
14			new(customer, params)
15		end
16	end
17
18	def initialize(customer, params)
19		@params = params
20		@params["CustomerOrderId"] = customer.customer_id
21		@params["SiteId"] = CONFIG[:bandwidth_site]
22		@params["PeerId"] = CONFIG[:bandwidth_peer]
23		@params["ProcessingStatus"] = "DRAFT"
24		@params["Subscriber"]["SubscriberType"] ||= "RESIDENTIAL"
25
26		@params["BillingTelephoneNumber"].gsub!(/[^\d]/, "")
27		@params["BillingTelephoneNumber"].gsub!(/\A1(\d{10})\Z/) { $1 }
28	end
29
30	def customer_id
31		@params["CustomerOrderId"]
32	end
33
34	def loa_authorizing_person
35		"%s %s" % [
36			@params.dig("Subscriber", "FirstName"),
37			@params.dig("Subscriber", "LastName")
38		]
39	end
40
41	def to_h
42		@params.dup.tap do |h|
43			h["LoaAuthorizingPerson"] = loa_authorizing_person
44			h["ListOfPhoneNumbers"] = {
45				"PhoneNumber" => h["BillingTelephoneNumber"]
46			}
47		end
48	end
49
50	def message(order_id)
51		url = "https://dashboard.bandwidth.com/portal/r/a/" \
52		      "#{CONFIG[:creds][:account]}/orders/portIn/#{order_id}"
53		"New port-in request for #{customer_id}: #{url}"
54	end
55
56	def already_inservice?
57		BandwidthIris::InServiceNumber.get(@params["BillingTelephoneNumber"])
58		Command.finish(
59			"That number is already in service with us. " \
60			"Please contact support if you need assistance.",
61			type: :error
62		)
63		true
64	rescue BandwidthIris::APIError
65		false
66	end
67
68	def complete_with
69		EMPromise.resolve(self)
70	end
71
72	class Canada < PortInOrder
73		def message(order_id)
74			"#{super} (canadian port from #{@losing_carrier})"
75		end
76
77		def complete_with
78			yield(FormTemplate.render("lnp_canada")).then { |form|
79				@losing_carrier = form.field("LosingCarrier").value
80				self
81			}
82		end
83	end
84end