transactionData.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. populate: function(req, res){
  84. if(!req.session.user){
  85. res.session.error = "Must be logged in to do that";
  86. return res.redirect("/");
  87. }
  88. function randomDate() {
  89. let start = new Date(2020, 0, 1);
  90. let end = new Date(2020, 11, 31);
  91. return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
  92. }
  93. Merchant.findOne({_id: req.session.user})
  94. .then((merchant)=>{
  95. let newTransactions = [];
  96. for(let i = 0; i < 25000; i++){
  97. let newTransaction = new Transaction({
  98. merchant: merchant._id,
  99. date: randomDate(),
  100. recipes: []
  101. });
  102. let numberOfRecipes = Math.floor((Math.random() * 5) + 1);
  103. for(let j = 0; j < numberOfRecipes; j++){
  104. let recipeNumber = Math.floor(Math.random() * merchant.recipes.length);
  105. let randQuantity = Math.floor((Math.random() * 3) + 1);
  106. newTransaction.recipes.push({
  107. recipe: merchant.recipes[recipeNumber],
  108. quantity: randQuantity
  109. });
  110. }
  111. newTransactions.push(newTransaction);
  112. }
  113. Transaction.create(newTransactions)
  114. .then((transactions)=>{
  115. console.log("completed");
  116. return;
  117. })
  118. .catch((err)=>{
  119. console.log(err);
  120. return;
  121. });
  122. })
  123. .catch((err)=>{
  124. console.log(err);
  125. return;
  126. });
  127. }
  128. }