transactionData.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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: {
  43. date: 1
  44. }}
  45. ])
  46. .then((transactions)=>{
  47. return res.json(transactions);
  48. })
  49. .catch((err)=>{
  50. return res.json("ERROR: UNABLE TO RETRIEVE YOUR TRANSACTIONS");
  51. });
  52. },
  53. /*
  54. POST - create a new transaction
  55. req.body = {
  56. date: date of the transaction,
  57. recipes: [{
  58. recipe: id of the recipe to add,
  59. quantity: quantity of the recipe sold
  60. }]
  61. }
  62. */
  63. createTransaction: function(req, res){
  64. if(!req.session.user){
  65. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  66. return res.redirect("/");
  67. }
  68. let newTransaction = new Transaction({
  69. merchant: req.session.user,
  70. date: new Date(req.body.date),
  71. device: "none",
  72. recipes: req.body.recipes
  73. });
  74. newTransaction.save()
  75. .then((response)=>{
  76. return res.json(response);
  77. })
  78. .catch((err)=>{
  79. return res.json("ERROR: UNABLE TO CREATE NEW TRANSACTION");
  80. });
  81. },
  82. /*
  83. DELETE - Remove a transaction from the database
  84. */
  85. remove: function(req, res){
  86. if(!req.session.user){
  87. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  88. return res.redirect("/");
  89. }
  90. Transaction.deleteOne({_id: req.params.id})
  91. .then((response)=>{
  92. return res.json({});
  93. })
  94. .catch((err)=>{
  95. return res.json("ERROR: UNABLE TO DELETE TRANSACTION");
  96. });
  97. },
  98. /*
  99. GET - Creates 5000 transactions for logged in merchant for testing
  100. */
  101. populate: function(req, res){
  102. if(!req.session.user){
  103. res.session.error = "Must be logged in to do that";
  104. return res.redirect("/");
  105. }
  106. function randomDate() {
  107. let now = new Date();
  108. let start = new Date();
  109. start.setFullYear(now.getFullYear() - 1);
  110. return new Date(start.getTime() + Math.random() * (now.getTime() - start.getTime()));
  111. }
  112. Merchant.findOne({_id: req.session.user})
  113. .then((merchant)=>{
  114. let newTransactions = [];
  115. for(let i = 0; i < 5000; i++){
  116. let newTransaction = new Transaction({
  117. merchant: merchant._id,
  118. date: randomDate(),
  119. recipes: []
  120. });
  121. let numberOfRecipes = Math.floor((Math.random() * 5) + 1);
  122. for(let j = 0; j < numberOfRecipes; j++){
  123. let recipeNumber = Math.floor(Math.random() * merchant.recipes.length);
  124. let randQuantity = Math.floor((Math.random() * 3) + 1);
  125. newTransaction.recipes.push({
  126. recipe: merchant.recipes[recipeNumber],
  127. quantity: randQuantity
  128. });
  129. }
  130. newTransactions.push(newTransaction);
  131. }
  132. Transaction.create(newTransactions)
  133. .then((transactions)=>{
  134. return res.redirect("/dashboard");
  135. })
  136. .catch((err)=>{
  137. return;
  138. });
  139. })
  140. .catch((err)=>{
  141. return;
  142. });
  143. }
  144. }