Commit a19e1c862e46b876964b134562558d3f0516f858

Authored by Larissa Reis
1 parent c8236d24

avoids duplicated invite pending tasks

  During Invitation task, before creating the InviteMember or
  InviteFriend task, search for existing identical pending task to avoid
  duplicates.

(ActionItem3099)
app/models/invitation.rb
@@ -77,9 +77,9 @@ class Invitation < Task @@ -77,9 +77,9 @@ class Invitation < Task
77 77
78 if !task_args.nil? 78 if !task_args.nil?
79 if profile.person? 79 if profile.person?
80 - InviteFriend.create(task_args) 80 + InviteFriend.create(task_args) if !user || user.person.tasks.pending.of("InviteFriend").find(:all, :conditions => {:requestor_id => person.id, :target_id => user.person.id}).blank?
81 elsif profile.community? 81 elsif profile.community?
82 - InviteMember.create(task_args.merge(:community_id => profile.id)) 82 + InviteMember.create(task_args.merge(:community_id => profile.id)) if !user || user.person.tasks.pending.of("InviteMember").find(:all, :conditions => {:requestor_id => person.id}).select { |t| t.data[:community_id] == profile.id }.blank?
83 else 83 else
84 raise NotImplementedError, 'Don\'t know how to invite people to a %s' % profile.class.to_s 84 raise NotImplementedError, 'Don\'t know how to invite people to a %s' % profile.class.to_s
85 end 85 end
test/unit/invitation_test.rb
@@ -133,4 +133,24 @@ class InvitationTest < ActiveSupport::TestCase @@ -133,4 +133,24 @@ class InvitationTest < ActiveSupport::TestCase
133 Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', person) 133 Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', person)
134 end 134 end
135 end 135 end
  136 +
  137 + should 'not invite friends if there is a pending invitation' do
  138 + person = create_user('testuser1').person
  139 + friend = create_user('testuser2').person
  140 + community = fast_create(Community)
  141 +
  142 + assert_difference InviteMember, :count do
  143 + Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', community)
  144 + end
  145 + assert_difference InviteFriend, :count do
  146 + Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', person)
  147 + end
  148 +
  149 + assert_no_difference InviteMember, :count do
  150 + Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', community)
  151 + end
  152 + assert_no_difference InviteFriend, :count do
  153 + Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', person)
  154 + end
  155 + end
136 end 156 end