新增:疑修操作记录、评论列表接口以及更新疑修无法产生操作记录修复

This commit is contained in:
yystopf 2023-02-17 16:11:39 +08:00
parent 2a8e0d2be8
commit b85913e485
17 changed files with 448 additions and 141 deletions

View File

@ -0,0 +1,39 @@
class Api::V1::Issues::JournalsController < Api::V1::IssuesController
before_action :require_public_and_member_above, only: [:index, :create, :destroy]
before_action :load_issue, only: [:index, :create, :destroy]
before_action :load_journal, only: [:destroy]
def index
@object_results = Api::V1::Issues::Journals::ListService.call(@issue, query_params, current_user)
@journals = kaminari_paginate(@object_results)
end
def create
@object_result = Api::V1::Issues::Journals::CreateService.call(@issue, journal_params, current_user)
end
def destroy
if @journal.destroy!
render_ok
else
render_error("删除评论失败!")
end
end
private
def query_params
params.permit(:category, :keyword, :sort_by, :sort_direction)
end
def journal_params
params.permit(:notes, :parent_id, :attachment_ids => [])
end
def load_journal
@journal = @issue.journals.find_by_id(params[:id])
return render_not_found("评论不存在!") unless @journal.present?
end
end

View File

@ -4,6 +4,9 @@ class Api::V1::IssuesController < Api::V1::BaseController
def index
@object_results = Api::V1::Issues::ListService.call(@project, query_params, current_user)
@opened_issues_count = @object_results.opened.size
@closed_issues_count = @object_results.closed.size
@issues = kaminari_paginate(@object_results)
end
@ -49,6 +52,31 @@ class Api::V1::IssuesController < Api::V1::BaseController
end
end
protected
def load_issue
@issue = @project.issues.where(project_issues_index: params[:id]).where.not(id: params[:id]).take || Issue.find_by_id(params[:id])
if @issue.blank?
render_not_found("疑修不存在!")
elsif @issue.present? && @issue.is_lock &&!(@project.member?(current_user) || current_user.admin?)
render_forbidden("您没有权限操作!")
end
end
def load_issues
return render_error("请输入正确的ID数组") unless params[:ids].is_a?(Array)
params[:ids].each do |id|
@issue = Issue.find_by_id(id)
if @issue.blank?
return render_not_found("ID为#{id}的疑修不存在!")
elsif @issue.present? && @issue.is_lock &&!(@project.member?(current_user) || current_user.admin?)
return render_forbidden("ID为#{id}的疑修您没有权限操作!")
end
end
@issues = Issue.where(id: params[:ids])
end
private
def query_params
@ -79,27 +107,4 @@ class Api::V1::IssuesController < Api::V1::BaseController
:issue_tag_ids => [],
:assigner_ids => [])
end
def load_issue
@issue = @project.issues.where(project_issues_index: params[:id]).where.not(id: params[:id]).take || Issue.find_by_id(params[:id])
if @issue.blank?
render_not_found("疑修不存在!")
elsif @issue.present? && @issue.is_lock &&!(@project.member?(current_user) || current_user.admin?)
render_forbidden("您没有权限操作!")
end
end
def load_issues
return render_error("请输入正确的ID数组") unless params[:ids].is_a?(Array)
params[:ids].each do |id|
@issue = Issue.find_by_id(id)
if @issue.blank?
return render_not_found("ID为#{id}的疑修不存在!")
elsif @issue.present? && @issue.is_lock &&!(@project.member?(current_user) || current_user.admin?)
return render_forbidden("ID为#{id}的疑修您没有权限操作!")
end
end
@issues = Issue.where(id: params[:ids])
end
end

View File

