diff --git a/app/controllers/internal/users_controller.rb b/app/controllers/internal/users_controller.rb index 716528f2d..2a6408afc 100644 --- a/app/controllers/internal/users_controller.rb +++ b/app/controllers/internal/users_controller.rb @@ -17,7 +17,11 @@ class Internal::UsersController < Internal::ApplicationController end def show - @user = User.find(params[:id]) + if params[:id] == "unmatched_mentee" + @user = MentorRelationship.unmatched_mentees.order("RANDOM()").first + else + @user = User.find(params[:id]) + end @user_mentee_relationships = MentorRelationship.where(mentor_id: @user.id) @user_mentor_relationships = MentorRelationship.where(mentee_id: @user.id) end @@ -29,7 +33,11 @@ class Internal::UsersController < Internal::ApplicationController handle_mentorship add_note @user.update!(user_params) - redirect_to "/internal/users/#{@user.id}" + if params[:quick_match] + redirect_to "/internal/users/unmatched_mentee" + else + redirect_to "/internal/users/#{@user.id}" + end end def handle_mentorship diff --git a/app/models/mentor_relationship.rb b/app/models/mentor_relationship.rb index 946c6fd2f..c7215eadc 100644 --- a/app/models/mentor_relationship.rb +++ b/app/models/mentor_relationship.rb @@ -15,6 +15,33 @@ class MentorRelationship < ApplicationRecord end end + def self.unmatched_mentees + sql = "SELECT mentee_form_updated_at, + users.id, + mentor_relationships.mentor_id + FROM users + LEFT JOIN notes ON users.id = notes.noteable_id + LEFT JOIN mentor_relationships ON users.id = mentor_relationships.mentee_id + WHERE seeking_mentorship IS TRUE + AND mentor_relationships.mentor_id IS null" + record_ids = ActiveRecord::Base.connection.execute(sql).pluck("id") + User.where(id: record_ids) + end + + def self.unmatched_mentors + sql = "SELECT mentor_form_updated_at, + users.id, + mentor_relationships.mentor_id + FROM users + LEFT JOIN notes ON users.id = notes.noteable_id + LEFT JOIN mentor_relationships ON users.id = mentor_relationships.mentor_id + WHERE offering_mentorship IS TRUE + AND mentor_relationships.mentee_id IS null" + record_ids = ActiveRecord::Base.connection.execute(sql).pluck("id") + User.where(id: record_ids) + end + + private def mutual_follow diff --git a/app/views/internal/users/show.html.erb b/app/views/internal/users/show.html.erb index 0eca10565..08186521e 100644 --- a/app/views/internal/users/show.html.erb +++ b/app/views/internal/users/show.html.erb @@ -102,22 +102,26 @@ <% if @user.seeking_mentorship %>

Mentee Description

-

<%= @user.mentee_description %>

+

<%= @user.mentee_description %>

Possible Mentors

<% count = 0 %> - <% User.where(offering_mentorship: true).where.not(mentor_form_updated_at: nil).order("mentor_form_updated_at DESC").each do |possible_mentor| %> + <% MentorRelationship. + unmatched_mentors.where.not(mentor_form_updated_at: nil, id: @user.id). + order("mentor_form_updated_at DESC").each do |possible_mentor| %> <% break if count > 20 %> - <% count = count + 1 %> <% tag_intersection = ( @user.cached_followed_tag_names & possible_mentor.cached_followed_tag_names ) %> - <% mentor_relationships_count = MentorRelationship.where(mentor_id: possible_mentor.id).size %> - <% if tag_intersection.any? && mentor_relationships_count < 1 %> -
+ <% if tag_intersection.any? %> + <% count = count + 1 %> +
-
+

Tag Intersection

<%= tag_intersection.join(", ") %>
@@ -132,6 +136,7 @@

Action

<%= form_for ([:internal, @user]) do |f| %> + <%= f.hidden_field :quick_match, value: true %> <%= f.hidden_field :add_mentor, value: possible_mentor.id %> <%= f.submit "Match!" %> <% end %> diff --git a/spec/models/mentor_relationship_spec.rb b/spec/models/mentor_relationship_spec.rb index 3f8515608..c8baa9626 100644 --- a/spec/models/mentor_relationship_spec.rb +++ b/spec/models/mentor_relationship_spec.rb @@ -31,4 +31,20 @@ RSpec.describe MentorRelationship, type: :model do relationship.save! expect(EmailMessage.all.size).to eq(2) end + + it "finds unmatched mentors" do + expect(MentorRelationship.unmatched_mentors.size).to eq(0) + new_mentor = create(:user, mentor_form_updated_at: Time.current, offering_mentorship: true) + expect(MentorRelationship.unmatched_mentors.size).to eq(1) + MentorRelationship.create(mentor_id: new_mentor.id, mentee_id: mentee.id) + expect(MentorRelationship.unmatched_mentors.size).to eq(0) + end + + it "finds unmatched mentees" do + expect(MentorRelationship.unmatched_mentees.size).to eq(0) + new_mentee = create(:user, mentee_form_updated_at: Time.current, seeking_mentorship: true) + expect(MentorRelationship.unmatched_mentees.size).to eq(1) + MentorRelationship.create(mentor_id: mentor.id, mentee_id: new_mentee.id) + expect(MentorRelationship.unmatched_mentors.size).to eq(0) + end end