Răsfoiți Sursa

Invetory check functionality all working. Added in the imperfect banner for notifications.

Lee Morgan 6 ani în urmă
părinte
comite
c1b3c069ef

+ 18 - 12
controllers/merchantData.js

@@ -307,8 +307,9 @@ module.exports = {
 
     //POST - Update the quantity for a merchant inventory item
     //Inputs:
-    //  req.body.ingredientId: Id of ingredient to update
-    //  req.body.quantityChange: Amount to change ingredient (not the new value)
+    //  req.body.ingredients: array of ingredient data
+    //      id: id of ingredient to update
+    //      quantityChange: Amount to change ingredient (not the new value)
     //Returns: Nothing
     updateMerchantIngredient: function(req, res){
         if(!req.session.user){
@@ -316,10 +317,22 @@ module.exports = {
             return res.redirect("/");
         }
 
+        let adjustments = [];
+
         Merchant.findOne({_id: req.session.user})
             .then((merchant)=>{
-                let updateIngredient = merchant.inventory.find(i => i.ingredient.toString() === req.body.ingredientId);
-                updateIngredient.quantity = (updateIngredient.quantity + req.body.quantityChange).toFixed(2);
+                for(let ingredient of req.body){
+                    console.log(ingredient);
+                    let updateIngredient = merchant.inventory.find(i => i.ingredient.toString() === ingredient.id);
+                    updateIngredient.quantity = (updateIngredient.quantity + ingredient.quantityChange);
+
+                    adjustments.push(new InventoryAdjustment({
+                        date: Date.now(),
+                        merchant: req.session.user,
+                        ingredient: ingredient.id,
+                        quantity: ingredient.quantityChange
+                    }));
+                }
                 merchant.save()
                     .then((newMerchant)=>{
                         res.json({});
@@ -332,14 +345,7 @@ module.exports = {
                 return res.json("Error: your data could not be retrieved");
             });
 
-        let invAdj = new InventoryAdjustment({
-            date: Date.now(),
-            merchant: req.session.user,
-            ingredient: req.body.ingredientId,
-            quantity: req.body.quantityChange
-        });
-
-        invAdj.save().catch((err)=>{});
+        InventoryAdjustment.create(adjustments).catch(()=>{});
     },
 
     //POST - Adds an ingredient to a recipe

+ 40 - 5
views/dashboardPage/dashboard.css

@@ -10,9 +10,10 @@ body{
 }
 
 .contentBlock{
-    padding: 25px;
     width: 100%;
-    height: 100%;
+    height: 100vh;
+    box-sizing: border-box;
+    padding: 25px;
 }
 
 /* Cards */
@@ -37,12 +38,13 @@ body{
 #homeStrand{
     display: flex;
     flex-direction: column;
-    height: 100%;
+    max-height: 100%;
     width: 100%;
 }
 
 #homeStrand h1{
     text-align: left;
+    margin: 15px;
 }
 
 .flexRow{
@@ -79,7 +81,7 @@ body{
 
 #graphCard{
     flex-grow: 1;
-    max-height: 50vh;
+    max-height: 40vh;
 }
 
     #graphCard canvas{
@@ -88,7 +90,40 @@ body{
     }
 
 #inventoryCheckCard ul{
-    text-decoration: none;
+    list-style: none;
+}
+
+#inventoryCheckCard li{
+    margin: 5px;
+}
+
+#inventoryCheckCard li > p{
+    font-weight: 300;
+    font-size: 15px;
+    margin-right: 25px;
+}
+
+#inventoryCheckCard li > p:nth-of-type(1){
+    width: 50%;
+}
+
+#inventoryCheckCard li input{
+    width: 50px;
+    margin-right: 25px;
+}
+
+#inventoryCheckCard li p:nth-of-type(2){
+    width: 25%;
+    min-width: 75px;
+}
+
+#inventoryCheckCard button{
+    margin: 0;
+    margin-top: 10px;
+    float: right;
+    height: 25px;
+    padding: 0 29px;
+    font-size: 15px;
 }
 
 /* Ingredients Strand */

+ 7 - 1
views/dashboardPage/dashboard.ejs

