~singpolyma/sgx-jmp

ref: e90dcfacd8c9bc08a2a7d14b52a44db8cb2c3e88 sgx-jmp/lib/customer_info_form.rb -rw-r--r-- 1005 bytes
e90dcfacStephen Paul Weber Support older ruby a year 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
# frozen_string_literal: true

require_relative "bwmsgsv2_repo"
require_relative "customer_repo"
require_relative "proxied_jid"

class CustomerInfoForm
	def initialize(customer_repo=CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new))
		@customer_repo = customer_repo
	end

	def find_customer(response)
		parse_something(response.form.field("q").value)
	end

	class NoCustomer
		def form
			FormTemplate.render("no_customer_info")
		end

		def admin_info
			self
		end
	end

	def parse_something(value)
		EMPromise.all([
			find_customer_one(value),
			find_customer_one(Blather::JID.new(value)),
			find_customer_one(ProxiedJID.proxy(value)),
			find_customer_by_phone(value),
			EMPromise.resolve(NoCustomer.new)
		]).then { |approaches| approaches.compact.first }
	end

	def find_customer_one(q)
		@customer_repo.find_by_format(q).catch { nil }
	end

	def find_customer_by_phone(value)
		value
			.gsub(/\D/, "")
			.match(/\A1?(\d{10})\Z/)
			&.[](1)
			&.then { |tn| find_customer_one("+1#{tn}") }
	end
end