transactionData.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const Transaction = require("../models/transaction");
  2. const Merchant = require("../models/merchant");
  3. module.exports = {
  4. /*
  5. GET - Creates 5000 transactions for logged in merchant for testing
  6. */
  7. populate: function(req, res){
  8. if(!req.session.user){
  9. res.session.error = "Must be logged in to do that";
  10. return res.redirect("/");
  11. }
  12. function randomDate() {
  13. let now = new Date();
  14. let start = new Date();
  15. start.setFullYear(now.getFullYear() - 1);
  16. return new Date(start.getTime() + Math.random() * (now.getTime() - start.getTime()));
  17. }
  18. Merchant.findOne({_id: req.session.user})
  19. .then((merchant)=>{
  20. let newTransactions = [];
  21. for(let i = 0; i < 5000; i++){
  22. let newTransaction = new Transaction({
  23. merchant: merchant._id,
  24. date: randomDate(),
  25. recipes: []
  26. });
  27. let numberOfRecipes = Math.floor((Math.random() * 5) + 1);
  28. for(let j = 0; j < numberOfRecipes; j++){
  29. let recipeNumber = Math.floor(Math.random() * merchant.recipes.length);
  30. let randQuantity = Math.floor((Math.random() * 3) + 1);
  31. newTransaction.recipes.push({
  32. recipe: merchant.recipes[recipeNumber],
  33. quantity: randQuantity
  34. });
  35. }
  36. newTransactions.push(newTransaction);
  37. }
  38. Transaction.create(newTransactions)
  39. .then((transactions)=>{
  40. return res.redirect("/dashboard");
  41. })
  42. .catch((err)=>{
  43. return;
  44. });
  45. })
  46. .catch((err)=>{
  47. return;
  48. });
  49. },
  50. /*
  51. POST - create a new transaction
  52. req.body = {
  53. date: date of the transaction,
  54. recipes: [{
  55. recipe: id of the recipe to add,
  56. quantity: quantity of the recipe sold
  57. }]
  58. }
  59. */
  60. createTransaction: function(req, res){
  61. if(!req.session.user){
  62. req.session.error = "Must be logged in to do that";
  63. return res.redirect("/");
  64. }
  65. let newTransaction = new Transaction({
  66. merchant: req.session.user,
  67. date: new Date(req.body.date),
  68. device: "none",
  69. recipes: req.body.recipes
  70. });
  71. newTransaction.save()
  72. .then((response)=>{
  73. return res.json(response);
  74. })
  75. .catch((err)=>{
  76. return res.json("ERROR: UNABLE TO CREATE NEW TRANSACTION");
  77. });
  78. },
  79. /*
  80. DELETE - Remove a transaction from the database
  81. */
  82. remove: function(req, res){
  83. if(!req.session.user){
  84. req.session.error = "Must be logged in to do that";
  85. return res.redirect("/");
  86. }
  87. Transaction.deleteOne({_id: req.params.id})
  88. .then((response)=>{
  89. return res.json({});
  90. })
  91. .catch((err)=>{
  92. return res.json("Error: unable to delete the transaction");
  93. });
  94. }
  95. }