diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index 48f217d47..f51815bf8 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -43,6 +43,7 @@ require "jam_ruby/resque/scheduled/icecast_source_check" require "jam_ruby/resque/scheduled/cleanup_facebook_signup" require "jam_ruby/resque/scheduled/unused_music_notation_cleaner" require "jam_ruby/resque/scheduled/user_progress_emailer" +require "jam_ruby/resque/scheduled/daily_session_emailer" require "jam_ruby/resque/google_analytics_event" require "jam_ruby/mq_router" require "jam_ruby/base_manager" @@ -149,6 +150,7 @@ require "jam_ruby/models/email_batch" require "jam_ruby/models/email_batch_periodic" require "jam_ruby/models/email_batch_new_musician" require "jam_ruby/models/email_batch_progression" +require "jam_ruby/models/email_batch_scheduled_sessions" require "jam_ruby/models/email_batch_set" require "jam_ruby/models/email_error" require "jam_ruby/app/mailers/async_mailer" diff --git a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb index 23bdd724c..9b2406c0d 100644 --- a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb +++ b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb @@ -391,6 +391,24 @@ end end + def scheduled_session_daily(receiver, sessions_and_latency) + sendgrid_category "Notification" + sendgrid_unique_args :type => "scheduled_session_daily" + + sendgrid_recipients([receiver.email]) + sendgrid_substitute('@USERID', [receiver.id]) + + @user = receiver + @sessions_and_latency = sessions_and_latency + + @title = 'New Scheduled Sessions Matched to You' + mail(:to => receiver.email, + :subject => EmailBatchScheduledSessions.subject) do |format| + format.text + format.html + end + end + def band_session_join(email, msg, session_id) subject = "A band that you follow has joined a session" unique_args = {:type => "band_session_join"} diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.html.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.html.erb new file mode 100644 index 000000000..40788d8de --- /dev/null +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.html.erb @@ -0,0 +1,38 @@ +<% provide(:title, @title) %> + +

Hello <%= @user.first_name %> -- +

+ +

The following new sessions that that have been posted during the last 24 hours: +

+
    +
  1. Need someone who plays an instrument that you play
  2. +
  3. Were posted by someone to whom you have either a good or medium latency connection
  4. +
+ +

Take a look through these new sessions below, and just click the RSVP button on the far right side of the row for any session in which you'd like to play. This will let the session organizer know you're interested, and you'll be notified if the session organizer accepts your request to play in that session! +

+ + + + + + + + + +<% @sessions_and_latency.each do |sess| %> + + + + + +<% end %> +
GENREDESCRIPTIONLATENCY
<%= sess.genre.description %><%= sess.description %><%= sess.latency_store %>
+ +

To see ALL the scheduled sessions that you might be interested in joining, view our Find Session page at: http://www.jamkazam.com/client#/findSession. +

+ +

Best Regards,

+ +Team JamKazam diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.text.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.text.erb new file mode 100644 index 000000000..16ce1e7c1 --- /dev/null +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.text.erb @@ -0,0 +1,21 @@ +<% provide(:title, @title) %> + +Hello <%= @user.first_name %> -- + +The following new sessions that that have been posted during the last 24 hours: + +1. Need someone who plays an instrument that you play +2. Were posted by someone to whom you have either a good or medium latency connection + +Take a look through these new sessions below, and just click the RSVP button on the far right side of the row for any session in which you'd like to play. This will let the session organizer know you're interested, and you'll be notified if the session organizer accepts your request to play in that session! + +GENRE | DESCRIPTION | LATENCY +<% @sessions_and_latency.each do |sess| %> +<%= sess.genre.description %> | <%= sess.description %> | <%= sess.latency_store %> +<% end %> + +To see ALL the scheduled sessions that you might be interested in joining, view our Find Session page at: http://www.jamkazam.com/client#/findSession. + +Best Regards, + +Team JamKazam diff --git a/ruby/lib/jam_ruby/models/email_batch_scheduled_sessions.rb b/ruby/lib/jam_ruby/models/email_batch_scheduled_sessions.rb new file mode 100644 index 000000000..db13ea5b8 --- /dev/null +++ b/ruby/lib/jam_ruby/models/email_batch_scheduled_sessions.rb @@ -0,0 +1,129 @@ +module JamRuby + class EmailBatchScheduledSessions < EmailBatchPeriodic + + BATCH_SIZE = 500 + SINCE_DAYS = 2 + MIN_HOURS_START = 2 + + TMP_SNAP = 'scheduled_session_snapshot' + TMP_USER = 'scheduled_session_user' + + def self.subject + "New sessions have been scheduled that may be a good match for you!" + end + + # def fetch_sessions + # objs = [] + # MusicSession.open_sessions + # .where(['created_at > ?', time_since_last_batch(SINCE_DAYS)]) + # .where(['scheduled_start >= ?', Time.now() + MIN_HOURS_START.hours]) + # .find_each do |ss| + # block_given? ? yield(ss) : objs << ss + # end + # objs + # end + + # inserts eligible sessions to temp table + def prep_tmp_table + ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_SNAP}") + sql =< '#{time_since_last_batch(SINCE_DAYS)}' AND + scheduled_start >= '#{Time.now() + MIN_HOURS_START.hours}' AND + (rrrs.rsvp_slot_id IS NULL OR rrrs.chosen != 't') +SQL + ActiveRecord::Base.connection.execute(sql) + end + + def fetch_recipients + objs = [] + # load eligible sessions into tmp table + self.prep_tmp_table + ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_USER}") + + # load eligible recipients into tmp table + sql =< JamRuby::EmailBatchProgression do end + factory :email_batch_scheduled_session, :class => JamRuby::EmailBatchScheduledSessions do + end + factory :notification, :class => JamRuby::Notification do factory :notification_text_message do @@ -474,7 +477,7 @@ FactoryGirl.define do factory :rsvp_slot, class: JamRuby::RsvpSlot do association :instrument, factory: :instrument association :music_session, factory: :music_session - association :rsvp_request_slot, factory: :rsvp_request_slot + # association :rsvp_request_slot, factory: :rsvp_request_slot proficiency_level 'beginner' end diff --git a/ruby/spec/jam_ruby/models/email_batch_spec_scheduled_session.rb b/ruby/spec/jam_ruby/models/email_batch_spec_scheduled_session.rb new file mode 100644 index 000000000..981a6d5f2 --- /dev/null +++ b/ruby/spec/jam_ruby/models/email_batch_spec_scheduled_session.rb @@ -0,0 +1,106 @@ +require 'spec_helper' + +describe EmailBatch do + + after(:each) do + Timecop.return + end + + before(:each) do + UserMailer.deliveries.clear + end + + describe 'daily scheduled' do + # before { pending } + + let (:scheduled_batch) { FactoryGirl.create(:email_batch_scheduled_session) } + + let (:drums) { FactoryGirl.create(:instrument, :description => 'drums') } + let (:guitar) { FactoryGirl.create(:instrument, :description => 'guitar') } + let (:bass) { FactoryGirl.create(:instrument, :description => 'bass') } + let (:vocals) { FactoryGirl.create(:instrument, :description => 'vocal') } + + let (:drummer) { FactoryGirl.create(:user, + :last_jam_locidispid => 1, + :last_jam_addr => 1) } + let (:guitarist) { FactoryGirl.create(:user, + :last_jam_locidispid => 1, + :last_jam_addr => 1) } + let (:bassist) { FactoryGirl.create(:user, + :last_jam_locidispid => 1, + :last_jam_addr => 1) } + let (:vocalist) { FactoryGirl.create(:user, + :last_jam_locidispid => 1, + :last_jam_addr => 1) } + let (:loser) { FactoryGirl.create(:user, + :last_jam_locidispid => 2, + :last_jam_addr => 2) } + + let (:session1) do + FactoryGirl.create(:music_session, + :creator => drummer, + :scheduled_start => Time.now() + 2.days, + :musician_access => true, + :approval_required => false) + end + let (:session2) do + FactoryGirl.create(:music_session, + :creator => drummer, + :scheduled_start => Time.now() + 2.days, + :musician_access => true, + :approval_required => false) + end + + before(:each) do + MusicianInstrument.delete_all + RsvpSlot.delete_all + JamRuby::Score.delete_all + + drummer.musician_instruments << FactoryGirl.build(:musician_instrument, user: drummer, instrument: drums, proficiency_level: 2) + drummer.musician_instruments << FactoryGirl.build(:musician_instrument, user: drummer, instrument: guitar, proficiency_level: 2) + + guitarist.musician_instruments << FactoryGirl.build(:musician_instrument, user: guitarist, instrument: guitar, proficiency_level: 2) + guitarist.musician_instruments << FactoryGirl.build(:musician_instrument, user: guitarist, instrument: bass, proficiency_level: 2) + + bassist.musician_instruments << FactoryGirl.build(:musician_instrument, user: bassist, instrument: bass, proficiency_level: 2) + bassist.musician_instruments << FactoryGirl.build(:musician_instrument, user: bassist, instrument: guitar, proficiency_level: 2) + + vocalist.musician_instruments << FactoryGirl.build(:musician_instrument, user: vocalist, instrument: vocals, proficiency_level: 2) + + loser.musician_instruments << FactoryGirl.build(:musician_instrument, user: loser, instrument: vocals, proficiency_level: 2) + loser.musician_instruments << FactoryGirl.build(:musician_instrument, user: loser, instrument: drums, proficiency_level: 2) + + FactoryGirl.create(:rsvp_slot, :instrument => drums, :music_session => session1) + FactoryGirl.create(:rsvp_slot, :instrument => guitar, :music_session => session1) + FactoryGirl.create(:rsvp_slot, :instrument => bass, :music_session => session1) + + FactoryGirl.create(:rsvp_slot, :instrument => drums, :music_session => session2) + FactoryGirl.create(:rsvp_slot, :instrument => guitar, :music_session => session2) + FactoryGirl.create(:rsvp_slot, :instrument => bass, :music_session => session2) + # oo = FactoryGirl.create(:rsvp_slot, :instrument => vocals, :music_session => session1) + # oo.rsvp_request_slot.update_attributes(chosen: true) + # oo = FactoryGirl.create(:rsvp_request, :user => vocalist, :rsvp_slot => oo) + # oo.rsvp_request_slot.update_attributes(chosen: true) + + JamRuby::Score.createx(1, 'a', 1, 1, 'a', 1, 10) + JamRuby::Score.createx(1, 'a', 1, 2, 'a', 2, Score::MAX_YELLOW_LATENCY + 1) + end + + before(:each) do + end + + it 'sets up data properly' do + expect(drummer.instruments.include?(drums)).to eq(true) + expect(drummer.instruments.include?(guitar)).to eq(true) + obj = scheduled_batch.fetch_recipients + expect(obj.count).to eq(3) + end + + it 'sends email' do + ebatch = scheduled_batch + ebatch.deliver_batch + expect(UserMailer.deliveries.length).to eq(3) + end + + end +end diff --git a/web/config/scheduler.yml b/web/config/scheduler.yml index 3617cd3ed..b5f180ea1 100644 --- a/web/config/scheduler.yml +++ b/web/config/scheduler.yml @@ -33,3 +33,8 @@ UserProgressEmailer: cron: "30 21 * * *" class: "JamRuby::UserProgressEmailer" description: "Sends periodic user progress emails" + +DailySessionEmailer: + cron: "0 6 * * *" + class: "JamRuby::DailySessionEmailer" + description: "Sends daily scheduled session emails"