Class: PartnerService::Staff::ForgotPassword

Inherits:
ApplicationService show all
Defined in:
app/services/partner_service/staff/forgot_password.rb

Instance Attribute Summary

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods inherited from ApplicationService

#execute, #execute!

Constructor Details

#initialize(staff, params = {}) ⇒ ForgotPassword

Returns a new instance of ForgotPassword.



8
9
10
11
12
13
# File 'app/services/partner_service/staff/forgot_password.rb', line 8

def initialize(staff, params = {})
  @staff = staff
  @password = params.fetch(:password, '')
  @password_confirmation = params.fetch(:password_confirmation, '')
  @reset_password_link = params.fetch(:reset_password_link, '')
end

Instance Method Details

#generate_password_token!Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/services/partner_service/staff/forgot_password.rb', line 15

def generate_password_token!
  return false if staff.blank?

  staff.reset_password_token = SecureRandom.hex(10)
  staff.reset_password_sent_at = Time.now.utc
  staff.sneaky_save

  reset_password_link << "?reset_password_token=#{staff.reset_password_token}"
  Partner::StaffMailer.forgot_password(staff, reset_password_link, I18n.locale.to_s,
                                       staff.reset_password_token).deliver_later!
  true
end

#reset_password!Object



28
29
30
31
32
33
34
35
36
37
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
# File 'app/services/partner_service/staff/forgot_password.rb', line 28

def reset_password!
  return false unless valid?

  ActiveRecord::Base.transaction do
    # Set a flag to prevent recursion
    staff.instance_variable_set(:@skip_password_sync, true)
    staff.reset_password_token = nil
    staff.password = password
    staff.audit_comment = 'Password reset via forgot password service'
    raise ActiveRecord::Rollback unless staff.save

    owner = staff.owner
    restaurant_group = staff.restaurant_group
    if owner.present?
      # Set a flag to prevent recursion
      owner.instance_variable_set(:@skip_staff_update, true)
      owner.password = password
      owner.audit_comment = "Password reset via forgot password service for Staff ID: #{staff.id}"
      unless owner.save
        # merge_errors(owner.errors)
        raise ActiveRecord::Rollback
      end
    elsif restaurant_group.present?
      # Set a flag to prevent recursion
      restaurant_group.instance_variable_set(:@skip_staff_update, true)
      restaurant_group.password = password
      restaurant_group.audit_comment = "Password reset via forgot password service for Staff ID: #{staff.id}"
      unless restaurant_group.save
        # merge_errors(restaurant_group.errors)
        raise ActiveRecord::Rollback
      end
    end
  end

  staff.errors.blank?
end