From db2d398d94d5e3a6b02864d8c9918a76e86dcc44 Mon Sep 17 00:00:00 2001 From: yystopf Date: Mon, 13 Feb 2023 09:43:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=EF=BC=9Aparticipants?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/issue.rb | 1 + app/models/project.rb | 2 +- app/services/api/v1/issues/create_service.rb | 46 +++++++++++++++++--- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/app/models/issue.rb b/app/models/issue.rb index be759d2b4..a1438761e 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -72,6 +72,7 @@ class Issue < ApplicationRecord has_many :assigners, through: :issue_assigners has_many :issue_participants has_many :participants, through: :issue_participants + has_many :show_participants, -> {joins(:issue_participants).where.not(issue_participants: {participant_type: "atme"}).distinct}, through: :issue_participants, source: :participant has_many :comment_journals, -> {where.not(notes: nil)}, class_name: "Journal", :as => :journalized has_many :operate_journals, -> {where(notes: nil)}, class_name: "Journal", :as => :journalized diff --git a/app/models/project.rb b/app/models/project.rb index d258fb0d8..0792c9cb0 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -429,7 +429,7 @@ class Project < ApplicationRecord last_issue = self.issues.last deleted_issue_count = ($redis_cache.hget("issue_cache_delete_count", self.id) || 0).to_i - last_issue&.project_issues_index.present? ? last_issue.project_issues_index + deleted_issue_count : 1 + last_issue&.project_issues_index.present? ? last_issue.project_issues_index + deleted_issue_count : 0 end def incre_project_issue_cache_delete_count diff --git a/app/services/api/v1/issues/create_service.rb b/app/services/api/v1/issues/create_service.rb index 6b3e9685a..8a795f395 100644 --- a/app/services/api/v1/issues/create_service.rb +++ b/app/services/api/v1/issues/create_service.rb @@ -32,17 +32,20 @@ class Api::V1::Issues::CreateService < ApplicationService check_issue_status check_issue_priority check_milestone if milestone_id.present? + check_issue_tags unless issue_tag_ids.blank? + check_assigners unless assigner_ids.blank? + check_attachments unless attachment_ids.blank? load_assigners unless assigner_ids.blank? load_attachments unless attachment_ids.blank? load_issue_tags unless issue_tag_ids.blank? - load_participants @created_issue = Issue.new(issue_attributes) @created_issue.assigners = @assigners unless assigner_ids.blank? @created_issue.attachments = @attachments unless attachment_ids.blank? @created_issue.issue_tags = @issue_tags unless issue_tag_ids.blank? - @created_issue.participants = @participants - + build_author_participants + build_assinger_participants unless assigner_ids.blank? + @created_issue.save! project.del_project_issue_cache_delete_count # 把缓存里存储项目删除issue的个数清除掉 @@ -67,6 +70,29 @@ class Api::V1::Issues::CreateService < ApplicationService raise Error, "Milestone不存在!" unless Version.find_by_id(milestone_id).present? end + def check_issue_tags + raise Error, "请输入正确的标记ID数组!" unless issue_tag_ids.is_a?(Array) + raise Error, "最多可选择3个标记" if issue_tag_ids.size > 3 + issue_tag_ids.each do |tid| + raise Error, "请输入正确的标记ID!" unless IssueTag.exists?(id: tid) + end + end + + def check_assigners + raise Error, "请输入正确的负责人ID数组!" unless assigner_ids.is_a?(Array) + raise Error, "最多可选择5个负责人" if assigner_ids.size > 5 + assigner_ids.each do |aid| + raise Error, "请输入正确的负责人ID!" unless User.exists?(id: aid) + end + end + + def check_attachments + raise Error, "请输入正确的附件ID数组!" unless assigner_ids.is_a?(Array) + attachment_ids.each do |aid| + raise Error, "请输入正确的附件ID!" unless Attachment.exists?(id: aid) + end + end + def load_assigners @assigners = User.where(id: assigner_ids) end @@ -79,10 +105,6 @@ class Api::V1::Issues::CreateService < ApplicationService @attachments = Attachment.where(id: attachment_ids) end - def load_participants - @participants = User.where(id: assigner_ids).or(User.where(id: current_user.id)) - end - def issue_attributes issue_attributes = { subject: subject, @@ -104,4 +126,14 @@ class Api::V1::Issues::CreateService < ApplicationService issue_attributes end + + def build_author_participants + @created_issue.issue_participants.new({participant_type: "authored", participant_id: current_user.id}) + end + + def build_assinger_participants + assigner_ids.each do |aid| + @created_issue.issue_participants.new({participant_type: "assigned", participant_id: aid}) + end + end end \ No newline at end of file