Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ContactImportJob < ActiveJob::Base
- queue_as :default
- include Concerns::Logging
- include Concerns::Errors
- # For each contact, import them and link them
- # up to the user. After, send a push notification
- # to notify the app that the contacts have synced
- # successfully. If there is an error, end
- # pre-emptively and send a standard push
- # notification error message.
- def perform(user: nil, contact_book: nil, contacts: [], push: AsyncJobPushNotification, phone_number_model: PhoneNumber)
- logger.info "Starting ContactImportJob"
- fail "Must supply a user" if user.nil?
- fail "Must supply a contact book" if contact_book.nil?
- fail "Contacts must be enumerable" unless contacts.respond_to? :each
- contacts.each do |contact|
- logger.info "Uploading contact..."
- contact.deep_symbolize_keys!
- # 1. Create PhoneNumber Record if necessary, otherwise return existing match
- phone_numbers = []
- contact[:phone_numbers].each do |pn|
- pn = pn.values.first
- phone_number = phone_number_model.import(pn)
- unless phone_number.valid?
- logger.error "Sending Error push notification: #{phone_number.errors.to_a.join(' ')}"
- push.error!(
- recipients: [user],
- job: self,
- message: phone_number.errors.to_a.join(' '),
- data: phone_number.errors.to_a
- )
- return
- end
- phone_numbers << phone_number
- end
- # 2. Create a contactbook item, linking up the phone number record
- if cbi = ContactBookItem.import(contact_book: contact_book, name: contact[:name])
- cbi.phone_numbers << phone_numbers
- cbi.save
- end
- end
- contact_book.save
- logger.info "Sending Successful push notification"
- push.success(recipients: [user], job: self.class, message: 'Completed Successfully')
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement