transactionData.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. const Transaction = require("../models/transaction");
  2. const Order = require("../models/order");
  3. const Merchant = require("../models/merchant");
  4. module.exports = {
  5. //POST - returns all transactions for a merchant between given dates
  6. //Inputs:
  7. // req.body.from: start date
  8. // req.body.to: end date
  9. //Returns:
  10. // List of transactions
  11. getTransactions: function(req, res){
  12. if(!req.session.user){
  13. req.session.error = "You must be logged in to view that page";
  14. return res.redirect("/");
  15. }
  16. let date = new Date();
  17. let firstDay, lastDay;
  18. if(req.body.from && req.body.to){
  19. firstDay = new Date(req.body.from);
  20. lastDay = new Date(req.body.to);
  21. }else{
  22. firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
  23. lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  24. }
  25. Transaction.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}},
  26. {_id: 0, date: 1, recipes: 1},
  27. {sort: {date: 1}})
  28. .then((transactions)=>{
  29. return res.json(transactions);
  30. })
  31. .catch((err)=>{
  32. return res.json("Error: could not retrieve sales data");
  33. });
  34. },
  35. getOrders: function(req, res){
  36. if(!req.session.user){
  37. req.session.error = "You must be logged in to view that page";
  38. return res.redirect("/");
  39. }
  40. Order.find({merchant: req.session.user})
  41. .then((orders)=>{
  42. return res.json(orders);
  43. })
  44. .catch((err)=>{
  45. return res.json("Error: could not retrieve order data");
  46. })
  47. },
  48. //POST - Update non-pos merchant inventory and create a transaction
  49. //Inputs:
  50. // recipesSold: list of recipes sold and how much (recipe._id and quantity)
  51. //Returns:
  52. // merchant.inventory: entire merchant inventory after being updated
  53. createTransaction: function(req, res){
  54. if(!req.session.user){
  55. res.session.error = "Must be logged in to do that";
  56. return res.redirect("/");
  57. }
  58. let transaction = new Transaction({
  59. date: Date.now(),
  60. merchant: req.session.user,
  61. recipes: []
  62. });
  63. for(let recipe of req.body){
  64. transaction.recipes.push({
  65. recipe: recipe.id,
  66. quantity: recipe.quantity
  67. });
  68. }
  69. //Calculate all ingredients used, store to list
  70. Merchant.findOne({_id: req.session.user})
  71. .populate("recipes")
  72. .then((merchant)=>{
  73. for(let reqRecipe of req.body){
  74. let merchRecipe = merchant.recipes.find(r => r._id.toString() === reqRecipe.id);
  75. for(let recipeIngredient of merchRecipe.ingredients){
  76. let merchInvIngredient = merchant.inventory.find(i => i.ingredient.toString() === recipeIngredient.ingredient.toString());
  77. merchInvIngredient.quantity -= recipeIngredient.quantity * reqRecipe.quantity;
  78. }
  79. }
  80. merchant.save()
  81. .then((merchant)=>{
  82. res.json({});
  83. })
  84. .catch((err)=>{
  85. return res.json("Error: unable to save user data");
  86. });
  87. })
  88. .catch((err)=>{
  89. return res.json("Error: unable to retrieve user data");
  90. });
  91. transaction.save()
  92. .then((transaction)=>{
  93. return;
  94. })
  95. .catch((err)=>{});
  96. },
  97. populate: function(req, res){
  98. if(!req.session.user){
  99. res.session.error = "Must be logged in to do that";
  100. return res.redirect("/");
  101. }
  102. function randomDate() {
  103. let now = new Date();
  104. let start = new Date();
  105. start.setFullYear(now.getFullYear() - 1);
  106. return new Date(start.getTime() + Math.random() * (now.getTime() - start.getTime()));
  107. }
  108. Merchant.findOne({_id: req.session.user})
  109. .then((merchant)=>{
  110. let newTransactions = [];
  111. for(let i = 0; i < 5000; i++){
  112. let newTransaction = new Transaction({
  113. merchant: merchant._id,
  114. date: randomDate(),
  115. recipes: []
  116. });
  117. let numberOfRecipes = Math.floor((Math.random() * 5) + 1);
  118. for(let j = 0; j < numberOfRecipes; j++){
  119. let recipeNumber = Math.floor(Math.random() * merchant.recipes.length);
  120. let randQuantity = Math.floor((Math.random() * 3) + 1);
  121. newTransaction.recipes.push({
  122. recipe: merchant.recipes[recipeNumber],
  123. quantity: randQuantity
  124. });
  125. }
  126. newTransactions.push(newTransaction);
  127. }
  128. Transaction.create(newTransactions)
  129. .then((transactions)=>{
  130. console.log("completed");
  131. return res.redirect("/data");
  132. })
  133. .catch((err)=>{
  134. console.log(err);
  135. return;
  136. });
  137. })
  138. .catch((err)=>{
  139. console.log(err);
  140. return;
  141. });
  142. }
  143. }