2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-09-07 10:37:42 +02:00
|
|
|
module Users
|
|
|
|
class TransfersController < UserController
|
|
|
|
def create
|
|
|
|
transfer = DossierTransfer.new(transfer_params)
|
2022-08-02 15:20:33 +02:00
|
|
|
|
|
|
|
if transfer.valid?
|
|
|
|
transfer.save!
|
2022-11-24 19:17:04 +01:00
|
|
|
flash.notice = t("users.dossiers.transferer.notice_sent")
|
2022-08-02 15:20:33 +02:00
|
|
|
redirect_to dossiers_path
|
|
|
|
else
|
|
|
|
flash.alert = transfer.errors.full_messages
|
|
|
|
redirect_to transferer_dossier_path(transfer_params[:dossiers].first)
|
|
|
|
end
|
2021-09-07 10:37:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
DossierTransfer.accept(params[:id], current_user)
|
|
|
|
redirect_to dossiers_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2023-03-01 17:19:06 +01:00
|
|
|
transfer = DossierTransfer.find(params[:id])
|
2023-03-09 11:39:45 +01:00
|
|
|
authorized = (transfer.email == current_user.email || transfer.dossiers.exists?(dossiers: { user: current_user }))
|
2023-03-01 17:19:06 +01:00
|
|
|
|
2023-03-09 11:39:45 +01:00
|
|
|
if authorized
|
|
|
|
transfer.destroy_and_nullify
|
|
|
|
flash.notice = t("users.dossiers.transferer.destroy")
|
|
|
|
else
|
|
|
|
flash.alert = t("users.dossiers.transferer.unauthorized_destroy")
|
|
|
|
end
|
2021-09-07 10:37:42 +02:00
|
|
|
redirect_to dossiers_path
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def transfer_params
|
2022-08-02 15:20:33 +02:00
|
|
|
transfer_params = params.require(:dossier_transfer).permit(:email, :dossier)
|
|
|
|
|
|
|
|
dossier_id = transfer_params.delete(:dossier)
|
|
|
|
dossiers = if dossier_id.present?
|
|
|
|
[current_user.dossiers.find(dossier_id)]
|
2021-09-07 10:37:42 +02:00
|
|
|
else
|
2022-08-02 15:20:33 +02:00
|
|
|
current_user.dossiers
|
2021-09-07 10:37:42 +02:00
|
|
|
end
|
2022-08-02 15:20:33 +02:00
|
|
|
|
|
|
|
transfer_params.merge(dossiers: dossiers)
|
2021-09-07 10:37:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|