transactionData.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const Transaction = require("../models/transaction");
  2. const Purchase = require("../models/purchase");
  3. const Merchant = require("../models/merchant");
  4. module.exports = {
  5. getTransactions: function(req, res){
  6. if(!req.session.user){
  7. req.session.error = "You must be logged in to view that page";
  8. return res.redirect("/");
  9. }
  10. let date = new Date();
  11. let firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
  12. let lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  13. Transaction.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}})
  14. .then((transactions)=>{
  15. return res.json(transactions);
  16. })
  17. .catch((err)=>{
  18. return res.json("Error: could not retrieve sales data");
  19. });
  20. },
  21. getPurchases: function(req, res){
  22. if(!req.session.user){
  23. req.session.error = "You must be logged in to view that page";
  24. return res.redirect("/");
  25. }
  26. Purchase.find({merchant: req.session.user})
  27. .then((purchases)=>{
  28. return res.json(purchases);
  29. })
  30. .catch((err)=>{
  31. return res.json("Error: could not retrieve purchases data");
  32. })
  33. },
  34. //POST - Update non-pos merchant inventory and create a transaction
  35. //Inputs:
  36. // recipesSold: list of recipes sold and how much (recipe._id and quantity)
  37. //Returns:
  38. // merchant.inventory: entire merchant inventory after being updated
  39. createTransaction: function(req, res){
  40. if(!req.session.user){
  41. res.session.error = "Must be logged in to do that";
  42. return res.redirect("/");
  43. }
  44. let transaction = new Transaction({
  45. date: Date.now(),
  46. merchant: req.session.user,
  47. recipes: []
  48. });
  49. for(let recipe of req.body){
  50. transaction.recipes.push({
  51. recipe: recipe.id,
  52. quantity: recipe.quantity
  53. });
  54. }
  55. //Calculate all ingredients used, store to list
  56. Merchant.findOne({_id: req.session.user})
  57. .populate("recipes")
  58. .then((merchant)=>{
  59. for(let reqRecipe of req.body){
  60. let merchRecipe = merchant.recipes.find(r => r._id.toString() === reqRecipe.id);
  61. for(let recipeIngredient of merchRecipe.ingredients){
  62. let merchInvIngredient = merchant.inventory.find(i => i.ingredient.toString() === recipeIngredient.ingredient.toString());
  63. merchInvIngredient.quantity -= recipeIngredient.quantity * reqRecipe.quantity;
  64. }
  65. }
  66. merchant.save()
  67. .then((merchant)=>{
  68. res.json({});
  69. })
  70. .catch((err)=>{
  71. return res.json("Error: unable to save user data");
  72. });
  73. })
  74. .catch((err)=>{
  75. return res.json("Error: unable to retrieve user data");
  76. });
  77. transaction.save()
  78. .then((transaction)=>{
  79. return;
  80. })
  81. .catch((err)=>{});
  82. },
  83. }