-
Notifications
You must be signed in to change notification settings - Fork 0
Add referrals #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add referrals #75
Conversation
| class CreateReferrals < ActiveRecord::Migration[5.2] | ||
| def change | ||
| create_table :referrals do |t| | ||
| t.references :referrer, index: true, foreign_key: { to_table: :users } |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
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.
Referrals API
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