From 97a9442a07000972bc1ace775b6f835164597639 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Tue, 3 Dec 2024 22:34:30 +0700 Subject: [PATCH] Create tieredRewards.js --- src/loyalty/tieredRewards.js | 51 ++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/loyalty/tieredRewards.js diff --git a/src/loyalty/tieredRewards.js b/src/loyalty/tieredRewards.js new file mode 100644 index 000000000..8538192bf --- /dev/null +++ b/src/loyalty/tieredRewards.js @@ -0,0 +1,51 @@ +// tieredRewards.js + +class TieredRewards { + constructor() { + this.userActivity = {}; // Store user activity levels + this.tiers = [ + { level: 1, minActivity: 0, reward: 10 }, + { level: 2, minActivity: 5, reward: 25 }, + { level: 3, minActivity: 10, reward: 50 }, + ]; + } + + // Log user activity + logActivity(userId) { + if (!this.userActivity[userId]) { + this.userActivity[userId] = 0; + } + this.userActivity[userId]++; + console.log(`User ${userId} activity logged. Total activity: ${this.userActivity[userId]}`); + } + + // Get the reward for a user based on their activity + getReward(userId) { + const activityLevel = this.userActivity[userId] || 0; + const tier = this.tiers.slice().reverse().find(t => activityLevel >= t.minActivity); + return tier ? tier.reward : 0; + } + + // Get tier details for a user + getTierDetails(userId) { + const activityLevel = this.userActivity[userId] || 0; + const tier = this.tiers.find(t => activityLevel >= t.minActivity); + return tier ? tier : null; + } +} + +// Example usage +const tieredRewards = new TieredRewards(); +tieredRewards.logActivity('user123'); +tieredRewards.logActivity('user123'); +tieredRewards.logActivity('user123'); +tieredRewards.logActivity('user123'); +tieredRewards.logActivity('user123'); // User has now reached level 2 + +const reward = tieredRewards.getReward('user123'); +console.log(`Reward for user123: $${reward}`); + +const tierDetails = tieredRewards.getTierDetails('user123'); +console.log(`Tier details for user123:`, tierDetails); + +export default TieredRewards;