FEATURE: Redeem invites for existent users (#15866)
This adds logic to increase an `InvitedUser` record, increase
`redemption_count` and create a `:invitee_accepted` to let the inviter
know that the invitee used the invite.
Initial support for this was implemented in commit 9969631
.
This commit is contained in:
parent
9e43f0303d
commit
2d8ebe989a
|
@ -22,31 +22,36 @@ class InvitesController < ApplicationController
|
|||
invite = Invite.find_by(invite_key: params[:id])
|
||||
if invite.present? && invite.redeemable?
|
||||
if current_user
|
||||
added_to_group = false
|
||||
InvitedUser.transaction do
|
||||
invited_user = InvitedUser.find_or_initialize_by(user: current_user, invite: invite)
|
||||
if invited_user.new_record?
|
||||
invited_user.save!
|
||||
Invite.increment_counter(:redemption_count, invite.id)
|
||||
invite.invited_by.notifications.create!(
|
||||
notification_type: Notification.types[:invitee_accepted],
|
||||
data: { display_username: current_user.username }.to_json
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
if invite.groups.present?
|
||||
invite_by_guardian = Guardian.new(invite.invited_by)
|
||||
new_group_ids = invite.groups.pluck(:id) - current_user.group_users.pluck(:group_id)
|
||||
new_group_ids.each do |id|
|
||||
if group = Group.find_by(id: id)
|
||||
if invite_by_guardian.can_edit_group?(group)
|
||||
group.add(current_user)
|
||||
added_to_group = true
|
||||
end
|
||||
group.add(current_user) if invite_by_guardian.can_edit_group?(group)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
create_topic_invite_notifications(invite, current_user)
|
||||
|
||||
if topic = invite.topics.first
|
||||
new_guardian = Guardian.new(current_user)
|
||||
return redirect_to(topic.url) if new_guardian.can_see?(topic)
|
||||
elsif added_to_group
|
||||
return redirect_to(path("/"))
|
||||
if current_user.guardian.can_see?(topic)
|
||||
create_topic_invite_notifications(invite, current_user)
|
||||
return redirect_to(topic.url)
|
||||
end
|
||||
end
|
||||
|
||||
return ensure_not_logged_in
|
||||
return redirect_to(path("/"))
|
||||
end
|
||||
|
||||
email = Email.obfuscate(invite.email)
|
||||
|
|
|
@ -105,13 +105,16 @@ describe InvitesController do
|
|||
expect(Notification.where(notification_type: Notification.types[:invited_to_topic], topic: topic).count).to eq(1)
|
||||
end
|
||||
|
||||
it 'fails for logged in users' do
|
||||
sign_in(Fabricate(:user))
|
||||
it 'creates a notification to inviter' do
|
||||
topic = Fabricate(:topic)
|
||||
TopicInvite.create!(topic: topic, invite: invite)
|
||||
|
||||
sign_in(user)
|
||||
|
||||
get "/invites/#{invite.invite_key}"
|
||||
expect(response.status).to eq(200)
|
||||
expect(response.body).to_not have_tag(:script, with: { src: '/assets/application.js' })
|
||||
expect(response.body).to include(I18n.t('login.already_logged_in'))
|
||||
expect(response).to redirect_to(topic.url)
|
||||
expect(Notification.where(user: user, notification_type: Notification.types[:invited_to_topic], topic: topic).count).to eq(1)
|
||||
expect(Notification.where(user: invite.invited_by, notification_type: Notification.types[:invitee_accepted]).count).to eq(1)
|
||||
end
|
||||
|
||||
it 'fails if invite does not exist' do
|
||||
|
|
Loading…
Reference in New Issue