Explorar el Código

Finish adding of new transactions

Lee Morgan hace 6 años
padre
commit
68b424520f

+ 32 - 0
controllers/transactionData.js

@@ -57,6 +57,38 @@ module.exports = {
             });
     },
 
+    /*
+    POST - create a new transaction
+    req.body = {
+        date: date of the transaction,
+        recipes: [{
+            recipe: id of the recipe to add,
+            quantity: quantity of the recipe sold
+        }]
+    }
+    */
+    createTransaction: function(req, res){
+        if(!req.session.user){
+            req.session.error = "Must be logged in to do that";
+            return res.redirect("/");
+        }
+
+        let newTransaction = new Transaction({
+            merchant: req.session.user,
+            date: new Date(req.body.date),
+            device: "none",
+            recipes: req.body.recipes
+        });
+
+        newTransaction.save()
+            .then((response)=>{
+                return res.json(response);
+            })
+            .catch((err)=>{
+                return res.json("ERROR: UNABLE TO CREATE NEW TRANSACTION");
+            });
+    },
+
     /*
     DELETE - Remove a transaction from the database
     */

+ 1 - 0
routes.js

@@ -36,6 +36,7 @@ module.exports = function(app){
     app.delete("/order/:id", orderData.removeOrder);
 
     //Transactions
+    app.post("/transaction", transactionData.createTransaction);
     app.delete("/transaction/:id", transactionData.remove);
     app.get("/populatesometransactions", transactionData.populate);
 

+ 7 - 0
views/dashboardPage/Merchant.js

@@ -217,16 +217,23 @@ class Merchant{
     }
 
     editTransactions(transaction, remove = false){
+        let isNew = true;
         for(let i = 0; i < this.transactions.length; i++){
             if(this.transactions[i] === transaction){
                 if(remove){
                     this.transactions.splice(i, 1);
                 }
 
+                isNew = false;
                 break;
             }
         }
 
+        if(isNew){
+            this.transactions.push(transaction);
+            this.transactions.sort((a, b) => a.date > b.date ? 1 : -1);
+        }
+
         transactionsStrandObj.isPopulated = false;
         transactionsStrandObj.display();
         closeSidebar();

+ 3 - 1
views/dashboardPage/sidebars/newTransaction.ejs

@@ -10,6 +10,8 @@
 
     <h2>CREATE NEW TRANSACTION</h2>
 
+    <input id="newTransactionDate" type="date">
+
     <div id="newTransactionRecipes" class="newTransactionRecipes"></div>
 
     <button class="button" onclick="newTransactionComp.submit()">Create</button>
@@ -17,7 +19,7 @@
     <template id="createTransaction">
         <div class="createTransaction smallItemDisplay">
             <p></p>
-            <input type="number" step="1">
+            <input type="number" min="0" step="1">
         </div>
     </template>
 </div>

+ 62 - 1
views/dashboardPage/sidebars/sidebars.js

@@ -943,6 +943,7 @@ let newTransactionComp = {
 
         for(let i = 0; i < merchant.recipes.length; i++){
             let recipeDiv = template.cloneNode(true);
+            recipeDiv.recipe = merchant.recipes[i];
             recipeList.appendChild(recipeDiv);
 
             recipeDiv.children[0].innerText = merchant.recipes[i].name;
@@ -952,6 +953,66 @@ let newTransactionComp = {
     },
 
     submit: function(){
-        console.log("submitting");
+        let recipeDivs = document.getElementById("newTransactionRecipes");
+        let date = document.getElementById("newTransactionDate").valueAsDate;
+        console.log(recipeDivs);
+        
+        if(date > new Date()){
+            banner.createError("Cannot have a date in the future");
+            return;
+        }
+        
+        let newTransaction = {
+            date: date,
+            recipes: []
+        };
+
+        for(let i = 0; i < recipeDivs.children.length;  i++){
+            let quantity = recipeDivs.children[i].children[1].value;
+            if(quantity !== "" && quantity > 0){
+                newTransaction.recipes.push({
+                    recipe: recipeDivs.children[i].recipe.id,
+                    quantity: quantity
+                });
+            }else if(quantity < 0){
+                banner.createError("Cannot have negative values");
+                return;
+            }
+        }
+
+        if(newTransaction.recipes.length > 0){
+            let loader = document.getElementById("loaderContainer");
+            loader.style.display = "flex";
+
+            fetch("/transaction", {
+                method: "post",
+                headers: {
+                    "Content-Type": "application/json;charset=utf-8"
+                },
+                body: JSON.stringify(newTransaction)
+            })
+                .then(response => response.json())
+                .then((response)=>{
+                    if(typeof(response) === "string"){
+                        banner.createError(response);
+                    }else{
+                        let transaction = new Transaction(
+                            response._id,
+                            response.date,
+                            response.recipes,
+                            merchant
+                        );
+                        merchant.editTransactions(transaction);
+                        banner.createNotification("NEW TRANSACTION CREATED");
+                    }
+                })
+                .catch((err)=>{
+                    console.log(err);
+                    banner.createError("Something went wrong, try refreshing the page");
+                })
+                .finally(()=>{
+                    loader.style.display = "none";
+                });
+        }
     }
 }