Skip to content

Instantly share code, notes, and snippets.

@artofhuman
Created August 24, 2021 14:10
Show Gist options
  • Save artofhuman/98f35e3b4cf88519489ddb8af0a229c1 to your computer and use it in GitHub Desktop.
Save artofhuman/98f35e3b4cf88519489ddb8af0a229c1 to your computer and use it in GitHub Desktop.
// rake users:import[path/to/file/on/s3]
task :import, %i[s3_key] => :environment do |_task, args|
company = User.by_email("owner@workaxle.com").company
departments = {
'Administration' => '1',
'Ventes' => '2',
'Bureau' => '3',
'Production' => '4',
'Entretien' => '5',
'Chauffeur' => '6',
'Exped/recept' => '7',
'Mécanique' => '8',
'Trav etranger mirabel' => '9',
'Trav etranger laval' => '10',
'Contremaître' => '11',
'Boutique' => '12',
'Assistant gérant' => '13',
'RSDE' => '14',
'Amen. Paysager' => '15',
'Contremaître mirabel' => '16',
'Pelleteur' => '40',
'Opérateur' => '60',
'Aménagement paysager' => '100'
}
roles = [
'ACHETEUR',
'BOUTIQUE',
'CHAUFFEUR',
'COMMIS BUREAU',
'TECH.COMPTABLE',
'CONTR. ENTRE.',
'CONTR. OPERAT.',
'CONTROLEUR',
'DIR. RH + OPER.',
'DIRECTEUR GEN.',
'JOURNALIERE',
'JOURNALIER',
'MECANICIEN',
'PREPOSEE BOUT.',
'REPRESENTANTE',
'RESP. ENTRETIEN',
'TR. DE SERRES',
"CHEF D'EQUIPE",
'RESP.EXPEDITION',
'GER. BOUT.',
'CONS. PROD. H.',
'AGRON.',
'GESTION LOGISTI',
'ASSIS.GERANTE',
'CHAUFFEUR-OPER',
'CONTREMAITRE',
'PELLETEUR'
]
create_user = Users::CreateService.new
create_employee = Employees::Create.new
content =
App::Container['integrations.aws.s3_client']
.get_object(bucket: ENV.fetch('AWS_S3_BUCKET_NAME'), key: args.fetch(:s3_key))
.body
.read
Sequel::Model.db.transaction do
company.branches.each do |branch|
departments.each do |name, code|
department = Department.create(
name: name,
export_code: code,
branch: branch,
company: company
)
AggregatedDepartment.find_or_create(company: company, name: name)
roles.each do |r_name|
Job.create(
department: department,
export_code: '99',
branch: branch,
company: company,
name: r_name
)
AggregatedJob.find_or_create(company: company, name: r_name)
end
end
end
CSV.parse(content, headers: true) do |row|
first_name, last_name = row.fetch("Last name, First name").split(", ")
user_email = row.fetch("Email")
branch = Branch.first!(location_code: row.fetch("Location code"), company: company)
department = Department.first!(export_code: row.fetch("Department code"), branch: branch)
job = Job.first!(department: department, name: row.fetch("Role name"))
user = User.by_email(user_email)
user ||= create_user.call(
profile_attributes: {
first_name: first_name,
last_name: last_name,
email: user_email
},
access_level: User::EMPLOYEE_ACCESS_LEVEL,
onboarding_completed: true
)
employee = Employee.first(company: company, user: user)
if employee
employee.add_job(job)
else
hiring_date = row.fetch("Hiring date")
date = Date.parse(hiring_date) if hiring_date.present?
create_employee.call(
user: user,
branch: branch,
jobs: [job],
hired_at: date,
timesheets_export_code: row.fetch('Employee code')
)
end
rescue StandardError => e
puts row.to_h
raise e
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment