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	end
26
27	def customer_id
28		@params["CustomerOrderId"]
29	end
30
31	def loa_authorizing_person
32		"%s %s" % [
33			@params.dig("Subscriber", "FirstName"),
34			@params.dig("Subscriber", "LastName")
35		]
36	end
37
38	def to_h
39		@params.dup.tap do |h|
40			h["LoaAuthorizingPerson"] = loa_authorizing_person
41			h["BillingTelephoneNumber"].gsub!(/[^\d]/, "")
42			h["BillingTelephoneNumber"].gsub!(/\A1(\d{10})\Z/) { $1 }
43			h["ListOfPhoneNumbers"] = {
44				"PhoneNumber" => h["BillingTelephoneNumber"]
45			}
46		end
47	end
48
49	def message(order_id)
50		url = "https://dashboard.bandwidth.com/portal/r/a/" \
51		      "#{CONFIG[:creds][:account]}/orders/portIn/#{order_id}"
52		"New port-in request for #{customer_id}: #{url}"
53	end
54
55	def already_inservice?
56		BandwidthIris::InServiceNumber.get(@params["BillingTelephoneNumber"])
57		Command.finish(
58			"That number is already in service with us. " \
59			"Please contact support if you need assistance.",
60			type: :error
61		)
62		true
63	rescue APIError
64		false
65	end
66
67	def complete_with
68		EMPromise.resolve(self)
69	end
70
71	class Canada < PortInOrder
72		def message(order_id)
73			"#{super} (canadian port from #{@losing_carrier})"
74		end
75
76		def complete_with
77			yield(FormTemplate.render("lnp_canada")).then { |form|
78				@losing_carrier = form.field("LosingCarrier").value
79				self
80			}
81		end
82	end
83end