diff --git a/app/controllers/v0/burial_claims_controller.rb b/app/controllers/v0/burial_claims_controller.rb index 620336c4f47..5ead02cc210 100644 --- a/app/controllers/v0/burial_claims_controller.rb +++ b/app/controllers/v0/burial_claims_controller.rb @@ -7,7 +7,19 @@ class BurialClaimsController < ClaimsBaseController service_tag 'burial-application' def show - render_burials_json + submission_attempt = determine_submission_attempt + if submission_attempt + state = submission_attempt.aasm_state == 'failure' ? 'failure' : 'success' + render(json: { data: { attributes: { state: } } }) + elsif central_mail_submission + render(json: central_mail_submission) + else + Rails.logger.error("ActiveRecord::RecordNotFound: Claim submission not found for claim_id: #{params[:id]}") + render(json: { data: { attributes: { state: 'not found' } } }, status: :not_found) + end + rescue => e + Rails.logger.error(e.to_s) + render(json: { data: { attributes: { state: 'error processing request' } } }, status: :unprocessable_entity) end def create @@ -44,19 +56,14 @@ def claim_class private - def render_burials_json - if (submission_attempt = determine_submission_attempt) - state = submission_attempt.aasm_state == 'failure' ? 'failure' : 'success' - render(json: { data: { attributes: { state: } } }) - else - render(json: CentralMailSubmission.joins(:central_mail_claim).find_by(saved_claims: { guid: params[:id] })) - end - end - def determine_submission_attempt - claim = claim_class.find_by!(guid: params[:id]) - form_submission = claim.form_submissions&.last + claim = claim_class.find_by(guid: params[:id]) + form_submission = claim&.form_submissions&.last form_submission&.form_submission_attempts&.last end + + def central_mail_submission + CentralMailSubmission.joins(:central_mail_claim).find_by(saved_claims: { guid: params[:id] }) + end end end diff --git a/spec/controllers/v0/burial_claims_controller_spec.rb b/spec/controllers/v0/burial_claims_controller_spec.rb index 2bfc11fbf18..a8e3393ad5c 100644 --- a/spec/controllers/v0/burial_claims_controller_spec.rb +++ b/spec/controllers/v0/burial_claims_controller_spec.rb @@ -45,5 +45,10 @@ def send_create expect(JSON.parse(response.body)['data']['attributes']['state']).to eq('success') end + + it 'returns an error if the claim is not found' do + get(:show, params: { id: '12345' }) + expect(response).to have_http_status(:not_found) + end end end