2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-12-19 16:31:57 +01:00
|
|
|
class RecoveriesController < ApplicationController
|
2023-12-12 15:25:22 +01:00
|
|
|
before_action :ensure_agent_connect_is_used, except: [:nature, :post_nature, :support]
|
2023-12-19 16:25:41 +01:00
|
|
|
before_action :ensure_collectivite_territoriale, except: [:nature, :post_nature, :support]
|
2023-12-12 15:25:22 +01:00
|
|
|
|
2023-12-19 16:31:57 +01:00
|
|
|
def nature
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_nature
|
|
|
|
if nature_params == 'collectivite'
|
|
|
|
redirect_to identification_recovery_path
|
|
|
|
else
|
2023-12-19 13:00:36 +01:00
|
|
|
redirect_to support_recovery_path(error: :other_nature)
|
2023-12-19 16:31:57 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def identification
|
2023-12-13 09:44:28 +01:00
|
|
|
@structure_name = structure_name
|
2023-12-19 16:31:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def post_identification
|
2024-03-15 14:20:31 +01:00
|
|
|
# cipher previous_user email
|
|
|
|
# to avoid leaks in the url
|
|
|
|
ciphered_email = cipher(previous_email)
|
2023-12-13 09:44:28 +01:00
|
|
|
|
2024-03-15 14:20:31 +01:00
|
|
|
redirect_to selection_recovery_path(ciphered_email:)
|
2023-12-19 16:31:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def selection
|
2024-03-15 14:20:31 +01:00
|
|
|
@previous_email = uncipher(params[:ciphered_email])
|
|
|
|
|
|
|
|
previous_user = User.find_by(email: @previous_email)
|
2023-12-19 11:18:51 +01:00
|
|
|
|
|
|
|
@recoverables = RecoveryService
|
|
|
|
.recoverable_procedures(previous_user:, siret:)
|
|
|
|
|
|
|
|
redirect_to support_recovery_path(error: :no_dossier) if @recoverables.empty?
|
2023-12-19 16:31:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def post_selection
|
2024-03-15 14:20:31 +01:00
|
|
|
previous_user = User.find_by(email: previous_email)
|
2023-12-19 11:18:51 +01:00
|
|
|
|
|
|
|
RecoveryService.recover_procedure!(previous_user:,
|
|
|
|
next_user: current_user,
|
|
|
|
siret:,
|
|
|
|
procedure_ids:)
|
|
|
|
|
2023-12-19 16:31:57 +01:00
|
|
|
redirect_to terminee_recovery_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def terminee
|
|
|
|
end
|
|
|
|
|
|
|
|
def support
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def nature_params = params[:nature]
|
2024-03-19 15:24:53 +01:00
|
|
|
def siret = current_instructeur.last_agent_connect_information.siret
|
2023-12-19 16:25:41 +01:00
|
|
|
def previous_email = params[:previous_email]
|
2023-12-19 11:18:51 +01:00
|
|
|
def procedure_ids = params[:procedure_ids].map(&:to_i)
|
2023-12-12 15:25:22 +01:00
|
|
|
|
2024-03-15 14:20:31 +01:00
|
|
|
def cipher(email) = message_verifier.generate(email, purpose: :agent_files_recovery, expires_in: 1.hour)
|
|
|
|
def uncipher(email) = message_verifier.verified(email, purpose: :agent_files_recovery) rescue nil
|
|
|
|
|
2023-12-13 09:44:28 +01:00
|
|
|
def structure_name
|
|
|
|
# we know that the structure exists because
|
|
|
|
# of the ensure_collectivite_territoriale guard
|
2024-10-15 18:35:18 +02:00
|
|
|
APIRechercheEntreprisesService.new.call(siret:).value![:nom_complet]
|
2023-12-13 09:44:28 +01:00
|
|
|
end
|
|
|
|
|
2023-12-12 15:25:22 +01:00
|
|
|
def ensure_agent_connect_is_used
|
2024-03-19 15:24:53 +01:00
|
|
|
if current_instructeur&.last_agent_connect_information.nil?
|
2023-12-12 15:25:22 +01:00
|
|
|
redirect_to support_recovery_path(error: :must_use_agent_connect)
|
|
|
|
end
|
|
|
|
end
|
2023-12-19 16:25:41 +01:00
|
|
|
|
|
|
|
def ensure_collectivite_territoriale
|
|
|
|
if !APIRechercheEntreprisesService.collectivite_territoriale?(siret:)
|
|
|
|
redirect_to support_recovery_path(error: 'not_collectivite_territoriale')
|
|
|
|
end
|
|
|
|
end
|
2023-12-19 16:31:57 +01:00
|
|
|
end
|