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

Conversation

BiswajitSahoo-tech
Copy link
Contributor

@BiswajitSahoo-tech BiswajitSahoo-tech commented Oct 8, 2022

Fixes [ #55 ]

Hi @roopeshsn,
I have added inventory management, below is a brief of my solution:

  1. First created a updateStockCount function on backend/controller/productController/
    few notes on the updateStockCount function and how it handles the edge cases

    • var product = await Product.findById(id) - find the product by id passed as argument
    •  if(product.countInStock < qty){
             throw new Error(" A MESSAGE")
       }  
      
      The above code throws an Error if the Quantity demanded is more than in the stock amount of any product present on
      OrderItem
    • Finally, if everything goes well, then the below code will be executed and stock will be updated for each product
      corresponding to the given order.
      product.countInStock -= qty
      const updatedProduct = await product.save()
      
  2. Add the following code in updateOrderToPaid function under backend/controller/orderController/

    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
      })
    }
    
    

    catch(err) will catch the error thrown by updateStockCount and immediately send a response to the client having
    an appropriate message of what actually happens also ends the request-response cycle such that in the case of a catch
    there will be no update on these fields.
    isPaid and paidAt - these fields are updated only when all the promises at updatePromises will be resolved successfully.

    order.isPaid = true
    order.paidAt = Date.now()
    

    The above code will execute only when all the promises at updatePromises will be resolved successfully.

  3. I have tested the feature using Postman and it is working as expected.

  4. No other breaking changes are made to the system

Thank You ❤️

@roopeshsn
Copy link
Owner

Hi, @BiswajitSahoo-tech Could you also check with the frontend and add an appropriate video or a screenshot?

@BiswajitSahoo-tech
Copy link
Contributor Author

BiswajitSahoo-tech commented Oct 8, 2022

Ok @roopeshsn, Got it
will get back to you with screenshots
Thanks

@BiswajitSahoo-tech
Copy link
Contributor Author

Hi @roopeshsn,
Below are the screenshots following the flow

  1. User goes to a product page and adds it to the cart (Note: status is In Stock)
    fb-ss-1

  2. User goes to cart and clicks Proceed to checkout
    fb-ss-2

  3. After following all the intermediate steps, the User places the order
    fb-ss-3

  4. Now we make PUT request to api/orders/63417f09c2fadfe36a1f7699/pay, since we have to pay for the order in order to have updated the stock amount
    fb-ss-4

  5. After making the payment, see the status of the paid order page
    fb-ss-5

  6. Now also check the product page: ( Note: Status is now Out of stock)
    fb-ss-6

Actually initially there is 5 kg carrot in stock and user ordered 5 kg carrots, therefore after payment we have carrot out of stock

@BiswajitSahoo-tech
Copy link
Contributor Author

@roopeshsn, Please have a look.

@roopeshsn
Copy link
Owner

Hi @BiswajitSahoo-tech Could you remove that .env.sample file and all its imports? We have .env in the root. Also, the code is not formatted. There is a prettier command to format the recursively check the package.json file.

@BiswajitSahoo-tech
Copy link
Contributor Author

Hi @roopeshsn,
I have formatted the code and removed the .env.sample file.
Please review it.

package.json Outdated
@@ -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

}
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.

@roopeshsn
Copy link
Owner

Good work @BiswajitSahoo-tech Let me know if you're interested to become a collaborator for this project.

@roopeshsn roopeshsn merged commit 54b2b78 into roopeshsn:master Oct 9, 2022
@roopeshsn roopeshsn added the hacktoberfest-accepted Accepted PR for hactoberfest label Oct 9, 2022
@BiswajitSahoo-tech
Copy link
Contributor Author

Thanks, @roopeshsn, for giving me this opportunity.
Just a doubt, what are the works of a project collaborator?

@roopeshsn
Copy link
Owner

The work of a maintainer or owner of a project. Collaborator will raise issues for a feature or a bug, review and merge PRs. In short, Collaborator will work for the improvement of the project.

@BiswajitSahoo-tech
Copy link
Contributor Author

Sorry, @roopeshsn, we have a term exam scheduled next week so, I don't think I can contribute much as a collaborator. Thank you very much for the offer.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
hacktoberfest-accepted Accepted PR for hactoberfest
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants