Przeglądaj źródła

Create populate route to create testing transactions. Test viabilityof data transfers.

Lee Morgan 6 lat temu
rodzic
commit
f05edf3fc1
2 zmienionych plików z 57 dodań i 0 usunięć
  1. 56 0
      controllers/transactionData.js
  2. 1 0
      routes.js

+ 56 - 0
controllers/transactionData.js

@@ -91,4 +91,60 @@ module.exports = {
             })
             .catch((err)=>{});
     },
+
+    populate: function(req, res){
+        if(!req.session.user){
+            res.session.error = "Must be logged in to do that";
+            return res.redirect("/");
+        }
+
+        function randomDate() {
+            let start = new Date(2020, 0, 1);
+            let end = new Date(2020, 11, 31);
+            return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
+        }
+
+        Merchant.findOne({_id: req.session.user})
+            .then((merchant)=>{
+                let newTransactions = [];
+
+                for(let i = 0; i < 25000; i++){
+                    let newTransaction = new Transaction({
+                        merchant: merchant._id,
+                        date: randomDate(),
+                        recipes: []
+                    });
+
+                    let numberOfRecipes = Math.floor((Math.random() * 5) + 1);
+
+                    for(let j = 0; j < numberOfRecipes; j++){
+                        let recipeNumber = Math.floor(Math.random() * merchant.recipes.length);
+                        let randQuantity = Math.floor((Math.random() * 3) + 1);
+
+                        newTransaction.recipes.push({
+                            recipe: merchant.recipes[recipeNumber],
+                            quantity: randQuantity
+                        });
+                    }
+
+                    newTransactions.push(newTransaction);
+                }
+
+                Transaction.create(newTransactions)
+                    .then((transactions)=>{
+                        console.log("completed");
+                        return;
+                    })
+                    .catch((err)=>{
+                        console.log(err);
+                        return;
+                    });
+            })
+            .catch((err)=>{
+                console.log(err);
+                return;
+            });
+        
+        
+    }
 }

+ 1 - 0
routes.js

@@ -44,4 +44,5 @@ module.exports = function(app){
     app.get("/transactions", transactionData.getTransactions);
     app.get("/purchases", transactionData.getPurchases);
     app.post("/transactions/create", transactionData.createTransaction);  //Creates transaction for non-pos merchant
+    app.get("/populate", transactionData.populate);
 }