Class: Employees::ImportService

Inherits:
Object
  • Object
show all
Includes:
SheetHelper
Defined in:
app/services/employees/import_service.rb

Constant Summary collapse

DATE_FORMAT =
'%m/%d/%y'
ACCEPTED_COLUMNS =
%i[
  employee_name date_of_birth ssn start_date termination_date termination_reason google_address
  address_line1 address_line2 city postal_code business_email personal_email phone_no mobile_phone_no
  fax_no provincial_tax_category federal_tax_category vacation_percentage health_insurance_amount
  dental_insurance_amount rrsp_amount long_term_disability_insurance_amount employer_rrsp_amount
  life_insurance_amount additional_federal_taxes_retained additional_provincial_taxes_retained
  serious_illness_amount cotisation_syndicale_amount employer_health_insurance_contribution_amount
  employer_dental_insurance_contribution_amount employer_life_insurance_contribution_amount
  accepts_direct_deposit child_support_amount color default_language active province_code
  employee_portal_login_email employee_portal_login_phone employee_portal_login_enabled
  bank_account_name bank_account_number bank_account_branch_number bank_account_institution_number
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SheetHelper

#each_row, #hashes_array_from_file, #process_import_results

Constructor Details

#initialize(current_user:, current_company:, importation_file:) ⇒ ImportService

Returns a new instance of ImportService.



25
26
27
28
29
30
31
32
33
# File 'app/services/employees/import_service.rb', line 25

def initialize(
  current_user:,
  current_company:,
  importation_file:
)
  @current_user     = current_user
  @current_company  = current_company
  @importation_file = importation_file
end

Instance Attribute Details

#current_companyObject (readonly)

Returns the value of attribute current_company.



21
22
23
# File 'app/services/employees/import_service.rb', line 21

def current_company
  @current_company
end

#current_userObject (readonly)

Returns the value of attribute current_user.



21
22
23
# File 'app/services/employees/import_service.rb', line 21

def current_user
  @current_user
end

#importation_fileObject (readonly)

Returns the value of attribute importation_file.



21
22
23
# File 'app/services/employees/import_service.rb', line 21

def importation_file
  @importation_file
end

Instance Method Details

#importObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/employees/import_service.rb', line 35

def import
  rows = []
  file_rows = hashes_array_from_file(
    file: importation_file,
    base64_csv: false
  )
  processed_rows_count = 0

  ActiveRecord::Base.transaction do
    file_rows.each do |row|
      record = create_employee(row)
      raise ActiveRecord::Rollback unless record.persisted?

      rows << create_creation_row(row, record)
    rescue StandardError => e
      rows << create_failed_creation_row(row, e)
      raise ActiveRecord::Rollback
    ensure
      processed_rows_count += 1
    end
  end

  process_import_results(rows, file_rows, processed_rows_count, 'Importation rollbacked')
end

#validateObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/services/employees/import_service.rb', line 60

def validate
  rows = []

  each_row(importation_file) do |row|
    invalid_columns = row.keys - ACCEPTED_COLUMNS
    if invalid_columns.any?
      rows << create_invalid_column_row(row, invalid_columns)
      next
    end

    record = build_employee_for_validation(row)
    rows << create_validated_row(row, record)
  end

  rows
end