feat: bulk actions to update conversation objects (#3934)
Added the endpoints for bulk updating conversation objects Fixes: #3845 #3940 #3943
This commit is contained in:
26
app/controllers/api/v1/accounts/bulk_actions_controller.rb
Normal file
26
app/controllers/api/v1/accounts/bulk_actions_controller.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
class Api::V1::Accounts::BulkActionsController < Api::V1::Accounts::BaseController
|
||||
before_action :type_matches?
|
||||
|
||||
def create
|
||||
if type_matches?
|
||||
::BulkActionsJob.perform_later(
|
||||
account: @current_account,
|
||||
user: current_user,
|
||||
params: permitted_params
|
||||
)
|
||||
head :ok
|
||||
else
|
||||
render json: { success: false }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def type_matches?
|
||||
['Conversation'].include?(params[:type])
|
||||
end
|
||||
|
||||
def permitted_params
|
||||
params.permit(:type, ids: [], fields: [:status, :assignee_id, :team_id], labels: [add: [], remove: []])
|
||||
end
|
||||
end
|
||||
59
app/jobs/bulk_actions_job.rb
Normal file
59
app/jobs/bulk_actions_job.rb
Normal file
@@ -0,0 +1,59 @@
|
||||
class BulkActionsJob < ApplicationJob
|
||||
queue_as :medium
|
||||
attr_accessor :records
|
||||
|
||||
MODEL_TYPE = ['Conversation'].freeze
|
||||
|
||||
def perform(account:, params:, user:)
|
||||
@account = account
|
||||
Current.user = user
|
||||
@params = params
|
||||
@records = records_to_updated(params[:ids])
|
||||
bulk_update
|
||||
ensure
|
||||
Current.reset
|
||||
end
|
||||
|
||||
def bulk_update
|
||||
bulk_remove_labels
|
||||
bulk_conversation_update
|
||||
end
|
||||
|
||||
def bulk_conversation_update
|
||||
params = available_params(@params)
|
||||
records.each do |conversation|
|
||||
bulk_add_labels(conversation)
|
||||
conversation.update(params) if params
|
||||
end
|
||||
end
|
||||
|
||||
def bulk_remove_labels
|
||||
records.each do |conversation|
|
||||
remove_labels(conversation)
|
||||
end
|
||||
end
|
||||
|
||||
def available_params(params)
|
||||
return unless params[:fields]
|
||||
|
||||
params[:fields].delete_if { |_k, v| v.nil? }
|
||||
end
|
||||
|
||||
def bulk_add_labels(conversation)
|
||||
conversation.add_labels(@params[:labels][:add]) if @params[:labels] && @params[:labels][:add]
|
||||
end
|
||||
|
||||
def remove_labels(conversation)
|
||||
return unless @params[:labels] && @params[:labels][:remove]
|
||||
|
||||
labels = conversation.label_list - @params[:labels][:remove]
|
||||
conversation.update(label_list: labels)
|
||||
end
|
||||
|
||||
def records_to_updated(ids)
|
||||
current_model = @params[:type].camelcase
|
||||
return unless MODEL_TYPE.include?(current_model)
|
||||
|
||||
current_model.constantize&.where(account_id: @account.id, display_id: ids)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user