2022-05-23 15:09:22 +02:00
|
|
|
class TargetedUserLink < ApplicationRecord
|
2022-06-13 16:00:41 +02:00
|
|
|
belongs_to :user, optional: true
|
2022-05-23 15:09:22 +02:00
|
|
|
belongs_to :target_model, polymorphic: true, optional: false
|
|
|
|
|
2022-06-13 16:00:41 +02:00
|
|
|
enum target_context: { avis: 'avis', invite: 'invite' }
|
2022-05-23 15:09:22 +02:00
|
|
|
|
|
|
|
def invalid_signed_in_user?(signed_in_user)
|
2022-07-11 13:57:28 +02:00
|
|
|
signed_in_user && signed_in_user.email != target_email
|
2022-05-23 15:09:22 +02:00
|
|
|
end
|
|
|
|
|
2022-06-13 16:00:41 +02:00
|
|
|
def target_email
|
|
|
|
case target_context
|
|
|
|
when 'avis'
|
|
|
|
user.email
|
|
|
|
when 'invite'
|
2022-10-24 14:31:34 +02:00
|
|
|
invite = find_invite!
|
|
|
|
invite.user&.email || invite.email
|
2022-06-13 16:00:41 +02:00
|
|
|
else
|
|
|
|
raise 'invalid target_context'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-23 15:09:22 +02:00
|
|
|
def redirect_url(url_helper)
|
|
|
|
case target_context
|
2022-06-13 16:00:41 +02:00
|
|
|
when "invite"
|
2022-10-24 14:31:34 +02:00
|
|
|
invite = find_invite!
|
2022-10-17 19:09:51 +02:00
|
|
|
|
2022-07-11 13:57:28 +02:00
|
|
|
user = User.find_by(email: target_email)
|
|
|
|
user&.active? ?
|
2022-10-24 14:31:34 +02:00
|
|
|
url_helper.invite_path(invite) :
|
|
|
|
url_helper.invite_path(invite, params: { email: invite.email })
|
2022-05-23 15:09:22 +02:00
|
|
|
when "avis"
|
|
|
|
avis = target_model
|
|
|
|
avis.expert.user.active? ?
|
|
|
|
url_helper.expert_avis_path(avis.procedure, avis) :
|
|
|
|
url_helper.sign_up_expert_avis_path(avis.procedure, avis, email: avis.expert.email)
|
|
|
|
end
|
|
|
|
end
|
2022-10-24 14:31:34 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_invite!
|
|
|
|
target_model || (fail ActiveRecord::RecordNotFound.new("Could not find Invite with id `#{target_model_id}`"))
|
|
|
|
end
|
2022-05-23 15:09:22 +02:00
|
|
|
end
|