transactionData.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. const Transaction = require("../models/transaction");
  2. const Merchant = require("../models/merchant");
  3. const helper = require("./helper.js");
  4. const ObjectId = require("mongoose").Types.ObjectId;
  5. module.exports = {
  6. /*
  7. POST - retrieves a list of transactions based on the filter
  8. req.body = {
  9. startDate: starting date to filter on,
  10. endDate: ending date to filter on,
  11. recipes: list of recipes to filter on
  12. }
  13. NOTE: May be a good idea to search recipes with for looping rather than query
  14. Needs some testing and playing with if so
  15. */
  16. getTransactions: function(req, res){
  17. if(!req.session.user){
  18. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  19. return res.redirect("/");
  20. }
  21. let objectifiedRecipes = [];
  22. for(let i = 0; i < req.body.recipes.length; i++){
  23. objectifiedRecipes.push(new ObjectId(req.body.recipes[i]));
  24. }
  25. let startDate = new Date(req.body.startDate);
  26. let endDate = new Date(req.body.endDate);
  27. endDate = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate() + 1);
  28. Transaction.aggregate([
  29. {$match: {
  30. merchant: new ObjectId(req.session.user),
  31. date: {
  32. $gte: startDate,
  33. $lt: endDate
  34. },
  35. recipes: {
  36. $elemMatch: {
  37. recipe: {
  38. $in: objectifiedRecipes
  39. }
  40. }
  41. }
  42. }},
  43. {$sort: {date: -1}}
  44. ])
  45. .then((transactions)=>{
  46. return res.json(transactions);
  47. })
  48. .catch((err)=>{
  49. return res.json("ERROR: UNABLE TO RETRIEVE YOUR TRANSACTIONS");
  50. });
  51. },
  52. /*
  53. POST - create a new transaction
  54. req.body = {
  55. date: date of the transaction,
  56. recipes: [{
  57. recipe: id of the recipe to add,
  58. quantity: quantity of the recipe sold (in main unit),
  59. }]
  60. ingredientUpdates: an object that contains all of the ingredients that
  61. need to be updated as well as the amount to change.
  62. keys = id
  63. values = quantity to change in grams
  64. }
  65. */
  66. createTransaction: function(req, res){
  67. if(!req.session.user){
  68. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  69. return res.redirect("/");
  70. }
  71. let newTransaction = new Transaction({
  72. merchant: req.session.user,
  73. date: new Date(req.body.date),
  74. device: "none",
  75. recipes: req.body.recipes
  76. });
  77. helper.updateIngredientQuantities(req.body.ingredientUpdates, req.session.user);
  78. newTransaction.save()
  79. .then((response)=>{
  80. return res.json(response);
  81. })
  82. .catch((err)=>{
  83. console.log(err);
  84. return res.json("ERROR: UNABLE TO CREATE NEW TRANSACTION");
  85. });
  86. },
  87. /*
  88. DELETE - Remove a transaction from the database
  89. */
  90. remove: function(req, res){
  91. if(!req.session.user){
  92. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  93. return res.redirect("/");
  94. }
  95. Transaction.deleteOne({_id: req.params.id})
  96. .then((response)=>{
  97. return res.json({});
  98. })
  99. .catch((err)=>{
  100. return res.json("ERROR: UNABLE TO DELETE TRANSACTION");
  101. });
  102. },
  103. /*
  104. GET - Creates 5000 transactions for logged in merchant for testing
  105. */
  106. populate: function(req, res){
  107. if(!req.session.user){
  108. res.session.error = "Must be logged in to do that";
  109. return res.redirect("/");
  110. }
  111. function randomDate() {
  112. let now = new Date();
  113. let start = new Date();
  114. start.setFullYear(now.getFullYear() - 1);
  115. return new Date(start.getTime() + Math.random() * (now.getTime() - start.getTime()));
  116. }
  117. Merchant.findOne({_id: req.session.user})
  118. .then((merchant)=>{
  119. let newTransactions = [];
  120. for(let i = 0; i < 5000; i++){
  121. let newTransaction = new Transaction({
  122. merchant: merchant._id,
  123. date: randomDate(),
  124. recipes: []
  125. });
  126. let numberOfRecipes = Math.floor((Math.random() * 5) + 1);
  127. for(let j = 0; j < numberOfRecipes; j++){
  128. let recipeNumber = Math.floor(Math.random() * merchant.recipes.length);
  129. let randQuantity = Math.floor((Math.random() * 3) + 1);
  130. newTransaction.recipes.push({
  131. recipe: merchant.recipes[recipeNumber],
  132. quantity: randQuantity
  133. });
  134. }
  135. newTransactions.push(newTransaction);
  136. }
  137. Transaction.create(newTransactions)
  138. .then((transactions)=>{
  139. return res.redirect("/dashboard");
  140. })
  141. .catch((err)=>{
  142. return;
  143. });
  144. })
  145. .catch((err)=>{
  146. return;
  147. });
  148. }
  149. }