Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/controllers/api/v1/callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def render_failure(error=nil)
end

def mobile_provider_params
params.require(:user).permit(:uid, :country_code, :mobile, :name, :id_token)
params.require(:user).permit(:uid,
:country_code,
:mobile,
:name,
:id_token,
:referral_code)
end
end
15 changes: 15 additions & 0 deletions app/controllers/api/v1/referrals_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Api::V1::ReferralsController < Api::BaseController
def create
if referral = Referral.create(referrer_id: current_user.id)
render json: referral, status: :created
else
render_error(:unprocessable_entity, referral.errors.full_messages.to_sentence)
end
end

protected

def referrals_params
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this required ?

params.require(:referrals).permit(:)
end
end
7 changes: 6 additions & 1 deletion app/controllers/api/v1/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ def create_params

def create_session_for_user
ensure_one_active_session
@session = Session.create!(create_params)
pending_referral = Referral.pending.for_candidate(current_user).first
create_params[:rewards] = pending_referral.reward if pending_referral
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep "rewards due to referrals" and "rewards due to sessions" as seperate things?
Or else by looking at a session object it would be hard to know if the reward has come from a referral or by staying online at home.

ActiveRecord::Base.transaction do
@session = Session.create!(create_params)
pending_referral.rewarded!
end
end

def ensure_one_active_session
Expand Down
24 changes: 24 additions & 0 deletions app/models/referral.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Referral < ApplicationRecord
CODE_PREFIX = 'STAYHOME'.freeze
REWARD_VALUE = 1000

enum status: { pending: 0, rewarded: 1 }

scope :for_candidate, ->(user) { where(candidate_id: user.id) }
scope :for_referrer, ->(user) { where(referrer_id: user.id) }

before_create :generate_code

private

def generate_code
new_code = "#{CODE_PREFIX}-#{SecureRandom.hex[0, 4].upcase}"

if Referral.exists?(referrer_id: referrer_id, code: new_code)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought an user will have a single referral code that would be shared among his peers.
It seems we are creating a new referral code for every new referral ?

generate_code
else
self.code = new_code
self.reward = REWARD_VALUE
end
end
end
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class User < ApplicationRecord
has_many :wallet_transactions, dependent: :destroy
has_many :notification_tokens, dependent: :destroy
has_many :sessions, dependent: :destroy
has_many :sent_referrals, class_name: 'Referral', foreign_key: :referrer_id
has_many :received_referrals, class_name: 'Referral', foreign_key: :candidate_id
before_validation :remove_devise_validations, unless: :email_auth_validations
after_validation :reverse_geocode

Expand Down Expand Up @@ -48,10 +50,16 @@ def self.onboard_from_mobile(params)
user = User.find_or_initialize_by(mobile: params[:mobile])
user.name = params[:name]
user.identities.find_or_initialize_by(provider: 'mobile', uid: params[:uid])
update_referrals(params[:referral_code]) if params[:referral_code]
user.save_provider_auth_user
user
end

def update_referrals(code)
referral = Referral.find_by(code: code, candidate_id: nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the current logic an user is entitled to refer ONLY one other person, as once that happens next time this method won't find any more entry with candidate_id: nil. Is this the expected behaviour ?

referral.candidate = self if referral
end

def password_complexity
return if password.blank? || password =~ /^(?=.*?[a-z])(?=.*?[#?!@$%^&*-]).{8,70}$/
errors.add :password, :password_complexity_error
Expand Down
3 changes: 3 additions & 0 deletions app/serializers/referral_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ReferralSerializer < ActiveModel::Serializer
attributes :code
end
6 changes: 5 additions & 1 deletion app/serializers/user_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :email, :mobile, :profile_picture_url, :wallet_balance,
:home_duration_in_seconds, :lat, :lng
:home_duration_in_seconds, :lat, :lng, :total_referral_rewards

def home_duration_in_seconds
active_session = object.active_session
Expand All @@ -20,4 +20,8 @@ def wallet_balance
object.wallet_balance
end
end

def total_referral_rewards
Referral.rewarded.for_referrer(object).sum(&:reward)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using sum(&:reward) would bring all the records data in memory and then do the sum.
I think triggering a SQL SUM (by sum(:reward)) might be better in performance.

end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
resource :home, only: :index
resource :user, only: :update do
get :profile, to: 'users#show'
resources :referrals, only: :create
end
resources :notification_tokens, only: :create
post '/sessions/ping', to: 'sessions#ping'
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20200513195719_create_referrals.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateReferrals < ActiveRecord::Migration[5.2]
def change
create_table :referrals do |t|
t.references :referrer, index: true, foreign_key: { to_table: :users }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to index all these columns?

t.references :candidate, index: true, foreign_key: { to_table: :users }, null: true
t.integer :status, index: true, default: 0
t.string :code, index: true
t.timestamp :expires_at
t.integer :reward
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this "reward" column is missed in schema.rb

t.timestamps
end
end
end
18 changes: 17 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_05_10_184356) do
ActiveRecord::Schema.define(version: 2020_05_13_195719) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -70,6 +70,20 @@
t.datetime "updated_at", null: false
end

create_table "referrals", force: :cascade do |t|
t.bigint "referrer_id"
t.bigint "candidate_id"
t.integer "status", default: 0
t.string "code"
t.datetime "expires_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["candidate_id"], name: "index_referrals_on_candidate_id"
t.index ["code"], name: "index_referrals_on_code"
t.index ["referrer_id"], name: "index_referrals_on_referrer_id"
t.index ["status"], name: "index_referrals_on_status"
end

create_table "questions", force: :cascade do |t|
t.string "name"
t.boolean "active", default: true, null: false
Expand Down Expand Up @@ -148,6 +162,8 @@
t.index ["user_id"], name: "index_wallet_transactions_on_user_id"
end

add_foreign_key "referrals", "users", column: "candidate_id"
add_foreign_key "referrals", "users", column: "referrer_id"
add_foreign_key "answers", "questions"
add_foreign_key "sessions", "users"
end