Class: AccountCpt::Registration::RegularUser::WithFacebookV2

Inherits:
Trailblazer::Operation
  • Object
show all
Extended by:
Contract::DSL
Includes:
ErrorOperationHelper, UserRegistration::MergeGuestBookings, UserRegistration::Notifications, UserRegistration::SubscribeVoucherReward
Defined in:
app/concepts/account_cpt/registration/regular_user/with_facebook_v2.rb

Overview

return registered user

Instance Method Summary collapse

Methods included from ErrorOperationHelper

#errors, #merge_errors

Methods included from UserRegistration::MergeGuestBookings

#remove_customer_guest_booking

Methods included from UserRegistration::SubscribeVoucherReward

#voucher_reward_subscription

Methods included from UserRegistration::Notifications

#send_registration_email, #send_registration_sms

Instance Method Details

#collect_guest_booking!(options) ⇒ Object



103
104
105
106
# File 'app/concepts/account_cpt/registration/regular_user/with_facebook_v2.rb', line 103

def collect_guest_booking!(options, *)
  remove_customer_guest_booking(user.id)
  true
end

#populate_model!(options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/concepts/account_cpt/registration/regular_user/with_facebook_v2.rb', line 38

def populate_model!(options, **)
  graph = Koala::Facebook::API.new(options['params']['access_token'])
  profile = graph.get_object('me', {})
  if profile['id'] != options['params']['uid']
    errors.add(:base, 'Invalid parameter, UID is not valid')
    return false
  end

  options['params'].delete 'phone' if options['params']['phone'].blank?
  new_attr = options['params'].merge(provider: 'facebook',
                                     name: profile['name'],
                                     fb_uid: profile['id'],
                                     uid: profile['id']).to_h.with_indifferent_access

  options['model'] = ::User.find_or_initialize_by(email: new_attr[:email])
  options['model'].attributes = new_attr
  if options['model'].new_record?
    options['new_user'] = true
    options['model'].password = options['model'].password_confirmation = Devise.friendly_token
  else
    options['new_user'] = false
  end

  if options['params']['email'].blank?
    errors.add(:email, "Can't be blank")
    return false
  end

  true
rescue Koala::Facebook::AuthenticationError, Koala::Facebook::OAuthTokenRequestError, Koala::Facebook::APIError => error
  APMErrorHandler.report('Failure API request to Facebook', e: error)
  errors.add(:base, 'Facebook authentication error')
  false
end

#save_user!(options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/concepts/account_cpt/registration/regular_user/with_facebook_v2.rb', line 73

def save_user!(options, **)
  user = options['model']
  # Check if the email has already been registered
  if User.exists?(email: user.email)
    user.errors.add(:email, 'has registered')
    merge_errors { user.errors }
    false
  end
  # Set phone-related fields to nil if phone_v2_full is blank
  if user.phone_v2_full.blank?
    user.phone = nil
    user.phone_v2 = nil
    user.calling_code = nil
  end
  # Save the user and return the result
  if user.valid? && user.save
    options[OpCons::OPERATION_RESULT] = user
  else
    merge_errors { user.errors }
    false
  end
end

#send_notifications!(options) ⇒ Object



96
97
98
99
100
101
# File 'app/concepts/account_cpt/registration/regular_user/with_facebook_v2.rb', line 96

def send_notifications!(options, *)
  return true unless options['new_user']
  send_registration_email
  voucher_reward_subscription
  true
end