feat: exporting contacts takes the filters into account (#9347)
- This PR allows contacts to be exported using the current filter in CRM view Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
@@ -1,44 +1,62 @@
|
||||
class Account::ContactsExportJob < ApplicationJob
|
||||
queue_as :low
|
||||
|
||||
def perform(account_id, column_names, email_to)
|
||||
account = Account.find(account_id)
|
||||
headers = valid_headers(column_names)
|
||||
generate_csv(account, headers)
|
||||
file_url = account_contact_export_url(account)
|
||||
def perform(account_id, user_id, column_names, params)
|
||||
@account = Account.find(account_id)
|
||||
@params = params
|
||||
@account_user = @account.users.find(user_id)
|
||||
|
||||
AdministratorNotifications::ChannelNotificationsMailer.with(account: account).contact_export_complete(file_url, email_to)&.deliver_later
|
||||
headers = valid_headers(column_names)
|
||||
generate_csv(headers)
|
||||
send_mail
|
||||
end
|
||||
|
||||
def generate_csv(account, headers)
|
||||
private
|
||||
|
||||
def generate_csv(headers)
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << headers
|
||||
account.contacts.each do |contact|
|
||||
contacts.each do |contact|
|
||||
csv << headers.map { |header| contact.send(header) }
|
||||
end
|
||||
end
|
||||
|
||||
attach_export_file(account, csv_data)
|
||||
attach_export_file(csv_data)
|
||||
end
|
||||
|
||||
def contacts
|
||||
if @params.present? && @params[:payload].present? && @params[:payload].any?
|
||||
result = ::Contacts::FilterService.new(@account, @account_user, @params).perform
|
||||
result[:contacts]
|
||||
elsif @params[:label].present?
|
||||
@account.contacts.resolved_contacts.tagged_with(@params[:label], any: true)
|
||||
else
|
||||
@account.contacts.resolved_contacts
|
||||
end
|
||||
end
|
||||
|
||||
def valid_headers(column_names)
|
||||
columns = (column_names.presence || default_columns)
|
||||
headers = columns.map { |column| column if Contact.column_names.include?(column) }
|
||||
headers.compact
|
||||
(column_names.presence || default_columns) & Contact.column_names
|
||||
end
|
||||
|
||||
def attach_export_file(account, csv_data)
|
||||
def attach_export_file(csv_data)
|
||||
return if csv_data.blank?
|
||||
|
||||
account.contacts_export.attach(
|
||||
@account.contacts_export.attach(
|
||||
io: StringIO.new(csv_data),
|
||||
filename: "#{account.name}_#{account.id}_contacts.csv",
|
||||
filename: "#{@account.name}_#{@account.id}_contacts.csv",
|
||||
content_type: 'text/csv'
|
||||
)
|
||||
end
|
||||
|
||||
def account_contact_export_url(account)
|
||||
Rails.application.routes.url_helpers.rails_blob_url(account.contacts_export)
|
||||
def send_mail
|
||||
file_url = account_contact_export_url
|
||||
mailer = AdministratorNotifications::ChannelNotificationsMailer.with(account: @account)
|
||||
mailer.contact_export_complete(file_url, @account_user.email)&.deliver_later
|
||||
end
|
||||
|
||||
def account_contact_export_url
|
||||
Rails.application.routes.url_helpers.rails_blob_url(@account.contacts_export)
|
||||
end
|
||||
|
||||
def default_columns
|
||||
|
||||
Reference in New Issue
Block a user