Class: MedicalCopays::VBS::Service

Inherits:
Object
  • Object
show all
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.



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

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
# File 'app/services/medical_copays/vbs/service.rb', line 14

class Service
  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 = request.post("#{settings.base_path}/GetStatementsByEDIPIAndVistaAccountNumber", request_data.to_hash)

    # 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'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 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
# File 'app/services/medical_copays/vbs/service.rb', line 14

class Service
  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 = request.post("#{settings.base_path}/GetStatementsByEDIPIAndVistaAccountNumber", request_data.to_hash)

    # 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'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 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
# File 'app/services/medical_copays/vbs/service.rb', line 14

class Service
  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 = request.post("#{settings.base_path}/GetStatementsByEDIPIAndVistaAccountNumber", request_data.to_hash)

    # 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'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 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.



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

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



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

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

Instance Method Details

#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:



66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/services/medical_copays/vbs/service.rb', line 66

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_copaysHash

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

Returns:

  • (Hash)

Raises:



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

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

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

  # 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)


84
85
86
87
88
89
90
91
92
# File 'app/services/medical_copays/vbs/service.rb', line 84

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

#send_statement_notifications(statements_json_byte) ⇒ Object



94
95
96
# File 'app/services/medical_copays/vbs/service.rb', line 94

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

#settingsObject



98
99
100
# File 'app/services/medical_copays/vbs/service.rb', line 98

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