~singpolyma/sgx-jmp

sgx-jmp/lib/customer_info_form.rb -rw-r--r-- 905 bytes
94298f5dStephen Paul Weber Refactor alt top up to use FormTemplate 2 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
# 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

	def parse_something(value)
		return EMPromise.resolve(nil) if value.to_s.empty?

		EMPromise.all([
			find_customer_one(value),
			find_customer_one(Blather::JID.new(value)),
			find_customer_one(ProxiedJID.proxy(value)),
			find_customer_by_phone(value)
		]).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