1# frozen_string_literal: true
2
3require "blather"
4require "blather/stanza/iq/command"
5
6module PortOutPin
7 def build_submit_form(pin, session_id)
8 build_port_out_command(:complete, session_id: session_id).tap { |iq|
9 iq.form.type = :submit
10 iq.form.fields = [
11 { var: "pin", value: pin, type: "text-private" },
12 { var: "confirm_pin", value: pin, type: "text-private" }
13 ]
14 }
15 end
16
17 def build_port_out_command(action, session_id: nil)
18 Blather::Stanza::Iq::Command.new.tap { |iq|
19 iq.to = jid
20 iq.from = from_jid
21 iq.node = "set-port-out-pin"
22 iq.action = action
23 iq.sessionid = session_id if session_id
24 }
25 end
26
27 def validate_submit_reply!(submit_reply)
28 sub_text = submit_reply.note&.text
29 case submit_reply.status
30 when :completed
31 raise sub_text if submit_reply.note&.[]("type") == "error"
32 when :canceled
33 raise CanceledError, reply.note&.text
34 else
35 raise sub_text
36 end
37 end
38
39 def handle_pin_submission_error(e)
40 if e.is_a?(Blather::StanzaError) || e.is_a?(RuntimeError)
41 EMPromise.reject(e)
42 else
43 Sentry.capture_exception(e)
44 EMPromise.reject(
45 RuntimeError.new(
46 "Unable to communicate with service. Please try again later."
47 )
48 )
49 end
50 end
51end