-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
54 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |