Class: MedicalCopays::VBS::Service

Inherits:
Object
  • Object
show all
Includes:
RedisCaching
Defined in:
app/services/medical_copays/vbs/service.rb

Overview

Service object for isolating dependencies in the MedicalCopaysController

Defined Under Namespace

Classes: StatementNotFound

Constant Summary collapse

STATSD_KEY_PREFIX =
'api.mcp.vbs'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Service

Returns a new instance of Service.



33
34
35
36
37
# File 'app/services/medical_copays/vbs/service.rb', line 33

def initialize(opts)
  @request = MedicalCopays::Request.build
  @user = opts[:user]
  @request_data = RequestData.build(user:) unless user.nil?
end

Instance Attribute Details

#requestMedicalCopays::Request



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/services/medical_copays/vbs/service.rb', line 14

class Service
  include RedisCaching
  class StatementNotFound < StandardError
  end

  attr_reader :request, :request_data, :user

  STATSD_KEY_PREFIX = 'api.mcp.vbs'

  ##
  # Builds a Service instance
  #
  # @param opts [Hash]
  # @return [Service] an instance of this class
  #
  def self.build(opts = {})
    new(opts)
  end

  def initialize(opts)
    @request = MedicalCopays::Request.build
    @user = opts[:user]
    @request_data = RequestData.build(user:) unless user.nil?
  end

  ##
  # Gets the user's medical copays by edipi and vista account numbers
  #
  # @return [Hash]
  #
  def get_copays
    raise InvalidVBSRequestError, request_data.errors unless request_data.valid?

    response = if Flipper.enabled?(:debts_cache_vbs_copays_empty_response)
                 get_cached_copay_response
               else
                 get_copay_response
               end

    # enable zero balance debt feature if flip is on
    if Flipper.enabled?(:medical_copays_zero_debt)
      zero_balance_statements = MedicalCopays::ZeroBalanceStatements.build(
        statements: response.body,
        facility_hash: user.vha_facility_hash
      )
      response.body.concat(zero_balance_statements.list)
    end

    ResponseData.build(response:).handle
  end

  def get_cached_copay_response
    StatsD.increment("#{STATSD_KEY_PREFIX}.init_cached_copays.fired")

    cached_response = get_user_cached_response
    if cached_response
      StatsD.increment("#{STATSD_KEY_PREFIX}.init_cached_copays.cached_response_returned")
      return cached_response
    end

    response = get_copay_response
    response_body = response.body

    if response_body.is_a?(Array) && response_body.empty?
      StatsD.increment("#{STATSD_KEY_PREFIX}.init_cached_copays.empty_response_cached")
      Rails.cache.write("vbs_copays_data_#{user.uuid}", response, expires_in: self.class.time_until_5am_utc)
    end

    response
  end

  ##
  # Get's the users' medical copay by statement id from list
  #
  # @param id [UUID] - uuid of the statement
  # @return [Hash] - JSON data of statement and status
  #
  def get_copay_by_id(id)
    all_statements = get_copays

    # Return hash with error information if bad response
    return all_statements unless all_statements[:status] == 200

    statement = get_copays[:data].find { |copay| copay['id'] == id }

    raise StatementNotFound if statement.nil?

    { data: statement, status: 200 }
  end

  ##
  # Gets the PDF medical copay statment by statment_id
  #
  # @return [Hash]
  #
  def get_pdf_statement_by_id(statement_id)
    StatsD.increment("#{STATSD_KEY_PREFIX}.pdf.total")
    response = request.get("#{settings.base_path}/GetPDFStatementById/#{statement_id}")

    Base64.decode64(response.body['statement'])
  rescue => e
    StatsD.increment("#{STATSD_KEY_PREFIX}.pdf.failure")
    raise e
  end

  def get_copay_response
    if Flipper.enabled?(:debts_copay_logging)
      Rails.logger.info("MedicalCopays::VBS::Service#get_copay_response request data: #{@user.uuid}")
    end

    request.post("#{settings.base_path}/GetStatementsByEDIPIAndVistaAccountNumber", request_data.to_hash)
  end

  def get_user_cached_response
    cache_key = "vbs_copays_data_#{user.uuid}"
    Rails.cache.read(cache_key)
  end

  def send_statement_notifications(statements_json_byte)
    CopayNotifications::ParseNewStatementsJob.perform_async(statements_json_byte)
  end

  def settings
    Flipper.enabled?(:medical_copays_api_key_change) ? Settings.mcp.vbs_v2 : Settings.mcp.vbs
  end
