|
1 | 1 | import System from '../../../models/schemas/System.js'; |
| 2 | +import User from '../../../models/schemas/User.js'; |
2 | 3 |
|
3 | 4 | /** |
4 | 5 | * Fetches rewards/rewards from the system. |
@@ -103,3 +104,79 @@ export const createReward = async (req, res) => { |
103 | 104 | return res.status(500).json({ message: 'Internal Server Error' }); |
104 | 105 | } |
105 | 106 | }; |
| 107 | + |
| 108 | +/** |
| 109 | + * Redeems a reward for a user if not already claimed. |
| 110 | + */ |
| 111 | +export const redeemReward = async (req, res) => { |
| 112 | + try { |
| 113 | + const key = req.headers.key; |
| 114 | + |
| 115 | + // Authorization check |
| 116 | + if (!key || key !== process.env.ACCESS_KEY) { |
| 117 | + return res.status(401).json({ message: 'Unauthorized' }); |
| 118 | + } |
| 119 | + |
| 120 | + const { code } = req.body; |
| 121 | + const userId = req.headers.id; // Adjust based on how you auth |
| 122 | + |
| 123 | + if (!code || !userId) { |
| 124 | + return res.status(400).json({ message: 'Missing code or user ID' }); |
| 125 | + } |
| 126 | + |
| 127 | + // Find the system document (assuming you have only one system doc) |
| 128 | + const system = await System.findById('system').select('rewards'); |
| 129 | + if (!system) { |
| 130 | + return res.status(500).json({ message: 'System configuration not found for rewards' }); |
| 131 | + } |
| 132 | + |
| 133 | + // Find the reward by _id (code) |
| 134 | + const reward = system.rewards.find(r => r._id.toLowerCase() === code.toLowerCase() && r.isActive); |
| 135 | + |
| 136 | + if (!reward) { |
| 137 | + return res.status(404).json({ message: 'Invalid or inactive reward code' }); |
| 138 | + } |
| 139 | + |
| 140 | + // Check valid date range if set |
| 141 | + const now = new Date(); |
| 142 | + if (reward.validFrom && now < reward.validFrom) { |
| 143 | + return res.status(400).json({ message: 'Reward is not yet valid' }); |
| 144 | + } |
| 145 | + if (reward.validUntil && now > reward.validUntil) { |
| 146 | + return res.status(400).json({ message: 'Reward has expired' }); |
| 147 | + } |
| 148 | + |
| 149 | + // Get user and check status_history |
| 150 | + const user = await User.findById(userId); |
| 151 | + if (!user) { |
| 152 | + return res.status(404).json({ message: 'User not found' }); |
| 153 | + } |
| 154 | + |
| 155 | + const alreadyClaimed = user.status_history?.some(status => status.value === code.toUpperCase()); |
| 156 | + if (alreadyClaimed) { |
| 157 | + return res.status(409).json({ message: 'Reward already claimed' }); |
| 158 | + } |
| 159 | + |
| 160 | + // Add reward to user's history |
| 161 | + user.status_history.push({ |
| 162 | + _id: user.status_history.length + 1, |
| 163 | + value: code.toUpperCase(), |
| 164 | + timestamp: new Date(), |
| 165 | + reason: 'Self Reward redeemed', |
| 166 | + }); |
| 167 | + |
| 168 | + await user.save(); |
| 169 | + |
| 170 | + return res.status(200).json({ |
| 171 | + message: 'Reward redeemed successfully', |
| 172 | + reward: { |
| 173 | + name: reward.name, |
| 174 | + type: reward.type, |
| 175 | + value: reward.value, |
| 176 | + }, |
| 177 | + }); |
| 178 | + } catch (err) { |
| 179 | + console.error('Error redeeming reward:', err); |
| 180 | + return res.status(500).json({ message: 'Internal Server Error' }); |
| 181 | + } |
| 182 | +}; |
0 commit comments