Module: FormAttachmentCreate

Instance Method Summary collapse

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

#createObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/concerns/form_attachment_create.rb', line 7

def create
  debug_timestamp = Time.current.iso8601
  if Flipper.enabled?(:hca_log_form_attachment_create)
    log_message_to_sentry(
      'begin form attachment creation',
      :info,
      file_data_present: filtered_params[:file_data].present?,
      klass: filtered_params[:file_data]&.class&.name,
      debug_timestamp:
    )
  end

  validate_file_upload_class!
  save_attachment_to_cloud!
  save_attachment_to_db!

  serialized = serializer_klass.new(form_attachment)

  if Flipper.enabled?(:hca_log_form_attachment_create)
    log_message_to_sentry('finish form attachment creation', :info, serialized: serialized.present?, debug_timestamp:)
  end

  render json: serialized
end

#extract_params_from_namespaceObject (private)



94
95
96
97
# File 'app/controllers/concerns/form_attachment_create.rb', line 94

def extract_params_from_namespace
  namespace = form_attachment_model.to_s.underscore.split('/').last
  params.require(namespace).permit(:file_data, :password)
end

#filtered_paramsObject (private)



90
91
92
# File 'app/controllers/concerns/form_attachment_create.rb', line 90

def filtered_params
  @filtered_params ||= extract_params_from_namespace
end

#form_attachmentObject (private)



82
83
84
# File 'app/controllers/concerns/form_attachment_create.rb', line 82

def form_attachment
  @form_attachment ||= form_attachment_model.new
end

#form_attachment_modelObject (private)



86
87
88
# File 'app/controllers/concerns/form_attachment_create.rb', line 86

def form_attachment_model
  @form_attachment_model ||= self.class::FORM_ATTACHMENT_MODEL
end

#save_attachment_to_cloud!Object (private)



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/concerns/form_attachment_create.rb', line 54

def save_attachment_to_cloud!
  form_attachment.set_file_data!(filtered_params[:file_data], filtered_params[:password])
rescue => e
  log_message_to_sentry(
    'form attachment error 2 - save to cloud',
    :info,
    has_pass: filtered_params[:password].present?,
    ext: File.extname(filtered_params[:file_data]).last(5),
    phase: 'FAC_cloud',
    exception: e.message
  )
  raise e
end

#save_attachment_to_db!Object (private)



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/concerns/form_attachment_create.rb', line 68

def save_attachment_to_db!
  form_attachment.save!
rescue => e
  log_message_to_sentry(
    'form attachment error 3 - save to db',
    :info,
    phase: 'FAC_db',
    errors: form_attachment.errors,
    exception: e.message
  )

  raise e
end

#serializer_klassObject (private)

Raises:

  • (NotImplementedError)


34
35
36
# File 'app/controllers/concerns/form_attachment_create.rb', line 34

def serializer_klass
  raise NotImplementedError, 'Class must implement serializer method'
end

#validate_file_upload_class!Object (private)



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/concerns/form_attachment_create.rb', line 38

def validate_file_upload_class!
  # is it either ActionDispatch::Http::UploadedFile or Rack::Test::UploadedFile
  unless filtered_params[:file_data].class.name.include? 'UploadedFile'
    raise Common::Exceptions::InvalidFieldValue.new('file_data', filtered_params[:file_data].class.name)
  end
rescue => e
  log_message_to_sentry(
    'form attachment error 1 - validate class',
    :info,
    phase: 'FAC_validate',
    klass: filtered_params[:file_data].class.name,
    exception: e.message
  )
  raise e
end