fix error in sorting musicians

This commit is contained in:
Nuwan 2021-12-22 18:56:13 +05:30
parent ef823df8eb
commit edab0fe9fc
5 changed files with 32 additions and 10 deletions

View File

@ -182,7 +182,7 @@ module JamRuby
def _joined_within(rel)
#debugger
if 0 < (val = json[KEY_JOINED_WITHIN].to_i)
rel = rel.where("created_at >= ?", val.days.ago.at_beginning_of_day)
rel = rel.where("users.created_at >= ?", val.days.ago.at_beginning_of_day)
end
rel
end

View File

@ -129,7 +129,7 @@ module JamRuby
def _active_within(rel)
if 0 <= (val = json[KEY_ACTIVE_WITHIN].to_i)
rel = rel.where("id IN (SELECT id FROM users GROUP BY id HAVING GREATEST(updated_at, last_jam_updated_at) >= ?)", val.days.ago.at_beginning_of_day)
rel = rel.where("users.id IN (SELECT users.id FROM users GROUP BY id HAVING GREATEST(updated_at, last_jam_updated_at) >= ?)", val.days.ago.at_beginning_of_day)
end
rel
end
@ -150,16 +150,33 @@ module JamRuby
rel
end
def _sort_by_ids_ordinality(rel, ids)
return rel if ids.empty?
values = []
ids.each_with_index do |id, index|
values << "('#{id}', #{index + 1})"
end
rel = rel.joins("JOIN (VALUES #{values.join(",")}) as x (id, ordering) ON users.id = x.id")
rel.order('x.ordering')
end
def do_search(params={}, user_ids)
rel = User.musicians.where('users.id <> ?', self.user.id)
#user_ids parameter is used to track users returned from neo4j user's latency data service. #if this variable is not null means we are calling this method with neo4j latency data (currently this call only comes from api_search_controller#filter)
#debugger
unless user_ids.nil?
if user_ids.empty?
return User.none # no user_ids have been passed from latency service. which means no results for the latency based filter conditions. So we return an empty data set
else
rel = rel.where(id: user_ids).where('users.id <> ?', self.user.id).order("array_position(ARRAY[#{user_ids.map { |i| "'#{i}'" }.join(',')}], id::TEXT)")
rel = rel.where(id: user_ids).where('users.id <> ?', self.user.id)
#following line does not work in postgresql 9.3 - array_position function was intruduced in postgresql 9.4
#NOTE: we can change to this once we upgrade postgresql
#rel = rel.where(id: user_ids).where('users.id <> ?', self.user.id).order("array_position(ARRAY[#{user_ids.map { |i| "'#{i}'" }.join(',')}], id::TEXT)")
rel = self._sort_by_ids_ordinality(rel, user_ids)
end
end
@ -177,6 +194,8 @@ module JamRuby
rel
end
def search_includes(rel)
rel.includes([:instruments, :followings, :friends])
end

View File

@ -164,7 +164,7 @@ module JamRuby
def scope_schools_together(rel, user)
if user.nil?
return rel.where("school_id is null")
return rel.where("users.school_id is null")
end
# platform instructors can search anybody (non-school and school). So no nothing special for them.
@ -173,9 +173,9 @@ module JamRuby
# make sure you can only see same-school. Or in the case of 'null school', you'll get other non-schoolers (i.e. normies)
# also, make sure anyone will find platform_instructors
if user.school_id.nil?
rel = rel.where("school_id is null")
rel = rel.where("users.school_id is null")
else
rel = rel.where("school_id = #{user.school_id} OR is_platform_instructor")
rel = rel.where("users.school_id = #{user.school_id} OR users.is_platform_instructor")
end
end

View File

@ -7,8 +7,10 @@ describe User do
before do
User.delete_all
#@user = User.new(first_name: "Example", last_name: "User", email: "user@example.com",
# password: "foobar", password_confirmation: "foobar", city: "Apex", state: "NC", country: "US", terms_of_service: true, musician: true)
@user = User.new(first_name: "Example", last_name: "User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar", city: "Apex", state: "NC", country: "US", terms_of_service: true, musician: true)
password: "foobar", password_confirmation: "foobar", city: "Apex", state: "NC", country: "US", terms_of_service: true)
@user.musician_instruments << FactoryGirl.build(:musician_instrument, player: @user)
@recurly = RecurlyClient.new
end
@ -372,7 +374,7 @@ describe User do
describe "remember token" do
before { @user.save }
its(:remember_token) { should_not be_blank }
it (:remember_token) { should_not be_blank }
end
describe "user progression only touches once" do
@ -434,7 +436,7 @@ describe User do
it { should be_valid }
its(:last_name) { should == "Call2" }
it (:last_name) { should == "Call2" }
end
end

View File

@ -53,6 +53,7 @@ describe "Musician Filter API", type: :request do
it "filter musicians when no latency option is selected" do
post '/api/filter.json', { latency_good: false, latency_fair: false, latency_high: false }
expect(JSON.parse(response.body)["musicians"].size).to eq(7)
expect(JSON.parse(response.body)["musicians"][0]["latency_data"]).not_to eq(nil)
expect(JSON.parse(response.body)["musicians"][0]["latency_data"]["audio_latency"]).not_to eq(nil)
@ -60,7 +61,7 @@ describe "Musician Filter API", type: :request do
expect(JSON.parse(response.body)["musicians"][0]["latency_data"]["ars_total_latency"]).not_to eq(nil)
end
it "filter musicians for all latency options", focus: true do
it "filter musicians for all latency options" do
post '/api/filter.json', { latency_good: true, latency_fair: true, latency_high: true }
expect(JSON.parse(response.body)["musicians"].size).to eq(6)
expect(JSON.parse(response.body)["musicians"][0]["latency_data"]).not_to eq(nil)