diff --git a/app/controllers/api/v1/profiles_controller.rb b/app/controllers/api/v1/profiles_controller.rb index 0753494..258a979 100644 --- a/app/controllers/api/v1/profiles_controller.rb +++ b/app/controllers/api/v1/profiles_controller.rb @@ -3,10 +3,10 @@ module V1 class ProfilesController < ApiController def index if params[:search].blank? - profiles = Profile.active.open_to_work + profiles = Profile.active.open_to_work.order_by_premium profiles = profiles.map { |profile| format_profile(profile) } else - profiles = Profile.active.open_to_work.get_profile_job_categories_json(params[:search]) + profiles = Profile.active.open_to_work.order_by_premium.get_profile_job_categories_json(params[:search]) end render status: :ok, json: { data: profiles } end diff --git a/app/models/profile.rb b/app/models/profile.rb index 663de5e..510652a 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -1,30 +1,30 @@ class Profile < ApplicationRecord belongs_to :user + + has_one_attached :photo has_one :personal_info, dependent: :destroy + has_many :professional_infos, dependent: :destroy has_many :education_infos, dependent: :destroy has_many :profile_job_categories, dependent: :destroy - - has_many :followers, class_name: 'Connection', foreign_key: :followed_profile_id, dependent: :destroy, - inverse_of: :follower - - has_many :followed_profiles, class_name: 'Connection', foreign_key: :follower_id, - dependent: :destroy, inverse_of: :followed_profile - - has_many :connections, foreign_key: :followed_profile_id, dependent: :destroy, inverse_of: :followed_profile - - has_many :job_categories, through: :profile_job_categories + has_many :invitations, dependent: :destroy + has_many :notifications, dependent: :destroy has_many :invitation_requests, dependent: :destroy - has_one_attached :photo - has_many :invitations, dependent: :destroy has_many :posts, through: :user - has_many :notifications, dependent: :destroy + has_many :job_categories, through: :profile_job_categories has_many :reports_submitted, class_name: 'Report', dependent: :destroy - has_many :reports_received, class_name: 'Report', as: :reportable, dependent: :destroy + has_many :connections, foreign_key: :followed_profile_id, dependent: :destroy, inverse_of: :followed_profile + + has_many :followers, class_name: 'Connection', foreign_key: :followed_profile_id, dependent: :destroy, + inverse_of: :follower + + has_many :followed_profiles, class_name: 'Connection', foreign_key: :follower_id, dependent: :destroy, + inverse_of: :followed_profile + accepts_nested_attributes_for :personal_info accepts_nested_attributes_for :professional_infos accepts_nested_attributes_for :education_infos @@ -40,6 +40,10 @@ class Profile < ApplicationRecord delegate :full_name, :email, to: :user + def self.order_by_premium + joins(user: :subscription).order('subscriptions.status DESC, users.full_name ASC') + end + def self.advanced_search(search_query) left_outer_joins(:job_categories, :personal_info, :user).where( 'job_categories.name LIKE :term OR diff --git a/spec/models/profile_spec.rb b/spec/models/profile_spec.rb index 217d17f..5c2c04c 100644 --- a/spec/models/profile_spec.rb +++ b/spec/models/profile_spec.rb @@ -248,4 +248,35 @@ expect(Connection.active.count).to eq 2 end end + + describe '#order_by_premium' do + it 'retorna perfis premium primeiro e depois os perfis free' do + create(:user, :free, full_name: 'André Porteira') + create(:user, :free, full_name: 'Eliseu Ramos') + create(:user, full_name: 'Moisés Campus') + user_premium_inactive = create(:user, full_name: 'Joao Almeida') + user_premium_inactive.subscription.inactive! + + result = Profile.order_by_premium + + expect(result.first.full_name).to eq 'Moisés Campus' + expect(result.second.full_name).to eq 'André Porteira' + expect(result.third.full_name).to eq 'Eliseu Ramos' + expect(result.fourth.full_name).to eq 'Joao Almeida' + end + + it 'ordena por nome em caso de mesmo status de assinatura' do + create(:user, :free, full_name: 'André Almeida') + create(:user, :free, full_name: 'André Barbosa') + create(:user, full_name: 'André Campus') + create(:user, full_name: 'André Dias') + + result = Profile.order_by_premium + + expect(result.first.full_name).to eq 'André Campus' + expect(result.second.full_name).to eq 'André Dias' + expect(result.third.full_name).to eq 'André Almeida' + expect(result.fourth.full_name).to eq 'André Barbosa' + end + end end diff --git a/spec/requests/apis/v1/search_user_by_job_categories_spec.rb b/spec/requests/apis/v1/search_user_by_job_categories_spec.rb index b8a5ccb..4c821f1 100644 --- a/spec/requests/apis/v1/search_user_by_job_categories_spec.rb +++ b/spec/requests/apis/v1/search_user_by_job_categories_spec.rb @@ -67,13 +67,13 @@ expect(json_response['data'].class).to eq Array expect(json_response['data'].first.keys).not_to include 'created_at' expect(json_response['data'].first.keys).not_to include 'updated_at' - expect(json_response['data'].first['profile_id']).to eq 1 - expect(json_response['data'].first['full_name']).to eq 'Joao Almeida' - expect(json_response['data'].first['job_categories']).to eq [{ 'name' => 'Ruby on Rails', - 'description' => 'Especialista em Ruby.' }] - expect(json_response['data'].second['profile_id']).to eq 2 - expect(json_response['data'].second['full_name']).to eq 'André Porteira' - expect(json_response['data'].second['job_categories']).to eq [] + expect(json_response['data'].first['profile_id']).to eq 2 + expect(json_response['data'].first['full_name']).to eq 'André Porteira' + expect(json_response['data'].first['job_categories']).to eq [] + expect(json_response['data'].second['profile_id']).to eq 1 + expect(json_response['data'].second['full_name']).to eq 'Joao Almeida' + expect(json_response['data'].second['job_categories']).to eq [{ 'name' => 'Ruby on Rails', + 'description' => 'Especialista em Ruby.' }] end it 'retorna um erro interno do servidor' do @@ -86,5 +86,39 @@ expect(json_response.class).to eq Hash expect(json_response['error']).to eq 'Houve um erro interno no servidor ao processar sua solicitação.' end + + it 'retorna perfis premium primeiro e depois os perfis comuns' do + create(:user, :free, full_name: 'Eliseu Ramos') + create(:user, :free, full_name: 'André Porteira') + create(:user, full_name: 'Moisés Campus') + create(:user, full_name: 'Joao Almeida') + + get '/api/v1/profiles' + + expect(response.status).to eq 200 + json_response = JSON.parse(response.body) + expect(json_response['data'].count).to eq 4 + expect(json_response['data'].first['full_name']).to eq 'Joao Almeida' + expect(json_response['data'].second['full_name']).to eq 'Moisés Campus' + expect(json_response['data'].third['full_name']).to eq 'André Porteira' + expect(json_response['data'].fourth['full_name']).to eq 'Eliseu Ramos' + end + + it 'retorna perfis premium primeiro e depois os perfis free na busca com parâmetro' do + ruby = create(:job_category, name: 'Ruby on Rails') + + user_premium = create(:user, full_name: 'Moisés Campus') + user_premium.profile.profile_job_categories.create(job_category: ruby, description: 'Sou um especialista em Ruby') + user_free = create(:user, :free, full_name: 'André Almeida') + user_free.profile.profile_job_categories.create(job_category: ruby, description: 'Fiz um e-commerce em Ruby') + + get '/api/v1/profiles', params: { search: 'ruby' } + + expect(response.status).to eq 200 + json_response = JSON.parse(response.body) + expect(json_response['data'].count).to eq 2 + expect(json_response['data'].first['full_name']).to eq 'Moisés Campus' + expect(json_response['data'].second['full_name']).to eq 'André Almeida' + end end end