新增:创建操作记录逻辑

This commit is contained in:
yystopf 2024-08-21 16:05:45 +08:00
parent 69b5cae870
commit 91399808c4
6 changed files with 188 additions and 85 deletions

View File

@ -6,16 +6,40 @@ class Api::Pm::IssueLinksController < Api::Pm::BaseController
end
def create
params[:link_ids].map { |e| @issue.pm_links.find_or_create_by(be_linkable_type: 'Issue', be_linkable_id: e) }
render_ok
begin
ActiveRecord::Base.transaction do
params[:link_ids].each do |e|
@issue.pm_links.find_or_create_by!(be_linkable_type: 'Issue', be_linkable_id: e)
tag_issue = Issue.find_by_id(e)
next unless tag_issue.present?
journal = tag_issue.journals.create!({user_id: current_id.id})
journal.journal_details.create!({property: "tag_link_issue", prop_key: "1", value: @issue.id.to_s})
end
journal = @issue.journals.create!({user_id: current_user.id})
journal.journal_details.create!({property: "tag_link_issue", prop_key: "#{params[:link_ids].size}", value: params[:link_ids].join(",")})
end
render_ok
rescue
render_error('创建失败!')
end
end
def destroy
@links = PmLink.where(be_linkable_id: @issue.id, be_linkable_type: 'Issue', linkable_id: params[:id], linkable_type: 'Issue').or(PmLink.where(linkable_id: @issue.id, linkable_type: 'Issue', be_linkable_id: params[:id], be_linkable_type: 'Issue'))
@link = @links.last
if @link.try(:destroy)
begin
ActiveRecord::Base.transaction do
@links = PmLink.where(be_linkable_id: @issue.id, be_linkable_type: 'Issue', linkable_id: params[:id], linkable_type: 'Issue').or(PmLink.where(linkable_id: @issue.id, linkable_type: 'Issue', be_linkable_id: params[:id], be_linkable_type: 'Issue'))
journal = @issue.journals.create!({user_id: current_user.id})
journal.journal_details.create!({property: "tag_link_issue", prop_key: "1", old_value: params[:id].to_s})
another_issue = Issue.find_by_id(params[:id])
if another_issue.present?
journal = another_issue.journals.create!({user_id: current_user.id})
journal.journal_details.create!({property: "tag_link_issue", prop_key: "1", old_value: @issue.id.to_s})
end
@link = @links.last
@link.destroy!
end
render_ok
else
rescue
render_error('删除失败!')
end
end

View File

@ -109,6 +109,20 @@ class Issue < ApplicationRecord
after_save :incre_or_decre_closed_issues_count, :change_versions_count, :send_update_message_to_notice_system, :associate_attachment_container, :generate_uuid
after_destroy :update_closed_issues_count_in_project!, :decre_project_common, :decre_user_statistic, :decre_platform_statistic, :destroy_be_pm_links
def pm_issue_type_string
case pm_issue_type
when 1
"requirement"
when 2
"task"
when 3
"bug"
else
"issue"
end
end
def destroy_be_pm_links
PmLink.where(be_linkable_type:"Issue",be_linkable_id:self.id).map(&:destroy)
end

View File

