diff --git a/admin/app/admin/email_batch.rb b/admin/app/admin/email_batch.rb index bd934f6da..3f15aadd3 100644 --- a/admin/app/admin/email_batch.rb +++ b/admin/app/admin/email_batch.rb @@ -17,42 +17,54 @@ ActiveAdmin.register JamRuby::EmailBatch, :as => 'Batch Emails' do column 'Test Emails' do |pp| pp.test_emails end column 'Email Count' do |pp| pp.qualified_count end column 'Send Count' do |pp| pp.sent_count end - column 'Started At' do |pp| pp.started_at end - column 'Completed At' do |pp| pp.completed_at end + column 'Started' do |pp| pp.started_at end + column 'Completed' do |pp| pp.completed_at end + column 'Send Test' do |pp| + link_to("Test Batch (#{pp.test_count})", + batch_test_admin_batch_email_path(pp.id), + :confirm => "Run test batch with #{pp.test_count} emails?") + end + column 'Send Live' do |pp| + link_to("Live Batch (#{User.email_opt_in.count})", + batch_send_admin_batch_email_path(pp.id), + :confirm => "Run LIVE batch with #{User.email_opt_in.count} emails?") + end default_actions end action_item :only => :show do - link_to("Run Test Batch (#{resource.test_count})", - "/admin/batch_emails/#{resource.id}/batch_test", + link_to("Send Test Batch (#{resource.test_count})", + batch_test_admin_batch_email_path(resource.id), :confirm => "Run test batch with #{resource.test_count} emails?") end action_item :only => :show do - link_to("Run Live Batch (#{User.email_opt_in.count})", - "/admin/batch_emails/#{resource.id}/batch_send", + link_to("Send Live Batch (#{User.email_opt_in.count})", + batch_send_admin_batch_email_path(resource.id), :confirm => "Run LIVE batch with #{User.email_opt_in.count} emails?") end show :title => 'Batch Email' do |obj| panel 'Email Contents' do attributes_table_for obj do - row 'Subject' do |obj| obj.subject end row 'From' do |obj| obj.from_email end row 'Test Emails' do |obj| obj.test_emails end + row 'Subject' do |obj| obj.subject end row 'Body' do |obj| obj.body end - row 'State' do |obj| obj.aasm_state end end end columns do column do panel 'Sending Parameters' do attributes_table_for obj do - row 'Opt-in User Count' do |obj| User.email_opt_in.count end + row 'State' do |obj| obj.aasm_state end + row 'User Count' do |obj| + obj.qualified_count ? obj.qualified_count : User.email_opt_in.count + end row 'Sent Count' do |obj| obj.sent_count end - row 'Started At' do |obj| obj.started_at end - row 'Completed At' do |obj| obj.completed_at end + row 'Started' do |obj| obj.started_at end + row 'Completed' do |obj| obj.completed_at end row 'Updated' do |obj| obj.updated_at end end end @@ -67,8 +79,9 @@ ActiveAdmin.register JamRuby::EmailBatch, :as => 'Batch Emails' do controller do def create - EmailBatch.create_with_params(params[:jam_ruby_email_batch]) - redirect_to admin_batch_emails_path + batch = EmailBatch.create_with_params(params[:jam_ruby_email_batch]) + redirect_to admin_batch_email_path(batch.id) + # redirect_to admin_batch_emails_path end def update diff --git a/db/up/emails.sql b/db/up/emails.sql index 9c68b643e..86f8b4f6f 100644 --- a/db/up/emails.sql +++ b/db/up/emails.sql @@ -20,19 +20,19 @@ CREATE TABLE email_batches ( updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); -CREATE TABLE email_batch_results ( - id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(), - email_batch_id VARCHAR(64) REFERENCES email_batches(id) ON DELETE CASCADE, - user_id VARCHAR(64) REFERENCES users(id) ON DELETE CASCADE, +-- CREATE TABLE email_batch_results ( +-- id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(), +-- email_batch_id VARCHAR(64) REFERENCES email_batches(id) ON DELETE CASCADE, +-- user_id VARCHAR(64) REFERENCES users(id) ON DELETE CASCADE, - error_type VARCHAR(32), - email_address VARCHAR(256), +-- error_type VARCHAR(32), +-- email_address VARCHAR(256), - created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP -); +-- created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, +-- updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +-- ); -ALTER TABLE email_batch_results ADD CONSTRAINT email_batch_uniqkey UNIQUE (email_batch_id); -ALTER TABLE email_batch_results ADD CONSTRAINT email_user_uniqkey UNIQUE (user_id); +-- ALTER TABLE email_batch_results ADD CONSTRAINT email_batch_uniqkey UNIQUE (email_batch_id); +-- ALTER TABLE email_batch_results ADD CONSTRAINT email_user_uniqkey UNIQUE (user_id); -ALTER TABLE users ADD COLUMN opt_out_email_batch boolean DEFAULT false; +ALTER TABLE users ALTER COLUMN subscribe_email SET DEFAULT true; diff --git a/ruby/lib/jam_ruby/app/mailers/batch_mailer.rb b/ruby/lib/jam_ruby/app/mailers/batch_mailer.rb index 76af3a456..11e21efd2 100644 --- a/ruby/lib/jam_ruby/app/mailers/batch_mailer.rb +++ b/ruby/lib/jam_ruby/app/mailers/batch_mailer.rb @@ -1,12 +1,12 @@ module JamRuby class BatchMailer < JamRuby::AsyncMailer - layout "batch_mailer" + layout "user_mailer" sendgrid_category :use_subject_lines sendgrid_unique_args :env => Environment.mode def _send_batch(batch, users) - @body = batch.body + @batch_body = batch.body emails = users.map(&:email) sendgrid_recipients(emails) diff --git a/ruby/lib/jam_ruby/app/views/layouts/user_mailer.html.erb b/ruby/lib/jam_ruby/app/views/layouts/user_mailer.html.erb index 6fcd69d53..1198a26f4 100644 --- a/ruby/lib/jam_ruby/app/views/layouts/user_mailer.html.erb +++ b/ruby/lib/jam_ruby/app/views/layouts/user_mailer.html.erb @@ -24,13 +24,11 @@

<%= yield(:title) %>

-

<%= yield %>

+

<%= @batch_body ? @batch_body.html_safe : yield %>


- - <% unless @suppress_user_has_account_footer == true %> @@ -39,8 +37,8 @@ +

This email was sent to you because you have an account at JamKazam.  Click here to unsubscribe and update your profile settings.

-

This email was sent to you because you have an account at JamKazam. diff --git a/ruby/lib/jam_ruby/app/views/layouts/user_mailer.text.erb b/ruby/lib/jam_ruby/app/views/layouts/user_mailer.text.erb index 8bd3c7483..5c8262f63 100644 --- a/ruby/lib/jam_ruby/app/views/layouts/user_mailer.text.erb +++ b/ruby/lib/jam_ruby/app/views/layouts/user_mailer.text.erb @@ -1,8 +1,11 @@ -<%= yield %> - +<% if @batch_body %> + <%= Nokogiri::HTML(@batch_body).text %> +<% else %> + <%= yield %> +<% end %> <% unless @suppress_user_has_account_footer == true %> -This email was sent to you because you have an account at JamKazam / http://www.jamkazam.com. +This email was sent to you because you have an account at JamKazam / http://www.jamkazam.com. Visit your profile page to unsubscribe: http://www.jamkazam.com/client#/account/profile. <% end %> Copyright <%= Time.now.year %> JamKazam, Inc. All rights reserved. diff --git a/ruby/lib/jam_ruby/models/email_batch.rb b/ruby/lib/jam_ruby/models/email_batch.rb index 1910cd7ff..a2fd36387 100644 --- a/ruby/lib/jam_ruby/models/email_batch.rb +++ b/ruby/lib/jam_ruby/models/email_batch.rb @@ -46,23 +46,15 @@ module JamRuby def self.create_with_params(params) obj = self.new - obj.update_with_params(params) - obj.save! + params.each { |kk,vv| vv.strip! } + obj.update_with_conflict_validation(params) obj end - def update_with_params(params) - self.from_email = params[:from_email].strip - self.subject = params[:subject].strip - self.test_emails = params[:test_emails].strip - self.body = params[:body].strip - self - end - def deliver_batch self.perform_event('do_batch_run!') - User.email_opt_in.pluck(:id).find_in_batches(batch_size: 1000) do |user_ids| - BatchMailer.send_batch_email(self.id, user_ids).deliver + User.email_opt_in.find_in_batches(batch_size: 1000) do |users| + BatchMailer.send_batch_email(self.id, users.map(&:id)).deliver end end diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index 7651d14ca..1ff5f9513 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -132,7 +132,7 @@ module JamRuby scope :fans, where(:musician => false) scope :geocoded_users, where(['lat IS NOT NULL AND lng IS NOT NULL']) scope :musicians_geocoded, musicians.geocoded_users - scope :email_opt_in, where(:opt_out_email_batch => false) + scope :email_opt_in, where(:subscribe_email => true) 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" ] diff --git a/ruby/spec/jam_ruby/models/email_batch_spec.rb b/ruby/spec/jam_ruby/models/email_batch_spec.rb index 19d8a125f..e3b14c32d 100644 --- a/ruby/spec/jam_ruby/models/email_batch_spec.rb +++ b/ruby/spec/jam_ruby/models/email_batch_spec.rb @@ -9,9 +9,10 @@ describe EmailBatch do it 'has test emails setup' do expect(email_batch.test_emails.present?).to be true + expect(email_batch.pending?).to be true users = email_batch.test_users - expect(email_batch.merged_body(users[0])).to match(/#{users[0].first_name} #{email_batch.body}/) + expect(email_batch.test_count).to eq(users.count) end end diff --git a/ruby/spec/mailers/batch_mailer_spec.rb b/ruby/spec/mailers/batch_mailer_spec.rb index a2d45dd6c..a1a216607 100644 --- a/ruby/spec/mailers/batch_mailer_spec.rb +++ b/ruby/spec/mailers/batch_mailer_spec.rb @@ -3,6 +3,8 @@ require "spec_helper" describe BatchMailer do describe "should send test emails" do + BatchMailer.deliveries.clear + let (:mail) { BatchMailer.deliveries[0] } batch = FactoryGirl.create(:email_batch)