Skip to content

Conversation

@nronas
Copy link
Contributor

@nronas nronas commented May 13, 2020

closes: #64

What

Implements a referral feature where users can referrer other users to join the app. Code generation is doing a hex[0, 4] which essentially has a maximum generation sample of 4.5Billion codes. The code is not safe against local brute force attack but since this is over the internet we can easily spot if someone tries to guess all the generated codes.

API Spec

On /api/v1/callbacks/mobile_sign_up

Now accepts referral_code to be passed in on sign up.

"user": {
  "referral_code": String
}

Referrals API

++ Request POST /api/v1/user/referrals
++ Response:
++++ { "code": String }

Upon session creation we will check if the user has any outstanding rewards from referrals and we are going to rewarded them with applying the total rewards.

TODO

  • Implement APIs and referral award flow
  • Cover with new flows with specs
  • Add referral management on admin

@nronas nronas added this to the v1.1 milestone May 13, 2020
@nronas nronas self-assigned this May 13, 2020
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?


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 ?

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

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

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 ?

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 ?

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Rewards API

4 participants