Class: BenefitsDocuments::Service

Inherits:
Common::Client::Base show all
Defined in:
lib/lighthouse/benefits_documents/service.rb

Constant Summary collapse

STATSD_KEY_PREFIX =
'api.benefits_documents'
STATSD_UPLOAD_LATENCY =
'lighthouse.api.benefits.documents.latency'

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

Constructor Details

#initialize(user) ⇒ Service

Returns a new instance of Service.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
# File 'lib/lighthouse/benefits_documents/service.rb', line 13

def initialize(user)
  @user = user
  raise ArgumentError, 'no user passed in for LH API request.' if @user.blank?

  super()
end

Instance Method Details

#build_lh_doc(file, file_params) ⇒ Object (private)



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/lighthouse/benefits_documents/service.rb', line 81

def build_lh_doc(file, file_params)
  claim_id = file_params[:claimId] || file_params[:claim_id]
  tracked_item_ids = file_params[:trackedItemIds] || file_params[:tracked_item_ids]
  document_type = file_params[:documentType] || file_params[:document_type]
  password = file_params[:password]

  LighthouseDocument.new(
    first_name: @user.first_name,
    participant_id: @user.participant_id,
    claim_id:,
    file_obj: file,
    uuid: SecureRandom.uuid,
    file_name: file.original_filename,
    tracked_item_id: tracked_item_ids,
    document_type:,
    password:
  )
end

#cleanup_after_uploadObject



55
56
57
# File 'lib/lighthouse/benefits_documents/service.rb', line 55

def cleanup_after_upload
  FileUtils.rm_rf(@base_path) if @base_path
end

#generate_multi_image_pdf(image_list) ⇒ Object (private)



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/lighthouse/benefits_documents/service.rb', line 109

def generate_multi_image_pdf(image_list)
  @base_path = Rails.root.join 'tmp', 'uploads', 'cache', SecureRandom.uuid
  img_path = "#{@base_path}/tempFile.jpg"
  pdf_filename = 'multifile.pdf'
  pdf_path = "#{@base_path}/#{pdf_filename}"
  FileUtils.mkpath @base_path
  Prawn::Document.generate(pdf_path) do |pdf|
    image_list.each do |img|
      File.binwrite(img_path, Base64.decode64(img))
      img = MiniMagick::Image.open(img_path)
      if img.height > pdf.bounds.top || img.width > pdf.bounds.right
        pdf.image img_path, fit: [pdf.bounds.right, pdf.bounds.top]
      else
        pdf.image img_path
      end
      pdf.start_new_page unless pdf.page_count == image_list.length
    end
  end
  temp_file = Tempfile.new(pdf_filename, encoding: 'ASCII-8BIT')
  temp_file.write(File.read(pdf_path))
  ActionDispatch::Http::UploadedFile.new(filename: pdf_filename, type: 'application/pdf', tempfile: temp_file)
end

#handle_error(error, lighthouse_client_id, endpoint) ⇒ Object (private)



100
101
102
103
104
105
106
107
# File 'lib/lighthouse/benefits_documents/service.rb', line 100

def handle_error(error, lighthouse_client_id, endpoint)
  Lighthouse::ServiceException.send_error(
    error,
    self.class.to_s.underscore,
    lighthouse_client_id,
    "#{config.base_api_path}/#{endpoint}"
  )
end

#queue_document_upload(params, lighthouse_client_id = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lighthouse/benefits_documents/service.rb', line 20

def queue_document_upload(params, lighthouse_client_id = nil)
  loggable_params = params.except(:password)
  Rails.logger.info('Parameters for document upload', loggable_params)

  start_timer = Time.zone.now
  claim_id = params[:claimId] || params[:claim_id]

  unless claim_id
    raise Common::Exceptions::InternalServerError,
          ArgumentError.new("Claim with id #{claim_id} not found")
  end

  jid = submit_document(params[:file], params, lighthouse_client_id)
  StatsD.measure(STATSD_UPLOAD_LATENCY, Time.zone.now - start_timer, tags: ['is_multifile:false'])
  jid
end

#queue_multi_image_upload_document(params, lighthouse_client_id = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lighthouse/benefits_documents/service.rb', line 37

def queue_multi_image_upload_document(params, lighthouse_client_id = nil)
  loggable_params = params.except(:password)
  loggable_params[:icn] = @user.icn
  Rails.logger.info('Parameters for document multi image upload', loggable_params)

  start_timer = Time.zone.now
  claim_id = params[:claimId] || params[:claim_id]
  unless claim_id
    raise Common::Exceptions::InternalServerError,
          ArgumentError.new("Claim with id #{claim_id} not found")
  end

  file_to_upload = generate_multi_image_pdf(params[:files])
  jid = submit_document(file_to_upload, params, lighthouse_client_id)
  StatsD.measure(STATSD_UPLOAD_LATENCY, Time.zone.now - start_timer, tags: ['is_multifile:true'])
  jid
end

#submit_document(file, file_params, lighthouse_client_id = nil) ⇒ Object (private)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lighthouse/benefits_documents/service.rb', line 61

def submit_document(file, file_params, lighthouse_client_id = nil)
  user_icn = @user.icn
  document_data = build_lh_doc(file, file_params)

  raise Common::Exceptions::ValidationErrors, document_data unless document_data.valid?

  uploader = LighthouseDocumentUploader.new(user_icn, document_data.uploader_ids)
  uploader.store!(document_data.file_obj)
  # the uploader sanitizes the filename before storing, so set our doc to match
  document_data.file_name = uploader.final_filename
  if Flipper.enabled?(:cst_synchronous_evidence_uploads, @user)
    Lighthouse::DocumentUploadSynchronous.upload(user_icn, document_data.to_serializable_hash)
  else
    Lighthouse::DocumentUpload.perform_async(user_icn, document_data.to_serializable_hash)
  end
rescue CarrierWave::IntegrityError => e
  handle_error(e, lighthouse_client_id, uploader.store_dir)
  raise e
end