From bb8333651406ff17687853c7a3ea1386a017235a Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Tue, 3 Dec 2024 22:33:42 +0700 Subject: [PATCH] Create referralProgram.js --- src/loyalty/referralProgram.js | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/loyalty/referralProgram.js diff --git a/src/loyalty/referralProgram.js b/src/loyalty/referralProgram.js new file mode 100644 index 000000000..a2efc8ecc --- /dev/null +++ b/src/loyalty/referralProgram.js @@ -0,0 +1,44 @@ +// referralProgram.js + +class ReferralProgram { + constructor() { + this.referrals = {}; // Store referrals by user ID + this.rewards = {}; // Store rewards by user ID + } + + // Refer a new user + refer(referrerId, referredId) { + if (!this.referrals[referrerId]) { + this.referrals[referrerId] = []; + } + this.referrals[referrerId].push(referredId); + console.log(`User ${referrerId} referred ${referredId}.`); + } + + // Reward the referrer for a successful referral + rewardReferrer(referrerId) { + if (!this.rewards[referrerId]) { + this.rewards[referrerId] = 0; + } + this.rewards[referrerId] += 50; // Example reward amount + console.log(`User ${referrerId} has been rewarded. Total rewards: $${this.rewards[referrerId]}`); + } + + // Get referral details for a user + getReferralDetails(userId) { + return { + referrals: this.referrals[userId] || [], + rewards: this.rewards[userId] || 0, + }; + } +} + +// Example usage +const referralProgram = new ReferralProgram(); +referralProgram.refer('user123', 'user456'); +referralProgram.rewardReferrer('user123'); + +const referralDetails = referralProgram.getReferralDetails('user123'); +console.log(`Referral details for user123:`, referralDetails); + +export default ReferralProgram;