Welcome to mirror list, hosted at ThFree Co, Russian Federation.

update_service.rb « assignees_service « widgets « work_items « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9176b71c85eafd1e2607fe09b265ccfe84761700 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# frozen_string_literal: true

module WorkItems
  module Widgets
    module AssigneesService
      class UpdateService < WorkItems::Widgets::BaseService
        def before_update_in_transaction(params:)
          return unless params.present? && params.has_key?(:assignee_ids)
          return unless has_permission?(:set_work_item_metadata)

          assignee_ids = filter_assignees_count(params[:assignee_ids])
          assignee_ids = filter_assignee_permissions(assignee_ids)

          return if assignee_ids.sort == work_item.assignee_ids.sort

          work_item.assignee_ids = assignee_ids
          work_item.touch
        end

        private

        def filter_assignees_count(assignee_ids)
          return assignee_ids if work_item.allows_multiple_assignees?

          assignee_ids.first(1)
        end

        def filter_assignee_permissions(assignee_ids)
          assignees = User.id_in(assignee_ids)

          assignees.select { |assignee| assignee.can?(:read_work_item, work_item) }.map(&:id)
        end
      end
    end
  end
end