Class: MAP::SignUp::Service

Inherits:
Common::Client::Base show all
Defined in:
lib/map/sign_up/service.rb

Instance Method Summary collapse

Methods inherited from Common::Client::Base

#config, configuration, #connection, #delete, #get, #perform, #post, #put, #raise_backend_exception, #raise_not_authenticated, #request, #sanitize_headers!, #service_name

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Instance Method Details

#agreements_accept(icn:, signature_name:, version:) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/map/sign_up/service.rb', line 23

def agreements_accept(icn:, signature_name:, version:)
  perform(:post,
          config.patients_agreements_path(icn),
          agreements_body(icn, signature_name, version),
          authenticated_header(icn))
  logger.info("#{logging_prefix} agreements accept success, icn: #{icn}")
rescue Common::Client::Errors::ParsingError => e
  message = "#{logging_prefix} agreements accept response parsing error"
  logger.error(message, { icn: })
  raise e, "#{message}, response unknown, icn: #{icn}"
rescue Common::Client::Errors::ClientError => e
  parse_and_raise_error(e, icn, 'agreements accept')
end

#agreements_body(icn, signature_name, version) ⇒ Object (private)



73
74
75
76
77
78
79
80
81
# File 'lib/map/sign_up/service.rb', line 73

def agreements_body(icn, signature_name, version)
  {
    responseDate: Time.zone.now,
    icn:,
    signatureName: signature_name,
    version: config.agreements_version_mapping[version],
    legalDisplayVersion: config.legal_display_version
  }.to_json
end

#agreements_decline(icn:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/map/sign_up/service.rb', line 37

def agreements_decline(icn:)
  perform(:delete, config.patients_agreements_path(icn), nil, authenticated_header(icn))
  logger.info("#{logging_prefix} agreements decline success, icn: #{icn}")
rescue Common::Client::Errors::ParsingError => e
  message = "#{logging_prefix} agreements decline response parsing error"
  logger.error(message, { icn: })
  raise e, "#{message}, response unknown, icn: #{icn}"
rescue Common::Client::Errors::ClientError => e
  parse_and_raise_error(e, icn, 'agreements decline')
end

#authenticated_header(icn) ⇒ Object (private)



68
69
70
71
# File 'lib/map/sign_up/service.rb', line 68

def authenticated_header(icn)
  access_token = SecurityToken::Service.new.token(application: :sign_up_service, icn:)
  config.authenticated_header(access_token[:access_token])
end

#build_response(response_body) ⇒ Object (private)



115
116
117
118
119
120
121
122
# File 'lib/map/sign_up/service.rb', line 115

def build_response(response_body)
  {
    agreement_signed: response_body['agreementSigned'],
    opt_out: response_body['optOut'],
    cerner_provisioned: response_body['cernerProvisioned'],
    bypass_eligible: response_body['bypassEligible']
  }
end

#loggerObject (private)



124
125
126
# File 'lib/map/sign_up/service.rb', line 124

def logger
  Rails.logger
end

#logging_prefixObject (private)



128
129
130
# File 'lib/map/sign_up/service.rb', line 128

def logging_prefix
  config.logging_prefix
end

#parse_and_raise_error(e, icn, action) ⇒ Object (private)

Raises:

  • (e)


101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/map/sign_up/service.rb', line 101

def parse_and_raise_error(e, icn, action)
  status = e.status
  parsed_body = e.body.presence || {}
  context = {
    id: parsed_body['id'],
    code: parsed_body['code'],
    error_code: parsed_body['errorCode'],
    message: parsed_body['message'],
    trace_id: parsed_body['traceId']
  }.compact
  raise e, "#{logging_prefix} #{action} failed, client error, status: #{status}," \
           " icn: #{icn}, context: #{context}"
end

#status(icn:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/map/sign_up/service.rb', line 11

def status(icn:)
  response = perform(:get, config.status_unauthenticated_path(icn), nil)
  logger.info("#{logging_prefix} status success, icn: #{icn}")
  build_response(response.body)
rescue Common::Client::Errors::ParsingError => e
  message = "#{logging_prefix} status response parsing error"
  logger.error(message, { icn: })
  raise e, "#{message}, response unknown, icn: #{icn}"
rescue Common::Client::Errors::ClientError => e
  parse_and_raise_error(e, icn, 'status')
end

#successful_update_provisioning_response(response, icn) ⇒ Object (private)



92
93
94
95
96
97
98
99
# File 'lib/map/sign_up/service.rb', line 92

def successful_update_provisioning_response(response, icn)
  parsed_response = build_response(response.body)

  logger.info("#{logging_prefix} update provisioning success," \
              " icn: #{icn}, parsed_response: #{parsed_response}")

  parsed_response
end

#update_provisioning(icn:, first_name:, last_name:, mpi_gcids:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/map/sign_up/service.rb', line 48

def update_provisioning(icn:, first_name:, last_name:, mpi_gcids:)
  response = perform(:put,
                     config.patients_provisioning_path(icn),
                     update_provisioning_params(first_name, last_name, mpi_gcids).to_json,
                     config.authenticated_provisioning_header)
  successful_update_provisioning_response(response, icn)
rescue Common::Client::Errors::ParsingError => e
  message = "#{logging_prefix} update provisioning response parsing error"
  logger.error(message, { icn: })
  raise e, "#{message}, response unknown, icn: #{icn}"
rescue Common::Client::Errors::ClientError => e
  if config.provisioning_acceptable_status.include?(e.status)
    successful_update_provisioning_response(e, icn)
  else
    parse_and_raise_error(e, icn, 'update provisioning')
  end
end

#update_provisioning_params(first_name, last_name, mpi_gcids) ⇒ Object (private)



83
84
85
86
87
88
89
90
# File 'lib/map/sign_up/service.rb', line 83

def update_provisioning_params(first_name, last_name, mpi_gcids)
  {
    provisionUser: true,
    gcids: mpi_gcids,
    firstName: first_name,
    lastName: last_name
  }
end