diff --git a/app/controllers/v0/dependents_applications_controller.rb b/app/controllers/v0/dependents_applications_controller.rb index e745fbd9595..e52434e7d8a 100644 --- a/app/controllers/v0/dependents_applications_controller.rb +++ b/app/controllers/v0/dependents_applications_controller.rb @@ -17,7 +17,7 @@ def create dependent_service.submit_686c_form(claim) Rails.logger.info "ClaimID=#{claim.confirmation_number} Form=#{claim.class::FORM}" - clear_saved_form(claim.form_id) + # clear_saved_form(claim.form_id) # We do not want to destroy the InProgressForm for this submission render(json: claim) end diff --git a/app/controllers/v0/in_progress_forms_controller.rb b/app/controllers/v0/in_progress_forms_controller.rb index 81795cacfd9..d0d75b5d521 100644 --- a/app/controllers/v0/in_progress_forms_controller.rb +++ b/app/controllers/v0/in_progress_forms_controller.rb @@ -8,7 +8,7 @@ class InProgressFormsController < ApplicationController def index # :unaltered prevents the keys from being deeply transformed, which might corrupt some keys # see https://github.com/department-of-veterans-affairs/va.gov-team/issues/17595 for more details - render json: InProgressForm.for_user(@current_user), key_transform: :unaltered + render json: InProgressForm.submission_pending.for_user(@current_user), key_transform: :unaltered end def show @@ -37,7 +37,7 @@ def destroy private def form_for_user - @form_for_user ||= InProgressForm.form_for_user(form_id, @current_user) + @form_for_user ||= InProgressForm.submission_pending.form_for_user(form_id, @current_user) end def form_id diff --git a/app/models/in_progress_form.rb b/app/models/in_progress_form.rb index 24b08e1bc70..7944ff88b96 100644 --- a/app/models/in_progress_form.rb +++ b/app/models/in_progress_form.rb @@ -31,6 +31,8 @@ def cast(value) scope :for_form, ->(form_id) { where(form_id:) } scope :not_submitted, -> { where.not("metadata -> 'submission' ->> 'status' = ?", 'applicationSubmitted') } scope :unsubmitted_fsr, -> { for_form('5655').not_submitted } + enum :status, %w[pending processing], prefix: :submission, default: :pending + scope :submission_pending, -> { where(status: [nil, 'pending']) } # override to include nil attribute :user_uuid, CleanUUID.new serialize :form_data, coder: JsonMarshal::Marshaller has_kms_key diff --git a/app/services/bgs/dependent_service.rb b/app/services/bgs/dependent_service.rb index a721a7b9cd5..02d9a90466f 100644 --- a/app/services/bgs/dependent_service.rb +++ b/app/services/bgs/dependent_service.rb @@ -39,6 +39,8 @@ def get_dependents def submit_686c_form(claim) Rails.logger.info('BGS::DependentService running!', { user_uuid: uuid, saved_claim_id: claim.id, icn: }) + InProgressForm.find_by(form_id: BGS::SubmitForm686cJob::FORM_ID, user_uuid: uuid)&.submission_processing! + encrypted_vet_info = KmsEncrypted::Box.new.encrypt(get_form_hash_686c.to_json) submit_pdf_job(claim:, encrypted_vet_info:) diff --git a/app/services/users/profile.rb b/app/services/users/profile.rb index f2dded83b7e..d7da58ca164 100644 --- a/app/services/users/profile.rb +++ b/app/services/users/profile.rb @@ -160,7 +160,7 @@ def veteran_status end def in_progress_forms - InProgressForm.for_user(user).map do |form| + InProgressForm.submission_pending.for_user(user).map do |form| { form: form.form_id, metadata: form.metadata, diff --git a/app/sidekiq/bgs/submit_form674_job.rb b/app/sidekiq/bgs/submit_form674_job.rb index 1a10dee76fc..f53199734cb 100644 --- a/app/sidekiq/bgs/submit_form674_job.rb +++ b/app/sidekiq/bgs/submit_form674_job.rb @@ -9,7 +9,7 @@ class Invalid674Claim < StandardError; end include Sidekiq::Job include SentryLogging - attr_reader :claim, :user, :in_progress_copy, :user_uuid, :saved_claim_id, :vet_info, :icn + attr_reader :claim, :user, :user_uuid, :saved_claim_id, :vet_info, :icn sidekiq_options retry: 14 @@ -19,23 +19,19 @@ class Invalid674Claim < StandardError; end Rails.logger.error('BGS::SubmitForm674Job failed, retries exhausted...', { user_uuid:, saved_claim_id:, icn:, error: }) - BGS::SubmitForm674Job.send_backup_submission(encrypted_user_struct_hash, vet_info, saved_claim_id) + BGS::SubmitForm674Job.send_backup_submission(encrypted_user_struct_hash, vet_info, saved_claim_id, user_uuid) end def perform(user_uuid, icn, saved_claim_id, encrypted_vet_info, encrypted_user_struct_hash = nil) Rails.logger.info('BGS::SubmitForm674Job running!', { user_uuid:, saved_claim_id:, icn: }) instance_params(encrypted_vet_info, icn, encrypted_user_struct_hash, user_uuid, saved_claim_id) - in_progress_form = InProgressForm.find_by(form_id: FORM_ID, user_uuid:) - @in_progress_copy = in_progress_form_copy(in_progress_form) - submit_form send_confirmation_email - in_progress_form&.destroy Rails.logger.info('BGS::SubmitForm674Job succeeded!', { user_uuid:, saved_claim_id:, icn: }) + InProgressForm.destroy_by(form_id: FORM_ID, user_uuid:) rescue => e - salvage_save_in_progress_form(FORM_ID, user_uuid, @in_progress_copy) if @in_progress_copy.present? handle_filtered_errors!(e:, encrypted_user_struct_hash:, encrypted_vet_info:) Rails.logger.warn('BGS::SubmitForm674Job received error, retrying...', @@ -52,7 +48,7 @@ def handle_filtered_errors!(e:, encrypted_user_struct_hash:, encrypted_vet_info: { user_uuid:, saved_claim_id:, icn:, error: e.message, nested_error: e.cause&.message }) vet_info = JSON.parse(KmsEncrypted::Box.new.decrypt(encrypted_vet_info)) - self.class.send_backup_submission(encrypted_user_struct_hash, vet_info, saved_claim_id) + self.class.send_backup_submission(encrypted_user_struct_hash, vet_info, saved_claim_id, user_uuid) raise Sidekiq::JobRetry::Skip end @@ -62,6 +58,7 @@ def instance_params(encrypted_vet_info, icn, encrypted_user_struct_hash, user_uu @icn = icn @user_uuid = user_uuid @saved_claim_id = saved_claim_id + @claim = SavedClaim::DependencyClaim.find(saved_claim_id) end def self.generate_user_struct(encrypted_user_struct, vet_info) @@ -85,7 +82,7 @@ def self.generate_user_struct(encrypted_user_struct, vet_info) ) end - def self.send_backup_submission(encrypted_user_struct_hash, vet_info, saved_claim_id) + def self.send_backup_submission(encrypted_user_struct_hash, vet_info, saved_claim_id, user_uuid) if Flipper.enabled?(:dependents_central_submission) user = generate_user_struct(encrypted_user_struct_hash, vet_info) CentralMail::SubmitCentralForm686cJob.perform_async(saved_claim_id, @@ -94,13 +91,16 @@ def self.send_backup_submission(encrypted_user_struct_hash, vet_info, saved_clai else DependentsApplicationFailureMailer.build(user).deliver_now if user&.email.present? # rubocop:disable Style/IfInsideElse # Temporary for flipper end + InProgressForm.destroy_by(form_id: FORM_ID, user_uuid:) + rescue => e + Rails.logger.warn('BGS::SubmitForm674Job backup submission failed...', + { user_uuid:, saved_claim_id:, error: e.message, nested_error: e.cause&.message }) + InProgressForm.find_by(form_id: FORM_ID, user_uuid:)&.submission_pending! end private def submit_form - @claim = SavedClaim::DependencyClaim.find(saved_claim_id) - claim.add_veteran_info(vet_info) raise Invalid674Claim unless claim.valid?(:run_686_form_jobs) diff --git a/app/sidekiq/bgs/submit_form686c_job.rb b/app/sidekiq/bgs/submit_form686c_job.rb index 84d58d13e61..697b906a898 100644 --- a/app/sidekiq/bgs/submit_form686c_job.rb +++ b/app/sidekiq/bgs/submit_form686c_job.rb @@ -9,7 +9,7 @@ class Invalid686cClaim < StandardError; end include Sidekiq::Job include SentryLogging - attr_reader :claim, :user, :in_progress_copy, :user_uuid, :saved_claim_id, :vet_info, :icn + attr_reader :claim, :user, :user_uuid, :saved_claim_id, :vet_info, :icn sidekiq_options retry: 14 @@ -19,23 +19,19 @@ class Invalid686cClaim < StandardError; end Rails.logger.error('BGS::SubmitForm686cJob failed, retries exhausted!', { user_uuid:, saved_claim_id:, icn:, error: }) - BGS::SubmitForm686cJob.send_backup_submission(vet_info, saved_claim_id) + BGS::SubmitForm686cJob.send_backup_submission(vet_info, saved_claim_id, user_uuid) end def perform(user_uuid, icn, saved_claim_id, encrypted_vet_info) Rails.logger.info('BGS::SubmitForm686cJob running!', { user_uuid:, saved_claim_id:, icn: }) instance_params(encrypted_vet_info, icn, user_uuid, saved_claim_id) - in_progress_form = InProgressForm.find_by(form_id: FORM_ID, user_uuid:) - @in_progress_copy = in_progress_form_copy(in_progress_form) - submit_forms(encrypted_vet_info) send_confirmation_email - in_progress_form&.destroy Rails.logger.info('BGS::SubmitForm686cJob succeeded!', { user_uuid:, saved_claim_id:, icn: }) + InProgressForm.destroy_by(form_id: FORM_ID, user_uuid:) unless claim.submittable_674? rescue => e - salvage_save_in_progress_form(FORM_ID, user_uuid, @in_progress_copy) if @in_progress_copy.present? handle_filtered_errors!(e:, encrypted_vet_info:) Rails.logger.warn('BGS::SubmitForm686cJob received error, retrying...', @@ -52,7 +48,7 @@ def handle_filtered_errors!(e:, encrypted_vet_info:) { user_uuid:, saved_claim_id:, icn:, error: e.message, nested_error: e.cause&.message }) vet_info = JSON.parse(KmsEncrypted::Box.new.decrypt(encrypted_vet_info)) - self.class.send_backup_submission(vet_info, saved_claim_id) + self.class.send_backup_submission(vet_info, saved_claim_id, user_uuid) raise Sidekiq::JobRetry::Skip end @@ -62,6 +58,7 @@ def instance_params(encrypted_vet_info, icn, user_uuid, saved_claim_id) @icn = icn @user_uuid = user_uuid @saved_claim_id = saved_claim_id + @claim = SavedClaim::DependencyClaim.find(saved_claim_id) end def self.generate_user_struct(vet_info) @@ -81,7 +78,7 @@ def self.generate_user_struct(vet_info) ) end - def self.send_backup_submission(vet_info, saved_claim_id) + def self.send_backup_submission(vet_info, saved_claim_id, user_uuid) if Flipper.enabled?(:dependents_central_submission) user = generate_user_struct(vet_info) CentralMail::SubmitCentralForm686cJob.perform_async(saved_claim_id, @@ -90,13 +87,16 @@ def self.send_backup_submission(vet_info, saved_claim_id) else DependentsApplicationFailureMailer.build(user).deliver_now if user&.email.present? # rubocop:disable Style/IfInsideElse end + InProgressForm.destroy_by(form_id: FORM_ID, user_uuid:) + rescue => e + Rails.logger.warn('BGS::SubmitForm686cJob backup submission failed...', + { user_uuid:, saved_claim_id:, error: e.message, nested_error: e.cause&.message }) + InProgressForm.find_by(form_id: FORM_ID, user_uuid:)&.submission_pending! end private def submit_forms(encrypted_vet_info) - @claim = SavedClaim::DependencyClaim.find(saved_claim_id) - claim.add_veteran_info(vet_info) raise Invalid686cClaim unless claim.valid?(:run_686_form_jobs)