FIX: Do not increase invite count for current user (#15952)
The current user could redeem an invite created by themselves.
This commit is contained in:
parent
d83da596be
commit
dd5373cc4c
|
@ -22,15 +22,17 @@ class InvitesController < ApplicationController
|
|||
invite = Invite.find_by(invite_key: params[:id])
|
||||
if invite.present? && invite.redeemable?
|
||||
if current_user
|
||||
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
|
||||
)
|
||||
if current_user != invite.invited_by
|
||||
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
|
||||
end
|
||||
|
||||
|
|
|
@ -117,6 +117,16 @@ describe InvitesController do
|
|||
expect(Notification.where(user: invite.invited_by, notification_type: Notification.types[:invitee_accepted]).count).to eq(1)
|
||||
end
|
||||
|
||||
it 'creates an invited user record' do
|
||||
sign_in(invite.invited_by)
|
||||
expect { get "/invites/#{invite.invite_key}" }.to change { InvitedUser.count }.by(0)
|
||||
expect(response.status).to eq(302)
|
||||
|
||||
sign_in(user)
|
||||
expect { get "/invites/#{invite.invite_key}" }.to change { InvitedUser.count }.by(1)
|
||||
expect(response.status).to eq(302)
|
||||
end
|
||||
|
||||
it 'fails if invite does not exist' do
|
||||
get '/invites/missing'
|
||||
expect(response.status).to eq(200)
|
||||
|
|
Loading…
Reference in New Issue