Class: StaffService::ForgotPassword

Inherits:
ApplicationService
  • Object
show all
Includes:
DefaultErrorContainer
Defined in:
app/services/staff_service/forgot_password.rb

Overview

Service to update password staff/owner Used by Staff restaurant

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Constructor Details

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

Returns a new instance of ForgotPassword.



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

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

Instance Method Details

#generate_password_token!Object



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

def generate_password_token!
  errors.clear
  return false if staff.blank?

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

  StaffMailer.forgot_password(staff).deliver_later!
  true
end

#reset_password!Object



27
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
# File 'app/services/staff_service/forgot_password.rb', line 27

def reset_password!
  errors.clear
  return false unless valid?

  ActiveRecord::Base.transaction do
    staff.reset_password_token = nil
    staff.password = password
    unless staff.save
      merge_errors(staff.errors)
      raise ActiveRecord::Rollback
    end
    owner = staff.owner
    restaurant_group = staff.restaurant_group
    if owner.present?
      owner.password = password
      unless owner.save
        merge_errors(owner.errors)
        raise ActiveRecord::Rollback
      end
    elsif restaurant_group.present?
      restaurant_group.password = password
      unless restaurant_group.save
        merge_errors(restaurant_group.errors)
        raise ActiveRecord::Rollback
      end
    end
  end

  true
end