@ -40,8 +40,11 @@ class Journal < ApplicationRecord
belongs_to :journalized, polymorphic: true
belongs_to :review, optional: true
belongs_to :resolveer, class_name: 'User', foreign_key: :resolveer_id, optional: true
belongs_to :parent_journal, class_name: 'Journal', foreign_key: :parent_id, optional: true
belongs_to :reply_journal, class_name: 'Journal', foreign_key: :reply_id, optional: true
has_many :journal_details, :dependent => :delete_all
has_many :attachments, as: :container, dependent: :destroy
has_many :first_ten_children_journals, -> { order(created_on: :asc).limit(10)}, class_name: 'Journal', foreign_key: :parent_id
has_many :children_journals, class_name: 'Journal', foreign_key: :parent_id
scope :journal_includes, ->{includes(:user, :journal_details, :attachments)}
@ -54,6 +57,81 @@ class Journal < ApplicationRecord
self.notes.blank? && self.journal_details.present?
end
def operate_content
content = ""
detail = self.journal_details.take
case detail.property
when 'issue'
return "创建了<b>疑修</b>"
when 'attachment'
old_value = Attachment.where(id: detail.old_value.split(",")).pluck(:filename).join("")
new_value = Attachment.where(id: detail.value.split(",")).pluck(:filename).join("")
if old_value.nil? || old_value.blank?
content += "添加了<b>#{new_value}</b>附件"
else
new_value = "" if new_value.blank?
content += "将附件由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
end
when 'issue_tag'
old_value = IssueTag.where(id: detail.old_value.split(",")).pluck(:name, :color).map{|t| "<span style='color: #{t[1]}'>#{t[0]}</span>"}.join(" ")
new_value = IssueTag.where(id: detail.value.split(",")).pluck(:name, :color).map{|t| "<span style='color: #{t[1]}'>#{t[0]}</span>"}.join(" ")
if old_value.nil? || old_value.blank?
content += "添加了<b>#{new_value}</b>标记"
else
new_value = "" if new_value.blank?
content += "将标记由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
end
when 'assigner'
old_value = User.where(id: detail.old_value.split(",")).pluck(:nickname).join("")
new_value = User.where(id: detail.value.split(",")).pluck(:nickname).join("")
if old_value.nil? || old_value.blank?
content += "添加负责人<b>#{new_value}</b>"
else
new_value = "" if new_value.blank?
content += "将负责人由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
end
when 'attr'
content = ""
case detail.prop_key
when 'subject'
return "修改了<b>标题</b>"
when 'description'
return "修改了<b>描述</b>"
when 'status_id'
old_value = IssueStatus.find_by_id(detail.old_value)&.name
new_value = IssueStatus.find_by_id(detail.value)&.name
content += "状态"
when 'priority_id'
old_value = IssuePriority.find_by_id(detail.old_value)&.name
new_value = IssuePriority.find_by_id(detail.value)&.name
content += "优先级"
when 'fixed_version_id'
old_value = Version.find_by_id(detail.old_value)&.name
new_value = Version.find_by_id(detail.value)&.name
content += "里程碑"
when 'branch_name'
old_value = detail.old_value
new_value = detail.value
content += "关联分支"
when 'start_date'
old_value = detail.old_value
new_value = detail.value
content += "开始日期"
when 'due_date'
old_value = detail.old_value
new_value = detail.value
content += "结束日期"
end
if old_value.nil? || old_value.blank?
content += "设置为<b>#{new_value}</b>"
else
new_value = "" if new_value.blank?
content += "由<b>#{old_value}</b>更改为<b>#{new_value}</b>"
end
end
end
def journal_content
send_details = []
if self.is_journal_detail?

View File

@ -13,7 +13,7 @@ class Api::V1::Issues::BatchDeleteService < ApplicationService
def call
raise Error, errors.full_messages.join(", ") unless valid?
try_lock # 开始写数据,加锁
try_lock("Api::V1::Issues::DeleteService:#{project.id}") # 开始写数据,加锁
delete_issues
@ -25,7 +25,7 @@ class Api::V1::Issues::BatchDeleteService < ApplicationService
end
end
unlock
unlock("Api::V1::Issues::DeleteService:#{project.id}")
return true
end
@ -35,13 +35,4 @@ class Api::V1::Issues::BatchDeleteService < ApplicationService
def delete_issues
raise Error, "批量删除疑修失败!" unless @issues.destroy_all
end
def try_lock
raise Error, "请稍后再试!" unless $redis_cache.set("Api::V1::Issues::BatchDeleteService:#{project.id}", 1, nx: true, ex: 60.seconds)
end
def unlock
$redis_cache.del("Api::V1::Issues::BatchDeleteService:#{project.id}")
end
end

View File

