diff --git a/app/controllers/api/v1/issues_controller.rb b/app/controllers/api/v1/issues_controller.rb index d1addcf31..567ef6b3b 100644 --- a/app/controllers/api/v1/issues_controller.rb +++ b/app/controllers/api/v1/issues_controller.rb @@ -1,6 +1,6 @@ class Api::V1::IssuesController < Api::V1::BaseController - before_action :require_public_and_member_above, only: [:index, :show, :create, :update, :destroy] + before_action :require_public_and_member_above, only: [:index, :show, :create, :update, :destroy, :batch_update, :batch_destroy] def index @object_results = Api::V1::Issues::ListService.call(@project, query_params, current_user) @@ -22,17 +22,31 @@ class Api::V1::IssuesController < Api::V1::BaseController def destroy @object_result = Api::V1::Issues::DeleteService.call(@project, @issue, current_user) + if @object_result + render_ok + else + render_error("删除疑修失败!") + end end - before_action :load_issues, only: [:batch_update, :batch_destroy] def batch_update @object_result = Api::V1::Issues::BatchUpdateService.call(@project, @issues, batch_issue_params, current_user) + if @object_result + render_ok + else + render_error("批量更新疑修失败!") + end end def batch_destroy @object_result = Api::V1::Issues::BatchDeleteService.call(@project, @issues, current_user) + if @object_result + render_ok + else + render_error("批量删除疑修失败!") + end end private @@ -55,7 +69,8 @@ class Api::V1::IssuesController < Api::V1::BaseController :subject, :description, :issue_tag_ids => [], :assigner_ids => [], - :attachment_ids => []) + :attachment_ids => [], + :receivers_login => []) end def batch_issue_params @@ -77,14 +92,14 @@ class Api::V1::IssuesController < Api::V1::BaseController def load_issues return render_error("请输入正确的ID数组!") unless params[:ids].is_a?(Array) params[:ids].each do |id| - @issue = @project.issues.where(project_issues_index: id).where.not(id: id).take || Issue.find_by_id(id) + @issue = Issue.find_by_id(id) if @issue.blank? return render_not_found("ID为#{id}的疑修不存在!") - elsif if @issue.present? && @issue.is_lock &&!(@project.member?(current_user) || current_user.admin?) + elsif @issue.present? && @issue.is_lock &&!(@project.member?(current_user) || current_user.admin?) return render_forbidden("ID为#{id}的疑修您没有权限操作!") end end - @issues = @project.issues.where(project_issues_index: params[:ids]).where.not(id: params[:ids]) || Issue.where(id: params[:ids]) + @issues = Issue.where(id: params[:ids]) end end \ No newline at end of file diff --git a/app/services/api/v1/issues/batch_delete_service.rb b/app/services/api/v1/issues/batch_delete_service.rb index 13da5d84a..133f098cd 100644 --- a/app/services/api/v1/issues/batch_delete_service.rb +++ b/app/services/api/v1/issues/batch_delete_service.rb @@ -26,10 +26,16 @@ class Api::V1::Issues::BatchDeleteService < ApplicationService end unlock + + return true end private + 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 diff --git a/app/services/api/v1/issues/batch_update_service.rb b/app/services/api/v1/issues/batch_update_service.rb index 2e3e4fab7..ffa1ba7df 100644 --- a/app/services/api/v1/issues/batch_update_service.rb +++ b/app/services/api/v1/issues/batch_update_service.rb @@ -22,6 +22,8 @@ class Api::V1::Issues::BatchUpdateService < ApplicationService @issues.each do |issue| Api::V1::Issues::UpdateService.call(project, issue, params, current_user) end + + return true end end diff --git a/app/services/api/v1/issues/create_service.rb b/app/services/api/v1/issues/create_service.rb index bee688d6d..e87745950 100644 --- a/app/services/api/v1/issues/create_service.rb +++ b/app/services/api/v1/issues/create_service.rb @@ -47,8 +47,8 @@ class Api::V1::Issues::CreateService < ApplicationService @created_issue = Issue.new(issue_attributes) build_author_participants build_assigner_participants unless assigner_ids.blank? - build_issue_journal_details if @updated_issue.previous_changes.present? # 操作记录 - build_issue_project_trends if status_id.present? # 开关时间 + build_issue_journal_details + build_issue_project_trends @created_issue.attachments = @attachments unless attachment_ids.blank? @created_issue.issue_tags = @issue_tags unless issue_tag_ids.blank? @@ -110,7 +110,7 @@ class Api::V1::Issues::CreateService < ApplicationService end def build_issue_journal_details - journal = @updated_issue.journals.new({user_id: current_user.id}) + journal = @created_issue.journals.new({user_id: current_user.id}) journal.journal_details.new({property: "issue", prop_key: 1, old_value: '', value: ''}) end diff --git a/app/services/api/v1/issues/delete_service.rb b/app/services/api/v1/issues/delete_service.rb index 02aaa441b..e6429390e 100644 --- a/app/services/api/v1/issues/delete_service.rb +++ b/app/services/api/v1/issues/delete_service.rb @@ -24,6 +24,8 @@ class Api::V1::Issues::DeleteService < ApplicationService end unlock + + return true end private diff --git a/app/services/api/v1/issues/update_service.rb b/app/services/api/v1/issues/update_service.rb index 49410e81a..3084e78a9 100644 --- a/app/services/api/v1/issues/update_service.rb +++ b/app/services/api/v1/issues/update_service.rb @@ -47,7 +47,7 @@ class Api::V1::Issues::UpdateService < ApplicationService try_lock @updated_issue = @issue - @updated_issue.load_attributes + 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? # 负责人 @@ -63,27 +63,40 @@ class Api::V1::Issues::UpdateService < ApplicationService 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('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 unlock + + return @updated_issue end end private + def issue_load_attributes + @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 if milestone_id.present? + @updated_issue.branch_name = branch_name if branch_name.present? + @updated_issue.start_date = start_date if start_date.present? + @updated_issue.due_date = due_date if due_date.present? + @updated_issue.subject = subject if subject.present? + @updated_issue.description = description if description.present? + end + def build_assigner_participants - @updated_issue.issue_participants.where.not(id: assigner_ids).each(&:destroy!) + @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_id: 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 def build_previous_issue_changes - @previous_issue_changes = @updated_issue.previous_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]) end @@ -97,7 +110,7 @@ class Api::V1::Issues::UpdateService < ApplicationService @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!) + @updated_issue.project_trends.where(action_type: ProjectTrend::CLOSE).each(&:destroy!) end end diff --git a/app/views/api/v1/issues/_detail.json.jbuilder b/app/views/api/v1/issues/_detail.json.jbuilder index e2e1bde8d..82fea8098 100644 --- a/app/views/api/v1/issues/_detail.json.jbuilder +++ b/app/views/api/v1/issues/_detail.json.jbuilder @@ -35,7 +35,7 @@ end json.assigners issue.assigners.each do |assigner| json.partial! "api/v1/users/simple_user", locals: {user: assigner} end -json.participants issue.participants.each do |participant| +json.participants issue.participants.distinct.each do |participant| json.partial! "api/v1/users/simple_user", locals: {user: participant} end json.comment_journals_count issue.comment_journals.size diff --git a/app/views/api/v1/issues/update.json.jbuilder b/app/views/api/v1/issues/update.json.jbuilder new file mode 100644 index 000000000..f45ef5b2f --- /dev/null +++ b/app/views/api/v1/issues/update.json.jbuilder @@ -0,0 +1 @@ +json.partial! "api/v1/issues/detail", locals: {issue: @object_result}