Class: HrLite::Admin::EmployeesController

Inherits:
LeadershipController show all
Defined in:
app/controllers/hr_lite/admin/employees_controller.rb

Overview

Employee HR profiles (leadership). Onboarding can create the login itself (config.onboard_user); offboarding stamps the exit date and revokes access (config.offboard_user) — nothing is ever deleted, payroll history is a statutory record.

Instance Method Summary collapse

Instance Method Details

#createObject



24
25
26
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
57
58
59
60
61
# File 'app/controllers/hr_lite/admin/employees_controller.rb', line 24

def create
   = params.dig(:employee_profile, :new_user_email).present?
  @profile = EmployeeProfile.new(profile_params)

  if 
    user = HrLite.config.onboard_user.call(
      name: params.dig(:employee_profile, :new_user_name).to_s,
      email: params.dig(:employee_profile, :new_user_email).to_s.strip,
      password: params.dig(:employee_profile, :new_user_password).to_s
    )
    @profile.user_id = user.id
  end

  if @profile.save
    if 
      invite_url = HrLite.config.invite_url_for&.call(@profile.user)
      Notifications.publish(
        "employee.onboarded",
        title: "Welcome aboard — your HR account is ready",
        body: invite_url ? "Set your password with the button below, then sign in with your email."
                         : "Sign in with your email; your manager has your starting password.",
        path: "/",
        link_url: invite_url,
        bell_to: [ @profile.user ],
        email_to: [ @profile.user ]
      )
    end
    redirect_to admin_employee_path(@profile), notice: "Employee #{'onboarded' if } profile created."
  else
    render :new, status: :unprocessable_entity
  end
rescue ActiveRecord::RecordInvalid => e
  @profile.errors.add(:base, "Could not create the login: #{e.record.errors.full_messages.to_sentence}")
  render :new, status: :unprocessable_entity
rescue ActiveRecord::RecordNotUnique
  @profile.errors.add(:base, "Could not create the login: that email already has an account")
  render :new, status: :unprocessable_entity
end

#editObject



63
64
65
# File 'app/controllers/hr_lite/admin/employees_controller.rb', line 63

def edit
  @profile = EmployeeProfile.find(params[:id])
end

#indexObject



8
9
10
11
12
# File 'app/controllers/hr_lite/admin/employees_controller.rb', line 8

def index
  @profiles = paginate(EmployeeProfile.includes(:user).order(:employee_code))
  @users_without_profile = HrLite.employees.reject { |u| EmployeeProfile.exists?(user_id: u.id) }
  @pending_resignations = Resignation.pending.includes(:user).recent_first
end

#newObject



20
21
22
# File 'app/controllers/hr_lite/admin/employees_controller.rb', line 20

def new
  @profile = EmployeeProfile.new(user_id: params[:user_id], date_of_joining: Date.current)
end

#offboardObject

Exit: stamps the date (attendance/payroll clip on it) and revokes access via the host hook. Audited via the profile update.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/hr_lite/admin/employees_controller.rb', line 78

def offboard
  @profile = EmployeeProfile.find(params[:id])
  exit_date = params[:date_of_exit].presence&.to_date || Date.current

  @profile.update!(date_of_exit: exit_date)
  begin
    HrLite.config.offboard_user.call(@profile.user)
  rescue => e
    Rails.logger.error("[hr_lite] offboard_user failed: #{e.class}: #{e.message}")
  end
  redirect_to admin_employee_path(@profile),
              notice: "Offboarded — last day #{exit_date.strftime('%d %b %Y')}, access revoked."
end

#showObject



14
15
16
17
18
# File 'app/controllers/hr_lite/admin/employees_controller.rb', line 14

def show
  @profile = EmployeeProfile.includes(:user).find(params[:id])
  @structures = SalaryStructure.where(user_id: @profile.user_id).order(effective_from: :desc)
  @pending_resignation = Resignation.pending.find_by(user_id: @profile.user_id)
end

#updateObject



67
68
69
70
71
72
73
74
# File 'app/controllers/hr_lite/admin/employees_controller.rb', line 67

def update
  @profile = EmployeeProfile.find(params[:id])
  if @profile.update(profile_params)
    redirect_to admin_employee_path(@profile), notice: "Employee profile updated."
  else
    render :edit, status: :unprocessable_entity
  end
end