Selaa lähdekoodia

Add ability to add a merchant to an owner.
Can only be done for non-pos at the moment.
Has not been checked that it actually works.

Lee Morgan 5 vuotta sitten
vanhempi
sitoutus
d090151bf8

+ 42 - 16
controllers/merchantData.js

@@ -44,22 +44,17 @@ module.exports = {
         let expirationDate = new Date();
         expirationDate.setDate(expirationDate.getDate() + 90);
 
-        let owner = {};
-        if(req.body.owner === undefined){
-            owner = new Owner({
-                email: email,
-                password: hash,
-                createdAt: new Date(),
-                status: ["unverified"],
-                session: {
-                    sessionId: helper.generateId(25),
-                    expiration: expirationDate
-                },
-                merchants: []
-            });
-        }else{
-            owner = await Owner.findOne({_id: req.body.owner})
-        }
+        let owner = new Owner({
+            email: email,
+            password: hash,
+            createdAt: new Date(),
+            status: ["unverified"],
+            session: {
+                sessionId: helper.generateId(25),
+                expiration: expirationDate
+            },
+            merchants: []
+        });
 
         let merchant = new Merchant({
             owner: owner._id,
@@ -77,6 +72,7 @@ module.exports = {
                 return res.redirect(`/verify/email/${response[0]._id}`);
             })
             .catch((err)=>{
+                console.log(err);
                 if(typeof(err) === "string"){
                     req.session.error = err;
                 }else if(err.name === "ValidationError"){
@@ -89,6 +85,36 @@ module.exports = {
             });
     },
 
+    /*
+    POST: create new merchant for existing owner
+    req.body = {
+        name: String
+    }
+    */
+    addMerchantNone: function(req, res){
+        let merchant = new Merchant({
+            owner: res.locals.owner._id,
+            name: req.body.name,
+            pos: "none",
+            createdAt: new Date(),
+            inventory: [],
+            recipes: []
+        });
+
+        res.locals.owner.push(merchant._id);
+
+        Promise.all([owner.save(), merchant.save()])
+            .then((response)=>{
+                return res.json(response);
+            })
+            .catch((err)=>{
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
+                }
+                return res.json("ERROR: UNABLE TO CREATE NEW MERCHANT");
+            });
+    },
+
     /*
     POST - Update the quantity for a merchant inventory item
     req.body = [{

+ 1 - 0
routes.js

@@ -25,6 +25,7 @@ module.exports = function(app){
     
     //Merchant
     app.post("/merchant/create/none", merchantData.createMerchantNone);
+    app.post("/merchant/add/none", session, merchantData.addMerchantNone);
     app.put("/merchant/ingredients/update", session, merchantData.updateIngredientQuantities); //also updates some data in ingredients
     app.post("/merchant/password", merchantData.updatePassword); //TODO: change to work with session
     app.put("/merchant/update", session, merchantData.updateData);

+ 21 - 0
views/dashboardPage/dashboard.css

@@ -690,6 +690,27 @@ Modal
 
         }
 
+    #modalNewMerchant{
+        display: flex;
+        flex-direction: column;
+        background: white;
+        padding: 50px;
+        min-width: 350px;
+    }
+
+        #modalNewMerchant h2{
+            text-align: center;
+        }
+
+        #modalNewMerchant > *{
+            margin: 15px;
+        }
+
+        #modalNewMerchant label{
+            display: flex;
+            flex-direction: column;
+        }
+
 @media screen and (orientation: portrait){
     @media screen and (max-width: 1400px){
         body{

+ 6 - 2
views/dashboardPage/js/classes/Merchant.js

@@ -84,7 +84,7 @@ class Merchant{
             ingredients,
             recipes,
             transactions,
-            merchants
+            owner
         ){
         this._name = name;
         this._email = email;
@@ -93,7 +93,11 @@ class Merchant{
         this._recipes = [];
         this._transactions = [];
         this._orders = [];
-        this._merchants = merchants;
+        this._owner = {
+            id: owner._id,
+            email: owner.email,
+            merchants: owner.merchants
+        };
         
         //populate ingredients
         for(let i = 0; i < ingredients.length; i++){

+ 1 - 1
views/dashboardPage/js/dashboard.js

@@ -30,7 +30,7 @@ window.merchant = new Merchant(
     data.merchant.inventory,
     data.merchant.recipes,
     data.transactions,
-    data.owner.merchants
+    data.owner
 );
 
 controller = {

+ 35 - 1
views/dashboardPage/js/modal.js

@@ -1,3 +1,5 @@
+const merchant = require("../../../models/merchant");
+
 let modal = {
     feedback: function(){
         let form = document.getElementById("modalFeedback");
@@ -43,7 +45,39 @@ let modal = {
     newMerchant: function(){
         let form = document.getElementById("modalNewMerchant");
         form.style.display = "flex";
-        console.log("new merchant");
+        form.onsubmit = ()=>{this.submitNewMerchantNone()};
+    },
+
+    submitNewMerchantNone(){
+        event.preventDefault();
+
+        let data = {
+            name: document.getElementById("addMerchantName").value,
+        };
+
+        let loader = document.getElementById("loaderContainer");
+        loader.style.display = "flex";
+
+        fetch("/merchant/add/none", {
+            method: "post",
+            headers: {
+                "Content-Type": "application/json;charset=utf-8"
+            },
+            body: JSON.stringify(data)
+        })
+            .then((response)=>{
+                if(typeof(response) === "string"){
+                    controller.createBanner(response, "error");
+                }else{
+                    console.log(response);
+                }
+            })
+            .catch((err)=>{
+                controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
+            })
+            .finally(()=>{
+                loader.style.display = "none";
+            });
     }
 };
 

+ 9 - 1
views/dashboardPage/modal.ejs

@@ -27,8 +27,16 @@
                 <input class="button" type="submit" value="SUBMIT">
             </form>
 
-            <form id="modalNewMerchant" style="display:none;" method="post" action="">
+            <form id="modalNewMerchant" style="display:none;" method="post">
                 <h2>NEW MERCHANT</h2>
+                    
+                <label>NAME:
+                    <input type="text" id="addMerchantName" required>
+                </label>
+
+                <h3>CREATE WITH:</h3>
+
+                <input type="submit" class="button" value="NO POS">
             </form>
         </div>
     </div>

+ 0 - 2
views/otherPages/register.ejs

@@ -65,8 +65,6 @@
 
             <button id="noneButton" class="button" formaction="/merchant/create/none">NO POS</button>
 
-            <!-- <a class="button buttonWithBorder" href="/cloverlogin">Sign Up with Clover</a> -->
-
             <a class="link" href="/login">Already have an account? Please Sign In</a>
         </form>