Skip to content

Commit

Permalink
Gift Lunch Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Ay4codes committed Sep 21, 2023
1 parent 7328c3c commit ee00221
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 40 deletions.
40 changes: 40 additions & 0 deletions controllers/giftLunchController.js
Original file line number Diff line number Diff line change
@@ -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}
41 changes: 1 addition & 40 deletions controllers/lunchControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
4 changes: 4 additions & 0 deletions routes/lunchRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
9 changes: 9 additions & 0 deletions utils/response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const response = (success, message, data) => {
return {
success: success,
message: message,
data: data
}
}

module.exports = (response)

0 comments on commit ee00221

Please sign in to comment.