~singpolyma/sgx-jmp

sgx-jmp/lib/port_in_order.rb -rw-r--r-- 1.7 KiB
1ef966f3Amolith Eliminate a registration race condition 19 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

class PortInOrder
	using FormToH

	def self.parse(customer, form)
		params = form.to_h.slice(
			"BillingTelephoneNumber", "Subscriber", "WirelessInfo"
		)
		# If the zip has anything that's not a digit, assume Canada
		if params.dig("Subscriber", "ServiceAddress", "Zip") =~ /[^-\d\s]/
			Canada.new(customer, params)
		else
			new(customer, params)
		end
	end

	def initialize(customer, params)
		@params = params
		@params["CustomerOrderId"] = customer.customer_id
		@params["SiteId"] = CONFIG[:bandwidth_site]
		@params["PeerId"] = CONFIG[:bandwidth_peer]
		@params["ProcessingStatus"] = "DRAFT"
		@params["Subscriber"]["SubscriberType"] ||= "RESIDENTIAL"
	end

	def customer_id
		@params["CustomerOrderId"]
	end

	def loa_authorizing_person
		"%s %s" % [
			@params.dig("Subscriber", "FirstName"),
			@params.dig("Subscriber", "LastName")
		]
	end

	def to_h
		@params.dup.tap do |h|
			h["LoaAuthorizingPerson"] = loa_authorizing_person
			h["BillingTelephoneNumber"].gsub!(/[^\d]/, "")
			h["BillingTelephoneNumber"].gsub!(/\A1(\d{10})\Z/) { $1 }
			h["ListOfPhoneNumbers"] = {
				"PhoneNumber" => h["BillingTelephoneNumber"]
			}
		end
	end

	def message(order_id)
		url = "https://dashboard.bandwidth.com/portal/r/a/" \
		      "#{CONFIG[:creds][:account]}/orders/portIn/#{order_id}"
		"New port-in request for #{customer_id}: #{url}"
	end

	def complete_with
		EMPromise.resolve(self)
	end

	class Canada < PortInOrder
		def message(order_id)
			"#{super} (canadian port from #{@losing_carrier})"
		end

		def complete_with
			yield(FormTemplate.render("lnp_canada")).then { |form|
				@losing_carrier = form.field("LosingCarrier").value
				self
			}
		end
	end
end