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:
Dan Ungureanu 2022-02-15 17:35:58 +02:00 committed by GitHub
parent d83da596be
commit dd5373cc4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 9 deletions

View File

@ -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

View File

@ -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)