~singpolyma/sgx-jmp

ref: f571b5e726750cf8c4523c22d77e110194c3e16a sgx-jmp/lib/customer_info_form.rb -rw-r--r-- 1.0 KiB
f571b5e7Stephen Paul Weber Postgres#query_one 7 months 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
# 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

		def registered?
			false
		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