Class: EmployeeRolesController

Inherits:
ApplicationController show all
Includes:
ApplicationHelper
Defined in:
app/controllers/employee_roles_controller.rb

Instance Method Summary collapse

Methods included from AuthentificationHelper

#accessible_company_ids_from_tenant_and_user, #application_mode_from_hostname, #current_company_id_from_accessible_company_ids, #current_tenant_from_user, #current_user_and_locale_from_token

Instance Method Details

#createObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/employee_roles_controller.rb', line 26

def create
  employee_role = EmployeeRole.new(**employee_role_params.except(:user))
  raise ActiveRecord::RecordInvalid.new(employee_role) unless employee_role.valid?

  render json: {
    employee_role_id: EmployeeRole.insert_update_procedure(**employee_role_params),
    message: I18n.t('messages.employee_role_created')
  }
rescue ActiveRecord::RecordInvalid => e
  render json: {
    errors: e.record.errors
  }, status: :bad_request
end

#destroyObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/employee_roles_controller.rb', line 40

def destroy
  employee_role = @employee.employee_roles.find_by(employee_role_id: params[:employee_role_id])
  if employee_role.employee_timesheets.exists?
    return render json: {
      message: I18n.t('messages.employee_role_has_timesheets')
    }, status: :bad_request
  end

  employee_role.destroy

  render json: {
    message: I18n.t('messages.employee_role_deleted')
  }
end

#formObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/controllers/employee_roles_controller.rb', line 101

def form
  ActiveRecord::Base.execute_procedure('pr_get_company_parameters', current_company_id)
  health_and_safety_references = current_company.company_parameters
                                                .where(
                                                  <<-SQL
        (type = 'N' AND TRY_CAST(value AS FLOAT) > 0) OR
        (type != 'N' AND value IS NOT NULL AND value <> '')
                                                  SQL
                                                )
                                                .as_json(
                                                  methods: [:reference_description],
                                                  only: [
                                                    :reference_code
                                                  ]
                                                )

  render json: {
    data: {
      health_and_safety_references: health_and_safety_references,
      default_hours_per_week: current_company.default_hours_per_week
    }
  }
end

#indexObject



14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/employee_roles_controller.rb', line 14

def index
  data,  = EmployeeRole.where_procedure(
    employee_id: @employee.employee_id,
    company_id: current_company.company_id
  ).values_at(:data, :metadata)

  render json: {
    data: data,
    metadata: 
  }
end

#update_from_table_editsObject



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/employee_roles_controller.rb', line 55

def update_from_table_edits
  request.params[:rows_edits].each do |row_edits|
    row_hash = row_edits[:rowHash]
    edits = row_edits[:edits]

    employee_role = EmployeeRole.find_by!(
      employee_id: @employee.employee_id,
      employee_role_id: row_hash[:employee_role_id]
    )

    attributes = edits.slice(
      :description,
      :hourly_rate,
      :apply_overtime,
      :work_hours_per_week
    ).reject { |_, v| v.nil? }

    employee_role.assign_attributes(attributes.merge(
                                      health_safety_reference_code: edits.dig(
                                        :health_and_safety_reference_description, :value
                                      ) ||
                                                                    employee_role.health_safety_reference_code
                                    ))
    raise ActiveRecord::RecordInvalid.new(employee_role) unless employee_role.valid?

    EmployeeRole.insert_update_procedure(
      employee_id: employee_role.employee_id,
      employee_role_id: employee_role.employee_role_id,
      description: employee_role.description,
      hourly_rate: employee_role.hourly_rate,
      apply_overtime: employee_role.apply_overtime,
      work_hours_per_week: employee_role.work_hours_per_week,
      health_safety_reference_code: employee_role.health_safety_reference_code,
      user: current_user.email.first(20)
    )
  end

  render json: {
    message: I18n.t('messages.employee_role_updated')
  }
rescue ActiveRecord::RecordInvalid => e
  render json: {
    message: e.record.errors.full_messages.join(', ')
  }, status: :bad_request
end