diff --git a/admin/app/admin/teachers.rb b/admin/app/admin/teachers.rb index d8f3d245e..9ffa7e3d9 100644 --- a/admin/app/admin/teachers.rb +++ b/admin/app/admin/teachers.rb @@ -6,15 +6,29 @@ ActiveAdmin.register JamRuby::Teacher, :as => 'Teachers' do config.batch_actions = false config.per_page = 100 config.paginate = true + config.filters = false - scope("Default", default: true) { |scope| scope.unscoped.order("background_check_at > '#{(Date.today - 365).to_s}}' NULLS FIRST, ready_for_session_at IS NULL DESC") } + scope("All", default: true) { |scope| scope.unscoped.order("background_check_at > '#{(Date.today - 365).to_s}}' NULLS FIRST, ready_for_session_at IS NULL DESC") } + scope("All Sorted By Sign Up") { |scope| scope.unscoped.order("teachers.created_at DESC, background_check_at > '#{(Date.today - 365).to_s}}' NULLS FIRST, ready_for_session_at IS NULL DESC") } + scope("50% and Session Ready" ) { |scope| scope.unscoped.where('profile_pct >= ?', 50.0).where('ready_for_session_at IS NOT NULL').order("background_check_at > '#{(Date.today - 365).to_s}}' NULLS FIRST, ready_for_session_at IS NULL DESC") } + scope("50% and Not Session Ready" ) { |scope| scope.unscoped.where('profile_pct > ?', 50.0).where('ready_for_session_at IS NULL').order("background_check_at > '#{(Date.today - 365).to_s}}' NULLS FIRST, ready_for_session_at IS NULL DESC") } index do column "Name" do |teacher| - link_to teacher.user.name, "#{Rails.application.config.external_root_url}/client#/profile/teacher/#{teacher.user.id}" + span do + link_to "#{teacher.user.name} (#{teacher.user.email})", "#{Rails.application.config.external_root_url}/client#/profile/teacher/#{teacher.user.id}" + end end - column "Email" do |teacher| - teacher.user.email + column "Instruments & Genres" do |teacher| + div do + div do + teacher.instruments.join(', ') + end + br + div do + teacher.genres.join(', ') + end + end end column "Location" do |teacher| teacher.user.location(country = true) @@ -57,11 +71,8 @@ ActiveAdmin.register JamRuby::Teacher, :as => 'Teachers' do link_to("mark as checked", mark_background_check_admin_teacher_path(teacher.id), {confirm: "Mark as background checked?"}) end end - - end end - column "Session Ready" do |teacher| div do if teacher.ready_for_session_at @@ -108,6 +119,10 @@ ActiveAdmin.register JamRuby::Teacher, :as => 'Teachers' do end end + column "Signed Up" do |teacher| + teacher.created_at.to_date + end + end show do @@ -228,6 +243,11 @@ ActiveAdmin.register JamRuby::Teacher, :as => 'Teachers' do end end + + row "Signed Up" do |teacher| + teacher.created_at.to_date + end + end end diff --git a/admin/app/controllers/email_controller.rb b/admin/app/controllers/email_controller.rb index 45af1bb2e..9644dfca1 100644 --- a/admin/app/controllers/email_controller.rb +++ b/admin/app/controllers/email_controller.rb @@ -21,4 +21,19 @@ class EmailController < ApplicationController @users = @users.select('DISTINCT users.id, email, first_name, last_name').joins(:sales => :sale_line_items).where("sale_line_items.product_type = 'JamTrack'") end end + + def dump_teachers + + if params[:code] != Rails.application.config.email_dump_code + render :text => "", :status => 404 + return + end + + headers['Content-Disposition'] = "attachment; filename=\"teacher-list.csv\"" + headers['Content-Type'] ||= 'text/csv' + + @users = User.joins(:teacher) + + render "dump_emailables.csv.erb" + end end \ No newline at end of file diff --git a/admin/config/routes.rb b/admin/config/routes.rb index 4565d26a2..7c671b018 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -30,6 +30,7 @@ JamAdmin::Application.routes.draw do match '/api/checks/latency_tester' => 'checks#check_latency_tester', :via => :get match '/api/users/emailables/:code' => 'email#dump_emailables', :via => :get + match '/api/teachers/:code' => 'email#dump_teachers', :via => :get match '/jam_tracks/top/:code' => 'jam_track#dump_top_selling', :via => :get match '/api/jam_tracks/released' => 'jam_track#dump_released', :via => :get, as: 'released_jamtracks_csv' diff --git a/db/manifest b/db/manifest index 2f7d0b329..4067937f3 100755 --- a/db/manifest +++ b/db/manifest @@ -340,4 +340,5 @@ jamblaster_pairing_active.sql email_blacklist.sql jamblaster_connection.sql teacher_progression.sql -lessons.sql +teacher_complete.sql +lessons.sql \ No newline at end of file diff --git a/db/up/teacher_complete.sql b/db/up/teacher_complete.sql new file mode 100644 index 000000000..7e67f1a20 --- /dev/null +++ b/db/up/teacher_complete.sql @@ -0,0 +1,2 @@ +ALTER TABLE teachers ADD COLUMN profile_pct NUMERIC(8,2) ; +ALTER TABLE teachers ADD COLUMN profile_pct_summary JSON; \ No newline at end of file diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index ab9cba921..daea0fa46 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -30,6 +30,7 @@ require "jam_ruby/constants/validation_messages" require "jam_ruby/errors/jam_permission_error" require "jam_ruby/errors/state_error" require "jam_ruby/errors/jam_argument_error" +require "jam_ruby/errors/jam_record_not_found" require "jam_ruby/errors/conflict_error" require "jam_ruby/lib/app_config" require "jam_ruby/lib/s3_manager_mixin" diff --git a/ruby/lib/jam_ruby/base_manager.rb b/ruby/lib/jam_ruby/base_manager.rb index cc633fd1a..bf5dab694 100644 --- a/ruby/lib/jam_ruby/base_manager.rb +++ b/ruby/lib/jam_ruby/base_manager.rb @@ -5,7 +5,10 @@ module JamRuby @@log = Logging.logger[BaseManager] - @@in_websocket_gateway = Rails.env != 'test' && !Object.const_defined?(:UserManager) + # this is not working as expected + #@@in_websocket_gateway = Rails.env != 'test' && !Object.const_defined?(:UserManager) + + @@in_websocket_gateway = false def initialize(options={}) @log = Logging.logger[self] @@ -24,7 +27,6 @@ module JamRuby # create a transaction, and pass the current connection to ConnectionManager. # this lets the entire operation work with the same transaction, # across Rails ActiveRecord and the pg-gem based code in ConnectionManager. - @@log.debug "DEBUG_TRAN -1" manager.pg_conn = connection.instance_variable_get("@connection") if @@in_websocket_gateway @@ -37,9 +39,7 @@ module JamRuby ActiveRecord::Base.connection.execute('ROLLBACK') end else - @@log.debug "DEBUG_TRAN -2" connection.transaction do - @@log.debug "DEBUG_TRAN -3" yield manager end end diff --git a/ruby/lib/jam_ruby/connection_manager.rb b/ruby/lib/jam_ruby/connection_manager.rb index 4ed60a919..64b84ddbb 100644 --- a/ruby/lib/jam_ruby/connection_manager.rb +++ b/ruby/lib/jam_ruby/connection_manager.rb @@ -405,17 +405,20 @@ SQL def join_music_session(user, client_id, music_session, as_musician, tracks, audio_latency, video_sources=nil) connection = nil - @log.debug "DEBUG_TRAN 1" ConnectionManager.active_record_transaction do |connection_manager| - @log.debug "DEBUG_TRAN 2" db_conn = connection_manager.pg_conn - @log.debug "DEBUG_TRAN 3" - connection = Connection.find_by_client_id_and_user_id!(client_id, user.id) - @log.debug "DEBUG_TRAN 4" + connection = Connection.find_by_client_id(client_id) + + if connection.nil? + raise JamRecordNotFound.new("Unable to find connection by client_id #{client_id}", 'Connection') + elsif connection.user_id.nil? + raise JamPermissionError, "no user_id associated with connection #{client_id}" + elsif connection.user_id != user.id + raise JamPermissionError, "wrong user_id associated with connection #{client_id}" + end connection.join_the_session(music_session, as_musician, tracks, user, audio_latency, video_sources) - @log.debug "DEBUG_TRAN 5" JamRuby::MusicSessionUserHistory.join_music_session(user.id, music_session.id) # connection.music_session_id = music_session.id # connection.as_musician = as_musician @@ -423,33 +426,18 @@ SQL # connection.joined_session_at = Time.now # associate_tracks(connection, tracks) # connection.save - @log.debug "DEBUG_TRAN 6" if connection.errors.any? - @log.debug "DEBUG_TRAN 7" raise ActiveRecord::Rollback else - @log.debug "DEBUG_TRAN 8" update_session_controller(music_session.id) end end - @log.debug "DEBUG_TRAN 9" connection end - def testfunc() - value = nil - - ConnectionManager.active_record_transaction do |connection_manager| - db_conn = connection_manager.pg_conn - value = 1 - raise ActiveRecord::Rollback - end - - value - end # if a blk is passed in, upon success, it will be called and you can issue notifications # within the connection table lock diff --git a/ruby/lib/jam_ruby/errors/jam_record_not_found.rb b/ruby/lib/jam_ruby/errors/jam_record_not_found.rb new file mode 100644 index 000000000..d000f58d5 --- /dev/null +++ b/ruby/lib/jam_ruby/errors/jam_record_not_found.rb @@ -0,0 +1,12 @@ +module JamRuby + class JamRecordNotFound < StandardError + + attr_accessor :missing_message, :record_type + + def initialize(message, record_type) + @message = message + @missing_message = message + @record_type = record_type + end + end +end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/jam_track.rb b/ruby/lib/jam_ruby/models/jam_track.rb index 5b544c991..1ad7fb09d 100644 --- a/ruby/lib/jam_ruby/models/jam_track.rb +++ b/ruby/lib/jam_ruby/models/jam_track.rb @@ -255,6 +255,15 @@ module JamRuby {artists: artists, songs:songs} end + def purchase_stubs(user) + JamTrack. + select(['jam_tracks.id', :name, :original_artist, :year, 'jam_track_rights.created_at AS purchased_at']). + joins(:jam_track_rights). + where("jam_track_rights.user_id = ?", user.id). + includes(:genres). + order([:original_artist, :name]) + end + def index(options, user) if options[:page] page = options[:page].to_i @@ -583,5 +592,9 @@ SQL self.find_by_sql(sql).first end + def genre_name + self.genres.first.try(:description) + end + end end diff --git a/ruby/lib/jam_ruby/models/teacher.rb b/ruby/lib/jam_ruby/models/teacher.rb index e1f02d239..101ed40f7 100644 --- a/ruby/lib/jam_ruby/models/teacher.rb +++ b/ruby/lib/jam_ruby/models/teacher.rb @@ -37,6 +37,15 @@ module JamRuby default_scope { includes(:genres).order('created_at desc') } + after_save :update_profile_pct + + def update_profile_pct + result = pct_complete + self.profile_pct = result[:pct] + self.profile_pct_summary = result.to_json + Teacher.where(id: id).update_all(profile_pct: self.profile_pct, profile_pct_summary: self.profile_pct_summary) + end + def self.index(user, params = {}) limit = params[:per_page] diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index 9d5379de5..4ab5a538d 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -36,7 +36,7 @@ module JamRuby acts_as_mappable - # after_save :check_lat_lng + after_save :update_teacher_pct attr_accessible :first_name, :last_name, :email, :city, :password, :password_confirmation, :state, :country, :birth_date, :subscribe_email, :terms_of_service, :original_fpfile, :cropped_fpfile, :cropped_large_fpfile, :cropped_s3_path, :cropped_large_s3_path, :photo_url, :large_photo_url, :crop_selection @@ -253,6 +253,11 @@ module JamRuby scope :musicians_geocoded, musicians.geocoded_users scope :email_opt_in, where(:subscribe_email => true) + def update_teacher_pct + if teacher + teacher.update_profile_pct + end + end def user_progression_fields @user_progression_fields ||= Set.new ["first_downloaded_client_at", "first_ran_client_at", "first_music_session_at", "first_real_music_session_at", "first_good_music_session_at", "first_certified_gear_at", "first_invited_at", "first_friended_at", "first_recording_at", "first_social_promoted_at", "first_played_jamtrack_at"] end diff --git a/ruby/spec/jam_ruby/connection_manager_spec.rb b/ruby/spec/jam_ruby/connection_manager_spec.rb index b723d3f5d..ae6d7feab 100644 --- a/ruby/spec/jam_ruby/connection_manager_spec.rb +++ b/ruby/spec/jam_ruby/connection_manager_spec.rb @@ -107,7 +107,7 @@ describe ConnectionManager, no_transaction: true do cc.locidispid.should == 17192000002 cc.udp_reachable.should == true - @connman.reconnect(cc, channel_id, nil, "33.1.2.3", STALE_TIME, EXPIRE_TIME, false, GATEWAY, false) + @connman.reconnect(cc, channel_id, nil, "33.1.2.3", STALE_TIME, EXPIRE_TIME, false, GATEWAY) cc = Connection.find_by_client_id!(client_id) cc.connected?.should be_true @@ -147,7 +147,7 @@ describe ConnectionManager, no_transaction: true do cc.locidispid.should == 17192000002 cc.udp_reachable.should == false - @connman.reconnect(cc, channel_id, nil, "33.1.2.3", STALE_TIME, EXPIRE_TIME, nil, GATEWAY, false) # heartbeat passes nil in for udp_reachable + @connman.reconnect(cc, channel_id, nil, "33.1.2.3", STALE_TIME, EXPIRE_TIME, nil, GATEWAY) # heartbeat passes nil in for udp_reachable cc = Connection.find_by_client_id!(client_id) cc.connected?.should be_true @@ -356,7 +356,7 @@ describe ConnectionManager, no_transaction: true do user = User.find(user_id) - expect { @connman.join_music_session(user, client_id, music_session, true, TRACKS, 10) }.to raise_error(ActiveRecord::RecordNotFound) + expect { @connman.join_music_session(user, client_id, music_session, true, TRACKS, 10) }.to raise_error(JamRuby::JamRecordNotFound) end @@ -429,7 +429,7 @@ describe ConnectionManager, no_transaction: true do @connman.create_connection(user_id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE, GATEWAY, false) # specify real user id, but not associated with this session - expect { @connman.join_music_session(user, client_id, music_session, true, TRACKS, 10) } .to raise_error(ActiveRecord::RecordNotFound) + expect { @connman.join_music_session(user, client_id, music_session, true, TRACKS, 10) } .to raise_error(JamRuby::JamPermissionError) end it "join_music_session fails if no music_session" do @@ -455,7 +455,7 @@ describe ConnectionManager, no_transaction: true do @connman.create_connection(user_id, client_id, channel_id, "1.1.1.1", 'client', STALE_TIME, EXPIRE_TIME, REACHABLE, GATEWAY, false) # specify real user id, but not associated with this session - expect { @connman.join_music_session(user, client_id, music_session, true, TRACKS, 10) } .to raise_error(ActiveRecord::RecordNotFound) + expect { @connman.join_music_session(user, client_id, music_session, true, TRACKS, 10) } .to raise_error(JamRuby::JamPermissionError) end diff --git a/ruby/spec/jam_ruby/models/active_music_session_spec.rb b/ruby/spec/jam_ruby/models/active_music_session_spec.rb index 827dcaaf7..2dc75cfc8 100644 --- a/ruby/spec/jam_ruby/models/active_music_session_spec.rb +++ b/ruby/spec/jam_ruby/models/active_music_session_spec.rb @@ -17,7 +17,14 @@ describe ActiveMusicSession do it "fails gracefully when no connection" do music_session = FactoryGirl.create(:active_music_session, :creator => user, :musician_access => false) - expect { ActiveMusicSession.participant_create(user, music_session.id, "junk", true, nil, 5) }.to raise_error(ActiveRecord::RecordNotFound) + begin + ActiveMusicSession.participant_create(user, music_session.id, "junk", true, nil, 5) + false.should be_true + rescue JamRuby::JamRecordNotFound => e + e.record_type.should eql "Connection" + e.missing_message.should eql "Unable to find connection by client_id junk" + end + end it "succeeds no active music session" do @@ -34,12 +41,6 @@ describe ActiveMusicSession do expect { ActiveMusicSession.participant_create(user, 'bad music session ID', conn.client_id, true, nil, 5) }.to raise_error(ActiveRecord::RecordNotFound) end - it "fails gracefully when invalid music session Id" do - music_session = FactoryGirl.create(:active_music_session, :creator => user, :musician_access => false) - conn = FactoryGirl.create(:connection, :user => user) - ActiveMusicSession.participant_create(user, 'bad music session ID', conn.client_id, true, nil, 5) - end - it "pulls out of other session" do music_session1 = FactoryGirl.create(:active_music_session, :creator => user, :musician_access => false) music_session2 = FactoryGirl.create(:active_music_session, :creator => user, :musician_access => false) @@ -52,7 +53,8 @@ describe ActiveMusicSession do it "user-less connection bails appropriately" do music_session1 = FactoryGirl.create(:active_music_session, :creator => user, :musician_access => false) conn = FactoryGirl.create(:connection) - expect { ActiveMusicSession.participant_create(user, music_session1.id, conn.client_id, true, nil, 5) }.to raise_error(ActiveRecord::RecordNotFound) + ActiveRecord::Base.connection.execute("update connections set user_id = NULL where connections.id = '#{conn.id}'") + expect { ActiveMusicSession.participant_create(user, music_session1.id, conn.client_id, true, nil, 5) }.to raise_error(JamRuby::JamPermissionError) end end diff --git a/web/app/controllers/api_controller.rb b/web/app/controllers/api_controller.rb index 94ed0a4a4..9b3328527 100644 --- a/web/app/controllers/api_controller.rb +++ b/web/app/controllers/api_controller.rb @@ -19,6 +19,10 @@ class ApiController < ApplicationController @exception = exception render "errors/permission_error", :status => 403 end + rescue_from 'JamRuby::JamRecordNotFound' do |exception| + @exception = exception + render "errors/record_not_found", :status => 404 + end rescue_from 'JamRuby::ConflictError' do |exception| @exception = exception render "errors/conflict_error", :status => 409 diff --git a/web/app/controllers/api_jam_tracks_controller.rb b/web/app/controllers/api_jam_tracks_controller.rb index 8498c5dc2..5bfae3f8c 100644 --- a/web/app/controllers/api_jam_tracks_controller.rb +++ b/web/app/controllers/api_jam_tracks_controller.rb @@ -142,7 +142,13 @@ class ApiJamTracksController < ApiController self._handlePurchasedHead return end - params[:show_purchased_only] = true + if params[:mobile] + @jam_tracks = JamTrack.purchase_stubs(current_user).to_a + response.headers['total-entries'] = @jam_tracks.count.to_s + render "api_jam_tracks/purchased_mobile", :layout => nil + return + end + params[:show_purchased_only] = true data = JamTrack.index(params, current_user) @jam_tracks, @next = data[0], data[1] diff --git a/web/app/views/api_jam_tracks/purchased_mobile.rabl b/web/app/views/api_jam_tracks/purchased_mobile.rabl new file mode 100644 index 000000000..c462fe1fe --- /dev/null +++ b/web/app/views/api_jam_tracks/purchased_mobile.rabl @@ -0,0 +1,3 @@ +node :jamtracks do |page| + partial "api_jam_tracks/show_for_mobile", object: @jam_tracks +end diff --git a/web/app/views/api_jam_tracks/show_for_mobile.rabl b/web/app/views/api_jam_tracks/show_for_mobile.rabl new file mode 100644 index 000000000..79b5183c7 --- /dev/null +++ b/web/app/views/api_jam_tracks/show_for_mobile.rabl @@ -0,0 +1,7 @@ +object @jam_track + +attributes :id, :name, :original_artist, :year, :genre_name + +node :purchased_at do |jt| + Time.parse(jt.purchased_at).to_i rescue Time.now.to_i +end diff --git a/web/app/views/errors/record_not_found.rabl b/web/app/views/errors/record_not_found.rabl new file mode 100644 index 000000000..c64f78d6e --- /dev/null +++ b/web/app/views/errors/record_not_found.rabl @@ -0,0 +1,11 @@ +object @exception + +attributes :record_type + +node "message" do |e| + e.missing_message +end + +node "type" do + "JamRecordNotFound" +end diff --git a/web/lib/tasks/users.rake b/web/lib/tasks/users.rake index b84af130e..e64e07ba0 100644 --- a/web/lib/tasks/users.rake +++ b/web/lib/tasks/users.rake @@ -15,4 +15,10 @@ namespace :users do end end + task :update_profile_pct do |task, args| + Teacher.all.each do |teacher| + teacher.update_profile_pct + end + end + end