|
| 1 | +import System from '../../../models/schemas/System.js'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Fetches rewards/rewards from the system. |
| 5 | + * Supports: |
| 6 | + * - All rewards |
| 7 | + * - Filtering by type (coupon, giftcard, voucher) |
| 8 | + * - Fetching a specific reward by its ID/code |
| 9 | + */ |
| 10 | +export const getRewards = async (req, res) => { |
| 11 | + try { |
| 12 | + const key = req.headers.key; |
| 13 | + const { id } = req.params; |
| 14 | + const { type } = req.query; |
| 15 | + |
| 16 | + // Authorization check |
| 17 | + // if (!key || key !== process.env.ACCESS_KEY) { |
| 18 | + // return res.status(401).json({ message: 'Unauthorized' }); |
| 19 | + // } |
| 20 | + |
| 21 | + // Fetch only the rewards field |
| 22 | + const system = await System.findById('system').select('rewards'); |
| 23 | + |
| 24 | + if (!system || !Array.isArray(system.rewards)) { |
| 25 | + return res.status(404).json({ message: 'No rewards found' }); |
| 26 | + } |
| 27 | + |
| 28 | + // If an ID is provided, return that specific reward |
| 29 | + if (id) { |
| 30 | + const reward = system.rewards.find(r => r._id === id); |
| 31 | + if (!reward) { |
| 32 | + return res.status(404).json({ message: 'Reward not found' }); |
| 33 | + } |
| 34 | + return res.status(200).json(reward); |
| 35 | + } |
| 36 | + |
| 37 | + // Otherwise, return all (optionally filtered by type) |
| 38 | + let rewards = system.rewards.filter(r => r.isActive); |
| 39 | + if (type) { |
| 40 | + rewards = rewards.filter(r => r.type === type); |
| 41 | + } |
| 42 | + |
| 43 | + return res.status(200).json({ |
| 44 | + count: rewards.length, |
| 45 | + rewards, |
| 46 | + }); |
| 47 | + } catch (error) { |
| 48 | + console.error('Error fetching rewards:', error); |
| 49 | + return res.status(500).json({ message: 'Internal Server Error' }); |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * Creates a new reward (giftcard, coupon, voucher) and adds it to the system. |
| 55 | + */ |
| 56 | +export const createReward = async (req, res) => { |
| 57 | + try { |
| 58 | + const key = req.headers.key; |
| 59 | + |
| 60 | + // Authorization check |
| 61 | + if (!key || key !== process.env.ACCESS_KEY) { |
| 62 | + return res.status(401).json({ message: 'Unauthorized' }); |
| 63 | + } |
| 64 | + |
| 65 | + const { id, type, description, discount, appliesTo, usage, validFrom, validUntil, isActive = true } = req.body; |
| 66 | + |
| 67 | + if (!id || !type || !discount || !discount.type || discount.value === undefined) { |
| 68 | + return res.status(400).json({ message: 'Missing required reward fields' }); |
| 69 | + } |
| 70 | + |
| 71 | + const system = await System.findById('system'); |
| 72 | + if (!system) { |
| 73 | + return res.status(404).json({ message: 'System not found' }); |
| 74 | + } |
| 75 | + |
| 76 | + // Check for existing reward with same ID |
| 77 | + const exists = system.rewards.find(r => r._id === id); |
| 78 | + if (exists) { |
| 79 | + return res.status(409).json({ message: 'Reward with this ID already exists' }); |
| 80 | + } |
| 81 | + |
| 82 | + const newReward = { |
| 83 | + _id: id, |
| 84 | + type, |
| 85 | + description, |
| 86 | + discount, |
| 87 | + appliesTo: appliesTo || {}, |
| 88 | + usage: usage || {}, |
| 89 | + validFrom, |
| 90 | + validUntil, |
| 91 | + isActive, |
| 92 | + }; |
| 93 | + |
| 94 | + system.rewards.push(newReward); |
| 95 | + await system.save(); |
| 96 | + |
| 97 | + return res.status(201).json({ |
| 98 | + message: 'Reward created successfully', |
| 99 | + reward: newReward, |
| 100 | + }); |
| 101 | + } catch (error) { |
| 102 | + console.error('Error creating reward:', error); |
| 103 | + return res.status(500).json({ message: 'Internal Server Error' }); |
| 104 | + } |
| 105 | +}; |
0 commit comments