feat: Add bulk imports API for contacts (#1724)

This commit is contained in:
Sojan Jose
2021-02-03 19:24:51 +05:30
committed by GitHub
parent 7748e0c56e
commit c61edff189
20 changed files with 278 additions and 14 deletions

View File

@@ -34,6 +34,7 @@ class Account < ApplicationRecord
has_many :account_users, dependent: :destroy
has_many :agent_bot_inboxes, dependent: :destroy
has_many :data_imports, dependent: :destroy
has_many :users, through: :account_users
has_many :inboxes, dependent: :destroy
has_many :conversations, dependent: :destroy

View File

@@ -27,6 +27,7 @@ class ContactInbox < ApplicationRecord
validates :inbox_id, presence: true
validates :contact_id, presence: true
validates :source_id, presence: true
validate :valid_source_id_format?
belongs_to :contact
belongs_to :inbox
@@ -47,4 +48,24 @@ class ContactInbox < ApplicationRecord
def current_conversation
conversations.last
end
private
def validate_twilio_source_id
# https://www.twilio.com/docs/glossary/what-e164#regex-matching-for-e164
if inbox.channel.medium == 'sms' && !/^\+[1-9]\d{1,14}$/.match?(source_id)
errors.add(:source_id, 'invalid source id for twilio sms inbox. valid Regex /^\+[1-9]\d{1,14}$/')
elsif inbox.channel.medium == 'whatsapp' && !/^whatsapp:\+[1-9]\d{1,14}$/.match?(source_id)
errors.add(:source_id, 'invalid source id for twilio whatsapp inbox. valid Regex /^whatsapp:\+[1-9]\d{1,14}$/')
end
end
def validate_email_source_id
errors.add(:source_id, "invalid source id for Email inbox. valid Regex #{Device.email_regexp}") unless Devise.email_regexp.match?(source_id)
end
def valid_source_id_format?
validate_twilio_source_id if inbox.channel_type == 'Channel::TwilioSms'
validate_email_source_id if inbox.channel_type == 'Channel::Email'
end
end

37
app/models/data_import.rb Normal file
View File

@@ -0,0 +1,37 @@
# == Schema Information
#
# Table name: data_imports
#
# id :bigint not null, primary key
# data_type :string not null
# processed_records :integer
# processing_errors :text
# status :integer default("pending"), not null
# total_records :integer
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
#
# Indexes
#
# index_data_imports_on_account_id (account_id)
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id)
#
class DataImport < ApplicationRecord
belongs_to :account
validates :data_type, inclusion: { in: ['contacts'], message: '%<value>s is an invalid data type' }
enum status: { pending: 0, processing: 1, completed: 2, failed: 3 }
has_one_attached :import_file
after_create_commit :process_data_import
private
def process_data_import
DataImportJob.perform_later(self)
end
end