Class: MetadataPresenter::SaveAndReturnController

Inherits:
EngineController show all
Defined in:
app/controllers/metadata_presenter/save_and_return_controller.rb

Instance Method Summary collapse

Methods inherited from EngineController

#allow_analytics?, #analytics_cookie_name, #analytics_tags_present?, #answered?, #back_link, #external_or_relative_link, #get_saved_progress, #increment_record_counter, #invalidate_record, #load_autocomplete_items, #maintenance_mode?, #reload_user_data, #save_form_progress, #show_cookie_confirmation?, #show_cookie_request?

Instance Method Details

#answered_pagesObject



177
178
179
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 177

def answered_pages
  TraversedPages.new(service, @user_data, @page).all
end

#confirm_emailObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 61

def confirm_email
  @email_confirmation = EmailConfirmation.new(confirmation_params[:email_confirmation])

  if @email_confirmation.valid?
    session['saved_form']['email'] = @email_confirmation.email_confirmation
    response = save_form_progress
    if response.status == 500
      internal_server_error and return
    end

    payload = response.body.merge(email: @email_confirmation.email_confirmation).deep_symbolize_keys
    create_save_and_return_submission(payload)

    redirect_to '/save/progress_saved'
  else
    render :email_confirmation, status: :unprocessable_entity
  end
end

#confirmation_paramsObject



214
215
216
217
218
219
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 214

def confirmation_params
  params.permit(
    :email_confirmation,
    :authenticity_token
  )
end

#confirmed_emailObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 25

def confirmed_email
  email = session['saved_form']['email']
  session['saved_form']['email'] = nil
  session['saved_form']['user_id'] = nil
  session['saved_form']['user_token'] = nil
  session['saved_form']['secret_answer'] = nil
  session['saved_form']['secret_question'] = nil
  session['saved_form']['secret_question_text'] = nil
  session['saved_form']['service_slug'] = nil
  session['saved_form']['service_version'] = nil
  email
end

#createObject



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

def create
  @saved_form = SavedForm.new
  @saved_form.populate_param_values(saved_form_params)
  @saved_form.secret_question_text = text_for(params['saved_form']['secret_question'])
  @saved_form.secret_question = params['saved_form']['secret_question']
  @saved_form.populate_service_values(service)
  @saved_form.populate_session_values(session)
  @saved_form.service_slug = service_slug
  if @saved_form.valid?
    # put in session until we have confirmed email address
    @saved_form.secret_question = @saved_form.secret_question_text
    session[:saved_form] = @saved_form
    redirect_to '/save/email_confirmation'
  else
    render :show, params: { page_slug: params[:page_slug], status: :unprocessable_entity }
  end
end

#email_confirmationObject



56
57
58
59
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 56

def email_confirmation
  @saved_form = session[:saved_form]
  @email_confirmation = EmailConfirmation.new(@saved_form['email'])
end

#get_service_nameObject



228
229
230
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 228

def get_service_name
  service.service_name
end

#get_uuidObject



99
100
101
102
103
104
105
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 99

def get_uuid
  if params[:uuid].present?
    return params[:uuid]
  end

  params[:resume_form][:uuid].presence
end

#label_text(text) ⇒ Object



232
233
234
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 232

def label_text(text)
  "<h2 class='govuk-heading-m'>#{text}</h2>"
end

#page_slugObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 9

def page_slug
  if params && params[:page_slug].present?
    return params[:page_slug]
  end
  if session['returning_slug'].present?
    return session['returning_slug']
  end
  if session['saved_form'].present?
    return session['saved_form']['page_slug']
  end

  if params['saved_form'].present?
    params['saved_form']['page_slug']
  end
end

#pages_presentersObject



181
182
183
184
185
186
187
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 181

def pages_presenters
  PageAnswersPresenter.map(
    view: view_context,
    pages: answered_pages,
    answers: @user_data
  )
end

#resume_form_paramsObject



221
222
223
224
225
226
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 221

def resume_form_params
  params.permit(
    { resume_form: %i[secret_answer uuid] },
    :authenticity_token
  )
end

#resume_progressObject

rubocop:enable Style/RescueStandardError



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 156

def resume_progress
  @user_data = load_user_data

  @page ||= service.checkanswers_page

  if @page
    @page_answers = PageAnswers.new(@page, @answered_pages)

    render template: 'metadata_presenter/save_and_return/resume_progress'
  else
    not_found
  end
end

#returnObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 80

def return
  response = get_saved_progress(get_uuid)

  if response.status == 404
    redirect_to '/record_error' and return
  end

  if response.status == 400
    redirect_to '/record_failure' and return
  end

  if response.status == 422
    redirect_to '/already_used' and return
  end

  @saved_form = SavedForm.new.from_json(response.body.to_json)
  @resume_form = ResumeForm.new(@saved_form.secret_question)
end

#save_progressObject



170
171
172
173
174
175
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 170

def save_progress
  session['saved_form']['user_id'] = nil
  session['saved_form']['user_token'] = nil
  session['user_id'] = nil
  session['user_token'] = nil
end

#saved_form_paramsObject



204
205
206
207
208
209
210
211
212
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 204

def saved_form_params
  params.permit(
    :email,
    :secret_answer,
    { saved_form: %i[page_slug secret_question] },
    :authenticity_token,
    :page_slug
  )
end

#secret_questionsObject



190
191
192
193
194
195
196
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 190

def secret_questions
  [
    OpenStruct.new(id: 1, name: I18n.t('presenter.save_and_return.secret_questions.one')),
    OpenStruct.new(id: 2, name: I18n.t('presenter.save_and_return.secret_questions.two')),
    OpenStruct.new(id: 3, name: I18n.t('presenter.save_and_return.secret_questions.three'))
  ]
end

#showObject



5
6
7
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 5

def show
  @saved_form = SavedForm.new
end

#submit_secret_answerObject

rubocop:disable Style/RescueStandardError



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 108

def submit_secret_answer
  response = get_saved_progress(get_uuid)

  if response.status != 200
    if response.status == 400
      redirect_to '/record_failure' and return
    end

    redirect_to '/record_error' and return
  end

  @saved_form = SavedForm.new.from_json(response.body.to_json)
  @resume_form = ResumeForm.new(@saved_form.secret_question)
  @resume_form.secret_answer = resume_form_params[:resume_form][:secret_answer]
  @resume_form.recorded_answer = @saved_form.secret_answer
  @resume_form.attempts_remaining = 2 - @saved_form.attempts.to_i

  if @resume_form.valid?
    # redirect back to right place in form
    session[:user_id] = @saved_form.user_id
    session[:user_token] = @saved_form.user_token
    session[:returning_slug] = @saved_form.page_slug

    invalidate_record(@saved_form.id)

    if @saved_form.service_version == service.version_id
      redirect_to '/resume_progress' and return
    else
      redirect_to '/resume_from_start' and return
    end
  else
    if @resume_form.attempts_remaining <= 0
      begin
        increment_record_counter(@saved_form.id)
      rescue => e
        Rails.logger.info(e)
        redirect_to '/record_failure' and return
      end
      redirect_to '/record_failure' and return
    end

    increment_record_counter(@saved_form.id)

    render :return, params: { uuid: @saved_form.id }
  end
end

#text_for(question) ⇒ Object



198
199
200
201
202
# File 'app/controllers/metadata_presenter/save_and_return_controller.rb', line 198

def text_for(question)
  return nil if question.blank?

  secret_questions.select { |s| s.id.to_s == question.to_s }.first.name
end