@@ -12,6 +12,8 @@
         <% include ../shared/menu %>
 
         <div class="contentBlock">
+            <% include ../shared/banner %>
+            
             <div id="homeStrand" class="strand">
                 <h1>Dashboard</h1>
 
@@ -40,12 +42,16 @@
                     </div>
                 </div>
 
-                <div>
+                <div class="flexRow">
                     <div id="inventoryCheckCard" class="card">
                         <p>Inventory Check</p>
 
                         <ul></ul>
+
+                        <button class="button" onclick="homeStrandObj.updateInventory()">Update</button>
                     </div>
+
+                    <div id=popularIngredientsCard class="card"></div>
                 </div>
             </div>
 

+ 80 - 2
views/dashboardPage/home.js

@@ -40,11 +40,37 @@ window.homeStrandObj = {
                 "Revenue"
             );
 
-            let ul = document.querySelector("#inventoryCheckCard ul");
+            //Inventory Check
+            let rands = [];
             for(let i = 0; i < 5; i++){
+                let rand = Math.floor(Math.random() * merchant.inventory.length);
+
+                if(rands.includes(rand)){
+                    i--;
+                }else{
+                    rands[i] = rand;
+                }
+            }
+
+            let ul = document.querySelector("#inventoryCheckCard ul");
+            for(let rand of rands){
                 let li = document.createElement("li");
-                li.innerText = merchant.inventory[i].ingredient.name;
+                li.classList = "flexRow";
+                li.ingredientIndex = rand;
                 ul.appendChild(li);
+
+                let name = document.createElement("p");
+                name.innerText = merchant.inventory[rand].ingredient.name;
+                li.appendChild(name);
+
+                let input = document.createElement("input");
+                input.type = "number";
+                input.value = merchant.inventory[rand].quantity;
+                li.appendChild(input);
+
+                let label = document.createElement("p");
+                label.innerText = merchant.inventory[rand].ingredient.unit;
+                li.appendChild(label);
             }
 
             this.isPopulated = true;
@@ -88,5 +114,57 @@ window.homeStrandObj = {
         }
 
         return dataList;
+    },
+
+    updateInventory: function(){
+        let lis = document.querySelectorAll("#inventoryCheckCard ul li");
+
+        let changes = [];
+
+        for(let li of lis){
+            if(li.children[1].value >= 0){
+                let merchIngredient = merchant.inventory[li.ingredientIndex];
+
+                let change = li.children[1].value - merchIngredient.quantity;
+
+                if(change !== 0){
+                    changes.push({
+                        id: merchIngredient.ingredient._id,
+                        quantityChange: change
+                    });
+
+
+                }
+            }else{
+                banner.createError("Cannot have negative ingredients");
+                return;
+            }
+        }
+        
+        if(changes.length > 0){
+            fetch("/merchant/ingredients/update", {
+                method: "POST",
+                headers: {
+                    "Content-Type": "application/json;charset=utf-8"
+                },
+                body: JSON.stringify(changes)
+            })
+                .then((response)=>{
+                    banner.createError("Ingredients updated");
+                    
+                    if(typeof(response.data) === "string"){
+                        console.log(err);
+                    }else{
+                        for(let change of changes){
+                            merchant.inventory.find((item)=> item.ingredient._id === change.id).quantity += change.quantityChange;
+                        }
+                    }
+                })
+                .catch((err)=>{
+                    console.log(err);
+                });
+
+            
+        }
     }
 }

+ 0 - 1
views/dataPage/purchase.js

@@ -15,7 +15,6 @@ window.purchaseObj = {
             let purchasesDiv = document.querySelector("#purchaseOptions");
 
             for(let item of data.merchant.inventory){
-                console.log(item);
                 let checkDiv = document.createElement("div");
                 checkDiv.classList = "checkboxDiv";
                 purchasesDiv.appendChild(checkDiv);

+ 0 - 1
views/shared/graphs.js

@@ -39,7 +39,6 @@ class LineGraph{
     //  xRange = array containing two elements, start and end for x axis data (currently only dates)
     //  name = string name for the line.  Used for display and finding lines.  Each must be unique
     addData(data, xRange, name){
-        console.log(data);
         data = {
             set: data,
             colorIndex: this.colorIndex,