Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Contact import improvements (#7787)
- Ensure existing contact information is updated on data import - Refactor the existing job to make it more readable - Fixes issues with import files in the wrong encoding fixes: #7307
- Loading branch information
1 parent
44f6a9e
commit acb7deb
Showing
11 changed files
with
174 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
class DataImport::ContactManager | ||
def initialize(account) | ||
@account = account | ||
end | ||
|
||
def build_contact(params) | ||
contact = find_or_initialize_contact(params) | ||
update_contact_attributes(params, contact) | ||
contact | ||
end | ||
|
||
def find_or_initialize_contact(params) | ||
contact = find_existing_contact(params) | ||
contact_params = params.slice(:email, :identifier, :phone_number) | ||
contact_params[:phone_number] = format_phone_number(contact_params[:phone_number]) if contact_params[:phone_number].present? | ||
contact ||= @account.contacts.new(contact_params) | ||
contact | ||
end | ||
|
||
def find_existing_contact(params) | ||
contact = find_contact_by_identifier(params) | ||
contact ||= find_contact_by_email(params) | ||
contact ||= find_contact_by_phone_number(params) | ||
|
||
update_contact_with_merged_attributes(params, contact) if contact.present? && contact.valid? | ||
contact | ||
end | ||
|
||
def find_contact_by_identifier(params) | ||
return unless params[:identifier] | ||
|
||
@account.contacts.find_by(identifier: params[:identifier]) | ||
end | ||
|
||
def find_contact_by_email(params) | ||
return unless params[:email] | ||
|
||
@account.contacts.find_by(email: params[:email]) | ||
end | ||
|
||
def find_contact_by_phone_number(params) | ||
return unless params[:phone_number] | ||
|
||
@account.contacts.find_by(phone_number: format_phone_number(params[:phone_number])) | ||
end | ||
|
||
def format_phone_number(phone_number) | ||
phone_number.start_with?('+') ? phone_number : "+#{phone_number}" | ||
end | ||
|
||
def update_contact_with_merged_attributes(params, contact) | ||
contact.identifier = params[:identifier] if params[:identifier].present? | ||
contact.email = params[:email] if params[:email].present? | ||
contact.phone_number = format_phone_number(params[:phone_number]) if params[:phone_number].present? | ||
update_contact_attributes(params, contact) | ||
contact.save | ||
end | ||
|
||
private | ||
|
||
def update_contact_attributes(params, contact) | ||
contact.name = params[:name] if params[:name].present? | ||
contact.additional_attributes ||= {} | ||
contact.additional_attributes[:company] = params[:company] if params[:company].present? | ||
contact.additional_attributes[:city] = params[:city] if params[:city].present? | ||
contact.assign_attributes(custom_attributes: contact.custom_attributes.merge(params.except(:identifier, :email, :name, :phone_number))) | ||
end | ||
end |
17 changes: 17 additions & 0 deletions
17
...s/administrator_notifications/channel_notifications_mailer/contact_import_complete.liquid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<p>Hello,</p> | ||
|
||
<p>Your contact import has been completed. Please check the contacts tab to view the imported contacts.</p> | ||
|
||
<p>Number of records imported: {{meta['imported_contacts']}}</p> | ||
|
||
<p>Number of records failed: {{meta['failed_contacts']}}</p> | ||
|
||
{% if meta['failed_contacts'] == 0 %} | ||
<p> | ||
Click <a href="{{action_url}}">here</a> to view the imported contacts. | ||
</p> | ||
{% else %} | ||
<p> | ||
Click <a href="{{ action_url }}" target="_blank">here</a> to view failed records. | ||
</p> | ||
{% endif %} |
14 changes: 0 additions & 14 deletions
14
...ws/mailers/administrator_notifications/channel_notifications_mailer/failed_records.liquid
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
name,email | ||
J�hn D�e,john.doe@example.com | ||
Jane Smith,jane.smith@example.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.