diff --git a/db/manifest b/db/manifest index 232cb12ef..627a79b18 100755 --- a/db/manifest +++ b/db/manifest @@ -143,3 +143,5 @@ emails.sql email_batch.sql user_progress_tracking2.sql bands_did_session.sql +affiliate_partners.sql + diff --git a/db/up/affiliate_partners.sql b/db/up/affiliate_partners.sql new file mode 100644 index 000000000..67f7dfd97 --- /dev/null +++ b/db/up/affiliate_partners.sql @@ -0,0 +1,15 @@ +CREATE TABLE affiliate_partners ( + id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(), + partner_name VARCHAR(128) NOT NULL, + partner_code VARCHAR(128) NOT NULL, + partner_user_id VARCHAR(64) NOT NULL, + user_email VARCHAR(255), + referral_user_count INTEGER NOT NULL DEFAULT 0, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE INDEX affiliate_partners_code_idx ON affiliate_partners(partner_code); +CREATE INDEX affiliate_partners_user_idx ON affiliate_partners(partner_user_id); + +ALTER TABLE users ADD COLUMN affiliate_referral_id VARCHAR(64) REFERENCES affiliate_partners(id); diff --git a/ruby/lib/jam_ruby/lib/em_helper.rb b/ruby/lib/jam_ruby/lib/em_helper.rb index c310cf2bf..9c382733a 100644 --- a/ruby/lib/jam_ruby/lib/em_helper.rb +++ b/ruby/lib/jam_ruby/lib/em_helper.rb @@ -65,7 +65,7 @@ module JamWebEventMachine end def self.run - + return if defined?(Rails::Console) current = Thread.current Thread.new do run_em(current) diff --git a/ruby/lib/jam_ruby/models/affiliate_partner.rb b/ruby/lib/jam_ruby/models/affiliate_partner.rb new file mode 100644 index 000000000..6f7a001b3 --- /dev/null +++ b/ruby/lib/jam_ruby/models/affiliate_partner.rb @@ -0,0 +1,27 @@ +class JamRuby::AffiliatePartner < ActiveRecord::Base + has_one :partner_user, :class_name => "JamRuby::User", :foreign_key => :partner_user_id + has_many :user_referrals, :class_name => "JamRuby::User", :foreign_key => :affiliate_referral_id + + attr_accessible :partner_name, :partner_code, :partner_user_id + + validates :user_email, format: {with: JamRuby::User::VALID_EMAIL_REGEX}, :if => :user_email + validates :partner_name, presence: true + validates :partner_code, presence: true + validates :partner_user, presence: true + + def self.create_with_params(params={}) + oo = self.new + oo.partner_name = params[:partner_name] + oo.partner_code = params[:partner_code] + oo.partner_user = User.where(:email => params[:user_email]).limit(1).first + oo.partner_user_id = oo.partner_user.try(:id) + oo.save! + oo + end + + def self.coded_id(code=nil) + self.where(:partner_code => code).limit(1).pluck(:id).first if code.present? + end + +end + diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index 77966d5cc..87e8ee49c 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -99,6 +99,10 @@ module JamRuby # events has_many :event_sessions, :class_name => "JamRuby::EventSession" + # affiliate_partner + has_one :affiliate_partner, :class_name => "JamRuby::AffiliatePartner", :foreign_key => :partner_user_id + belongs_to :affiliate_referral, :class_name => "JamRuby::AffiliatePartner", :foreign_key => :affiliate_referral_id, :counter_cache => :referral_user_count + # This causes the authenticate method to be generated (among other stuff) #has_secure_password @@ -735,6 +739,7 @@ module JamRuby invited_user = options[:invited_user] fb_signup = options[:fb_signup] signup_confirm_url = options[:signup_confirm_url] + affiliate_referral_id = options[:affiliate_referral_id] user = User.new @@ -836,6 +841,10 @@ module JamRuby if user.errors.any? raise ActiveRecord::Rollback else + if user.affiliate_referral = AffiliatePartner.find_by_id(affiliate_referral_id) + user.save + end if affiliate_referral_id.present? + # don't send an signup email if email is already confirmed if user.email_confirmed UserMailer.welcome_message(user).deliver diff --git a/web/app/controllers/application_controller.rb b/web/app/controllers/application_controller.rb index c2922f055..d28b4ea46 100644 --- a/web/app/controllers/application_controller.rb +++ b/web/app/controllers/application_controller.rb @@ -8,6 +8,14 @@ class ApplicationController < ActionController::Base # inject username/email into bugsnag data before_bugsnag_notify :add_user_info_to_bugsnag + def affiliate_code + cookies[:affiliate_code] + end + + def affiliate_code=(code) + cookies[:affiliate_code] = code if code.present? + end + private def add_user_info_to_bugsnag(notif) # Add some app-specific data which will be displayed on a custom diff --git a/web/app/controllers/users_controller.rb b/web/app/controllers/users_controller.rb index d31bb4094..d380c6b34 100644 --- a/web/app/controllers/users_controller.rb +++ b/web/app/controllers/users_controller.rb @@ -149,19 +149,20 @@ class UsersController < ApplicationController musician = params[:jam_ruby_user][:musician] @user = UserManager.new.signup(remote_ip: request.remote_ip, - first_name: params[:jam_ruby_user][:first_name], - last_name: params[:jam_ruby_user][:last_name], - email: params[:jam_ruby_user][:email], - password: params[:jam_ruby_user][:password], - password_confirmation: params[:jam_ruby_user][:password_confirmation], - terms_of_service: terms_of_service, - instruments: instruments, - birth_date: birth_date, - location: location, - musician: musician, - invited_user: @invited_user, - fb_signup: @fb_signup, - signup_confirm_url: ApplicationHelper.base_uri(request) + "/confirm") + first_name: params[:jam_ruby_user][:first_name], + last_name: params[:jam_ruby_user][:last_name], + email: params[:jam_ruby_user][:email], + password: params[:jam_ruby_user][:password], + password_confirmation: params[:jam_ruby_user][:password_confirmation], + terms_of_service: terms_of_service, + instruments: instruments, + birth_date: birth_date, + location: location, + musician: musician, + invited_user: @invited_user, + fb_signup: @fb_signup, + signup_confirm_url: ApplicationHelper.base_uri(request) + "/confirm", + affiliate_referral_id: AffiliatePartner.coded_id(self.affiliate_code)) # check for errors if @user.errors.any? diff --git a/web/lib/user_manager.rb b/web/lib/user_manager.rb index 728bcb652..34be86248 100644 --- a/web/lib/user_manager.rb +++ b/web/lib/user_manager.rb @@ -26,6 +26,7 @@ class UserManager < BaseManager invited_user = options[:invited_user] fb_signup = options[:fb_signup] signup_confirm_url = options[:signup_confirm_url] + affiliate_referral_id = options[:affiliate_referral_id] @user = User.new @@ -60,7 +61,8 @@ class UserManager < BaseManager photo_url: photo_url, invited_user: invited_user, fb_signup: fb_signup, - signup_confirm_url: signup_confirm_url) + signup_confirm_url: signup_confirm_url, + affiliate_referral_id: affiliate_referral_id) return @user #end diff --git a/web/spec/managers/user_manager_spec.rb b/web/spec/managers/user_manager_spec.rb index ba4d3a33d..e09c9406c 100644 --- a/web/spec/managers/user_manager_spec.rb +++ b/web/spec/managers/user_manager_spec.rb @@ -11,6 +11,13 @@ describe UserManager do end describe "signup" do + let!(:user) { FactoryGirl.create(:user) } + let!(:partner) { + AffiliatePartner.create_with_params({:partner_name => Faker::Company.name, + :partner_code => Faker::Lorem.words(1)[0], + :user_email => user.email}) + } + it "signup successfully" do MaxMindIsp.delete_all # prove that city/state/country will remain nil if no maxmind data MaxMindGeo.delete_all @@ -24,7 +31,8 @@ describe UserManager do terms_of_service: true, instruments: @instruments, musician:true, - signup_confirm_url: "http://localhost:3000/confirm" ) + signup_confirm_url: "http://localhost:3000/confirm", + affiliate_referral_id: AffiliatePartner.coded_id(partner.partner_code)) @user.errors.any?.should be_false @user.first_name.should == "bob" @@ -38,6 +46,9 @@ describe UserManager do @user.subscribe_email.should be_true @user.signup_token.should_not be_nil + @user.reload + expect(@user.affiliate_referral).to eq(partner) + UserMailer.deliveries.length.should == 1 end