Advertisement
Guest User

Untitled

a guest
Nov 24th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. class ContactImportJob < ActiveJob::Base
  2. queue_as :default
  3. include Concerns::Logging
  4. include Concerns::Errors
  5.  
  6. # For each contact, import them and link them
  7. # up to the user. After, send a push notification
  8. # to notify the app that the contacts have synced
  9. # successfully. If there is an error, end
  10. # pre-emptively and send a standard push
  11. # notification error message.
  12. def perform(user: nil, contact_book: nil, contacts: [], push: AsyncJobPushNotification, phone_number_model: PhoneNumber)
  13. logger.info "Starting ContactImportJob"
  14.  
  15. fail "Must supply a user" if user.nil?
  16. fail "Must supply a contact book" if contact_book.nil?
  17. fail "Contacts must be enumerable" unless contacts.respond_to? :each
  18.  
  19. contacts.each do |contact|
  20. logger.info "Uploading contact..."
  21. contact.deep_symbolize_keys!
  22.  
  23. # 1. Create PhoneNumber Record if necessary, otherwise return existing match
  24. phone_numbers = []
  25. contact[:phone_numbers].each do |pn|
  26. pn = pn.values.first
  27. phone_number = phone_number_model.import(pn)
  28. unless phone_number.valid?
  29. logger.error "Sending Error push notification: #{phone_number.errors.to_a.join(' ')}"
  30. push.error!(
  31. recipients: [user],
  32. job: self,
  33. message: phone_number.errors.to_a.join(' '),
  34. data: phone_number.errors.to_a
  35. )
  36. return
  37. end
  38.  
  39. phone_numbers << phone_number
  40. end
  41.  
  42. # 2. Create a contactbook item, linking up the phone number record
  43. if cbi = ContactBookItem.import(contact_book: contact_book, name: contact[:name])
  44. cbi.phone_numbers << phone_numbers
  45. cbi.save
  46. end
  47. end
  48. contact_book.save
  49.  
  50. logger.info "Sending Successful push notification"
  51. push.success(recipients: [user], job: self.class, message: 'Completed Successfully')
  52.  
  53. end
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement