VRFS-1483 adding first set of updates for batch emails

This commit is contained in:
Jonathan Kolyer 2014-03-18 15:07:45 +00:00
parent 44da456a42
commit e6d0851391
8 changed files with 463 additions and 0 deletions

View File

@ -136,4 +136,5 @@ events.sql
cascading_delete_constraints_for_release.sql
events_social_description.sql
fix_broken_cities.sql
emails.sql

32
db/up/emails.sql Normal file
View File

@ -0,0 +1,32 @@
CREATE TABLE email_batches (
id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
subject VARCHAR(256) NOT NULL,
body TEXT NOT NULL,
aasm_state VARCHAR(32) NOT NULL default 'pending',
test_emails TEXT NOT NULL,
batch_size INTEGER NOT NULL default 1000,
started_at TIMESTAMP,
completed_at TIMESTAMP,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
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,
error_type VARCHAR(32),
email_address VARCHAR(256),
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 users ADD COLUMN opt_out_email_batch boolean DEFAULT false;

View File

@ -131,6 +131,7 @@ require "jam_ruby/models/playable_play"
require "jam_ruby/models/country"
require "jam_ruby/models/region"
require "jam_ruby/models/city"
require "jam_ruby/models/email_batch"
include Jampb

View File

@ -0,0 +1,241 @@
module JamRuby
class BatchMailer < ActionMailer::Base
include SendGrid
layout "batch_mailer"
DEFAULT_SENDER = "support@jamkazam.com"
default :from => DEFAULT_SENDER
sendgrid_category :batch_email
sendgrid_category :use_subject_lines
sendgrid_unique_args :env => Environment.mode
def send_batch_email(user, signup_confirm_url)
@user = user
@signup_confirm_url = signup_confirm_url
sendgrid_category "Confirm Email"
sendgrid_unique_args :type => "confirm_email"
mail(:to => user.email, :subject => "Please confirm your JamKazam email") do |format|
format.text
format.html
end
end
def welcome_message(user)
@user = user
sendgrid_category "Welcome"
sendgrid_unique_args :type => "welcome_message"
mail(:to => user.email, :subject => "Welcome to JamKazam") do |format|
format.text
format.html
end
end
def password_changed(user)
@user = user
sendgrid_unique_args :type => "password_changed"
mail(:to => user.email, :subject => "JamKazam Password Changed") do |format|
format.text
format.html
end
end
def password_reset(user, password_reset_url)
@user = user
@password_reset_url = password_reset_url
sendgrid_unique_args :type => "password_reset"
mail(:to => user.email, :subject => "JamKazam Password Reset") do |format|
format.text
format.html
end
end
def updating_email(user)
@user = user
sendgrid_unique_args :type => "updating_email"
mail(:to => user.update_email, :subject => "JamKazam Email Change Confirmation") do |format|
format.text
format.html
end
end
def updated_email(user)
@user = user
sendgrid_unique_args :type => "updated_email"
mail(:to => user.email, :subject => "JamKazam Email Changed") do |format|
format.text
format.html
end
end
def new_musicians(user, new_nearby, host='www.jamkazam.com')
@user, @new_nearby, @host = user, new_nearby, host
sendgrid_unique_args :type => "new_musicians"
mail(:to => user.email, :subject => "JamKazam New Musicians in Your Area") do |format|
format.text
format.html
end
end
#################################### NOTIFICATION EMAILS ####################################
def friend_request(email, msg)
subject = "You have a new friend request on JamKazam"
unique_args = {:type => "friend_request"}
@body = msg
sendgrid_category "Notification"
sendgrid_unique_args :type => unique_args[:type]
mail(:to => email, :subject => subject) do |format|
format.text
format.html
end
end
def friend_request_accepted(email, msg)
subject = "You have a new friend on JamKazam"
unique_args = {:type => "friend_request_accepted"}
@body = msg
sendgrid_category "Notification"
sendgrid_unique_args :type => unique_args[:type]
mail(:to => email, :subject => subject) do |format|
format.text
format.html
end
end
def new_user_follower(email, msg)
subject = "You have a new follower on JamKazam"
unique_args = {:type => "new_user_follower"}
@body = msg
sendgrid_category "Notification"
sendgrid_unique_args :type => unique_args[:type]
mail(:to => email, :subject => subject) do |format|
format.text
format.html
end
end
def new_band_follower(email, msg)
subject = "Your band has a new follower on JamKazam"
unique_args = {:type => "new_band_follower"}
@body = msg
sendgrid_category "Notification"
sendgrid_unique_args :type => unique_args[:type]
mail(:bcc => email, :subject => subject) do |format|
format.text
format.html
end
end
def session_invitation(email, msg)
subject = "You have been invited to a session on JamKazam"
unique_args = {:type => "session_invitation"}
@body = msg
sendgrid_category "Notification"
sendgrid_unique_args :type => unique_args[:type]
mail(:to => email, :subject => subject) do |format|
format.text
format.html
end
end
def musician_session_join(email, msg, session_id)
subject = "Someone you know is in a session on JamKazam"
unique_args = {:type => "musician_session_join"}
@body = msg
@session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session_id}"
sendgrid_category "Notification"
sendgrid_unique_args :type => unique_args[:type]
mail(:bcc => email, :subject => 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"}
@body = msg
@session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session_id}"
sendgrid_category "Notification"
sendgrid_unique_args :type => unique_args[:type]
mail(:bcc => email, :subject => subject) do |format|
format.text
format.html
end
end
def musician_recording_saved(email, msg)
subject = "A musician has saved a new recording on JamKazam"
unique_args = {:type => "musician_recording_saved"}
@body = msg
sendgrid_category "Notification"
sendgrid_unique_args :type => unique_args[:type]
mail(:bcc => email, :subject => subject) do |format|
format.text
format.html
end
end
def band_recording_saved(email, msg)
subject = "A band has saved a new recording on JamKazam"
unique_args = {:type => "band_recording_saved"}
@body = msg
sendgrid_category "Notification"
sendgrid_unique_args :type => unique_args[:type]
mail(:bcc => email, :subject => subject) do |format|
format.text
format.html
end
end
def band_invitation(email, msg)
subject = "You have been invited to join a band on JamKazam"
unique_args = {:type => "band_invitation"}
@body = msg
sendgrid_category "Notification"
sendgrid_unique_args :type => unique_args[:type]
mail(:to => email, :subject => subject) do |format|
format.text
format.html
end
end
def band_invitation_accepted(email, msg)
subject = "Your band invitation was accepted"
unique_args = {:type => "band_invitation_accepted"}
@body = msg
sendgrid_category "Notification"
sendgrid_unique_args :type => unique_args[:type]
mail(:to => email, :subject => subject) do |format|
format.text
format.html
end
end
# def send_notification(email, subject, msg, unique_args)
# @body = msg
# sendgrid_category "Notification"
# sendgrid_unique_args :type => unique_args[:type]
# mail(:bcc => email, :subject => subject) do |format|
# format.text
# format.html
# end
# end
#############################################################################################
end
end

View File

@ -0,0 +1,21 @@
module JamRuby
class EmailBatch < ActiveRecord::Base
self.table_name = "email_batches"
# has_many :email_batch_results, :class_name => 'JamRuby::EmailBatchResult'
def self.qualified_users
User.select(:email)
.where(:opt_out_email_batch => false)
.order('created_at DESC')
end
def deliver
self.class.qualified_users.each
end
def test_batch
end
end
end

View File

@ -424,4 +424,11 @@ FactoryGirl.define do
factory :event_session, :class => JamRuby::EventSession do
end
factory :email_batch, :class => JamRuby::EmailBatch do
subject Faker::Lorem.sentence
body Faker::Lorem.paragraph(3)
test_emails 4.times.collect { Faker::Internet.safe_email }.join(',')
end
end

View File

@ -0,0 +1,11 @@
require 'spec_helper'
describe EmailBatch do
let (:email_batch) { FactoryGirl.create(:email_batch) }
it 'runs test' do
expect(email_batch.test_emails.present?).to be true
# email_batch.test_batch
end
end

View File

@ -0,0 +1,149 @@
require "spec_helper"
describe BatchMailer do
let(:user) { FactoryGirl.create(:user) }
let(:batch) { FactoryGirl.create(:email_batch) }
before(:each) do
BatchMailer.deliveries.clear
end
describe "should create a batch email" do
let (:mail) { BatchMailer.deliveries[0] }
let (:signup_confirmation_url) { "http://example.com/confirm" }
let (:signup_confirmation_url_with_token ) { "#{signup_confirmation_url}/#{user.signup_token}" }
before(:each) do
BatchMailer.confirm_email(user, signup_confirmation_url_with_token).deliver
end
it { UserMailer.deliveries.length.should == 1 }
it { mail['from'].to_s.should == UserMailer::DEFAULT_SENDER }
it { mail['to'].to_s.should == user.email }
it { mail.multipart?.should == true } # because we send plain + html
# verify that the messages are correctly configured
it { mail.html_part.body.include?("Welcome").should be_true }
it { mail.html_part.body.include?(signup_confirmation_url_with_token).should be_true }
it { mail.text_part.body.include?("Welcome").should be_true }
it { mail.text_part.body.include?(signup_confirmation_url_with_token).should be_true }
end
describe "should send welcome email" do
let (:mail) { UserMailer.deliveries[0] }
before(:each) do
UserMailer.welcome_message(user).deliver
end
it { UserMailer.deliveries.length.should == 1 }
it { mail['from'].to_s.should == UserMailer::DEFAULT_SENDER }
it { mail['to'].to_s.should == user.email }
it { mail.multipart?.should == true } # because we send plain + html
# verify that the messages are correctly configured
it { mail.html_part.body.include?("delighted").should be_true }
it { mail.text_part.body.include?("delighted").should be_true }
end
describe "should send reset password" do
let(:mail) { UserMailer.deliveries[0] }
before(:each) do
UserMailer.password_reset(user, '/reset_password').deliver
end
it { UserMailer.deliveries.length.should == 1 }
it { mail['from'].to_s.should == UserMailer::DEFAULT_SENDER }
it { mail['to'].to_s.should == user.email }
it { mail.multipart?.should == true } # because we send plain + html
# verify that the messages are correctly configured
it { mail.html_part.body.include?("Reset").should be_true }
it { mail.text_part.body.include?("Reset").should be_true }
end
describe "should send change password confirmation" do
let(:mail) { UserMailer.deliveries[0] }
before(:each) do
UserMailer.password_changed(user).deliver
end
it { UserMailer.deliveries.length.should == 1 }
it { mail['from'].to_s.should == UserMailer::DEFAULT_SENDER }
it { mail['to'].to_s.should == user.email }
it { mail.multipart?.should == true } # because we send plain + html
# verify that the messages are correctly configured
it { mail.html_part.body.include?("changed your password").should be_true }
it { mail.text_part.body.include?("changed your password").should be_true }
end
describe "should send update email confirmation" do
let(:mail) { UserMailer.deliveries[0] }
before(:each) do
UserMailer.updated_email(user).deliver
end
it { UserMailer.deliveries.length.should == 1 }
it { mail['from'].to_s.should == UserMailer::DEFAULT_SENDER }
it { mail['to'].to_s.should == user.email }
it { mail.multipart?.should == true } # because we send plain + html
# verify that the messages are correctly configured
it { mail.html_part.body.include?("<b>#{user.email}</b> has been confirmed as your new email address.").should be_true }
it { mail.text_part.body.include?("#{user.email} has been confirmed as your new email address.").should be_true }
end
describe "should send updating email" do
let(:mail) { UserMailer.deliveries[0] }
before(:each) do
user.update_email = "my_new_email@jamkazam.com"
UserMailer.updating_email(user).deliver
end
it { UserMailer.deliveries.length.should == 1 }
it { mail['from'].to_s.should == UserMailer::DEFAULT_SENDER }
it { mail['to'].to_s.should == user.update_email }
it { mail.multipart?.should == true } # because we send plain + html
# verify that the messages are correctly configured
it { mail.html_part.body.include?("to confirm your change in email").should be_true }
it { mail.text_part.body.include?("to confirm your change in email").should be_true }
end
describe "sends new musicians email" do
let(:mail) { UserMailer.deliveries[0] }
before(:each) do
UserMailer.new_musicians(user, User.musicians).deliver
end
it { UserMailer.deliveries.length.should == 1 }
it { mail['from'].to_s.should == UserMailer::DEFAULT_SENDER }
it { mail['to'].to_s.should == user.email }
it { mail.multipart?.should == true } # because we send plain + html
# verify that the messages are correctly configured
it { mail.html_part.body.include?("New JamKazam Musicians in your Area").should be_true }
it { mail.text_part.body.include?("New JamKazam Musicians in your Area").should be_true }
end
end