Class: AccountCpt::Registration::RegularUser::WithFirebase

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_firebase.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



108
109
110
111
112
# File 'app/concepts/account_cpt/registration/regular_user/with_firebase.rb', line 108

def collect_guest_booking!(_options, *)
  # guest_booking_ids = options['guest_booking_ids'] || options[:guest_booking_ids]
  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
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/concepts/account_cpt/registration/regular_user/with_firebase.rb', line 38

def populate_model!(options, **)
  unless FirebaseIdToken::Certificates.present?
    FirebaseIdToken::Certificates.request!
  end

  firebase_profile = FirebaseIdToken::Signature.verify(options['params']['access_token'])
  if firebase_profile.nil?
    errors.add(:base, 'Token is invalid')
    return false
  end

  if firebase_profile['user_id'] != options['params']['uid']
    errors.add(:base, 'Invalid parameter, UID is not valid')
    return false
  end

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

  email = firebase_profile['email'].presence || options['params']['email']
  new_user_attr = options['params'].merge(
    provider: User::PROVIDER_FIREBASE.to_s,
    name: firebase_profile['name'],
    email: email,
    uid: firebase_profile['user_id'],
  ).to_h.with_indifferent_access

  options['model'] = ::User.find_or_initialize_by(email: new_user_attr[:email])
  if options['model'].new_record?
    options['model'].attributes = new_user_attr
    options['new_user'] = true
    options['model'].password = options['model'].password_confirmation = Devise.friendly_token
    options['model'].remote_avatar_url = firebase_profile['picture'] if firebase_profile['picture'].present?
  else
    options['model'].attributes = options['params']
    options['new_user'] = false
  end

  true
rescue FirebaseIdToken::Exceptions::NoCertificatesError => e
  APMErrorHandler.report('Failure API request to firebase', e: e)
  errors.add(:base, 'firebase authentication error')
  false
end

#save_user!(options) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/concepts/account_cpt/registration/regular_user/with_firebase.rb', line 85

def save_user!(options, **)
  user = options['model']
  if user.phone_v2_full.blank?
    user.phone = nil
    user.phone_v2 = nil
    user.calling_code = nil
  end
  if user.valid? && user.save
    options[OpCons::OPERATION_RESULT] = user
  else
    merge_errors { user.errors }
    false
  end
end

#send_notifications!(options) ⇒ Object



100
101
102
103
104
105
106
# File 'app/concepts/account_cpt/registration/regular_user/with_firebase.rb', line 100

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

  send_registration_email
  voucher_reward_subscription
  true
end