end

#request_dataRequestData

Returns:



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/services/medical_copays/vbs/service.rb', line 14

class Service
  include RedisCaching
  class StatementNotFound < StandardError
  end

  attr_reader :request, :request_data, :user

  STATSD_KEY_PREFIX = 'api.mcp.vbs'

  ##
  # Builds a Service instance
  #
  # @param opts [Hash]
  # @return [Service] an instance of this class
  #
  def self.build(opts = {})
    new(opts)
  end

  def initialize(opts)
    @request = MedicalCopays::Request.build
    @user = opts[:user]
    @request_data = RequestData.build(user:) unless user.nil?
  end

  ##
  # Gets the user's medical copays by edipi and vista account numbers
  #
  # @return [Hash]
  #
  def get_copays
    raise InvalidVBSRequestError, request_data.errors unless request_data.valid?

    response = if Flipper.enabled?(:debts_cache_vbs_copays_empty_response)
                 get_cached_copay_response
               else
                 get_copay_response
               end

    # enable zero balance debt feature if flip is on
    if Flipper.enabled?(:medical_copays_zero_debt)
      zero_balance_statements = MedicalCopays::ZeroBalanceStatements.build(
        statements: response.body,
        facility_hash: user.vha_facility_hash
      )
      response.body.concat(zero_balance_statements.list)
    end

    ResponseData.build(response:).handle
  end

  def get_cached_copay_response
    StatsD.increment("#{STATSD_KEY_PREFIX}.init_cached_copays.fired")

    cached_response = get_user_cached_response
    if cached_response
      StatsD.increment("#{STATSD_KEY_PREFIX}.init_cached_copays.cached_response_returned")
      return cached_response
    end

    response = get_copay_response
    response_body = response.body

    if response_body.is_a?(Array) && response_body.empty?
      StatsD.increment("#{STATSD_KEY_PREFIX}.init_cached_copays.empty_response_cached")
      Rails.cache.write("vbs_copays_data_#{user.uuid}", response, expires_in: self.class.time_until_5am_utc)
    end

    response
  end

  ##
  # Get's the users' medical copay by statement id from list
  #
  # @param id [UUID] - uuid of the statement
  # @return [Hash] - JSON data of statement and status
  #
  def get_copay_by_id(id)
    all_statements = get_copays

    # Return hash with error information if bad response
    return all_statements unless all_statements[:status] == 200

    statement = get_copays[:data].find { |copay| copay['id'] == id }

    raise StatementNotFound if statement.nil?

    { data: statement, status: 200 }
  end

  ##
  # Gets the PDF medical copay statment by statment_id
  #
  # @return [Hash]
  #
  def get_pdf_statement_by_id(statement_id)
    StatsD.increment("#{STATSD_KEY_PREFIX}.pdf.total")
    response = request.get("#{settings.base_path}/GetPDFStatementById/#{statement_id}")

    Base64.decode64(response.body['statement'])
  rescue => e
    StatsD.increment("#{STATSD_KEY_PREFIX}.pdf.failure")
    raise e
  end

  def get_copay_response
    if Flipper.enabled?(:debts_copay_logging)
      Rails.logger.info("MedicalCopays::VBS::Service#get_copay_response request data: #{@user.uuid}")
    end

    request.post("#{settings.base_path}/GetStatementsByEDIPIAndVistaAccountNumber", request_data.to_hash)
  end

  def get_user_cached_response
    cache_key = "vbs_copays_data_#{user.uuid}"
    Rails.cache.read(cache_key)
  end

  def send_statement_notifications(statements_json_byte)
    CopayNotifications::ParseNewStatementsJob.perform_async(statements_json_byte)
  end

  def settings
    Flipper.enabled?(:medical_copays_api_key_change) ? Settings.mcp.vbs_v2 : Settings.mcp.vbs
  end
end

#response_dataResponseData

Returns:



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/services/medical_copays/vbs/service.rb', line 14

class Service
  include RedisCaching
  class StatementNotFound < StandardError
  end

  attr_reader :request, :request_data, :user

  STATSD_KEY_PREFIX = 'api.mcp.vbs'

  ##
  # Builds a Service instance
  #
  # @param opts [Hash]
  # @return [Service] an instance of this class
  #
  def self.build(opts = {})
    new(opts)
  end

  def initialize(opts)
    @request = MedicalCopays::Request.build
    @user = opts[:user]
    @request_data = RequestData.build(user:) unless user.nil?
  end

  ##
  # Gets the user's medical copays by edipi and vista account numbers
  #
  # @return [Hash]
  #
  def get_copays
    raise InvalidVBSRequestError, request_data.errors unless request_data.valid?

    response = if Flipper.enabled?(:debts_cache_vbs_copays_empty_response)
                 get_cached_copay_response
               else
                 get_copay_response
               end

    # enable zero balance debt feature if flip is on
    if Flipper.enabled?(:medical_copays_zero_debt)
      zero_balance_statements = MedicalCopays::ZeroBalanceStatements.build(
        statements: response.body,
        facility_hash: user.vha_facility_hash
      )
      response.body.concat(zero_balance_statements.list)
    end

    ResponseData.build(response:).handle
  end

  def get_cached_copay_response
    StatsD.increment("#{STATSD_KEY_PREFIX}.init_cached_copays.fired")

    cached_response = get_user_cached_response
    if cached_response
      StatsD.increment("#{STATSD_KEY_PREFIX}.init_cached_copays.cached_response_returned")
      return cached_response
    end

    response = get_copay_response
    response_body = response.body

    if response_body.is_a?(Array) && response_body.empty?
      StatsD.increment("#{STATSD_KEY_PREFIX}.init_cached_copays.empty_response_cached")
      Rails.cache.write("vbs_copays_data_#{user.uuid}", response, expires_in: self.class.time_until_5am_utc)
    end

    response
  end

  ##
  # Get's the users' medical copay by statement id from list
  #
  # @param id [UUID] - uuid of the statement
  # @return [Hash] - JSON data of statement and status
  #
  def get_copay_by_id(id)
    all_statements = get_copays

    # Return hash with error information if bad response
    return all_statements unless all_statements[:status] == 200

    statement = get_copays[:data].find { |copay| copay['id'] == id }

    raise StatementNotFound if statement.nil?

    { data: statement, status: 200 }
  end

  ##
  # Gets the PDF medical copay statment by statment_id
  #
  # @return [Hash]
  #
  def get_pdf_statement_by_id(statement_id)
    StatsD.increment("#{STATSD_KEY_PREFIX}.pdf.total")
    response = request.get("#{settings.base_path}/GetPDFStatementById/#{statement_id}")

    Base64.decode64(response.body['statement'])
  rescue => e
    StatsD.increment("#{STATSD_KEY_PREFIX}.pdf.failure")
    raise e
  end

  def get_copay_response
    if Flipper.enabled?(:debts_copay_logging)
      Rails.logger.info("MedicalCopays::VBS::Service#get_copay_response request data: #{@user.uuid}")
    end

    request.post("#{settings.base_path}/GetStatementsByEDIPIAndVistaAccountNumber", request_data.to_hash)
  end

  def get_user_cached_response
    cache_key = "vbs_copays_data_#{user.uuid}"
    Rails.cache.read(cache_key)
  end

  def send_statement_notifications(statements_json_byte)
    CopayNotifications::ParseNewStatementsJob.perform_async(statements_json_byte)
  end

  def settings
    Flipper.enabled?(:medical_copays_api_key_change) ? Settings.mcp.vbs_v2 : Settings.mcp.vbs
  end
end

#userObject (readonly)

Returns the value of attribute user.



19
20
21
# File 'app/services/medical_copays/vbs/service.rb', line 19

def user
  @user
end

Class Method Details

.build(opts = {}) ⇒ Service

Builds a Service instance

Parameters:

  • opts (Hash) (defaults to: {})

Returns:

  • (Service)

    an instance of this class



29
30
31
# File 'app/services/medical_copays/vbs/service.rb', line 29

def self.build(opts = {})
  new(opts)
end

Instance Method Details

#get_cached_copay_responseObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/services/medical_copays/vbs/service.rb', line 65

def get_cached_copay_response
  StatsD.increment("#{STATSD_KEY_PREFIX}.init_cached_copays.fired")

  cached_response = get_user_cached_response
  if cached_response
    StatsD.increment("#{STATSD_KEY_PREFIX}.init_cached_copays.cached_response_returned")
    return cached_response
  end

  response = get_copay_response
  response_body = response.body

  if response_body.is_a?(Array) && response_body.empty?
    StatsD.increment("#{STATSD_KEY_PREFIX}.init_cached_copays.empty_response_cached")
    Rails.cache.write("vbs_copays_data_#{user.uuid}", response, expires_in: self.class.time_until_5am_utc)
  end

  response
end

#get_copay_by_id(id) ⇒ Hash

Get’s the users’ medical copay by statement id from list

Parameters:

  • id (UUID)
    • uuid of the statement

Returns:

  • (Hash)
    • JSON data of statement and status

Raises:



91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/services/medical_copays/vbs/service.rb', line 91

def get_copay_by_id(id)
  all_statements = get_copays

  # Return hash with error information if bad response
  return all_statements unless all_statements[:status] == 200

  statement = get_copays[:data].find { |copay| copay['id'] == id }

  raise StatementNotFound if statement.nil?

  { data: statement, status: 200 }
end

#get_copay_responseObject



119
120
121
122
123
124
125
# File 'app/services/medical_copays/vbs/service.rb', line 119

def get_copay_response
  if Flipper.enabled?(:debts_copay_logging)
    Rails.logger.info("MedicalCopays::VBS::Service#get_copay_response request data: #{@user.uuid}")
  end

  request.post("#{settings.base_path}/GetStatementsByEDIPIAndVistaAccountNumber", request_data.to_hash)
end

#get_copaysHash

Gets the user’s medical copays by edipi and vista account numbers

Returns:

  • (Hash)

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/services/medical_copays/vbs/service.rb', line 44

def get_copays
  raise InvalidVBSRequestError, request_data.errors unless request_data.valid?

  response = if Flipper.enabled?(:debts_cache_vbs_copays_empty_response)
               get_cached_copay_response
             else
               get_copay_response
             end

  # enable zero balance debt feature if flip is on
  if Flipper.enabled?(:medical_copays_zero_debt)
    zero_balance_statements = MedicalCopays::ZeroBalanceStatements.build(
      statements: response.body,
      facility_hash: user.vha_facility_hash
    )
    response.body.concat(zero_balance_statements.list)
  end

  ResponseData.build(response:).handle
end

#get_pdf_statement_by_id(statement_id) ⇒ Hash

Gets the PDF medical copay statment by statment_id

Returns:

  • (Hash)


109
110
111
112
113
114
115
116
117
# File 'app/services/medical_copays/vbs/service.rb', line 109

def get_pdf_statement_by_id(statement_id)
  StatsD.increment("#{STATSD_KEY_PREFIX}.pdf.total")
  response = request.get("#{settings.base_path}/GetPDFStatementById/#{statement_id}")

  Base64.decode64(response.body['statement'])
rescue => e
  StatsD.increment("#{STATSD_KEY_PREFIX}.pdf.failure")
  raise e
end

#get_user_cached_responseObject



127
128
129
130
# File 'app/services/medical_copays/vbs/service.rb', line 127

def get_user_cached_response
  cache_key = "vbs_copays_data_#{user.uuid}"
  Rails.cache.read(cache_key)
end

#send_statement_notifications(statements_json_byte) ⇒ Object



132
133
134
# File 'app/services/medical_copays/vbs/service.rb', line 132

def send_statement_notifications(statements_json_byte)
  CopayNotifications::ParseNewStatementsJob.perform_async(statements_json_byte)
end

#settingsObject



136
137
138
# File 'app/services/medical_copays/vbs/service.rb', line 136

def settings
  Flipper.enabled?(:medical_copays_api_key_change) ? Settings.mcp.vbs_v2 : Settings.mcp.vbs
end