@ -114,8 +114,8 @@ class Journal < ApplicationRecord
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
old_value = "<b>#{Issue.find_by_id(detail.old_value)&.subject}</b>"
new_value = "<b>#{Issue.find_by_id(detail.value)&.subject}</b>"
if old_value.nil? || old_value.blank?
content += "关联了父需求<b><#{new_value}></b>"
else
@ -127,28 +127,28 @@ class Journal < ApplicationRecord
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("")
old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("")
new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("")
if old_value.nil? || old_value.blank?
content += "新建了子需求<b>#{new_value}</b>"
content += "新建了子需求#{new_value}"
else
if new_value.nil? || new_value.blank?
content += "删除了关联的子需求<b>#{old_value}</b>"
content += "删除了关联的子需求#{old_value}"
else
content += "新建了子需求<b>#{new_value}</b>"
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("")
old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("")
new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("")
if old_value.nil? || old_value.blank?
content += "关联了子需求<b>#{new_value}</b>"
content += "关联了子需求#{new_value}"
else
if new_value.nil? || new_value.blank?
content += "取消了关联的子需求<b>#{old_value}</b>"
content += "取消了关联的子需求#{old_value}"
else
content += "关联了子需求<b>#{new_value}</b>"
content += "关联了子需求#{new_value}"
end
end
return content
@ -174,8 +174,8 @@ class Journal < ApplicationRecord
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
old_value = "<b>#{Issue.find_by_id(detail.old_value)&.subject}</b>"
new_value = "<b>#{Issue.find_by_id(detail.value)&.subject}</b>"
if old_value.nil? || old_value.blank?
content += "关联了父任务<b><#{new_value}></b>"
else
@ -187,28 +187,28 @@ class Journal < ApplicationRecord
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("")
old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("")
new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("")
if old_value.nil? || old_value.blank?
content += "新建了子任务<b>#{new_value}</b>"
content += "新建了子任务#{new_value}"
else
if new_value.nil? || new_value.blank?
content += "删除了关联的子任务<b>#{old_value}</b>"
content += "删除了关联的子任务#{old_value}"
else
content += "新建了子任务<b>#{new_value}</b>"
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("")
old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("")
new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("")
if old_value.nil? || old_value.blank?
content += "关联了子任务<b>#{new_value}</b>"
content += "关联了子任务#{new_value}"
else
if new_value.nil? || new_value.blank?
content += "取消了关联的子任务<b>#{old_value}</b>"
content += "取消了关联的子任务#{old_value}"
else
content += "关联了子任务<b>#{new_value}</b>"
content += "关联了子任务#{new_value}"
end
end
return content
@ -234,8 +234,8 @@ class Journal < ApplicationRecord
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
old_value = "<b>#{Issue.find_by_id(detail.old_value)&.subject}</b>"
new_value = "<b>#{Issue.find_by_id(detail.value)&.subject}</b>"
if old_value.nil? || old_value.blank?
content += "关联了父缺陷<b><#{new_value}></b>"
else
@ -247,28 +247,28 @@ class Journal < ApplicationRecord
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("")
old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("")
new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("")
if old_value.nil? || old_value.blank?
content += "新建了子缺陷<b>#{new_value}</b>"
content += "新建了子缺陷#{new_value}"
else
if new_value.nil? || new_value.blank?
content += "删除了关联的子缺陷<b>#{old_value}</b>"
content += "删除了关联的子缺陷#{old_value}"
else
content += "新建了子缺陷<b>#{new_value}</b>"
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("")
old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("")
new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<b><#{i.subject}></b>"}.join("")
if old_value.nil? || old_value.blank?
content += "关联了子缺陷<b>#{new_value}</b>"
content += "关联了子缺陷#{new_value}"
else
if new_value.nil? || new_value.blank?
content += "取消了关联的子缺陷<b>#{old_value}</b>"
content += "取消了关联的子缺陷#{old_value}"
else
content += "关联了子缺陷<b>#{new_value}</b>"
content += "关联了子缺陷#{new_value}"
end
end
return content
@ -429,41 +429,41 @@ class Journal < ApplicationRecord
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("")
old_value = User.where(id: detail.old_value.split(",")).map{|u| "<b>#{u.real_name}</b>"}.join("")
new_value = User.where(id: detail.value.split(",")).map{|u| "<b>#{u.real_name}</b>"}.join("")
if old_value.nil? || old_value.blank?
content += "添加负责人<b>#{new_value}</b>"
content += "添加负责人#{new_value}"
else
if new_value.nil? || new_value.blank?
content += "将负责人更改为<b>未设置</b>"
else
content += "将负责人由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
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("")
old_value = IssueTag.where(id: detail.old_value.split(",")).map{|t| "<b>#{t.name}</b>"}.join("")
new_value = IssueTag.where(id: detail.value.split(",")).map{|t| "<b>#{t.name}</b>"}.join("")
if old_value.nil? || old_value.blank?
content += "添加标记<b>#{new_value}</b>"
content += "添加标记#{new_value}"
else
if new_value.nil? || new_value.blank?
content += "将标记更改为<b>未设置</b>"
else
content += "将标记由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
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("")
old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<b>[#{i.pm_issue_type}]-<#{i.subject}></b>"}.join("")
new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<b>[#{i.pm_issue_type}]-<#{i.subject}></b>"}.join("")
if old_value.nil? || old_value.blank?
content += "关联了工作项<b>#{new_value}</b>"
content += "新建了关联的工作项#{new_value}"
else
if new_value.nil? || new_value.blank?
content += "取消了关联的工作项<b>#{old_value}</b>"
content += "删除了关联的工作项#{old_value}"
else
content += "关联了工作项<b>#{new_value}</b>"
content += "新建了关联的工作项#{new_value}"
end
end
content.gsub!('1', "需求")
@ -471,17 +471,20 @@ class Journal < ApplicationRecord
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("")
old_value = Issue.where(id: detail.old_value.to_s.split(",")).map{|i| "<b>[#{i.pm_issue_type}]-<#{i.subject}></b>"}.join("")
new_value = Issue.where(id: detail.value.to_s.split(",")).map{|i| "<b>[#{i.pm_issue_type}]-<#{i.subject}></b>"}.join("")
if old_value.nil? || old_value.blank?
content += "新建了关联的工作项<b>#{new_value}</b>"
content += "关联了工作项#{new_value}"
else
if new_value.nil? || new_value.blank?
content += "删除了关联的工作项<b>#{old_value}</b>"
content += "取消了关联的工作项#{old_value}"
else
content += "新建了关联的工作项<b>#{new_value}</b>"
content += "关联了工作项#{new_value}"
end
end
content.gsub!('1', "需求")
content.gsub!('2', "任务")
content.gsub!('3', "缺陷")
return content
when 'issue'
issue = self.issue

View File

@ -55,16 +55,6 @@ class Api::Pm::Issues::CreateService < ApplicationService
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)
@ -75,12 +65,35 @@ class Api::Pm::Issues::CreateService < ApplicationService
else
@created_issue.root_id = @root_id
end
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.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 @created_issue.parent_issue.present?
parent_issue = @created_issue.parent_issue
if @created_issue.root_id.present? && parent_issue.present?
journal = parent_issue.journals.create!({user_id: current_user.id})
journal.journal_details.create!({property: @created_issue.pm_issue_type_string, prop_key: 'leaf_issue', value: @created_issue.id.to_s})
end
end
if @linkable_id.present?
PmLink.create(be_linkable_type: 'Issue', be_linkable_id: @created_issue.id, linkable_type: 'Issue', linkable_id: @linkable_id)
another_issue = Issue.find_by_id(@linkable_id)
if another_issue.present?
journal = another_issue.journals.create!({user_id: @current_user.id})
journal.journal_details.create!({property: 'link_issue', prop_key: "1", value: @created_issue.id.to_s})
end
end
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})
@ -163,6 +176,6 @@ class Api::Pm::Issues::CreateService < ApplicationService
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: ''})
journal.journal_details.new({property: @created_issue.pm_issue_type_string, prop_key: 1, old_value: '', value: ''})
end
end

View File

@ -14,21 +14,27 @@ class Api::Pm::Issues::DeleteService < ApplicationService
def call
raise Error, errors.full_messages.join(", ") unless valid?
try_lock("Api::V1::Issues::DeleteService:#{project.id}") # 开始写数据,加锁
ActiveRecord::Base.transaction do
parent_issue = @issue.parent_issue
if @issue.root_id.present? && parent_issue.present?
journal = parent_issue.journals.create!({user_id: current_user.id})
journal.journal_details.create!({property: @issue.pm_issue_type_string, prop_key: 'leaf_issue', old_value: @issue.id.to_s})
end
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)
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
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?
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
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

View File

@ -196,7 +196,7 @@ class Api::Pm::Issues::UpdateService < ApplicationService
# 修改状态
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]})
journal.journal_details.create!({property: @updated_issue.pm_issue_type_string, prop_key: "status_id", old_value: @updated_issue.previous_changes["status_id"][0], value: @updated_issue.previous_changes["status_id"][1]})
end
# 修改优先级
@ -205,6 +205,24 @@ class Api::Pm::Issues::UpdateService < ApplicationService
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["pm_issue_type"].present?
journal = @updated_issue.journals.create!({user_id: current_user.id})
journal.journal_details.create!({property: "attr", prop_key: "pm_issue_type", old_value: @updated_issue.previous_changes["pm_issue_type"][0], value: @updated_issue.previous_changes["pm_issue_type"][1]})
end
# 修改迭代
if @updated_issue.previous_changes["pm_sprint_id"].present?
journal = @updated_issue.journals.create!({user_id: current_user.id})
journal.journal_details.create!({property: "attr", prop_key: "pm_sprint_id", old_value: @updated_issue.previous_changes["pm_sprint_id"][0], value: @updated_issue.previous_changes["pm_sprint_id"][1]})
end
# 修改代码库
if @updated_issue.previous_changes["project_id"].present?
journal = @updated_issue.journals.create!({user_id: current_user.id})
journal.journal_details.create!({property: "attr", prop_key: "project_id", old_value: @updated_issue.previous_changes["project_id"][0], value: @updated_issue.previous_changes["project_id"][1]})
end
# 修改里程碑
if @updated_issue.previous_changes["fixed_version_id"].present?
journal = @updated_issue.journals.create!({user_id: current_user.id})
@ -228,6 +246,31 @@ class Api::Pm::Issues::UpdateService < ApplicationService
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
# 更改预估工时
if @updated_issue.previous_changes["time_scale"].present?
journal = @updated_issue.journals.create!({user_id: current_user.id})
journal.journal_details.create!({property: "attr", prop_key: "time_scale", old_value: @updated_issue.previous_changes["time_scale"][0], value: @updated_issue.previous_changes["time_scale"][1]})
end
# 更改父工作项
if @updated_issue.previous_changes["root_id"].present?
journal = @updated_issue.journals.create!({user_id: current_user.id})
journal.journal_details.create!({property: @updated_issue.pm_issue_type_string, prop_key: "root_id", old_value: @updated_issue.previous_changes["root_id"][0], value: @updated_issue.previous_changes["root_id"][1]})
# 更改子工作项
before_parent_issue = Issue.find_by_id(@updated_issue.previous_changes["root_id"][0])
if before_parent_issue.present?
journal = before_parent_issue.journals.create!({user_id: current_user.id})
journal.journal_details.create!({property: @updated_issue.pm_issue_type_string, prop_key: "tag_leaf_issue", old_value: @updated_issue.previous_changes["root_id"][0]})
end
after_parent_issue = Issue.find_by_id(@updated_issue.previous_changes["root_id"][1])
if after_parent_issue.present?
journal = after_parent_issue.journals.create!({user_id: current_user.id})
journal.journal_details.create!({property: @updated_issue.pm_issue_type_string, prop_key: "tag_leaf_issue", value: @updated_issue.previous_changes["root_id"][1]})
end
end
rescue
raise Error, "创建操作记录失败!"
end