Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Added inventory management #80

Merged
merged 6 commits into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MONGODB_URI=
PORT=
NODE_ENV=
JWT_SECRET=
2 changes: 1 addition & 1 deletion backend/config/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const mongoose = require('mongoose')

const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGODB_URI, {
const conn = await mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
Expand Down
19 changes: 18 additions & 1 deletion backend/controllers/orderControllers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const asyncHandler = require('express-async-handler')
const Order = require('../models/orderModel')
const Product = require('../models/productModel')
const productController = require('./../controllers/productController')

// @desc Create new order
// @route POST /api/orders
Expand All @@ -17,7 +18,8 @@ const addOrderItems = asyncHandler(async (req, res) => {
// Product price validation
orderItems.forEach(async (item) => {
let lookupItem = await Product.findById(item.product)
if (item.price !== lookupItem.price) {
if (parseFloat( item.price) !== lookupItem.price) {

res.status(400)
throw new Error(
'There is a discrepancy between the prices of the items, and whats in the Database, please try again!',
Expand Down Expand Up @@ -91,6 +93,21 @@ const updateOrderToPaid = asyncHandler(async (req, res) => {
const order = await Order.findById(req.params.id)

if (order) {
//below .map() function return an array of promises
const updatePromises = order.orderItems.map(async (item)=> {
await productController.updateStockCount( req, res, item.product,parseInt( item.qty))
})
//below function to resolve the array of promises
try{
await Promise.all( updatePromises)
}catch( error){
return res.status(404).json({
status:'fail',
message: error.message
})
}


order.isPaid = true
order.paidAt = Date.now()
const updatedOrder = await order.save()
Expand Down
18 changes: 18 additions & 0 deletions backend/controllers/productController.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,28 @@ const updateProduct = asyncHandler(async (req, res) => {
}
})

const updateStockCount =asyncHandler( async (req, res, id, qty)=>{
try{
var product = await Product.findById(id)
}catch(err){
throw new Error(err.message)
}

if(product.countInStock < qty){
throw new Error("We are really sorry. We have not "+qty+" amount of "+
product.name+" currently on stock. Either you wait for stock to fullfilled or you can decrease your ordered quantity of "+product.name)
}
product.countInStock -= qty
const updatedProduct = await product.save()
return updateProduct
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why this line is added?

Copy link
Contributor Author

@BiswajitSahoo-tech BiswajitSahoo-tech Oct 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return updateProduct this line?
This is added such that the updated product will be in the resolved result of all promises that we awaiting in orderController.
But there is no requirement for this result, we can omit this.


})

module.exports = {
getProducts,
getProductById,
deleteProduct,
createProduct,
updateProduct,
updateStockCount
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"mongoose": "^6.4.6",
"multer": "^1.4.3",
"nodemailer": "^6.6.5"

Copy link
Owner

@roopeshsn roopeshsn Oct 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You haven't added any packages in the package.json file right then why you have committed the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roopeshsn I removed it, check it now please

},
"devDependencies": {
"chai": "^4.3.6",
Expand Down