Merge pull request #5848 from betagouv/import_instructeur_csv
Import instructeur csv
This commit is contained in:
commit
f698dc798c
3 changed files with 202 additions and 0 deletions
39
app/services/instructeurs_import_service.rb
Normal file
39
app/services/instructeurs_import_service.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
class InstructeursImportService
|
||||
def import(procedure, groupes_emails)
|
||||
admins = procedure.administrateurs
|
||||
|
||||
errors = []
|
||||
|
||||
groupes_emails.each do |groupe_emails|
|
||||
groupe = groupe_emails["groupe"].strip
|
||||
instructeur_email = groupe_emails["email"].strip.downcase
|
||||
|
||||
if groupe.present? && Devise.email_regexp.match?(instructeur_email)
|
||||
gi = procedure.groupe_instructeurs.find_or_create_by!(label: groupe)
|
||||
|
||||
instructeur = Instructeur.by_email(instructeur_email) || create_instructeur(admins, instructeur_email)
|
||||
|
||||
if !gi.instructeurs.include?(instructeur)
|
||||
gi.instructeurs << instructeur
|
||||
|
||||
end
|
||||
else
|
||||
errors << instructeur_email
|
||||
end
|
||||
end
|
||||
|
||||
errors
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_instructeur(administrateurs, email)
|
||||
user = User.create_or_promote_to_instructeur(
|
||||
email,
|
||||
SecureRandom.hex,
|
||||
administrateurs: administrateurs
|
||||
)
|
||||
user.invite!
|
||||
user.instructeur
|
||||
end
|
||||
end
|
25
lib/tasks/instructeurs.rake
Normal file
25
lib/tasks/instructeurs.rake
Normal file
|
@ -0,0 +1,25 @@
|
|||
require Rails.root.join("lib", "tasks", "task_helper")
|
||||
|
||||
namespace :instructeurs do
|
||||
desc <<~EOD
|
||||
Import several instructeurs for a procedure
|
||||
rails instructeurs:import\[procedure_id,csv_path\]
|
||||
EOD
|
||||
task :import, [:procedure_id, :csv] => :environment do |_t, args|
|
||||
procedure_id = args[:procedure_id]
|
||||
csv = args[:csv]
|
||||
lines = CSV.readlines(csv, headers: true)
|
||||
|
||||
rake_puts "Import en cours..."
|
||||
|
||||
errors =
|
||||
InstructeursImportService.new.import(Procedure.find(procedure_id), lines)
|
||||
|
||||
if errors.present?
|
||||
rake_puts "Ces instructeurs n'ont pas pu être importés :"
|
||||
rake_puts errors
|
||||
end
|
||||
|
||||
rake_puts "Import terminé"
|
||||
end
|
||||
end
|
138
spec/services/instructeurs_import_service_spec.rb
Normal file
138
spec/services/instructeurs_import_service_spec.rb
Normal file
|
@ -0,0 +1,138 @@
|
|||
describe InstructeursImportService do
|
||||
describe '#import' do
|
||||
let(:service) { InstructeursImportService.new }
|
||||
let(:procedure) { create(:procedure) }
|
||||
|
||||
let(:procedure_groupes) do
|
||||
procedure
|
||||
.groupe_instructeurs
|
||||
.map { |gi| [gi.label, gi.instructeurs.map(&:email)] }
|
||||
end
|
||||
|
||||
subject { service.import(procedure, lines) }
|
||||
|
||||
context 'nominal case' do
|
||||
let(:lines) do
|
||||
[
|
||||
{ "groupe" => "Auvergne Rhone-Alpes", "email" => "john@lennon.fr" },
|
||||
{ "groupe" => " Occitanie ", "email" => "paul@mccartney.uk" },
|
||||
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" }
|
||||
]
|
||||
end
|
||||
|
||||
it 'imports' do
|
||||
errors = subject
|
||||
|
||||
expect(procedure_groupes).to match_array([
|
||||
["Auvergne Rhone-Alpes", ["john@lennon.fr"]],
|
||||
["Occitanie", ["paul@mccartney.uk", "ringo@starr.uk"]],
|
||||
["défaut", []]
|
||||
])
|
||||
|
||||
expect(errors).to match_array([])
|
||||
end
|
||||
end
|
||||
|
||||
context 'when group already exists' do
|
||||
let!(:gi) { create(:groupe_instructeur, label: 'Occitanie', procedure: procedure) }
|
||||
let(:lines) do
|
||||
[
|
||||
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" }
|
||||
]
|
||||
end
|
||||
|
||||
before do
|
||||
gi.instructeurs << create(:instructeur, email: 'george@harisson.uk')
|
||||
end
|
||||
|
||||
it 'adds instructeur to existing groupe' do
|
||||
subject
|
||||
|
||||
expect(procedure_groupes).to match_array([
|
||||
["Occitanie", ["george@harisson.uk", "ringo@starr.uk"]],
|
||||
["défaut", []]
|
||||
])
|
||||
end
|
||||
end
|
||||
|
||||
context 'when an email is malformed' do
|
||||
let(:lines) do
|
||||
[
|
||||
{ "groupe" => "Occitanie", "email" => "paul" },
|
||||
{ "groupe" => "Occitanie", "email" => " Paul@mccartney.uk " },
|
||||
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" }
|
||||
]
|
||||
end
|
||||
|
||||
it 'ignores or corrects' do
|
||||
errors = subject
|
||||
|
||||
expect(procedure_groupes).to match_array([
|
||||
["Occitanie", ["paul@mccartney.uk", "ringo@starr.uk"]],
|
||||
["défaut", []]
|
||||
])
|
||||
|
||||
expect(errors).to match_array(['paul'])
|
||||
end
|
||||
end
|
||||
|
||||
context 'when an instructeur already exists' do
|
||||
let!(:instructeur) { create(:instructeur) }
|
||||
let(:lines) do
|
||||
[
|
||||
{ "groupe" => "Occitanie", "email" => instructeur.email },
|
||||
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" }
|
||||
]
|
||||
end
|
||||
|
||||
it 'reuses instructeur' do
|
||||
subject
|
||||
|
||||
expect(procedure_groupes).to match_array([
|
||||
["Occitanie", [instructeur.email, "ringo@starr.uk"]],
|
||||
["défaut", []]
|
||||
])
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there are 2 emails of same instructeur to be imported' do
|
||||
let(:lines) do
|
||||
[
|
||||
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" },
|
||||
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" }
|
||||
]
|
||||
end
|
||||
|
||||
it 'ignores duplicated instructeur' do
|
||||
subject
|
||||
|
||||
expect(procedure_groupes).to match_array([
|
||||
["Occitanie", ["ringo@starr.uk"]],
|
||||
["défaut", []]
|
||||
])
|
||||
end
|
||||
end
|
||||
|
||||
context 'when label of group is empty' do
|
||||
let(:lines) do
|
||||
[
|
||||
{ "groupe" => "", "email" => "ringo@starr.uk" },
|
||||
{ "groupe" => " ", "email" => "paul@starr.uk" }
|
||||
]
|
||||
end
|
||||
|
||||
it 'ignores instructeur' do
|
||||
errors = subject
|
||||
|
||||
expect(procedure_groupes).to match_array([
|
||||
["défaut", []]
|
||||
])
|
||||
|
||||
expect(errors).to match_array([
|
||||
'ringo@starr.uk',
|
||||
'paul@starr.uk'
|
||||
])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue