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:
@@ -46,7 +46,8 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
|
||||
|
||||
def export
|
||||
column_names = params['column_names']
|
||||
Account::ContactsExportJob.perform_later(Current.account.id, column_names, Current.user.email)
|
||||
filter_params = { :payload => params.permit!['payload'], :label => params.permit!['label'] }
|
||||
Account::ContactsExportJob.perform_later(Current.account.id, Current.user.id, column_names, filter_params)
|
||||
head :ok, message: I18n.t('errors.contacts.export.success')
|
||||
end
|
||||
|
||||
@@ -61,7 +62,7 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
|
||||
def show; end
|
||||
|
||||
def filter
|
||||
result = ::Contacts::FilterService.new(params.permit!, current_user).perform
|
||||
result = ::Contacts::FilterService.new(Current.account, Current.user, params.permit!).perform
|
||||
contacts = result[:contacts]
|
||||
@contacts_count = result[:count]
|
||||
@contacts = fetch_contacts(contacts)
|
||||
|
||||
@@ -77,8 +77,8 @@ class ContactAPI extends ApiClient {
|
||||
return axios.delete(`${this.url}/${contactId}/avatar`);
|
||||
}
|
||||
|
||||
exportContacts() {
|
||||
return axios.get(`${this.url}/export`);
|
||||
exportContacts(queryPayload) {
|
||||
return axios.post(`${this.url}/export`, queryPayload);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -84,6 +84,7 @@
|
||||
"CONFIRM": {
|
||||
"TITLE": "Export Contacts",
|
||||
"MESSAGE": "Are you sure you want to export all contacts?",
|
||||
"FILTERED_MESSAGE": "Are you sure you want to export all the filtered contacts?",
|
||||
"YES": "Yes, Export",
|
||||
"NO": "No, Cancel"
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<template>
|
||||
<div class="w-full flex flex-row">
|
||||
<div class="flex flex-row w-full">
|
||||
<div class="flex flex-col h-full" :class="wrapClass">
|
||||
<contacts-header
|
||||
:search-query="searchQuery"
|
||||
@@ -391,8 +391,19 @@ export default {
|
||||
this.fetchContacts(this.pageParameter);
|
||||
},
|
||||
onExportSubmit() {
|
||||
let query = { payload: [] };
|
||||
|
||||
if (this.hasActiveSegments) {
|
||||
query = this.activeSegment.query;
|
||||
} else if (this.hasAppliedFilters) {
|
||||
query = filterQueryGenerator(this.getAppliedContactFilters);
|
||||
}
|
||||
|
||||
try {
|
||||
this.$store.dispatch('contacts/export');
|
||||
this.$store.dispatch('contacts/export', {
|
||||
...query,
|
||||
label: this.label,
|
||||
});
|
||||
this.showAlert(this.$t('EXPORT_CONTACTS.SUCCESS_MESSAGE'));
|
||||
} catch (error) {
|
||||
this.showAlert(
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
<template>
|
||||
<header
|
||||
class="bg-white dark:bg-slate-900 border-b border-slate-50 dark:border-slate-800"
|
||||
class="bg-white border-b dark:bg-slate-900 border-slate-50 dark:border-slate-800"
|
||||
>
|
||||
<div class="flex justify-between w-full py-2 px-4">
|
||||
<div class="flex justify-between w-full px-4 py-2">
|
||||
<div class="flex items-center justify-center max-w-full min-w-[6.25rem]">
|
||||
<woot-sidemenu-icon />
|
||||
<h1
|
||||
class="m-0 text-xl text-slate-900 dark:text-slate-100 overflow-hidden whitespace-nowrap text-ellipsis my-0 mx-2"
|
||||
class="m-0 mx-2 my-0 overflow-hidden text-xl text-slate-900 dark:text-slate-100 whitespace-nowrap text-ellipsis"
|
||||
>
|
||||
{{ headerTitle }}
|
||||
</h1>
|
||||
@@ -18,7 +18,7 @@
|
||||
<div class="flex items-center absolute h-full left-2.5">
|
||||
<fluent-icon
|
||||
icon="search"
|
||||
class="h-5 leading-9 text-sm text-slate-700 dark:text-slate-200"
|
||||
class="h-5 text-sm leading-9 text-slate-700 dark:text-slate-200"
|
||||
/>
|
||||
</div>
|
||||
<input
|
||||
@@ -59,7 +59,7 @@
|
||||
<div v-if="!hasActiveSegments" class="relative">
|
||||
<div
|
||||
v-if="hasAppliedFilters"
|
||||
class="absolute h-2 w-2 top-1 right-3 bg-slate-500 dark:bg-slate-500 rounded-full"
|
||||
class="absolute w-2 h-2 rounded-full top-1 right-3 bg-slate-500 dark:bg-slate-500"
|
||||
/>
|
||||
<woot-button
|
||||
class="clear"
|
||||
@@ -116,7 +116,7 @@
|
||||
<woot-confirm-modal
|
||||
ref="confirmExportContactsDialog"
|
||||
:title="$t('EXPORT_CONTACTS.CONFIRM.TITLE')"
|
||||
:description="$t('EXPORT_CONTACTS.CONFIRM.MESSAGE')"
|
||||
:description="exportDescription"
|
||||
:confirm-label="$t('EXPORT_CONTACTS.CONFIRM.YES')"
|
||||
:cancel-label="$t('EXPORT_CONTACTS.CONFIRM.NO')"
|
||||
/>
|
||||
@@ -162,6 +162,11 @@ export default {
|
||||
hasActiveSegments() {
|
||||
return this.segmentsId !== 0;
|
||||
},
|
||||
exportDescription() {
|
||||
return this.hasAppliedFilters
|
||||
? this.$t('EXPORT_CONTACTS.CONFIRM.FILTERED_MESSAGE')
|
||||
: this.$t('EXPORT_CONTACTS.CONFIRM.MESSAGE');
|
||||
},
|
||||
},
|
||||
methods: {
|
||||
onToggleSegmentsModal() {
|
||||
|
||||
@@ -138,9 +138,10 @@ export const actions = {
|
||||
}
|
||||
},
|
||||
|
||||
export: async ({ commit }) => {
|
||||
export: async ({ commit }, { payload, label }) => {
|
||||
try {
|
||||
await ContactAPI.exportContacts();
|
||||
await ContactAPI.exportContacts({ payload, label });
|
||||
|
||||
commit(types.SET_CONTACT_UI_FLAG, { isCreating: false });
|
||||
} catch (error) {
|
||||
commit(types.SET_CONTACT_UI_FLAG, { isCreating: false });
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
class Contacts::FilterService < FilterService
|
||||
ATTRIBUTE_MODEL = 'contact_attribute'.freeze
|
||||
|
||||
def initialize(account, user, params)
|
||||
@account = account
|
||||
# TODO: Change the order of arguments in FilterService maybe?
|
||||
# account, user, params makes more sense
|
||||
super(params, user)
|
||||
end
|
||||
|
||||
def perform
|
||||
@contacts = query_builder(@filters['contacts'])
|
||||
|
||||
@@ -21,8 +28,9 @@ class Contacts::FilterService < FilterService
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: @account.contacts.resolved_contacts ? to stay consistant with the behavior in ui
|
||||
def base_relation
|
||||
Current.account.contacts
|
||||
@account.contacts
|
||||
end
|
||||
|
||||
def filter_config
|
||||
|
||||
Reference in New Issue
Block a user