Home > Enterprise >  Building a referral system using Node/React/MongoDB
Building a referral system using Node/React/MongoDB

Time:05-08

I am making a referral system for my website. I have two schema one is user and other is referral. The userschema is as follows:

const UserSchema = new Schema({
  name: {
    type: String,
    required: true,
  },
  email: {
    type: String,
    required: true,
  },
  refId: {
    type: Schema.Types.ObjectId,
    ref: "referral",
  },
  link: {
    type: String, },
  createdAt: {
    type: Date,
    default: Date.now(),
  },
})

The referral schema is as follows:

const ReferralSchema = new Schema({
  referralId: {
    type: String,
    unique: true
  },
  link: {
    type: String,
    unique: true
  },
  userId: {
    type: Schema.Types.ObjectId,
    ref: 'user'
  },
senderMsg: {
type: String},
senderName: {
type: String},
  createdAt: {
    type: Date,
    default: Date.now()
  }
})

The referral link is created on signup and after that when a user refer the website to his friend/relative or anyone else, he/she will enter the email id and will send the email to the referrer with the referral link. Now I want to keep the track of the count of those referrals who registered through that referral link and the referral name should be linked to the under the user. The issue I am facing is that I want to show the referral link like: http://example.com/invite/[email protected]

How to make this as referral link. Secondly how to add the count of no of user registered and save the count in mongodb.

Any help will be highly appreciated. Thanks

CodePudding user response:

How to make this as referral link - When a user is referring any other user and will type the user email, you can save that partcular instance in the DB and you can also track if the referred user is signing from the link or not. You can set a flag(boolean), if a user've clicked on their referral link.

how to add the count of no of user registered and save the count - As for the count part, when you will track if any of the referred user signed up or not via a link, you can get a count as well(you've to run a query on that flag).

link: {
    text: String,
    isClicked: Boolean,
  },
  • Related