1# frozen_string_literal: true
2
3require_relative "bwmsgsv2_repo"
4require_relative "customer_repo"
5require_relative "proxied_jid"
6
7class CustomerInfoForm
8 def initialize(customer_repo=CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new))
9 @customer_repo = customer_repo
10 end
11
12 def find_customer(response)
13 parse_something(response.form.field("q").value)
14 end
15
16 def parse_something(value)
17 return EMPromise.resolve(nil) if value.to_s.empty?
18
19 EMPromise.all([
20 find_customer_one(value),
21 find_customer_one(Blather::JID.new(value)),
22 find_customer_one(ProxiedJID.proxy(value)),
23 find_customer_by_phone(value)
24 ]).then { |approaches| approaches.compact.first }
25 end
26
27 def find_customer_one(q)
28 @customer_repo.find_by_format(q).catch { nil }
29 end
30
31 def find_customer_by_phone(value)
32 value
33 .gsub(/\D/, "")
34 .match(/\A1?(\d{10})\Z/)
35 &.[](1)
36 &.then { |tn| find_customer_one("+1#{tn}") }
37 end
38end