transactionData.js 4.4 KB

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