diff --git a/admin/app/admin/school_user_uploads.rb b/admin/app/admin/school_user_uploads.rb index 30ed6c6c6..73de5d84e 100644 --- a/admin/app/admin/school_user_uploads.rb +++ b/admin/app/admin/school_user_uploads.rb @@ -7,22 +7,25 @@ ActiveAdmin.register_page "SchoolUserUploads" do puts params + @client = RecurlyClient.new + file = params[:jam_ruby_user][:csv] - created = 0 - already_existing = 0 - array_of_arrays = CSV.read(file.tempfile.path, headers:true, encoding: 'bom|utf-8') + errors = 0 + updated = 0 + skipped = 0 + array_of_arrays = CSV.read(file.tempfile.path, headers: true, encoding: 'bom|utf-8') array_of_arrays.each do |row| - school_name = row['School Name'] - school_tag = row['School ID'] + school_name = row['School Name'].strip + school_tag = row['School ID'].strip school = School.autocreate_find_from_upload(school_name, school_tag) - first_name = row['First Name'] - last_name = row['Last Name'] - email_address = row['Email Address'] - license_start = parse_date(row['License Start Date']) - license_end = parse_date(row['License End Date']) - source = row['Source'] + first_name = row['First Name'].strip + last_name = row['Last Name'].strip + email_address = row['Email Address'].strip + license_start = parse_date(row['License Start Date'].strip) + license_end = parse_date(row['License End Date'].strip) + source = row['Source'].strip password = SecureRandom.uuid options = { first_name: first_name, @@ -39,30 +42,80 @@ ActiveAdmin.register_page "SchoolUserUploads" do password_confirmation: password } - parse_user_type(row['User Type'], options) + parse_user_type(row['User Type'].strip, options) instrument = Instrument.find('electric guitar') instruments= [{instrument_id: instrument.id, proficiency_level: 3, priority: 1}] options[:instruments] = instruments - user = User.signup(options) - if user.errors.any? - puts "User #{user.name} #{user.email} had errors" - puts user.errors.inspect - already_existing = already_existing + 1 + existing = User.find_by_email(options[:email]) + + if existing + if existing.import_source.nil? + if options[:student] + existing.school = school + existing.is_a_student = true + existing.import_source = options[:import_source] + existing.license_start = options[:license_start] + existing.license_end = options[:license_end] + elsif options[:teacher] + existing.school = school + existing.import_source = options[:import_source] + existing.license_start = options[:license_start] + existing.license_end = options[:license_end] + if existing.teacher.nil? + existing.teacher = Teacher.build_teacher(existing, validate_introduction: true, biography: "Empty biography", school_id: school.id) + end + elsif options[:platform_instructor] + existing.import_source = options[:import_source] + existing.is_platform_instructor = true + existing.license_start = options[:license_start] + existing.license_end = options[:license_end] + end + + existing.save + + if existing.errors.any? + puts "User #{user.name} #{user.email} had errors" + puts user.errors.inspect + errors = errors + 1 + else + @client.sync_subscription(existing) + updated = updated + 1 + if options[:student] + UserMailer.school_welcome_message(existing, nil).deliver_now + elsif options[:teacher] + UserMailer.school_welcome_message(existing, nil).deliver_now + elsif options[:platform_instructor] + + end + end + else + skipped = skipped + 1 + end else - puts "User #{user.email} created" - created = created + 1 + user = User.signup(options) + + if user.errors.any? + puts "User #{user.name} #{user.email} had errors" + puts user.errors.inspect + errors = errors + 1 + else + @client.sync_subscription(user) + puts "User #{user.email} created" + created = created + 1 + end end + end - redirect_to admin_schooluseruploads_path, :notice => "Created #{created} school students. Ignored #{already_existing} because already existed." + redirect_to admin_schooluseruploads_path, :notice => "Created #{created}. Updated #{updated}. Errors #{errors}. Skipped #{skipped}" end end content do panel "Help" do - link_to "Download Sample CSV", asset_path("Sample_School_User_Upload.csv", target: "_blank", download: "Sample_School_User_Upload.csv") + link_to "Download Sample CSV", asset_path("Sample_School_User_Upload.csv", target: "_blank", download: "Sample_School_User_Upload.csv") end active_admin_form_for User.new, :url => admin_schooluseruploads_upload_schooluseruploads_path, :builder => ActiveAdmin::FormBuilder do |f| diff --git a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb index 6fbd9c649..7aac67be8 100644 --- a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb +++ b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb @@ -244,7 +244,12 @@ module JamRuby def school_welcome_message(user, reset_url) @user = user - @subject= "Set a password on your new JamKazam account for your #{user.school.name} music program" + if reset_url + @subject= "Set a password on your new JamKazam account for your #{user.school.name} music program" + else + @subject= "Your JamKazam account has been linked to your #{user.school.name} music program" + end + @school = user.school @reset_url = reset_url diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/school_welcome_message.html.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/school_welcome_message.html.erb index db1bf29cf..07e6c365b 100644 --- a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/school_welcome_message.html.erb +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/school_welcome_message.html.erb @@ -13,6 +13,7 @@ You can also use JamKazam to play and record yourself on your own.
+<% if @reset_url %>To use the JamKazam app, you will need to sign in to the app using an email address and a password. The email address is the one to which this email was sent. @@ -26,6 +27,14 @@ Set Password
+ <% else %> ++ We noticed you already have an account at JamKazam, under the email address to which this email was sent. + Next time you log into the application, your account will have been associated with your school and has a subscription license from your school. + If you're worried this is a scam, please check with your school or music program instructor to verify that this is a legitimate email. +
+ + <% end %>diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/school_welcome_message.text.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/school_welcome_message.text.erb index 306724c13..63f47576b 100644 --- a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/school_welcome_message.text.erb +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/school_welcome_message.text.erb @@ -7,12 +7,18 @@ so that you can participate in an online music education program through your sc JamKazam is an application and a platform that lets you play music with other students over the Internet, live and in sync. You can also use JamKazam to play and record yourself on your own. +<% if @reset_url %> To use the JamKazam app, you will need to sign in to the app using an email address and a password. The email address is the one to which this email was sent. Click the button below to set a password for your JamKazam account. If you're worried this is a scam, please check with your school or music program instructor to verify that this is a legitimate email. Set Password: <%= @reset_url %> + <% else %> + We noticed you already have an account at JamKazam, under the email address to which this email was sent. + Next time you log into the application, your account will have been associated with your school and has a subscription license from your school. + If you're worried this is a scam, please check with your school or music program instructor to verify that this is a legitimate email. + <% end %> Your instructor will help you to start using the JamKazam app in your music program with other students. If you already have your gear and want to set it up and start playing around with it, diff --git a/ruby/lib/jam_ruby/recurly_client.rb b/ruby/lib/jam_ruby/recurly_client.rb index 5de946f96..c37cf21cd 100644 --- a/ruby/lib/jam_ruby/recurly_client.rb +++ b/ruby/lib/jam_ruby/recurly_client.rb @@ -596,8 +596,6 @@ module JamRuby user.subscription_sync_code = 'good_standing_ignored' user.subscription_sync_msg = "user is in good standing but the desired plan is less than subscription plan; plan_code not touched" end - - else puts "good standing user #{user.email} had no changes" user.subscription_sync_code = 'good_standing_unchanged'