From 2ff43a33202943e439257bbd88f019f1ed7c0777 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Tue, 26 Mar 2019 14:54:34 +0100 Subject: [PATCH] Change filtering logic when no filters are selected [SCI-3223] --- .../global_activities/side_pane.js | 21 +++++++++--- .../global_activities_controller.rb | 19 +++++------ app/services/activities_service.rb | 34 ++++++++----------- 3 files changed, 40 insertions(+), 34 deletions(-) diff --git a/app/assets/javascripts/global_activities/side_pane.js b/app/assets/javascripts/global_activities/side_pane.js index 415b64ddd..94287481a 100644 --- a/app/assets/javascripts/global_activities/side_pane.js +++ b/app/assets/javascripts/global_activities/side_pane.js @@ -34,11 +34,23 @@ function GlobalActivitiesFilterPrepareArray() { if (subjectFilter[splitObject[0]] === undefined) subjectFilter[splitObject[0]] = []; subjectFilter[splitObject[0]].push(parseInt(splitObject[1], 10)); }); + + // Clear request parameters if all options are selected + if (teamFilter.length === $('.ga-side .team-selector option').length) { + teamFilter = []; + } + if (userFilter.length === $('.ga-side .user-selector option').length) { + userFilter = []; + } + if (activityFilter.length === $('.ga-side .activity-selector option').length) { + activityFilter = []; + } + return { - teams: JSON.stringify(teamFilter), - users: JSON.stringify(userFilter), - types: JSON.stringify(activityFilter), - subjects: JSON.stringify(subjectFilter), + teams: teamFilter, + users: userFilter, + types: activityFilter, + subjects: subjectFilter, from_date: GlobalActivitiesFiltersGetDates().from, to_date: GlobalActivitiesFiltersGetDates().to }; @@ -171,6 +183,7 @@ $(function() { $('#calendar-from-date').data('DateTimePicker').clear(); $('#calendar-to-date').data('DateTimePicker').clear(); updateRunning = false; + GlobalActivitiesUpdateTopPaneTags(); reloadActivities(); }); diff --git a/app/controllers/global_activities_controller.rb b/app/controllers/global_activities_controller.rb index 4deb4cd8f..36a2beb9e 100644 --- a/app/controllers/global_activities_controller.rb +++ b/app/controllers/global_activities_controller.rb @@ -2,15 +2,20 @@ class GlobalActivitiesController < ApplicationController def index - teams = activity_filters[:teams] @teams = current_user.teams - teams = current_team if teams.blank? + selected_teams = if request.format.html? + current_team + elsif activity_filters[:teams].present? + @teams.where(id: activity_filters[:teams]) + else + @teams + end @activity_types = Activity.activity_types_list @user_list = User.where(id: UserTeam.where(team: current_user.teams).select(:user_id)) .distinct .pluck(:full_name, :id) @grouped_activities, @more_activities = - ActivitiesService.load_activities(current_user, teams, activity_filters) + ActivitiesService.load_activities(current_user, selected_teams, activity_filters) last_day = @grouped_activities.keys.last @next_date = (Date.parse(last_day) - 1.day).strftime('%Y-%m-%d') if last_day respond_to do |format| @@ -63,14 +68,6 @@ class GlobalActivitiesController < ApplicationController private def activity_filters - begin - params[:types] = JSON.parse(params[:types]) - params[:users] = JSON.parse(params[:users]) - params[:teams] = JSON.parse(params[:teams]) - params[:subjects] = JSON.parse(params[:subjects]) - rescue StandardError - end - params.permit( :from_date, :to_date, types: [], subjects: {}, users: [], teams: [] ) diff --git a/app/services/activities_service.rb b/app/services/activities_service.rb index a58cbd771..77820d8c6 100644 --- a/app/services/activities_service.rb +++ b/app/services/activities_service.rb @@ -3,10 +3,10 @@ class ActivitiesService def self.load_activities(user, teams, filters = {}) # Create condition for view permissions checking first - visible_projects = Project.viewable_by_user(user, teams) - query = Activity.where('project_id IS NULL AND team_id IN (?)', teams) - .or(Activity.where(project: visible_projects)) - + visible_teams = user.teams.where(id: teams) + visible_projects = Project.viewable_by_user(user, visible_teams) + query = Activity.where(project: visible_projects) + .or(Activity.where(project: nil, team: visible_teams)) if filters[:subjects].present? query = query.where( filters[:subjects] @@ -18,20 +18,16 @@ class ActivitiesService query = query.where(owner_id: filters[:users]) if filters[:users] query = query.where(type_of: filters[:types]) if filters[:types] - if filters[:from_date].present? && filters[:to_date].present? - activities = query.where( - 'created_at <= :from AND created_at >= :to', - from: Time.zone.parse(filters[:from_date]).beginning_of_day.utc, - to: Time.zone.parse(filters[:to_date]).end_of_day.utc - ) - elsif filters[:from_date].present? && filters[:to_date].blank? - activities = query.where( - 'created_at <= :from', - from: Time.zone.parse(filters[:from_date]).beginning_of_day.utc - ) - else - activities = query - end + activities = + if filters[:from_date].present? && filters[:to_date].present? + query.where('created_at <= :from AND created_at >= :to', + from: Time.zone.parse(filters[:from_date]).end_of_day.utc, + to: Time.zone.parse(filters[:to_date]).beginning_of_day.utc) + elsif filters[:from_date].present? && filters[:to_date].blank? + query.where('created_at <= :from', from: Time.zone.parse(filters[:from_date]).end_of_day.utc) + else + query + end activities = activities.order(created_at: :desc) .limit(Constants::ACTIVITY_AND_NOTIF_SEARCH_LIMIT) @@ -51,7 +47,7 @@ class ActivitiesService more_left = query.where( 'created_at < :from', - from: Time.zone.parse(last_date).end_of_day.utc + from: Time.zone.parse(last_date).prev_day.end_of_day.utc ).exists? results[last_date] = activities.to_a