diff --git a/controllers/giftLunchController.js b/controllers/giftLunchController.js new file mode 100644 index 00000000..c7f64b28 --- /dev/null +++ b/controllers/giftLunchController.js @@ -0,0 +1,40 @@ +const { Op } = require('sequelize'); +const Lunch = require('../models/lunches.model'); +const User = require('../models/user.model'); +const response = require('../utils/response'); + +const giftLunch = async (req, res) => { + try { + const userId = req.user.id; + + const {receiver_id, quantity, note } = req.body + + if (!receiver_id || !quantity || !note) return res.status(400).json(response(false, 'Missing required fields', null)); + + const user = await User.findOne({ where: { id: userId } }); + + if (!user) return res.status(404).json(response(false, 'User does not exist', null)); + + const lunch = {receiver_id, quantity, note, redeemed: false} + + // Create Launch + const newLunch = await Lunch.create(lunch) + + const sender = await User.findOne({ where: { id: userId } }); + + const receiver = await User.findOne({ where: { id: receiver_id } }); + + //Update the sender's balance + await sender.update({balance: sender.balance - quantity}); + + //Update the receiver's balance + await receiver.update({balance: receiver.balance + quantity}); + + return res.status(201).json(response(true, 'Lunch gifted successfully', {lunch: newLunch})); + + } catch (error) { + res.status(500).json(response(false, 'Internal Server Error', null)); + } +} + +module.exports = {giftLunch} \ No newline at end of file diff --git a/controllers/lunchControllers.js b/controllers/lunchControllers.js index 20b2b97e..ce821ec0 100644 --- a/controllers/lunchControllers.js +++ b/controllers/lunchControllers.js @@ -37,43 +37,4 @@ const getAllLunch = async (req, res) => { } }; -const sendLunch = async (req, res) => { - const { receiverId, quantity, note } = req.body; - - try { - //Create a new lunch - const lunch = await Lunch.create({ - sender_id: req.user.id, - receiver_id: receiverId, - quantity, - note, - }); - - const sender = await User.findOne({ where: { id: req.id } }); - const receiver = await User.findOne({ where: { id: receiverId } }); - - //Update the sender's balance - await sender.update({ - balance: sender.balance - quantity, - }); - - //Update the receiver's balance - await receiver.update({ - balance: receiver.balance + quantity, - }); - - res.status(201).json({ - success: true, - message: 'Lunch sent successfully', - data: lunch, - }); - } catch (error) { - res.status(500).json({ - success: false, - message: 'Internal Server Error', - data: null, - }); - } -}; - -module.exports = { getAllLunch, sendLunch }; +module.exports = { getAllLunch }; diff --git a/routes/lunchRoutes.js b/routes/lunchRoutes.js index 772be115..69337c73 100644 --- a/routes/lunchRoutes.js +++ b/routes/lunchRoutes.js @@ -2,8 +2,12 @@ const express = require('express'); const router = express.Router(); const lunchControllers = require('../controllers/lunchControllers'); +const { giftLunch } = require('../controllers/giftLunchController'); //GET all available lunches for a user router.get('/user/:userId/lunch/all', lunchControllers.getAllLunch); +// Gift Launch +router.post('/gift_lunch', giftLunch) + module.exports = router; diff --git a/utils/response.js b/utils/response.js new file mode 100644 index 00000000..1aca66a3 --- /dev/null +++ b/utils/response.js @@ -0,0 +1,9 @@ +const response = (success, message, data) => { + return { + success: success, + message: message, + data: data + } +} + +module.exports = (response) \ No newline at end of file