0

I was trying to implement round robin assignment of leads to the members-users. But when ever my function is called user cycle starts again, and all the leads are assigned to the first user. How can I assign next leads to the user that was next to the previously assigned user. eg; if lead1 is assigned to U2 and claimed, then next lead2 assignment should continue from U3.

Code that I used: Rough:

def round_robin(selected_lead)
  lead = selected_lead
  users = %i(U1 U2 U3).cycle
  while lead.status !=“Claimed” do
    assigned_user=users.next
    # assigned_user <= Assign lead to
    # Check to wait 10 min for claimed response...
  end
  puts “Lead #{lead} has been assigned to #{assigned_user} 
end

I was doing this in ruby language. any suggestions would be appreciated.

user4157124
  • 2,809
  • 13
  • 27
  • 42
Jawad
  • 1
  • 3
  • Your `users` array doesn't preserve its state between the calls to `round_robin` (it is local for the method, so it gets discarded after the method call). As the result, on each call you assign to the result of `%i(U1 U2 U3).cycle.next`, which is obviously always the 1st element... You need to keep the iterator state between the calls to `round_robin` (so it must **not** be a local variable) – Konstantin Strukov Aug 23 '22 at 14:46
  • Maybe also consider just grabbing a random one (`%i(U1 U2 U3).sample`), it won't cycle through them in order, but will result in a random sample across your array element without having to maintain any state. – smathy Aug 23 '22 at 16:52
  • if U1 has a lead assigned and claimed. U2 has a lead assigned and not claimed. Who is the next lead assigned to? – Les Nightingill Aug 23 '22 at 19:00
  • @LesNightingill if U2 has lead assigned and not claimed then lead should be assigned to the next user U3. – Jawad Aug 24 '22 at 11:54

1 Answers1

0

You need to persist the concept of the next-user-to-be assigned a lead. I suggest doing this in a boolean column in the users table next. Only one user will ever have this attribute set to true, all others have it set to nil or false.

So in the User model, User.next is the next user that will receive a lead assignment

# in user.rb
  has_many :leads

  def self.next
    find_by(next: true) || first
  end

and User.next_next is the User after User.next where the order is simply the value of the id. (Can't just add 1 to the id, b/c there may be missing ids, due to deleted users)

# in user.rb

  def self.next_next
    # get the list of all User ids, and find the index of the one that has next=true
    next_index = ids.index(next.id)

    # find the index in the list of ids of the User after next, modulo User.count. 
    # So when you get to the last user, "round-robin" back to the first
    next_next_index = next_index.succ % count

    # return the User after next
    find(ids[next_next_index])
  end

When a lead is assigned, it is assigned to User.next, the value of that user's next attribute is set to false, and the value of the user after User.next has the value of the next attribute set to true

# in lead.rb
  belongs_to :user

  def assign_to_next_user
    User.assign_lead(self)
  end

# in user.rb

  def self.assign_lead(lead)
    next.leads << self
    next.update(next: false)
    next_next.update(next: true)
  end
Les Nightingill
  • 5,662
  • 1
  • 29
  • 32