John S.
asked on
How to write pure Javascript 'class' or library or SDK - Customer Referrals
We want to introduce a Loyalty / Referral feature to our site.
-- A user can enter a referral code on their checkout form to receive a discount
-- The person who referred them will be rewarded, at this time simply by points incremented.
-- A user can refer someone and a code is generated that is associated with them
-- A code can only be used once
-- A user can only refer XX amount of people.
That being said, we are using JavaScript to code this with.
I have been tasked with this, and the boss wants this done in some sort of OOP / MVVM pattern. The reason I am highlighting this fact is because they know my coding skills are mid level at best.
Ideally, I know my end result should look something like this:
That is how I would approach this I guess. I COULD try TypeScript if a class would be better, but new to that.
Can someone show me the best way to approach this using pure JS? The code will not do anything more than I described.
There will be API endpoints to actually hit the data.
I would really love to see some clean, modern approach to a simple function group like this. For instance, getting and setting should be a global function, right??? IDK...
Help!
Thanks!
-- A user can enter a referral code on their checkout form to receive a discount
-- The person who referred them will be rewarded, at this time simply by points incremented.
-- A user can refer someone and a code is generated that is associated with them
-- A code can only be used once
-- A user can only refer XX amount of people.
That being said, we are using JavaScript to code this with.
I have been tasked with this, and the boss wants this done in some sort of OOP / MVVM pattern. The reason I am highlighting this fact is because they know my coding skills are mid level at best.
Ideally, I know my end result should look something like this:
let referral = {
generateNewCode: function(){
},
getNumberOfReferralsByUserId: function(id){
},
codeHasBeenUsed: function(code){
return true;
}
}
That is how I would approach this I guess. I COULD try TypeScript if a class would be better, but new to that.
Can someone show me the best way to approach this using pure JS? The code will not do anything more than I described.
There will be API endpoints to actually hit the data.
I would really love to see some clean, modern approach to a simple function group like this. For instance, getting and setting should be a global function, right??? IDK...
Help!
Thanks!
Since the referrals must be saved and tracked, that means that it's a database function. Javascript by itself can not do that. You need a server side code like in PHP or ASP.NET to do the database part.
ASKER
Thanks for responding Dave. All those data calls will be hit by calling our API for the project.
For instance, in the above example, an endpoint would be available for me to generate a new referral... ie...
http://www.oursite.com/api/projectName/users/createReferral
etc,
For instance, in the above example, an endpoint would be available for me to generate a new referral... ie...
http://www.oursite.com/api/projectName/users/createReferral
etc,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
After reading your comments and thinking on this, you both are correct.
This stuff should be done server side. I could write a simple utility to communicate with that backend service, but the logic I initially described in my question really does belong on the server. As Julian mentioned, I would have to double check all the values coming in anyways.
Thanks guys. I think I am just looking for a reason to write a "library" or "SDK". I have recently been introduced to Node, and I love the concept of Modules I guess. :)
This stuff should be done server side. I could write a simple utility to communicate with that backend service, but the logic I initially described in my question really does belong on the server. As Julian mentioned, I would have to double check all the values coming in anyways.
Thanks guys. I think I am just looking for a reason to write a "library" or "SDK". I have recently been introduced to Node, and I love the concept of Modules I guess. :)