transactionData.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const Transaction = require("../models/transaction");
  2. const Order = require("../models/order");
  3. const Merchant = require("../models/merchant");
  4. module.exports = {
  5. /*
  6. GET - Creates a 5000 transactions for logged in merchant for testing
  7. */
  8. populate: function(req, res){
  9. if(!req.session.user){
  10. res.session.error = "Must be logged in to do that";
  11. return res.redirect("/");
  12. }
  13. function randomDate() {
  14. let now = new Date();
  15. let start = new Date();
  16. start.setFullYear(now.getFullYear() - 1);
  17. return new Date(start.getTime() + Math.random() * (now.getTime() - start.getTime()));
  18. }
  19. Merchant.findOne({_id: req.session.user})
  20. .then((merchant)=>{
  21. let newTransactions = [];
  22. for(let i = 0; i < 5000; i++){
  23. let newTransaction = new Transaction({
  24. merchant: merchant._id,
  25. date: randomDate(),
  26. recipes: []
  27. });
  28. let numberOfRecipes = Math.floor((Math.random() * 5) + 1);
  29. for(let j = 0; j < numberOfRecipes; j++){
  30. let recipeNumber = Math.floor(Math.random() * merchant.recipes.length);
  31. let randQuantity = Math.floor((Math.random() * 3) + 1);
  32. newTransaction.recipes.push({
  33. recipe: merchant.recipes[recipeNumber],
  34. quantity: randQuantity
  35. });
  36. }
  37. newTransactions.push(newTransaction);
  38. }
  39. Transaction.create(newTransactions)
  40. .then((transactions)=>{
  41. return res.redirect("/dashboard");
  42. })
  43. .catch((err)=>{
  44. return;
  45. });
  46. })
  47. .catch((err)=>{
  48. return;
  49. });
  50. }
  51. }