Commit a75e82d5 authored by Jarka Košanová's avatar Jarka Košanová

Merge branch 'mwaw/214581-delete-star-metrics-dashboard-service' into 'master'

#214581 Add delete metrics user starred dashboard service

See merge request gitlab-org/gitlab!31465
parents 80503089 72f1b801
# frozen_string_literal: true
# Delete all matching Metrics::UsersStarredDashboard entries for given user based on matched dashboard_path, project
module Metrics
module UsersStarredDashboards
class DeleteService < ::BaseService
def initialize(user, project, dashboard_path = nil)
@user, @project, @dashboard_path = user, project, dashboard_path
end
def execute
ServiceResponse.success(payload: { deleted_rows: starred_dashboards.delete_all })
end
private
attr_reader :user, :project, :dashboard_path
def starred_dashboards
# since deleted records are scoped to their owner there is no need to
# check if that user can delete them, also if user lost access to
# project it shouldn't block that user from removing them
dashboards = user.metrics_users_starred_dashboards
if dashboard_path.present?
dashboards.for_project_dashboard(project, dashboard_path)
else
dashboards.for_project(project)
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Metrics::UsersStarredDashboards::DeleteService do
subject(:service_instance) { described_class.new(user, project, dashboard_path) }
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project) }
describe '#execute' do
let_it_be(:user_starred_dashboard_1) { create(:metrics_users_starred_dashboard, user: user, project: project, dashboard_path: 'dashboard_1') }
let_it_be(:user_starred_dashboard_2) { create(:metrics_users_starred_dashboard, user: user, project: project) }
let_it_be(:other_user_starred_dashboard) { create(:metrics_users_starred_dashboard, project: project) }
let_it_be(:other_project_starred_dashboard) { create(:metrics_users_starred_dashboard, user: user) }
context 'without dashboard_path' do
let(:dashboard_path) { nil }
it 'does not scope user starred dashboards by dashboard path' do
result = service_instance.execute
expect(result.success?).to be_truthy
expect(result.payload[:deleted_rows]).to be(2)
expect(Metrics::UsersStarredDashboard.all).to contain_exactly(other_user_starred_dashboard, other_project_starred_dashboard)
end
end
context 'with dashboard_path' do
let(:dashboard_path) { 'dashboard_1' }
it 'does scope user starred dashboards by dashboard path' do
result = service_instance.execute
expect(result.success?).to be_truthy
expect(result.payload[:deleted_rows]).to be(1)
expect(Metrics::UsersStarredDashboard.all).to contain_exactly(user_starred_dashboard_2, other_user_starred_dashboard, other_project_starred_dashboard)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment