iachat/app/jobs/account/contacts_export_job.rb
Pranjal Kushwaha 0dc2af3c78
feat: Ability to delete account for administrators (#1874)
## Description

Add account delete option in the user account settings.

Fixes #1555 

## Type of change

- [ ] New feature (non-breaking change which adds functionality)


![image](https://user-images.githubusercontent.com/40784971/110349673-edcc5200-8058-11eb-8ded-a31d15aa0759.png)

![image](https://user-images.githubusercontent.com/40784971/110349778-0c324d80-8059-11eb-9291-abfbffedde5e.png)


## Checklist:

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules

---------

Co-authored-by: Sojan Jose <sojan@pepalo.com>
Co-authored-by: Sojan Jose <sojan.official@gmail.com>
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
2025-04-03 10:41:39 +05:30

66 lines
1.7 KiB
Ruby

class Account::ContactsExportJob < ApplicationJob
queue_as :low
def perform(account_id, user_id, column_names, params)
@account = Account.find(account_id)
@params = params
@account_user = @account.users.find(user_id)
headers = valid_headers(column_names)
generate_csv(headers)
send_mail
end
private
def generate_csv(headers)
csv_data = CSV.generate do |csv|
csv << headers
contacts.each do |contact|
csv << headers.map { |header| contact.send(header) }
end
end
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)
(column_names.presence || default_columns) & Contact.column_names
end
def attach_export_file(csv_data)
return if csv_data.blank?
@account.contacts_export.attach(
io: StringIO.new(csv_data),
filename: "#{@account.name}_#{@account.id}_contacts.csv",
content_type: 'text/csv'
)
end
def send_mail
file_url = account_contact_export_url
mailer = AdministratorNotifications::AccountNotificationMailer.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
%w[id name email phone_number]
end
end