-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
3821394
commit 6f1f66a
Showing
6 changed files
with
177 additions
and
18 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,80 @@ | ||
const Expense = require('../models/expense'); | ||
const User = require('../models/user') | ||
const { dateBeforeXFromNow } = require('../utilities/date'); | ||
|
||
module.exports = { | ||
addNewExpense: async (req, res) => { | ||
const { body } = req; | ||
try { | ||
const user = await User.findById(body.userID); | ||
const { userID, category, amount } = body; | ||
if (!user.categories.includes(category)) { | ||
return res.status(400).json({ message: "invalid category" }); | ||
} | ||
if (amount > user.amountLeft) { | ||
return res.status(400).json({ message: "budget not enough" }); | ||
} | ||
const expense = new Expense({ userID, category, amount }); | ||
user.currentExpensesIDs.push(expense._id); | ||
user.amountLeft = user.amountLeft - amount; | ||
await expense.save(); | ||
await user.save(); | ||
res.status(200).json({ message: "expense saved", id: expense._id }); | ||
} catch (e) { | ||
console.log(e); | ||
res.status(400).json({ message: "expense not saved" }); | ||
} | ||
}, | ||
|
||
// only update amount or deleted | ||
updateExpense: async (req, res) => { | ||
const { body } = req; | ||
try { | ||
const { userID, amount, deleted } = body; | ||
const { id } = req.params; | ||
const user = await User.findById(userID); | ||
const expense = await Expense.findById(id); | ||
if (deleted != undefined) { | ||
if (expense.deleted !== deleted) { | ||
let newAmount = user.amountLeft + ((2 * deleted - 1) * expense.amount); | ||
if (newAmount >= 0) { | ||
expense.deleted = deleted; | ||
user.amountLeft = newAmount; | ||
await expense.save(); | ||
await user.save(); | ||
return res.status(200).json({ message: "expense updated" }); | ||
} | ||
return res.status(400).json({ message: "budget not enough" }); | ||
} | ||
} | ||
else if ((amount !== undefined) && (!expense.deleted)) { | ||
let newAmount = user.amountLeft + (expense.amount - amount); | ||
if (newAmount >= 0) { | ||
expense.amount = amount; | ||
user.amountLeft = newAmount; | ||
await expense.save(); | ||
await user.save(); | ||
return res.status(200).json({ message: "expense updated" }); | ||
} | ||
return res.status(400).json({ message: "budget not enough" }); | ||
} | ||
} catch (e) { | ||
console.log(e); | ||
res.status(400).json({ message: "expense not updated" }); | ||
} | ||
}, | ||
|
||
// gives expense data upto x days from now | ||
getSummaryUptoXDays: async (req, res) => { | ||
const { body } = req; | ||
try { | ||
const { userID } = body; | ||
const { days } = req.query; | ||
const expenses = await Expense.find({ userID }).where('date').gt(dateBeforeXFromNow(days)).sort('date'); | ||
res.status(200).json({ data: expenses, message: "data retrieved" }); | ||
} catch (e) { | ||
console.log(e); | ||
res.status(400).json({ message: "data could not be retrieved" }); | ||
} | ||
} | ||
} |
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 router = require('express').Router() | ||
const { addNewExpense, updateExpense, getSummaryUptoXDays } = require('../controllers/expense'); | ||
const getUserFromJWT = require('../middlewares/validateUser'); | ||
|
||
router.post('/new', getUserFromJWT, addNewExpense); | ||
router.put('/update/:id', getUserFromJWT, updateExpense); | ||
router.get("/summaryUpto", getUserFromJWT, getSummaryUptoXDays) | ||
|
||
module.exports = router; |
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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
const router = require('express').Router() | ||
const { signUp, login } = require('../controllers/user') | ||
|
||
const { signUp, login, updateSetting, currentExpenses, getDetails} = require('../controllers/user') | ||
const getUserFromJWT = require('../middlewares/validateUser'); | ||
|
||
router.post('/signUp', signUp); | ||
router.post('/login', login); | ||
router.put('/updateSetting', getUserFromJWT, updateSetting); | ||
router.get('/currentExpenses', getUserFromJWT, currentExpenses); | ||
router.get('/getDetails', getUserFromJWT, getDetails); | ||
|
||
module.exports = router; |
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 dateBeforeXFromNow = (daysBefore) => { | ||
const date = new Date(); | ||
date.setDate(date.getDate() - daysBefore); | ||
return date; | ||
} | ||
|
||
module.exports = { dateBeforeXFromNow }; |
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,6 @@ | ||
const assert = function (condition, message) { | ||
if (!condition) | ||
throw Error('Assert failed: ' + (message || '')); | ||
}; | ||
|
||
module.exports = { assert } |