From c11dd13c387e3f3a0dc1d34d9759905f904ae285 Mon Sep 17 00:00:00 2001 From: Paulo Henrique Date: Wed, 14 Feb 2024 21:23:22 -0300 Subject: [PATCH 1/6] =?UTF-8?q?feat/adiciona=20m=C3=A9todo=20que=20ordena?= =?UTF-8?q?=20perfis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -ordena perfis por tipo de assinatura e depois por ordem alfabética --- app/controllers/api/v1/profiles_controller.rb | 4 +- app/models/profile.rb | 32 +++++++------ spec/factories/subscriptions.rb | 2 +- spec/models/profile_spec.rb | 31 ++++++++++++ .../v1/search_user_by_job_categories_spec.rb | 48 ++++++++++++++++--- spec/system/subscription/paid_user_spec.rb | 0 6 files changed, 93 insertions(+), 24 deletions(-) delete mode 100644 spec/system/subscription/paid_user_spec.rb 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/factories/subscriptions.rb b/spec/factories/subscriptions.rb index 61e6e6b..c93afed 100644 --- a/spec/factories/subscriptions.rb +++ b/spec/factories/subscriptions.rb @@ -1,7 +1,7 @@ FactoryBot.define do factory :subscription do user { nil } - start_date { "2024-02-14" } + start_date { '2024-02-14' } status { 1 } end end 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 diff --git a/spec/system/subscription/paid_user_spec.rb b/spec/system/subscription/paid_user_spec.rb deleted file mode 100644 index e69de29..0000000 From f6aaebb9f3686f9a8966aac266627f937f5073ad Mon Sep 17 00:00:00 2001 From: Paulo Henrique Date: Wed, 14 Feb 2024 21:46:31 -0300 Subject: [PATCH 2/6] =?UTF-8?q?remove=20linhas=20n=C3=A3o=20usadas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/projects_controller.rb | 10 +-- app/javascript/components/projects_vue.js | 1 - app/views/invitation_requests/index.html.erb | 84 +++++++++----------- app/views/projects/index.html.erb | 11 +-- app/views/shared/_navbar.html.erb | 6 +- config/locales/subscription.pt-BR.yml | 10 --- spec/models/subscription_spec.rb | 1 - spec/system/subscription/free_user_spec.rb | 27 ------- 8 files changed, 46 insertions(+), 104 deletions(-) delete mode 100644 config/locales/subscription.pt-BR.yml delete mode 100644 spec/system/subscription/free_user_spec.rb diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 73ba19a..836951b 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -1,12 +1,10 @@ class ProjectsController < ApplicationController before_action :authenticate_user! - before_action :authenticate_subscriber, only: :create_invitation_request def index @invitation_request = current_user.invitation_requests.build @invitation_requests = current_user.invitation_requests.pluck(:project_id).to_json @invitation_requests_projects_ids = current_user.invitation_requests.pluck(:project_id) - @free_user = current_user.subscription.inactive? end def create_invitation_request @@ -21,15 +19,9 @@ def create_invitation_request private - def authenticate_subscriber - return if current_user.subscription.active? - - redirect_to root_path, alert: t('alerts.unauthorized') - end - def invitation_request_params invitation_request_params = params.require(:invitation_request).permit(:message) invitation_request_params['project_id'] = params['project_id'] invitation_request_params end -end +end \ No newline at end of file diff --git a/app/javascript/components/projects_vue.js b/app/javascript/components/projects_vue.js index 264b1ba..de014f3 100644 --- a/app/javascript/components/projects_vue.js +++ b/app/javascript/components/projects_vue.js @@ -10,7 +10,6 @@ export default { showingForm: false, currentProjectId: null, invitationRequestsProjectsIds: window.invitationRequestsProjectsIds, - freeUser: window.freeUser, errorMsg: false, } }, diff --git a/app/views/invitation_requests/index.html.erb b/app/views/invitation_requests/index.html.erb index 5f997e1..187dc77 100644 --- a/app/views/invitation_requests/index.html.erb +++ b/app/views/invitation_requests/index.html.erb @@ -1,54 +1,48 @@

<%= InvitationRequest.model_name.human(count: @invitation_request_infos.length) %>

-<% if current_user.subscription.inactive? %> -
-

<%= t('subscriptions.become') %> <%= t('subscriptions.subscriber') %> <%= t('subscriptions.see_and_request') %>

-
-<% else %> -
-
- <%= form_with url: invitation_requests_path, method: :get, class: 'col-10 me-5 px-0' do |f| %> -
- <%= f.label :filter, class: 'd-none' %> - <%= f.select :filter, - invitation_request_filter_options, - { include_blank: 'Todas', selected: params[:filter] }, - class: 'form-select' %> - <%= f.submit t(:filter_btn ), class: 'btn btn-sm btn-primary' %> -
- <% end %> +
+
+ <%= form_with url: invitation_requests_path, method: :get, class: 'col-10 me-5 px-0' do |f| %> +
+ <%= f.label :filter, class: 'd-none' %> + <%= f.select :filter, + invitation_request_filter_options, + { include_blank: 'Todas', selected: params[:filter] }, + class: 'form-select' %> + <%= f.submit t(:filter_btn ), class: 'btn btn-sm btn-primary' %> +
+ <% end %> - <% if @invitation_request_infos.any? %> - <% @invitation_request_infos.each do |invitation_request| %> -
-
-
-
-

<%= invitation_request.project_title %>

-
-

<%= invitation_request.project_description %>

-

<%= invitation_request.project_category %>

-
-
-
-

- <%= t(:time_ago, time: time_ago_in_words(invitation_request.created_at)) %> -

-

- <%= InvitationRequest.human_attribute_name("status.#{invitation_request.status}") %> -

+ <% if @invitation_request_infos.any? %> + <% @invitation_request_infos.each do |invitation_request| %> +
+
+
+
+

<%= invitation_request.project_title %>

+
+

<%= invitation_request.project_description %>

+

<%= invitation_request.project_category %>

+
+

+ <%= t(:time_ago, time: time_ago_in_words(invitation_request.created_at)) %> +

+

+ <%= InvitationRequest.human_attribute_name("status.#{invitation_request.status}") %> +

+
- <% end %> - <% elsif @error %> -

<%= t(:project_api_error) %>

- <% elsif params[:filter].nil? || params[:filter].blank? %> -

<%= t(:no_invitation_request_msg) %>

- <% else %> -

<%= t(:no_filter_results_msg) %>

+
<% end %> -
+ <% elsif @error %> +

<%= t(:project_api_error) %>

+ <% elsif params[:filter].nil? || params[:filter].blank? %> +

<%= t(:no_invitation_request_msg) %>

+ <% else %> +

<%= t(:no_filter_results_msg) %>

+ <% end %>
-<% end %> +
\ No newline at end of file diff --git a/app/views/projects/index.html.erb b/app/views/projects/index.html.erb index 1d92514..b744574 100644 --- a/app/views/projects/index.html.erb +++ b/app/views/projects/index.html.erb @@ -1,11 +1,7 @@

<%= t('.index_title') %>

-
-

<%= t('subscriptions.become') %> <%= t('subscriptions.subscriber') %> <%= t('subscriptions.see_listed_projects') %>

-
- -
+

{{ emptyData }}

@@ -44,7 +40,7 @@

<%= t('.errors.no_result') %>

- +

{{ project.title }}

@@ -75,5 +71,4 @@ + \ No newline at end of file diff --git a/app/views/shared/_navbar.html.erb b/app/views/shared/_navbar.html.erb index 9703ba1..2192c4a 100644 --- a/app/views/shared/_navbar.html.erb +++ b/app/views/shared/_navbar.html.erb @@ -34,13 +34,13 @@ <%= link_to "#{current_user.description}", current_user.profile, class: 'nav-link' %>
- + \ No newline at end of file From ac2565c6ebd160d3823c8d22dffff11cd44f99e2 Mon Sep 17 00:00:00 2001 From: Paulo Henrique Date: Wed, 14 Feb 2024 21:52:16 -0300 Subject: [PATCH 4/6] fix/rubocop --- app/controllers/projects_controller.rb | 2 +- app/views/projects/index.html.erb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 836951b..d0996a6 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -24,4 +24,4 @@ def invitation_request_params invitation_request_params['project_id'] = params['project_id'] invitation_request_params end -end \ No newline at end of file +end diff --git a/app/views/projects/index.html.erb b/app/views/projects/index.html.erb index b744574..4d36291 100644 --- a/app/views/projects/index.html.erb +++ b/app/views/projects/index.html.erb @@ -71,4 +71,4 @@ \ No newline at end of file + From 43fb3c31158e54a427f9bd6384714038d9c42ef9 Mon Sep 17 00:00:00 2001 From: Paulo Henrique Date: Wed, 14 Feb 2024 21:54:38 -0300 Subject: [PATCH 5/6] fix/rubocop --- app/views/invitation_requests/index.html.erb | 2 +- app/views/shared/_navbar.html.erb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/invitation_requests/index.html.erb b/app/views/invitation_requests/index.html.erb index 187dc77..bd101c2 100644 --- a/app/views/invitation_requests/index.html.erb +++ b/app/views/invitation_requests/index.html.erb @@ -45,4 +45,4 @@

<%= t(:no_filter_results_msg) %>

<% end %>
-
\ No newline at end of file +
diff --git a/app/views/shared/_navbar.html.erb b/app/views/shared/_navbar.html.erb index 4c78dc1..9f54c3e 100644 --- a/app/views/shared/_navbar.html.erb +++ b/app/views/shared/_navbar.html.erb @@ -83,4 +83,4 @@
- \ No newline at end of file + From 78ff119ddfcdeb3e8d77a7e3dee31abba42befeb Mon Sep 17 00:00:00 2001 From: Paulo Henrique Date: Wed, 14 Feb 2024 22:12:13 -0300 Subject: [PATCH 6/6] =?UTF-8?q?remove=20arquivos=20n=C3=A3o=20usados?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/factories/subscriptions.rb | 7 ------- spec/models/subscription_spec.rb | 4 ---- 2 files changed, 11 deletions(-) delete mode 100644 spec/factories/subscriptions.rb delete mode 100644 spec/models/subscription_spec.rb diff --git a/spec/factories/subscriptions.rb b/spec/factories/subscriptions.rb deleted file mode 100644 index c93afed..0000000 --- a/spec/factories/subscriptions.rb +++ /dev/null @@ -1,7 +0,0 @@ -FactoryBot.define do - factory :subscription do - user { nil } - start_date { '2024-02-14' } - status { 1 } - end -end diff --git a/spec/models/subscription_spec.rb b/spec/models/subscription_spec.rb deleted file mode 100644 index 67bb6fc..0000000 --- a/spec/models/subscription_spec.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'rails_helper' - -RSpec.describe Subscription, type: :model do -end