Skip to content

Commit

Permalink
Merge pull request #226 from TreinaDev/feat/priorizando-usuarios-premium
Browse files Browse the repository at this point in the history
Prioriza usuários premium
  • Loading branch information
hreis1 authored Feb 15, 2024
2 parents a0ca345 + a47964c commit d216962
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 23 deletions.
4 changes: 2 additions & 2 deletions app/controllers/api/v1/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 18 additions & 14 deletions app/models/profile.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
31 changes: 31 additions & 0 deletions spec/models/profile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 41 additions & 7 deletions spec/requests/apis/v1/search_user_by_job_categories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit d216962

Please sign in to comment.