transactionData.js 5.0 KB

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