Procházet zdrojové kódy

Add ability for users to register with no pos.

Lee Morgan před 5 roky
rodič
revize
5ee986bd9d

+ 7 - 6
controllers/emailVerification.js

@@ -1,3 +1,4 @@
+const Owner = require("../models/owner.js");
 const Merchant = require("../models/merchant.js");
 
 const mailgun = require("mailgun-js")({apiKey: process.env.MG_SUBLINE_APIKEY, domain: "mail.thesubline.net"});
@@ -5,21 +6,21 @@ const verifyEmail = require("../emails/verifyEmail.js");
 
 module.exports = {
     sendVerifyEmail: function(req, res){
-        Merchant.findOne({_id: req.params.id})
-            .then((merchant)=>{
+        Owner.findOne({_id: req.params.id})
+            .then((owner)=>{
                 const mailgunData = {
                     from: "The Subline <clientsupport@thesubline.net>",
-                    to: merchant.email,
+                    to: owner.email,
                     subject: "Email verification",
                     html: verifyEmail({
-                        name: merchant.name,
-                        link: `${process.env.SITE}/verify/${merchant._id}/${merchant.session.sessionId}`,
+                        name: owner.email,
+                        link: `${process.env.SITE}/verify/${owner._id}/${owner.session.sessionId}`,
                     })
                 };
                 mailgun.messages().send(mailgunData, (err, body)=>{});
 
 
-                return res.render(`verifyPage/verify`, {id: merchant._id, email: merchant.email, banner: res.locals.merchant});
+                return res.render(`verifyPage/verify`, {id: owner._id, email: owner.email, banner: res.locals.merchant});
             })
             .catch((err)=>{
                 req.session.error = "ERROR: UNABLE TO SEND VERIFICATION EMAIL";

+ 22 - 12
controllers/merchantData.js

@@ -1,3 +1,4 @@
+const Owner = require("../models/owner.js");
 const Merchant = require("../models/merchant");
 const InventoryAdjustment = require("../models/inventoryAdjustment");
 
@@ -29,7 +30,8 @@ module.exports = {
             return res.redirect("/register");
         }
 
-        const merchantFind = await Merchant.findOne({email: req.body.email.toLowerCase()});
+        let email = req.body.email.toLowerCase();
+        const merchantFind = await Merchant.findOne({email: email});
         if(merchantFind !== null){
             req.session.error = "USER WITH THIS EMAIL ADDRESS ALREADY EXISTS";
             return res.redirect("/login");
@@ -41,24 +43,32 @@ module.exports = {
         let expirationDate = new Date();
         expirationDate.setDate(expirationDate.getDate() + 90);
 
-        let merchant = new Merchant({
-            name: req.body.name,
-            email: req.body.email.toLowerCase(),
+        let owner = new Owner({
+            email: email,
             password: hash,
-            pos: "none",
-            createdAt: Date.now(),
+            createdAt: new Date(),
             status: ["unverified"],
-            inventory: [],
-            recipes: [],
             session: {
                 sessionId: helper.generateId(25),
                 expiration: expirationDate
-            }
+            },
+            merchants: []
         });
 
-        merchant.save()
-            .then((merchant)=>{
-                return res.redirect(`/verify/email/${merchant._id}`);
+        let merchant = new Merchant({
+            owner: owner._id,
+            name: req.body.name,
+            pos: "none",
+            createdAt: Date.now(),
+            inventory: [],
+            recipes: []
+        });
+
+        owner.merchants.push(merchant._id);
+
+        Promise.all([owner.save(), merchant.save()])
+            .then((response)=>{
+                return res.redirect(`/verify/email/${response[0]._id}`);
             })
             .catch((err)=>{
                 if(typeof(err) === "string"){

+ 9 - 0
models/merchant.js

@@ -3,6 +3,11 @@ const isSanitary = require("../controllers/helper.js").isSanitary;
 const mongoose = require("mongoose");
 
 const MerchantSchema = new mongoose.Schema({
+    owner: {
+        type: mongoose.Schema.Types.ObjectId,
+        ref: "Owner",
+        required: true
+    },
     name: {
         type: String,
         required: [true, "MERCHANT NAME IS REQUIRED"],
@@ -11,6 +16,10 @@ const MerchantSchema = new mongoose.Schema({
             message: "NAME CONTAINS ILLEGAL CHARACTERS"
         }
     },
+    pos: {
+        type: String,
+        required: true
+    },
     createdAt: {
         type: Date,
         default: new Date()

+ 5 - 5
models/owner.js

@@ -18,10 +18,6 @@ const OwnerSchema = new mongoose.Schema({
         type: String,
         required: true
     },
-    pos: {
-        type: String,
-        required: true
-    },
     square: {
         id: String,
         accessToken: String,
@@ -40,7 +36,11 @@ const OwnerSchema = new mongoose.Schema({
             index: true
         },
         expiration: Date
-    }
+    },
+    merchants: [{
+        type: mongoose.Schema.Types.ObjectId,
+        ref: "Merchant"
+    }]
 });
 
 module.exports = mongoose.model("Owner", OwnerSchema);