From b7aa1e51b3c8cc6a12c449f6b9d30fcfada64c04 Mon Sep 17 00:00:00 2001 From: yystopf Date: Tue, 20 Aug 2024 15:56:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/pm/issues_controller.rb | 10 +- app/controllers/api/pm/journals_controller.rb | 2 +- app/models/journal.rb | 365 ++++++++++++++++++ .../api/pm/issues/batch_delete_service.rb | 38 ++ .../api/pm/issues/batch_update_service.rb | 33 ++ app/services/api/pm/issues/create_service.rb | 168 ++++++++ app/services/api/pm/issues/delete_service.rb | 42 ++ app/services/api/pm/issues/update_service.rb | 283 ++++++++++++++ .../pm/issues/journals/_detail.json.jbuilder | 25 ++ .../pm/issues/journals/index.json.jbuilder | 8 + 10 files changed, 968 insertions(+), 6 deletions(-) create mode 100644 app/services/api/pm/issues/batch_delete_service.rb create mode 100644 app/services/api/pm/issues/batch_update_service.rb create mode 100644 app/services/api/pm/issues/create_service.rb create mode 100644 app/services/api/pm/issues/delete_service.rb create mode 100644 app/services/api/pm/issues/update_service.rb create mode 100644 app/views/api/pm/issues/journals/_detail.json.jbuilder create mode 100644 app/views/api/pm/issues/journals/index.json.jbuilder diff --git a/app/controllers/api/pm/issues_controller.rb b/app/controllers/api/pm/issues_controller.rb index e56ef5e53..cdf5f1d7d 100644 --- a/app/controllers/api/pm/issues_controller.rb +++ b/app/controllers/api/pm/issues_controller.rb @@ -62,17 +62,17 @@ class Api::Pm::IssuesController < Api::Pm::BaseController end def create - @object_result = Api::V1::Issues::CreateService.call(@project, issue_params, current_user) + @object_result = Api::Pm::Issues::CreateService.call(@project, issue_params, current_user) render 'api/v1/issues/create' end def update - @object_result = Api::V1::Issues::UpdateService.call(@project, @issue, issue_params, current_user) + @object_result = Api::Pm::Issues::UpdateService.call(@project, @issue, issue_params, current_user) render 'api/v1/issues/update' end def batch_update - @object_result = Api::V1::Issues::BatchUpdateService.call(@project, @issues, batch_issue_params, current_user) + @object_result = Api::Pm::Issues::BatchUpdateService.call(@project, @issues, batch_issue_params, current_user) if @object_result render_ok else @@ -82,7 +82,7 @@ class Api::Pm::IssuesController < Api::Pm::BaseController def batch_destroy return render_ok if params[:ids].is_a?(Array) && params[:ids].blank? - @object_result = Api::V1::Issues::BatchDeleteService.call(@project, @issues, current_user) + @object_result = Api::Pm::Issues::BatchDeleteService.call(@project, @issues, current_user) if @object_result render_ok else @@ -116,7 +116,7 @@ class Api::Pm::IssuesController < Api::Pm::BaseController def destroy - @object_result = Api::V1::Issues::DeleteService.call(@project, @issue, current_user) + @object_result = Api::Pm::Issues::DeleteService.call(@project, @issue, current_user) if @object_result render_ok else diff --git a/app/controllers/api/pm/journals_controller.rb b/app/controllers/api/pm/journals_controller.rb index 14f386860..b10cb4829 100644 --- a/app/controllers/api/pm/journals_controller.rb +++ b/app/controllers/api/pm/journals_controller.rb @@ -10,7 +10,7 @@ class Api::Pm::JournalsController < Api::Pm::BaseController @total_operate_journals_count = @object_result[:total_operate_journals_count] @total_comment_journals_count = @object_result[:total_comment_journals_count] @journals = kaminary_select_paginate(@object_result[:data]) - render 'api/v1/issues/journals/index' + render 'api/pm/issues/journals/index' end def create diff --git a/app/models/journal.rb b/app/models/journal.rb index 0fdca0f9d..ea50646b6 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -82,6 +82,371 @@ class Journal < ApplicationRecord end end + def pm_operate_content + content = "" + detail = self.journal_details.take + case detail.property + when 'requirement' + case detail.prop_key + when 'status_id' + old_value = IssueStatus.find_by_id(detail.old_value)&.name + new_value = IssueStatus.find_by_id(detail.value)&.name + content += "将状态" + if old_value.nil? || old_value.blank? + content += "设置为#{new_value}" + else + new_value = "未设置" if new_value.blank? + content += "由#{old_value}更改为#{new_value}" + end + content.gsub!('新增', '待评审') + content.gsub!('正在解决', '进行中') + content.gsub!('已解决', '已完成') + content.gsub!('关闭', '已关闭') + content.gsub!('拒绝', '已拒绝') + return content + when 'root_id' + old_value = Issue.find_by_id(detail.old_value)&.subject + new_value = Issue.find_by_id(detail.value)&.subject + if old_value.nil? || old_value.blank? + content += "关联了父需求<#{new_value}>" + else + if new_value.nil? || new_value.blank? + content += "取消了关联的父需求<#{old_value}>" + else + content += "将关联的父需求由<#{old_value}>更改为<#{new_value}>" + end + end + return content + when 'leaf_issue' + old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<#{i.subject}>"}.join("、") + new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<#{i.subject}>"}.join("、") + if old_value.nil? || old_value.blank? + content += "新建了子需求#{new_value}" + else + if new_value.nil? || new_value.blank? + content += "删除了关联的子需求#{old_value}" + else + content += "新建了子需求#{new_value}" + end + end + return content + when 'tag_leaf_issue' + old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<#{i.subject}>"}.join("、") + new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<#{i.subject}>"}.join("、") + if old_value.nil? || old_value.blank? + content += "关联了子需求#{new_value}" + else + if new_value.nil? || new_value.blank? + content += "取消了关联的子需求#{old_value}" + else + content += "关联了子需求#{new_value}" + end + end + return content + else + return "创建了需求" + end + when 'task' + case detail.prop_key + when 'status_id' + old_value = IssueStatus.find_by_id(detail.old_value)&.name + new_value = IssueStatus.find_by_id(detail.value)&.name + content += "将状态" + if old_value.nil? || old_value.blank? + content += "设置为#{new_value}" + else + new_value = "未设置" if new_value.blank? + content += "由#{old_value}更改为#{new_value}" + end + content.gsub!('新增', '待处理') + content.gsub!('正在解决', '进行中') + content.gsub!('已解决', '已完成') + content.gsub!('关闭', '已关闭') + content.gsub!('拒绝', '已拒绝') + return content + when 'root_id' + old_value = Issue.find_by_id(detail.old_value)&.subject + new_value = Issue.find_by_id(detail.value)&.subject + if old_value.nil? || old_value.blank? + content += "关联了父任务<#{new_value}>" + else + if new_value.nil? || new_value.blank? + content += "取消了关联的父任务<#{old_value}>" + else + content += "将关联的父任务由<#{old_value}>更改为<#{new_value}>" + end + end + return content + when 'leaf_issue' + old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<#{i.subject}>"}.join("、") + new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<#{i.subject}>"}.join("、") + if old_value.nil? || old_value.blank? + content += "新建了子任务#{new_value}" + else + if new_value.nil? || new_value.blank? + content += "删除了关联的子任务#{old_value}" + else + content += "新建了子任务#{new_value}" + end + end + return content + when 'tag_leaf_issue' + old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<#{i.subject}>"}.join("、") + new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<#{i.subject}>"}.join("、") + if old_value.nil? || old_value.blank? + content += "关联了子任务#{new_value}" + else + if new_value.nil? || new_value.blank? + content += "取消了关联的子任务#{old_value}" + else + content += "关联了子任务#{new_value}" + end + end + return content + else + return "创建了任务" + end + when 'bug' + case detail.prop_key + when 'status_id' + old_value = IssueStatus.find_by_id(detail.old_value)&.name + new_value = IssueStatus.find_by_id(detail.value)&.name + content += "将状态" + if old_value.nil? || old_value.blank? + content += "设置为#{new_value}" + else + new_value = "未设置" if new_value.blank? + content += "由#{old_value}更改为#{new_value}" + end + content.gsub!('新增', '待修复') + content.gsub!('正在解决', '修复中') + content.gsub!('已解决', '已修复') + content.gsub!('关闭', '已关闭') + content.gsub!('拒绝', '已拒绝') + return content + when 'root_id' + old_value = Issue.find_by_id(detail.old_value)&.subject + new_value = Issue.find_by_id(detail.value)&.subject + if old_value.nil? || old_value.blank? + content += "关联了父缺陷<#{new_value}>" + else + if new_value.nil? || new_value.blank? + content += "取消了关联的父缺陷<#{old_value}>" + else + content += "将关联的父缺陷由<#{old_value}>更改为<#{new_value}>" + end + end + return content + when 'leaf_issue' + old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<#{i.subject}>"}.join("、") + new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<#{i.subject}>"}.join("、") + if old_value.nil? || old_value.blank? + content += "新建了子缺陷#{new_value}" + else + if new_value.nil? || new_value.blank? + content += "删除了关联的子缺陷#{old_value}" + else + content += "新建了子缺陷#{new_value}" + end + end + return content + when 'tag_leaf_issue' + old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<#{i.subject}>"}.join("、") + new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<#{i.subject}>"}.join("、") + if old_value.nil? || old_value.blank? + content += "关联了子缺陷#{new_value}" + else + if new_value.nil? || new_value.blank? + content += "取消了关联的子缺陷#{old_value}" + else + content += "关联了子缺陷#{new_value}" + end + end + return content + else + return "创建了缺陷" + end + when 'attr' + case detail.prop_key + when 'subject' + return "修改了标题" + when 'description' + return "修改了正文" + when 'priority_id' + old_value = IssuePriority.find_by_id(detail.old_value)&.name + new_value = IssuePriority.find_by_id(detail.value)&.name + if old_value.nil? || old_value.blank? + content += "将优先级设置为#{new_value}" + else + new_value = "未设置" if new_value.blank? + content += "将优先级由#{old_value}更改为#{new_value}" + end + return content + when 'pm_issue_type' + old_value = detail.old_value + new_value = detail.value + if old_value.nil? || old_value.blank? + content += "将工作项类型设置为#{new_value}" + else + new_value = "未设置" if new_value.blank? + content += "将工作项类型由#{old_value}更改为#{new_value}" + end + content.gsub!('1', '需求') + content.gsub!('2', '任务') + content.gsub!('3', '缺陷') + return content + when 'pm_sprint_id' + old_value = detail.old_value + new_value = detail.value + if old_value.nil? || old_value.blank? + content += "添加了关联迭代" + else + if new_value.nil? || new_value.blank? + content += "将关联迭代更改为未设置" + else + content += "变更了关联迭代" + end + end + return content + when 'project_id' + old_value = Project.find_by_id(detail.old_value)&.name + new_value = Project.find_by_id(detail.value)&.name + if old_value.nil? || old_value.blank? + content += "添加关联代码库#{new_value}" + else + if new_value.nil? || new_value.blank? + content += "将关联代码库更改为未设置" + else + content += "将关联代码库由#{old_value}改为#{new_value}" + end + end + return content + when 'branch_name' + old_value = detail.old_value + new_value = detail.value + if old_value.nil? || old_value.blank? + content += "添加关联分支#{new_value}" + else + if new_value.nil? || new_value.blank? + content += "将关联分支更改为未设置" + else + content += "将关联分支由#{old_value}改为#{new_value}" + end + end + return content + + when 'start_date' + old_value = detail.old_value + new_value = detail.value + if old_value.nil? || old_value.blank? + content += "添加开始时间#{new_value}" + else + if new_value.nil? || new_value.blank? + content += "将开始时间更改为未设置" + else + content += "将开始时间由#{old_value}改为#{new_value}" + end + end + return content + + when 'due_date' + old_value = detail.old_value + new_value = detail.value + if old_value.nil? || old_value.blank? + content += "添加结束时间#{new_value}" + else + if new_value.nil? || new_value.blank? + content += "将结束时间更改为未设置" + else + content += "将结束时间由#{old_value}改为#{new_value}" + end + end + return content + when 'time_scale' + old_value = detail.old_value + new_value = detail.value + if old_value.nil? || old_value.blank? + content += "添加预估工时#{new_value}" + else + if new_value.nil? || new_value.blank? + content += "将预估工时更改为未设置" + else + content += "将预估工时由#{old_value}改为#{new_value}" + end + end + return content + end + when 'attachment' + old_value = detail.old_value.to_s + new_value = detail.value.to_s + if old_value.nil? || old_value.blank? + content += "上传了附件" + else + if new_value.nil? || new_value.blank? + content += "删除了附件" + else + content += "上传了附件" + end + end + return content + when 'assigner' + old_value = User.where(id: detail.old_value.split(",")).map{|u| u.real_name}.join("、") + new_value = User.where(id: detail.value.split(",")).map{|u| u.real_name}.join("、") + if old_value.nil? || old_value.blank? + content += "添加负责人#{new_value}" + else + if new_value.nil? || new_value.blank? + content += "将负责人更改为未设置" + else + content += "将负责人由#{old_value}更改为#{new_value}" + end + end + return content + when 'issue_tag' + old_value = IssueTag.where(id: detail.old_value.split(",")).pluck(:name).join("、") + new_value = IssueTag.where(id: detail.value.split(",")).pluck(:name).join("、") + if old_value.nil? || old_value.blank? + content += "添加标记#{new_value}" + else + if new_value.nil? || new_value.blank? + content += "将标记更改为未设置" + else + content += "将标记由#{old_value}更改为#{new_value}" + end + end + return content + when 'link_issue' + old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "[#{i.pm_issue_type}]-<#{i.subject}>"}.join("、") + new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "[#{i.pm_issue_type}]-<#{i.subject}>"}.join("、") + if old_value.nil? || old_value.blank? + content += "关联了工作项#{new_value}" + else + if new_value.nil? || new_value.blank? + content += "取消了关联的工作项#{old_value}" + else + content += "关联了工作项#{new_value}" + end + end + content.gsub!('1', "需求") + content.gsub!('2', "任务") + content.gsub!('3', "缺陷") + return content + when 'tag_link_issue' + old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "[#{i.pm_issue_type}]-<#{i.subject}>"}.join("、") + new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "[#{i.pm_issue_type}]-<#{i.subject}>"}.join("、") + if old_value.nil? || old_value.blank? + content += "新建了关联的工作项#{new_value}" + else + if new_value.nil? || new_value.blank? + content += "删除了关联的工作项#{old_value}" + else + content += "新建了关联的工作项#{new_value}" + end + end + return content + end + end + def operate_content content = "" detail = self.journal_details.take diff --git a/app/services/api/pm/issues/batch_delete_service.rb b/app/services/api/pm/issues/batch_delete_service.rb new file mode 100644 index 000000000..9afdfdb81 --- /dev/null +++ b/app/services/api/pm/issues/batch_delete_service.rb @@ -0,0 +1,38 @@ +class Api::Pm::Issues::BatchDeleteService < ApplicationService + include ActiveModel::Model + + attr_reader :project, :issues, :current_user + + validates :project, :issues, :current_user, presence: true + + def initialize(project, issues, current_user = nil) + @project = project + @issues = issues.includes(:assigners) + @current_user = current_user + end + + def call + raise Error, errors.full_messages.join(", ") unless valid? + try_lock("Api::V1::Issues::DeleteService:#{project.id}") # 开始写数据,加锁 + + delete_issues + + project.incre_project_issue_cache_delete_count(@issues.size) + + if Site.has_notice_menu? && !project.id.zero? + @issues.each do |issue| + SendTemplateMessageJob.perform_later('IssueDeleted', current_user.id, @issue&.subject, @issue.assigners.pluck(:id), @issue.author_id) + end + end + + unlock("Api::V1::Issues::DeleteService:#{project.id}") + + return true + end + + private + + def delete_issues + raise Error, "批量删除疑修失败!" unless @issues.destroy_all + end +end \ No newline at end of file diff --git a/app/services/api/pm/issues/batch_update_service.rb b/app/services/api/pm/issues/batch_update_service.rb new file mode 100644 index 000000000..b7eabf2cb --- /dev/null +++ b/app/services/api/pm/issues/batch_update_service.rb @@ -0,0 +1,33 @@ +class Api::Pm::Issues::BatchUpdateService < ApplicationService + include ActiveModel::Model + include Api::V1::Issues::Concerns::Checkable + include Api::V1::Issues::Concerns::Loadable + + attr_reader :project, :issues, :params, :current_user + attr_reader :status_id, :priority_id, :milestone_id, :project_id + attr_reader :issue_tag_ids, :assigner_ids + + validates :project, :issues, :current_user, presence: true + + def initialize(project, issues, params, current_user = nil) + @project = project + @issues = issues + @params = params + @current_user = current_user + end + + def call + raise Error, errors.full_messages.join(", ") unless valid? + ActiveRecord::Base.transaction do + @issues.each do |issue| + if issue.issue_classify == "issue" + Api::Pm::Issues::UpdateService.call(project, issue, params, current_user) + end + end + + return true + end + end + + +end \ No newline at end of file diff --git a/app/services/api/pm/issues/create_service.rb b/app/services/api/pm/issues/create_service.rb new file mode 100644 index 000000000..796faf1e5 --- /dev/null +++ b/app/services/api/pm/issues/create_service.rb @@ -0,0 +1,168 @@ +class Api::Pm::Issues::CreateService < ApplicationService + include ActiveModel::Model + include Api::V1::Issues::Concerns::Checkable + include Api::V1::Issues::Concerns::Loadable + + attr_reader :project, :current_user + attr_reader :status_id, :priority_id, :milestone_id, :branch_name, :start_date, :due_date, :subject, :description, :blockchain_token_num, :root_subject + attr_reader :issue_tag_ids, :assigner_ids, :attachment_ids, :receivers_login + attr_accessor :created_issue + + validates :subject, presence: true + validates :status_id, :priority_id, presence: true + validates :project, :current_user, presence: true + validates :blockchain_token_num, numericality: {greater_than: 0}, allow_blank: true + + def initialize(project, params, current_user = nil) + @project = project + @current_user = current_user + @status_id = params[:status_id] + @priority_id = params[:priority_id] + @milestone_id = params[:milestone_id] + @branch_name = params[:branch_name] + @start_date = params[:start_date] + @due_date = params[:due_date] + @subject = params[:subject] + @description = params[:description] + @blockchain_token_num = params[:blockchain_token_num] + @issue_tag_ids = params[:issue_tag_ids] + @assigner_ids = params[:assigner_ids] + @attachment_ids = params[:attachment_ids] + @receivers_login = params[:receivers_login] + @pm_project_id = params[:pm_project_id] + @pm_sprint_id = params[:pm_sprint_id] + @pm_issue_type = params[:pm_issue_type] + @root_id = params[:root_id] + @time_scale = params[:time_scale] + @linkable_id = params[:link_able_id] + @root_subject = params[:root_subject] + end + + def call + raise Error, errors.full_messages.join(', ') unless valid? + ActiveRecord::Base.transaction do + check_issue_status(status_id) + check_issue_priority(priority_id) + check_milestone(milestone_id) if milestone_id.present? + check_issue_tags(issue_tag_ids) unless issue_tag_ids.blank? + check_assigners(assigner_ids) unless assigner_ids.blank? + check_attachments(attachment_ids) unless attachment_ids.blank? + check_atme_receivers(receivers_login) unless receivers_login.blank? + check_blockchain_token_num(current_user.id, project.id, blockchain_token_num) if blockchain_token_num.present? + load_assigners(assigner_ids) unless assigner_ids.blank? + load_attachments(attachment_ids) unless attachment_ids.blank? + load_issue_tags(issue_tag_ids) unless issue_tag_ids.blank? + load_atme_receivers(receivers_login) unless receivers_login.blank? + try_lock("Api::Pm::Issues::CreateService:#{project.id}") # 开始写数据,加锁 + @created_issue = Issue.new(issue_attributes) + build_author_participants + build_assigner_participants unless assigner_ids.blank? + build_atme_participants if @atme_receivers.present? + build_issue_journal_details + build_issue_project_trends + @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.pm_project_id = @pm_project_id + @created_issue.pm_sprint_id = @pm_sprint_id + @created_issue.pm_issue_type = @pm_issue_type + if @root_subject.present? && @pm_issue_type.to_i == 4 + @root_issue = Issue.find_by(subject: @root_subject, pm_issue_type: 4, pm_project_id: @pm_project_id) + unless @root_issue.present? + @root_issue = Issue.create(subject: @root_subject, pm_issue_type: 4, pm_project_id: @pm_project_id, status_id: 1, priority_id: 1, tracker_id: Tracker.first.id, project_id: @project.id, author_id: current_user.id) + end + @created_issue.root_id = @root_issue.id + else + @created_issue.root_id = @root_id + end + @created_issue.time_scale = @time_scale + @created_issue.issue_tags_value = @issue_tags.order('id asc').pluck(:id).join(',') unless issue_tag_ids.blank? + @created_issue.changer_id = @current_user.id + @created_issue.save! + + PmLink.create(be_linkable_type: 'Issue', be_linkable_id: @created_issue.id, linkable_type: 'Issue', linkable_id: @linkable_id) if @linkable_id.present? + if Site.has_blockchain? && @project.use_blockchain + if @created_issue.blockchain_token_num.present? && @created_issue.blockchain_token_num > 0 + Blockchain::CreateIssue.call({user_id: current_user.id, project_id: @created_issue.project_id, token_num: @created_issue.blockchain_token_num}) + end + + push_activity_2_blockchain('issue_create', @created_issue) + end + + project.del_project_issue_cache_delete_count # 把缓存里存储项目删除issue的个数清除掉 + unless @project.id.zero? + # 新增时向grimoirelab推送事件 + IssueWebhookJob.set(wait: 5.seconds).perform_later(@created_issue.id) + + # @信息发送 + AtmeService.call(current_user, @atme_receivers, @created_issue) unless receivers_login.blank? + + # 发消息 + if Site.has_notice_menu? + SendTemplateMessageJob.perform_later('IssueAssigned', current_user.id, @created_issue&.id, assigner_ids) unless assigner_ids.blank? + SendTemplateMessageJob.perform_later('ProjectIssue', current_user.id, @created_issue&.id) + end + + # 触发webhook + TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueCreate', @created_issue&.id, current_user.id) + TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueLabel', @created_issue&.id, current_user.id, {issue_tag_ids: [[], issue_tag_ids]}) unless issue_tag_ids.blank? + TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueAssign', @created_issue&.id, current_user.id, {assigner_ids: [[], assigner_ids]}) unless assigner_ids.blank? + end + unlock("Api::Pm::Issues::CreateService:#{project.id}") # 结束写数据,解锁 + end + + return @created_issue + end + + private + + def issue_attributes + issue_attributes = { + subject: subject, + project_id: project.id, + author_id: current_user.id, + tracker_id: Tracker.first.id, + status_id: status_id, + priority_id: priority_id, + project_issues_index: (project.get_last_project_issues_index + 1), + issue_type: '1', + issue_classify: 'issue' + } + + issue_attributes.merge!({description: description}) if description.present? + issue_attributes.merge!({fixed_version_id: milestone_id}) if milestone_id.present? + issue_attributes.merge!({start_date: start_date}) if start_date.present? + issue_attributes.merge!({due_date: due_date}) if due_date.present? + issue_attributes.merge!({branch_name: branch_name}) if branch_name.present? + issue_attributes.merge!({blockchain_token_num: blockchain_token_num}) if blockchain_token_num.present? + + issue_attributes + end + + def build_author_participants + @created_issue.issue_participants.new({participant_type: 'authored', participant_id: current_user.id}) + end + + def build_assigner_participants + assigner_ids.each do |aid| + @created_issue.issue_participants.new({participant_type: 'assigned', participant_id: aid}) + end + end + + def build_atme_participants + @atme_receivers.each do |receiver| + @created_issue.issue_participants.new({participant_type: 'atme', participant_id: receiver.id}) + end + end + + def build_issue_project_trends + return if @project.id == 0 + @created_issue.project_trends.new({user_id: current_user.id, project_id: @project.id, action_type: 'create'}) + @created_issue.project_trends.new({user_id: current_user.id, project_id: @project.id, action_type: ProjectTrend::CLOSE}) if status_id.to_i == 5 + end + + def build_issue_journal_details + journal = @created_issue.journals.new({user_id: current_user.id}) + journal.journal_details.new({property: 'issue', prop_key: 1, old_value: '', value: ''}) + end +end \ No newline at end of file diff --git a/app/services/api/pm/issues/delete_service.rb b/app/services/api/pm/issues/delete_service.rb new file mode 100644 index 000000000..2d5df500e --- /dev/null +++ b/app/services/api/pm/issues/delete_service.rb @@ -0,0 +1,42 @@ +class Api::Pm::Issues::DeleteService < ApplicationService + include ActiveModel::Model + + attr_reader :project, :issue, :current_user + + validates :project, :issue, :current_user, presence: true + + def initialize(project, issue, current_user = nil) + @project = project + @issue = issue + @current_user = current_user + end + + def call + raise Error, errors.full_messages.join(", ") unless valid? + try_lock("Api::V1::Issues::DeleteService:#{project.id}") # 开始写数据,加锁 + + delete_issue + #删除双向关联 + PmLink.where(be_linkable_id: @issue.id, be_linkable_type: 'Issue').or(PmLink.where(linkable_id: @issue.id, linkable_type: 'Issue')).map(&:destroy) + + project.incre_project_issue_cache_delete_count + + if Site.has_blockchain? && @project.use_blockchain && !project.id.zero? + unlock_balance_on_blockchain(@issue.author_id.to_s, @project.id.to_s, @issue.blockchain_token_num.to_i) if @issue.blockchain_token_num.present? + end + + if Site.has_notice_menu? && !project.id.zero? + SendTemplateMessageJob.perform_later('IssueDeleted', current_user.id, @issue&.subject, @issue.assigners.pluck(:id), @issue.author_id) + end + + unlock("Api::V1::Issues::DeleteService:#{project.id}") + + return true + end + + private + + def delete_issue + raise Error, "删除疑修失败!" unless issue.destroy! + end +end \ No newline at end of file diff --git a/app/services/api/pm/issues/update_service.rb b/app/services/api/pm/issues/update_service.rb new file mode 100644 index 000000000..99896e884 --- /dev/null +++ b/app/services/api/pm/issues/update_service.rb @@ -0,0 +1,283 @@ +class Api::Pm::Issues::UpdateService < ApplicationService + include ActiveModel::Model + include Api::V1::Issues::Concerns::Checkable + include Api::V1::Issues::Concerns::Loadable + + attr_reader :project, :issue, :current_user + attr_reader :status_id, :priority_id, :milestone_id, :branch_name, :start_date, :due_date, :subject, :description, :blockchain_token_num + attr_reader :target_pm_project_id, :pm_sprint_id, :pm_issue_type, :root_id, :time_scale + attr_reader :issue_tag_ids, :assigner_ids, :attachment_ids, :receivers_login, :before_issue_tag_ids, :before_assigner_ids, :project_id + attr_accessor :add_assigner_ids, :previous_issue_changes, :updated_issue, :atme_receivers + + validates :project, :issue, :current_user, presence: true + validates :blockchain_token_num, numericality: {greater_than: 0}, allow_blank: true + + def initialize(project, issue, params, current_user = nil) + @project = project + @issue = issue + @current_user = current_user + @status_id = params[:status_id] + @priority_id = params[:priority_id] + @milestone_id = params[:milestone_id] + @branch_name = params[:branch_name] + @start_date = params[:start_date] + @due_date = params[:due_date] + @subject = params[:subject] + @description = params[:description] + @blockchain_token_num = params[:blockchain_token_num] + @issue_tag_ids = params[:issue_tag_ids] + @assigner_ids = params[:assigner_ids] + @before_issue_tag_ids = issue.issue_tags.pluck(:id) + @before_assigner_ids = issue.assigners.pluck(:id) + @attachment_ids = params[:attachment_ids] + @receivers_login = params[:receivers_login] + @target_pm_project_id = params[:target_pm_project_id] + @pm_sprint_id = params[:pm_sprint_id] + @pm_issue_type = params[:pm_issue_type] + @root_id = params[:root_id] + @time_scale = params[:time_scale] + @project_id = params[:project_id] + @add_assigner_ids = [] + @previous_issue_changes = {} + end + + def call + raise Error, errors.full_messages.join(", ") unless valid? + ActiveRecord::Base.transaction do + check_issue_status(status_id) if status_id.present? + check_issue_priority(priority_id) if priority_id.present? + check_milestone(milestone_id) if milestone_id.present? + check_root_issue(issue, root_id) if root_id.present? + check_issue_tags(issue_tag_ids) unless issue_tag_ids.nil? + check_assigners(assigner_ids) unless assigner_ids.nil? + check_attachments(attachment_ids) unless attachment_ids.nil? + check_atme_receivers(receivers_login) unless receivers_login.nil? + check_blockchain_token_num(issue.author_id, project.id, blockchain_token_num, (@issue.blockchain_token_num || 0)) if blockchain_token_num.present? && current_user.id == @issue.author_id && !PullAttachedIssue.exists?(issue_id: @issue, fixed: true) + load_assigners(assigner_ids) + load_attachments(attachment_ids) + load_issue_tags(issue_tag_ids) + load_atme_receivers(receivers_login) unless receivers_login.nil? + + try_lock("Api::Pm::Issues::UpdateService:#{project.id}:#{issue.id}") + @updated_issue = @issue + issue_load_attributes + build_assigner_issue_journal_details unless assigner_ids.nil?# 操作记录 + build_attachment_issue_journal_details unless attachment_ids.nil? + build_issue_tag_issue_journal_details unless issue_tag_ids.nil? + build_issue_project_trends if status_id.present? # 开关时间记录 + build_assigner_participants unless assigner_ids.nil? # 负责人 + build_edit_participants + build_atme_participants if @atme_receivers.present? + unless assigner_ids.nil? + @previous_issue_changes.merge!(assigned_to_id: [@updated_issue.assigners.pluck(:id), @assigners.pluck(:id)]) + @updated_issue.assigners = @assigners || User.none + end + @updated_issue.attachments = @attachments || Attachment.none unless attachment_ids.nil? + @updated_issue.issue_tags_relates.destroy_all & @updated_issue.issue_tags = @issue_tags || IssueTag.none unless issue_tag_ids.nil? + @updated_issue.issue_tags_value = @issue_tags.order("id asc").pluck(:id).join(",") unless issue_tag_ids.nil? + + #Pm相关 + @updated_issue.pm_project_id = @target_pm_project_id unless @target_pm_project_id.nil? + @updated_issue.pm_sprint_id = @pm_sprint_id unless @pm_sprint_id.nil? + if @updated_issue.children_issues.count == 0 && @updated_issue.parent_id.nil? + @updated_issue.pm_issue_type = @pm_issue_type unless @pm_issue_type.nil? + end + @updated_issue.root_id = @root_id unless @root_id.nil? #不为 nil的时候更新 + @updated_issue.root_id = nil if @root_id.try(:zero?) #为 0 的时候设置为 nil + @updated_issue.time_scale = @time_scale unless @time_scale.nil? + @updated_issue.project_id = @project_id unless @project_id.nil? + @updated_issue.updated_on = Time.now + @updated_issue.changer_id = @current_user.id + @updated_issue.save! + + build_after_issue_journal_details if @updated_issue.previous_changes.present? # 操作记录 + build_previous_issue_changes + build_cirle_blockchain_token if blockchain_token_num.present? + unless @project.id.zero? + # @信息发送 + AtmeService.call(current_user, @atme_receivers, @issue) unless receivers_login.blank? + # 消息发送 + if Site.has_notice_menu? + SendTemplateMessageJob.perform_later('IssueChanged', current_user.id, @issue&.id, previous_issue_changes) unless previous_issue_changes.blank? + SendTemplateMessageJob.perform_later('IssueAssigned', current_user.id, @issue&.id, add_assigner_ids) unless add_assigner_ids.blank? + end + # 触发webhook + Rails.logger.info "################### 触发webhook" + TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueUpdate', @updated_issue&.id, current_user.id, previous_issue_changes.except(:issue_tags_value, :assigned_to_id)) + TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueLabel', @issue&.id, current_user.id, {issue_tag_ids: [before_issue_tag_ids, issue_tag_ids]}) unless issue_tag_ids.nil? + TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueAssign', @issue&.id, current_user.id, {assigner_ids: [before_assigner_ids, assigner_ids]}) unless assigner_ids.nil? + end + + unlock("Api::Pm::Issues::UpdateService:#{project.id}:#{issue.id}") + return @updated_issue + end + end + + private + + def issue_load_attributes + if current_user.id == @updated_issue.author_id && !PullAttachedIssue.exists?(issue_id: @updated_issue, fixed: true) + @updated_issue.blockchain_token_num = blockchain_token_num unless blockchain_token_num.nil? + end + @updated_issue.status_id = status_id if status_id.present? + @updated_issue.priority_id = priority_id if priority_id.present? + @updated_issue.fixed_version_id = milestone_id unless milestone_id.nil? + @updated_issue.branch_name = branch_name unless branch_name.nil? + @updated_issue.start_date = start_date unless start_date.nil? + @updated_issue.due_date = due_date unless due_date.nil? + @updated_issue.subject = subject if subject.present? + @updated_issue.description = description unless description.nil? + end + + def build_assigner_participants + if assigner_ids.blank? + @updated_issue.issue_participants.where(participant_type: "assigned").each(&:destroy!) + else + @updated_issue.issue_participants.where(participant_type: "assigned").where.not(participant_id: assigner_ids).each(&:destroy!) + assigner_ids.each do |aid| + next if @updated_issue.issue_participants.exists?(participant_type: "assigned", participant_id: aid) + @updated_issue.issue_participants.new({participant_type: "assigned", participant_id: aid}) + @add_assigner_ids << aid + end + end + end + + def build_edit_participants + @updated_issue.issue_participants.new({participant_type: "edited", participant_id: current_user.id}) unless @updated_issue.issue_participants.exists?(participant_type: "edited", participant_id: current_user.id) + end + + def build_atme_participants + @atme_receivers.each do |receiver| + next if @updated_issue.issue_participants.exists?(participant_type: "atme", participant_id: receiver.id) + @updated_issue.issue_participants.new({participant_type: "atme", participant_id: receiver.id}) + end + end + + def build_previous_issue_changes + @previous_issue_changes.merge!(@updated_issue.previous_changes.slice("status_id", "priority_id", "fixed_version_id", "issue_tags_value", "branch_name", "subject").symbolize_keys) + if @updated_issue.previous_changes[:start_date].present? + @previous_issue_changes.merge!(start_date: [@updated_issue.previous_changes[:start_date][0].to_s, @updated_issue.previous_changes[:start_date][1].to_s]) + end + if @updated_issue.previous_changes[:due_date].present? + @previous_issue_changes.merge!(due_date: [@updated_issue.previous_changes[:due_date][0].to_s, @updated_issue.previous_changes[:due_date][1].to_s]) + end + end + + def build_cirle_blockchain_token + if @updated_issue.previous_changes["blockchain_token_num"].present? + unlock_balance_on_blockchain(@updated_issue&.author_id.to_s, @updated_issue.project_id.to_s, @updated_issue.previous_changes["blockchain_token_num"][0].to_i) if @updated_issue.previous_changes["blockchain_token_num"][0].present? + lock_balance_on_blockchain(@updated_issue&.author_id.to_s, @updated_issue.project_id.to_s, @updated_issue.previous_changes["blockchain_token_num"][1].to_i) if @updated_issue.previous_changes["blockchain_token_num"][1].present? + end + end + + def build_issue_project_trends + if @updated_issue.previous_changes["status_id"].present? && @updated_issue.previous_changes["status_id"][1] == 5 + @updated_issue.project_trends.new({user_id: current_user.id, project_id: @project.id, action_type: ProjectTrend::CLOSE}) + end + if @updated_issue.previous_changes["status_id"].present? && @updated_issue.previous_changes["status_id"][0] == 5 + @updated_issue.project_trends.where(action_type: ProjectTrend::CLOSE).each(&:destroy!) + end + end + + def build_after_issue_journal_details + begin + # 更改标题 + if @updated_issue.previous_changes["subject"].present? + journal = @updated_issue.journals.create!({user_id: current_user.id}) + journal.journal_details.create!({property: "attr", prop_key: "subject", old_value: @updated_issue.previous_changes["subject"][0], value: @updated_issue.previous_changes["subject"][1]}) + end + + # 更改描述 + if @updated_issue.previous_changes["description"].present? + journal = @updated_issue.journals.create!({user_id: current_user.id}) + journal.journal_details.create!({property: "attr", prop_key: "description", old_value: @updated_issue.previous_changes["description"][0], value: @updated_issue.previous_changes["description"][1]}) + end + + # 修改状态 + if @updated_issue.previous_changes["status_id"].present? + journal = @updated_issue.journals.create!({user_id: current_user.id}) + journal.journal_details.create!({property: "attr", prop_key: "status_id", old_value: @updated_issue.previous_changes["status_id"][0], value: @updated_issue.previous_changes["status_id"][1]}) + end + + # 修改优先级 + if @updated_issue.previous_changes["priority_id"].present? + journal = @updated_issue.journals.create!({user_id: current_user.id}) + journal.journal_details.create!({property: "attr", prop_key: "priority_id", old_value: @updated_issue.previous_changes["priority_id"][0], value: @updated_issue.previous_changes["priority_id"][1]}) + end + + # 修改里程碑 + if @updated_issue.previous_changes["fixed_version_id"].present? + journal = @updated_issue.journals.create!({user_id: current_user.id}) + journal.journal_details.create!({property: "attr", prop_key: "fixed_version_id", old_value: @updated_issue.previous_changes["fixed_version_id"][0], value: @updated_issue.previous_changes["fixed_version_id"][1]}) + end + + # 更改分支 + if @updated_issue.previous_changes["branch_name"].present? + journal = @updated_issue.journals.create!({user_id: current_user.id}) + journal.journal_details.create!({property: "attr", prop_key: "branch_name", old_value: @updated_issue.previous_changes["branch_name"][0], value: @updated_issue.previous_changes["branch_name"][1]}) + end + + # 更改开始时间 + if @updated_issue.previous_changes["start_date"].present? + journal = @updated_issue.journals.create!({user_id: current_user.id}) + journal.journal_details.create!({property: "attr", prop_key: "start_date", old_value: @updated_issue.previous_changes["start_date"][0], value: @updated_issue.previous_changes["start_date"][1]}) + end + + # 更改结束时间 + if @updated_issue.previous_changes["due_date"].present? + journal = @updated_issue.journals.create!({user_id: current_user.id}) + journal.journal_details.create!({property: "attr", prop_key: "due_date", old_value: @updated_issue.previous_changes["due_date"][0], value: @updated_issue.previous_changes["due_date"][1]}) + end + rescue + raise Error, "创建操作记录失败!" + end + end + + def build_assigner_issue_journal_details + begin + # 更改负责人 + new_assigner_ids = @assigner_ids + new_assigner_ids = [] if @assigner_ids.nil? + now_assigner_ids = @updated_issue.assigners.pluck(:id) + if !(now_assigner_ids & assigner_ids).empty? || !(now_assigner_ids.empty? && new_assigner_ids.empty?) + journal = @updated_issue.journals.create!({user_id: current_user.id}) + journal.journal_details.create!({property: "assigner", prop_key: "#{new_assigner_ids.size}", old_value: now_assigner_ids.join(","), value: new_assigner_ids.join(",")}) + end + + rescue + raise Error, "创建操作记录失败!" + end + end + + def build_issue_tag_issue_journal_details + begin + # 更改标记 + new_issue_tag_ids = @issue_tag_ids + new_issue_tag_ids = [] if @issue_tag_ids.nil? + now_issue_tag_ids = @updated_issue.issue_tags.pluck(:id) + if !(now_issue_tag_ids & new_issue_tag_ids).empty? || !(now_issue_tag_ids.empty? && new_issue_tag_ids.empty?) + journal = @updated_issue.journals.create!({user_id: current_user.id}) + journal.journal_details.create!({property: "issue_tag", prop_key: "#{new_issue_tag_ids.size}", old_value: now_issue_tag_ids.join(","), value: new_issue_tag_ids.join(",")}) + end + rescue + raise Error, "创建操作记录失败!" + end + end + + + def build_attachment_issue_journal_details + begin + # 更改附件 + new_attachment_ids = @attachment_ids + new_attachment_ids = [] if @attachment_ids.nil? + now_attachment_ids = @updated_issue.attachments.pluck(:id) + if !(now_attachment_ids & new_attachment_ids).empty? || !(now_attachment_ids.empty? && new_attachment_ids.empty?) + journal = @updated_issue.journals.create!({user_id: current_user.id}) + journal.journal_details.create!({property: "attachment", prop_key: "#{new_attachment_ids.size}", old_value: now_attachment_ids.join(","), value: new_attachment_ids.join(",")}) + end + rescue + raise Error, "创建操作记录失败!" + end + end + +end \ No newline at end of file diff --git a/app/views/api/pm/issues/journals/_detail.json.jbuilder b/app/views/api/pm/issues/journals/_detail.json.jbuilder new file mode 100644 index 000000000..58a7ed8fd --- /dev/null +++ b/app/views/api/pm/issues/journals/_detail.json.jbuilder @@ -0,0 +1,25 @@ +json.id journal.id +json.is_journal_detail journal.is_journal_detail? +json.created_at journal.created_on.strftime("%Y-%m-%d %H:%M") +json.updated_at journal.updated_on.strftime("%Y-%m-%d %H:%M") +json.user do + if journal.user.present? + json.partial! "api/v1/users/simple_user", user: journal.user + else + json.nil! + end +end +if journal.is_journal_detail? + detail = journal.journal_details.take + json.operate_category detail.property == "attr" ? detail.prop_key : detail.property + json.operate_content journal.is_journal_detail? ? journal.pm_operate_content : nil +else + json.notes journal.notes + json.comments_count journal.comments_count + json.children_journals journal.first_ten_children_journals.each do |journal| + json.partial! "api/v1/issues/journals/children_detail", journal: journal + end + json.attachments journal.attachments do |attachment| + json.partial! "api/v1/attachments/simple_detail", locals: {attachment: attachment} + end +end diff --git a/app/views/api/pm/issues/journals/index.json.jbuilder b/app/views/api/pm/issues/journals/index.json.jbuilder new file mode 100644 index 000000000..0ca2a50e6 --- /dev/null +++ b/app/views/api/pm/issues/journals/index.json.jbuilder @@ -0,0 +1,8 @@ +json.total_journals_count @total_journals_count +json.total_operate_journals_count @total_operate_journals_count +json.total_comment_journals_count @total_comment_journals_count +json.total_count @journals.total_count +json.journals @journals do |journal| + journal.associate_attachment_container + json.partial! "api/pm/issues/journals/detail", journal: journal +end \ No newline at end of file