181 lines
8.8 KiB
Ruby
181 lines
8.8 KiB
Ruby
class Api::V1::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
|
|
attr_reader :issue_tag_ids, :assigner_ids, :attachment_ids, :receivers_login
|
|
attr_accessor :add_assigner_ids, :previous_issue_changes, :updated_issue
|
|
|
|
validates :project, :issue, :current_user, presence: 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]
|
|
@issue_tag_ids = params[:issue_tag_ids]
|
|
@assigner_ids = params[:assigner_ids]
|
|
@attachment_ids = params[:attachment_ids]
|
|
@receivers_login = params[:receivers_login]
|
|
@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_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?
|
|
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
|
|
@updated_issue = @issue
|
|
@updated_issue.load_attributes
|
|
build_issue_journal_details if @updated_issue.previous_changes.present? # 操作记录
|
|
build_issue_project_trends if status_id.present? # 开关时间记录
|
|
build_assigner_participants unless assigner_ids.blank? # 负责人
|
|
@updated_issue.assigners = @assigners unless assigner_ids.blank?
|
|
@updated_issue.attachments = @attachments unless attachment_ids.blank?
|
|
@updated_issue.issue_tags = @issue_tags unless issue_tag_ids.blank?
|
|
|
|
@updated_issue.save!
|
|
|
|
build_previous_issue_changes
|
|
|
|
# @信息发送
|
|
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_changes) unless previous_issue_changes.blank?
|
|
SendTemplateMessageJob.perform_later('IssueAssigned', current_user.id, @issue&.id, add_assigner_ids) unless add_assigner_ids.blank?
|
|
end
|
|
|
|
unlock
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def build_assigner_participants
|
|
@updated_issue.issue_participants.where.not(id: assigner_ids).each(&:destroy!)
|
|
assigner_ids.each do |aid|
|
|
next if @updated_issue.issue_participants.exists?(participant_id: aid)
|
|
@updated_issue.issue_participants.new({participant_type: "assigned", participant_id: aid})
|
|
@add_assigner_ids << aid
|
|
end
|
|
end
|
|
|
|
def build_previous_issue_changes
|
|
@previous_issue_changes = @updated_issue.previous_changes
|
|
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_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_issue_journal_details
|
|
# 更改标题
|
|
if @updated_issue.previous_changes[:subject].present?
|
|
journal = @updated_issue.journals.new({user_id: current_user.id})
|
|
journal.journal_details.new({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.new({user_id: current_user.id})
|
|
journal.journal_details.new({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.new({user_id: current_user.id})
|
|
journal.journal_details.new({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.new({user_id: current_user.id})
|
|
journal.journal_details.new({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.new({user_id: current_user.id})
|
|
journal.journal_details.new({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.new({user_id: current_user.id})
|
|
journal.journal_details.new({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.new({user_id: current_user.id})
|
|
journal.journal_details.new({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.new({user_id: current_user.id})
|
|
journal.journal_details.new({property: "attr", prop_key: "due_date", old_value: @updated_issue.previous_changes[:due_date][0], value: @updated_issue.previous_changes[:due_date][1]})
|
|
end
|
|
|
|
# 更改负责人
|
|
if !@updated_issue.assigners.pluck(:id).sort! == assigner_ids.sort!
|
|
journal = @updated_issue.journals.new({user_id: current_user.id})
|
|
journal.journal_details.new({property: "assigner", prop_key: "#{assigner_ids.size}", old_value: @updated_issue.assigners.pluck(:nickname).join(","), value: @assigners.pluck(:nickname).join(",")})
|
|
end
|
|
|
|
# 更改标记
|
|
if !@updated_issue.issue_tags.pluck(:id).sort! == issue_tag_ids.sort!
|
|
journal = @updated_issue.journals.new({user_id: current_user.id})
|
|
journal.journal_details.new({property: "issue_tag", prop_key: "#{issue_tag_ids.size}", old_value: @updated_issue.issue_tags.pluck(:name).join(","), value: @issue_tags.pluck(:name).join(",")})
|
|
end
|
|
|
|
# 更改附件
|
|
if !@updated_issue.attachments.pluck(:id).sort! == attachment_ids.sort!
|
|
journal = @updated_issue.journals.new({user_id: current_user.id})
|
|
journal.journal_details.new({property: "attachment", prop_key: "#{attachment_ids.size}", old_value: @updated_issue.attachments.pluck(:filename).join(","), value: @attachments.pluck(:filename).join(",")})
|
|
end
|
|
end
|
|
|
|
def try_lock
|
|
raise Error, "请稍后再试!" unless $redis_cache.set("Api::V1::Issues::UpdateService:#{project.id}:#{issue.id}", 1, nx: true, ex: 60.seconds)
|
|
end
|
|
|
|
def unlock
|
|
$redis_cache.del("Api::V1::Issues::UpdateService:#{project.id}:#{issue.id}")
|
|
end
|
|
|
|
|
|
end |