FIX: 'resend all invite' button was not working as expected

FIX: ‘resend all invite’ button was not working as expected

diff --git a/app/models/invite.rb b/app/models/invite.rb
index 80858c5..a7340ae 100644
--- a/app/models/invite.rb
+++ b/app/models/invite.rb
@@ -319,8 +319,9 @@ class Invite < ActiveRecord::Base
 
   def self.resend_all_invites_from(user_id)
     Invite.single_use_invites
-      .joins(:invited_users)
+      .left_outer_joins(:invited_users)
       .where('invited_users.user_id IS NULL AND invites.email IS NOT NULL AND invited_by_id = ?', user_id)
+      .group('invites.id')
       .find_each do |invite|
       invite.resend_invite
     end
diff --git a/spec/models/invite_spec.rb b/spec/models/invite_spec.rb
index 047b58c..9bc3e33 100644
--- a/spec/models/invite_spec.rb
+++ b/spec/models/invite_spec.rb
@@ -587,6 +587,28 @@ describe Invite do
     end
   end
 
+  describe '.resend_all_invites_from' do
+    it 'resends all non-redeemed invites by a user' do
+      SiteSetting.invite_expiry_days = 30
+      user = Fabricate(:user)
+      new_invite = Fabricate(:invite, invited_by: user)
+      expired_invite = Fabricate(:invite, invited_by: user)
+      expired_invite.update!(expires_at: 2.days.ago)
+      redeemed_invite = Fabricate(:invite, invited_by: user)
+      Fabricate(:invited_user, invite: redeemed_invite, user: Fabricate(:user))
+      redeemed_invite.update!(expires_at: 5.days.ago)
+
+      Invite.resend_all_invites_from(user.id)
+      new_invite.reload
+      expired_invite.reload
+      redeemed_invite.reload
+
+      expect(new_invite.expires_at.to_date).to eq(30.days.from_now.to_date)
+      expect(expired_invite.expires_at.to_date).to eq(30.days.from_now.to_date)
+      expect(redeemed_invite.expires_at.to_date).to eq(5.days.ago.to_date)
+    end
+  end
+
   describe '.rescind_all_expired_invites_from' do
     it 'removes all expired invites sent by a user' do
       SiteSetting.invite_expiry_days = 1

GitHub sha: 78beb436

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/resend-all-invites-broken/157078/4

This commit has been mentioned on Discourse Meta. There might be relevant details there: