transactionData.js 5.3 KB

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