transactionData.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. Transaction.aggregate([
  27. {$match: {
  28. merchant: new ObjectId(req.session.user),
  29. date: {
  30. $gte: startDate,
  31. $lt: endDate
  32. },
  33. recipes: {
  34. $elemMatch: {
  35. recipe: {
  36. $in: objectifiedRecipes
  37. }
  38. }
  39. }
  40. }},
  41. {$sort: {
  42. date: 1
  43. }}
  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
  59. }]
  60. }
  61. */
  62. createTransaction: function(req, res){
  63. if(!req.session.user){
  64. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  65. return res.redirect("/");
  66. }
  67. let newTransaction = new Transaction({
  68. merchant: req.session.user,
  69. date: new Date(req.body.date),
  70. device: "none",
  71. recipes: req.body.recipes
  72. });
  73. newTransaction.save()
  74. .then((response)=>{
  75. return res.json(response);
  76. })
  77. .catch((err)=>{
  78. return res.json("ERROR: UNABLE TO CREATE NEW TRANSACTION");
  79. });
  80. },
  81. /*
  82. DELETE - Remove a transaction from the database
  83. */
  84. remove: function(req, res){
  85. if(!req.session.user){
  86. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  87. return res.redirect("/");
  88. }
  89. Transaction.deleteOne({_id: req.params.id})
  90. .then((response)=>{
  91. return res.json({});
  92. })
  93. .catch((err)=>{
  94. return res.json("ERROR: UNABLE TO DELETE TRANSACTION");
  95. });
  96. },
  97. /*
  98. GET - Creates 5000 transactions for logged in merchant for testing
  99. */
  100. populate: function(req, res){
  101. if(!req.session.user){
  102. res.session.error = "Must be logged in to do that";
  103. return res.redirect("/");
  104. }
  105. function randomDate() {
  106. let now = new Date();
  107. let start = new Date();
  108. start.setFullYear(now.getFullYear() - 1);
  109. return new Date(start.getTime() + Math.random() * (now.getTime() - start.getTime()));
  110. }
  111. Merchant.findOne({_id: req.session.user})
  112. .then((merchant)=>{
  113. let newTransactions = [];
  114. for(let i = 0; i < 5000; i++){
  115. let newTransaction = new Transaction({
  116. merchant: merchant._id,
  117. date: randomDate(),
  118. recipes: []
  119. });
  120. let numberOfRecipes = Math.floor((Math.random() * 5) + 1);
  121. for(let j = 0; j < numberOfRecipes; j++){
  122. let recipeNumber = Math.floor(Math.random() * merchant.recipes.length);
  123. let randQuantity = Math.floor((Math.random() * 3) + 1);
  124. newTransaction.recipes.push({
  125. recipe: merchant.recipes[recipeNumber],
  126. quantity: randQuantity
  127. });
  128. }
  129. newTransactions.push(newTransaction);
  130. }
  131. Transaction.create(newTransactions)
  132. .then((transactions)=>{
  133. return res.redirect("/dashboard");
  134. })
  135. .catch((err)=>{
  136. return;
  137. });
  138. })
  139. .catch((err)=>{
  140. return;
  141. });
  142. }
  143. }