@ -43,10 +43,11 @@ class Api::V1::Issues::CreateService < ApplicationService
load_issue_tags(issue_tag_ids) unless issue_tag_ids.blank?
load_atme_receivers(receivers_login) unless receivers_login.blank?
try_lock # 开始写数据,加锁
try_lock("Api::V1::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.attachments = @attachments unless attachment_ids.blank?
@ -64,7 +65,7 @@ class Api::V1::Issues::CreateService < ApplicationService
SendTemplateMessageJob.perform_later('ProjectIssue', current_user.id, @issue&.id)
end
unlock # 结束写数据,解锁
unlock("Api::V1::Issues::CreateService:#{project.id}") # 结束写数据,解锁
end
return @created_issue
@ -104,6 +105,12 @@ class Api::V1::Issues::CreateService < ApplicationService
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
@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
@ -113,12 +120,4 @@ class Api::V1::Issues::CreateService < ApplicationService
journal = @created_issue.journals.new({user_id: current_user.id})
journal.journal_details.new({property: "issue", prop_key: 1, old_value: '', value: ''})
end
def try_lock
raise Error, "请稍后再试!" unless $redis_cache.set("Api::V1::Issues::CreateService:#{project.id}", 1, nx: true, ex: 60.seconds)
end
def unlock
$redis_cache.del("Api::V1::Issues::CreateService:#{project.id}")
end
end

View File

@ -13,7 +13,7 @@ class Api::V1::Issues::DeleteService < ApplicationService
def call
raise Error, errors.full_messages.join(", ") unless valid?
try_lock # 开始写数据,加锁
try_lock("Api::V1::Issues::DeleteService:#{project.id}") # 开始写数据,加锁
delete_issue
@ -23,7 +23,7 @@ class Api::V1::Issues::DeleteService < ApplicationService
SendTemplateMessageJob.perform_later('IssueDeleted', current_user.id, @issue&.subject, @issue.assigners.pluck(:id), @issue.author_id)
end
unlock
unlock("Api::V1::Issues::DeleteService:#{project.id}")
return true
end
@ -34,12 +34,4 @@ class Api::V1::Issues::DeleteService < ApplicationService
raise Error, "删除疑修失败!" unless issue.destroy!
end
def try_lock
raise Error, "请稍后再试!" unless $redis_cache.set("Api::V1::Issues::DeleteService:#{project.id}", 1, nx: true, ex: 60.seconds)
end
def unlock
$redis_cache.del("Api::V1::Issues::DeleteService:#{project.id}")
end
end

View File

@ -0,0 +1,48 @@
class Api::V1::Issues::Journals::CreateService < ApplicationService
include ActiveModel::Model
attr_reader :issue, :current_user, :notes, :parent_id, :attachment_ids
validates :issue, :current_user, presence: true
def initialize(issue, params, current_user=nil)
@issue = issue
@notes = params[:notes]
@parent_id = params[:parent_id]
@attachment_ids = params[:attachment_ids]
@current_user = current_user
end
def call
raise Error, errors.full_messages.join(", ") unless valid?
ActiveRecord::Base.transaction do
check_attachments(attachment_ids) unless attachment_ids.blank?
load_attachments(attachment_ids) unless attachment_ids.blank?
try_lock("Api::V1::Issues::Journals::CreateService:#{@issue.id}")
@created_journal = Journal.new(journal_attributes)
build_comment_participants
@created_journal.attachments = @attachments
@created_journal.save!
unlock("Api::V1::Issues::Journals::CreateService:#{@issue.id}")
end
end
private
def journal_attributes
journal_attributes = {
notes: notes
}
journal_attributes.merge!({parent_id: parent_id}) if parent_id.present?
journal_attributes
end
def build_comment_participants
@issue.issue_participants.new({participant_type: "commented", participant_id: current_user.id}) unless @issue.issue_participants.exists?(participant_type: "commented", participant_id: current_user.id)
end
end

View File

@ -0,0 +1,52 @@
class Api::V1::Issues::Journals::ListService < ApplicationService
include ActiveModel::Model
attr_reader :issue, :category, :keyword, :sort_by, :sort_direction
attr_accessor :queried_journals
validates :category, inclusion: {in: %w(all comment operate), message: "请输入正确的Category"}
validates :sort_by, inclusion: {in: Journal.column_names, message: '请输入正确的SortBy'}, allow_blank: true
validates :sort_direction, inclusion: {in: %w(asc desc), message: '请输入正确的SortDirection'}, allow_blank: true
def initialize(issue, params, current_user=nil)
@issue = issue
@keyword = params[:keyword]
@category = params[:category] || 'all'
@sort_by = params[:sort_by].present? ? params[:sort_by] : 'created_on'
@sort_direction = (params[:sort_direction].present? ? params[:sort_direction] : 'asc').downcase
end
def call
raise Error, errors.full_messages.join(", ") unless valid?
begin
journal_query_data
@queried_journals
rescue
raise Error, "服务器错误,请联系系统管理员!"
end
end
private
def journal_query_data
@queried_journals = issue.journals
case category
when 'comment'
@queried_journals = issue.comment_journals
when 'operate'
@queried_journals = issue.operate_journals
end
@queried_journals = @queried_journals.parent_journals
@queried_journals = @queried_journals.ransack(notes_cont: keyword).result if keyword.present?
@queried_journals = @queried_journals.includes(:journal_details, :user, :attachments, first_ten_children_journals: [:parent_journal, :reply_journal])
@queried_journals = @queried_journals.reorder("journals.#{sort_by} #{sort_direction}").distinct
@queried_journals
end
end

View File

@ -79,7 +79,6 @@ class Api::V1::Issues::ListService < ApplicationService
scope = q.result.includes(:priority, :issue_status, :user, :assigners, :version, :issue_tags, :comment_journals)
scope = scope.reorder("issues.#{sort_by} #{sort_direction}").distinct
@queried_issues = scope

View File

@ -6,7 +6,7 @@ class Api::V1::Issues::UpdateService < ApplicationService
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
attr_accessor :add_assigner_ids, :previous_issue_changes, :updated_issue, :atme_receivers
validates :project, :issue, :current_user, presence: true
@ -40,23 +40,29 @@ class Api::V1::Issues::UpdateService < ApplicationService
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_assigners(assigner_ids)
load_attachments(attachment_ids)
load_issue_tags(issue_tag_ids)
load_atme_receivers(receivers_login) unless receivers_login.blank?
try_lock
try_lock("Api::V1::Issues::UpdateService:#{project.id}:#{issue.id}")
@updated_issue = @issue
issue_load_attributes
build_issue_journal_details if @updated_issue.previous_changes.present? # 操作记录
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.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?
build_assigner_participants unless assigner_ids.nil? # 负责人
build_edit_participants
build_atme_participants if @atme_receivers.present?
@updated_issue.assigners = @assigners || User.none unless assigner_ids.nil?
@updated_issue.attachments = @attachments || Attachment.none unless attachment_ids.nil?
@updated_issue.issue_tags = @issue_tags || IssueTag.none unless issue_tag_ids.nil?
@updated_issue.updated_on = Time.now
@updated_issue.save!
build_after_issue_journal_details if @updated_issue.previous_changes.present? # 操作记录
build_previous_issue_changes
# @信息发送
@ -67,7 +73,7 @@ class Api::V1::Issues::UpdateService < ApplicationService
SendTemplateMessageJob.perform_later('IssueAssigned', current_user.id, @issue&.id, add_assigner_ids) unless add_assigner_ids.blank?
end
unlock
unlock("Api::V1::Issues::UpdateService:#{project.id}:#{issue.id}")
return @updated_issue
end
@ -87,108 +93,147 @@ class Api::V1::Issues::UpdateService < ApplicationService
end
def build_assigner_participants
@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
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 = @updated_issue.previous_changes.except("updated_on", "created_on")
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])
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])
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
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
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
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.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["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.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["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.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["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.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["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.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["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.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["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.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(",")})
# 更改结束时间
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 try_lock
raise Error, "请稍后再试!" unless $redis_cache.set("Api::V1::Issues::UpdateService:#{project.id}:#{issue.id}", 1, nx: true, ex: 60.seconds)
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 unlock
$redis_cache.del("Api::V1::Issues::UpdateService:#{project.id}:#{issue.id}")
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

View File

@ -9,6 +9,15 @@ class ApplicationService
content.gsub(regex, '')
end
protected
def try_lock(key)
raise Error, "请稍后再试!" unless $redis_cache.set(key, 1, nx: true, ex: 60.seconds)
end
def unlock(key)
$redis_cache.del(key)
end
private
def strip(str)

View File

@ -1,6 +1,6 @@
json.total_count @issues.total_count
json.opened_count @issues.opened.size
json.closed_count @issues.closed.size
json.opened_count @opened_issues_count
json.closed_count @closed_issues_count
json.issues @issues.each do |issue|
json.partial! "simple_detail", locals: {issue: issue}
end

View File

@ -0,0 +1,17 @@
json.(journal, :id, :notes, :comments_count)
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
json.reply_user do
if journal.reply_journal&.user&.present?
json.partial! "api/v1/users/simple_user", user: journal.reply_journal.user
else
json.nil!
end
end

View File

@ -0,0 +1,20 @@
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?
json.operate_content journal.is_journal_detail? ? journal.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! "children_detail", journal: journal
end
end

View File

@ -0,0 +1 @@
json.partial! "detail", journal: @object_result

View File

@ -0,0 +1,4 @@
json.total_count @journals.total_count
json.journals @journals do |journal|
json.partial! "detail", journal: journal
end

View File

@ -30,6 +30,14 @@ defaults format: :json do
patch :batch_update
delete :batch_destroy
end
member do
resources :journals, module: :issues, only: [:index, :create, :update, :destroy] do
member do
get :children_journals
end
end
end
end
scope module: :issues do
resources :issue_tags, except: [:new, :edit]