diff --git a/whatsapp-web.js/src/businessLogic/logic.js b/whatsapp-web.js/src/businessLogic/logic.js index 489aad5..095bd44 100644 --- a/whatsapp-web.js/src/businessLogic/logic.js +++ b/whatsapp-web.js/src/businessLogic/logic.js @@ -1,44 +1,275 @@ const { Website } = require("../models/website"); const { data } = require("../models/data"); const { Product } = require('../models/product'); +const { User } = require('../models/user'); -module.exports.onWG = () => { - console.log('hello world'); +/** + * user to be sent the message to + * @type {User} + */ +let user = null; + +/** + * input to be used in the function if exists + * @type {string} + */ +let input = null; + +const addProduct = (id) => { + + // Create a product and select it. + //At this point all the product properties will be default + var product = new Product({id : id}); + // Add a product to the selected website. + user.getSelectedWebsite().addProduct(product); } -module.exports.onCreate = (id, companyName) => { +const checkSelectedWebsiteExist= () => { - //get the user - let user = data.getUser(id); - if(user == undefined) + if(user.getSelectedWebsite() == null) { - data.addUser(id); - user = data.getUser(id); + return [ + `A website should be selected or created + To create website, please use the following command : `, + `*wg create *`, + + ]; } - //add the website with the company name - user.addWebsite(new Website(companyName)); + return ''; } -module.exports.addProduct = (id,website) => { - if(id==null) +const checkSelectedProductExist = () => { + if(user.getSelectedWebsite().getSelectedProduct() == null) { - // Setting up the product id if not given - id = Math.floor((Math.random() * 100000) + 1); - while(website.products.isData(id))// This is will check if the generated id is already used + return [ + `A product should be created or selected` + ] + } + + return ''; + +} + +module.exports.setUser = (id) =>{ + + if(!data.isUser(id)) + { + data.addUser(id); + } + user = data.getUser(id) +}; + +module.exports.setInput= (value) => { + input = value; +} + + +module.exports.onCreateProduct = () => { + + let check = checkSelectedWebsiteExist(); + if(check) + { + //add the product + addProduct(input['id']); + + let selectedId = user.getSelectedWebsite().getSelectedProduct().id; + + //mesage to be sent to the user + let message = [ + `The product has been created with the id : ${selectedId}` , + ` + This product has been selected. + Now you can make change to the product with the following commands : + 1. wg product info + 2. wg product name + 3. wg product cost + 4. wg product desc + 5. wg product image + ` + ]; + + return message; + } + return check; +} + +module.exports.onDeleteProduct = () => { + + + let check = checkSelectedWebsiteExist(); + if(check) + { + + if(!user.getSelectedWebsite().deleteProduct(input['id'])) { - id = Math.floor((Math.random() * 100000) + 1); + return [ + `No product with the given id` + ]; + } + + return [ + `The product has been deleted with the id : ` + input['id'] + ]; + + } + + return check; +} + +module.exports.onSelectProduct = () => { + + + let check = checkSelectedWebsiteExist(); + if(check) + { + // Find the product with the given id + if(!user.getSelectedWebsite().selectProduct(input['id'])) + { + return [ + `There was no product with the given id. Use the following command to get the list of all id's`, + `wg product all` + ]; + } + + return [ + `The product has been selected with the id : ${user.getSelectedWebsite().getSelectedProduct()}` , + ` + This product has been selected. + Now you can make change to the product with the following commands : + 1. wg product info + 2. wg product name + 3. wg product cost + 4. wg product desc + 5. wg product image + ` + ]; + } + + return check; +} + +module.exports.onGetAllProducts = () => { + + + let check = checkSelectedWebsiteExist(); + if(check) + { + let message = [ + 'Your website has following products: ' + ]; + + for(let p of user.getSelectedWebsite().getAllProduct()) + { + let m = 'Product id: ' + p.id + '\nProduct Name : ' + p.name + '\nProduct description : '+ p.desc + '\nProduct Cost: ' + p.cost + '\nProduct Image : '+ p.image; + message.push(m); + } + + return message; + } + + return check; + +} + +module.exports.onGetProductInfo = () => { + + + let check = checkSelectedWebsiteExist(); + if(check) + { + let check = checkSelectedProductExist(); + if(check) + { + let selected_product = user.getSelectedWebsite().getSelectedProduct(); + return [ + ` + Product id: ${selected_product.id} + Product Name : ${selected_product.name} + Product description : ${selected_product.desc } + Product Cost: ${selected_product.cost} + Product Image : ${selected_product.image} + ` + ]; + } + } + return check; + + +} + +module.exports.onSetProductName = () => { + let check = checkSelectedWebsiteExist(); + if(check) + { + let check = checkSelectedProductExist(); + if(check) + { + let selected_product = user.getSelectedWebsite().getSelectedProduct(); + selected_product.setName(input['product-name']); + let message = [ + 'The product name has been set to '+ selected_product.name + ]; + return message; + } + } + + return check; +} + +module.exports.onSetProductCost = () => { + let check = checkSelectedWebsiteExist(); + if(check) + { + let check = checkSelectedProductExist(); + if(check) + { + let selected_product = user.getSelectedWebsite().getSelectedProduct(); + selected_product.setCost(input['product-cost']); + let message = [ + 'The product cost has been set to '+ selected_product.cost + ]; + return message; } } - // Create a product and select it. At this point all the product properties will be default including the id will be empty - var product = new Product(); - product.setId(id); - // Add a product to the selected website. We will add the id based on the count of the products on the website - website.addProduct(product); - return product; + return check; } -module.exports.deleteProduct = (id,website) => { - return website.deleteProduct(id); +module.exports.onSetProductDesc = () => { + let check = checkSelectedWebsiteExist(); + if(check) + { + let check = checkSelectedProductExist(); + if(check) + { + let selected_product = user.getSelectedWebsite().getSelectedProduct(); + selected_product.setDesc(input['product-desc']); + let message = [ + 'The product cost has been set to '+ selected_product.desc + ]; + return message; + } + } + return check; } + +module.exports.onSetProductImage = () => { + let check = checkSelectedWebsiteExist(); + if(check) + { + let check = checkSelectedProductExist(); + if(check) + { + let selected_product = user.getSelectedWebsite().getSelectedProduct(); + selected_product.setImage(input['product-image']); + let message = [ + 'The product cost has been set to '+ selected_product.image + ]; + return message; + } + } + return check; +} + + diff --git a/whatsapp-web.js/src/models/user.js b/whatsapp-web.js/src/models/user.js index 0bac638..d001707 100644 --- a/whatsapp-web.js/src/models/user.js +++ b/whatsapp-web.js/src/models/user.js @@ -34,6 +34,9 @@ class User{ return this.websites.selectData(companyName); } + /** + * @returns {Website} + */ getSelectedWebsite(){ return this.websites.selectedData; } diff --git a/whatsapp-web.js/src/models/website.js b/whatsapp-web.js/src/models/website.js index 5e3198b..5981556 100644 --- a/whatsapp-web.js/src/models/website.js +++ b/whatsapp-web.js/src/models/website.js @@ -39,7 +39,7 @@ class Website { /** * - * @returns {[]} + * returns all the products in the website */ getAllProduct(){ return this.products.getAllData().values(); @@ -58,6 +58,9 @@ class Website { return this.products.selectData(productId); } + /** + * @returns {Product} + */ getSelectedProduct() { return this.products.selectedData; diff --git a/whatsapp-web.js/src/userInteraction/messages.js b/whatsapp-web.js/src/userInteraction/messages.js index d75293c..8469674 100644 --- a/whatsapp-web.js/src/userInteraction/messages.js +++ b/whatsapp-web.js/src/userInteraction/messages.js @@ -31,231 +31,64 @@ const commands = [ new Command({ //User command command: `wg product new `, - //function to be executed - callback : (input) => { - - if(selected_website==null) - { - let message = [ - `A website should be selected or created before creating a product ` - ]; - return message; - } - selected_product= logic.addProduct(input['id'],selected_website); - //mesage to be sent to the user - let message = [ - `The product has been created with the id : ` + selected_product.id , - `This product has been selected . \nNow you can make change to the product with the following commands : \n1. wg product info \n2. wg product name \n3. wg product cost \n4. wg product desc \n5. wg product image ` - ]; - - return message; - } + callback : () => logic.onCreateProduct() }), new Command({ //User command command: `wg product delete `, //function to be executed - callback : (input) => { - - if(selected_website==null) - { - let message = [ - `A website should be selected or created before deleting a product ` - ]; - return message; - } - - if(logic.deleteProduct(input['id'],selected_website) == false) - { - let message = [ - `No product with the given id` - ]; - return message; - } - - let message = [ - `The product has been deleted with the id : ` + id - ]; - return message; - } + callback : () => logic.onDeleteProduct() }), new Command({ //User command command: `wg product select `, //function to be executed - callback : (input) => { - - if(selected_website==null) - { - let message = [ - `A website should be selected or created before selecting a product ` - ]; - return message; - } - // Find the product with the given id - selected_product = selected_website.getProduct(input['id']); - if(selected_product == null) - { - logic.addProduct(input['id'],selected_website); - selected_product = selected_website.getProduct(input['id']); - let message = [ - `There was no product with the give id. So a new product was created and selected with the id : ` + selected_product.id , - `Now you can make change to the product with the following commands : \n1. wg product info \n2. wg product name \n3. wg product cost \n4. wg product desc \n5. wg product image ` - ]; - - return message; - } - - let message = [ - `The product has been selected with the id : ` + selected_product.id , - 'Now you can make change to the product with the following commands : \n1. wg product info \n2. wg product name \n3. wg product cost \n4. wg product desc \n5. wg product image ' - - ]; - - return message; - } + callback : () => logic.onSelectProduct() }), new Command({ //User command command: `wg product all`, //function to be executed - callback : () => { - - if(selected_website==null) - { - let message = [ - `A website should be selected or created ` - ]; - return message; - } - let message = [ - 'Your website has following products: ' - ]; - for(let p of selected_website.getAllProduct()) - { - let m = 'Product id: ' + p.id + '\nProduct Name : ' + p.name + '\nProduct description : '+ p.desc + '\nProduct Cost: ' + p.cost + '\nProduct Image : '+ p.image; - message.push(m); - } - - return message; - } + callback : () => logic.onGetAllProducts() }), new Command({ //User command command: `wg product info`, //function to be executed - callback : () => { - - if(selected_product == null) - { - let message = [ - `A product should be selected ` - ]; - return message; - } - let message = [ - 'Product id: ' + selected_product.id, - 'Product Name : ' + selected_product.name , - 'Product description : '+ selected_product.desc , - 'Product Cost: ' + selected_product.cost , - 'Product Image : '+ selected_product.image - ]; - - return message; - } + callback : () => logic.onGetProductInfo() }), new Command({ //User command command: `wg product name `, //function to be executed - callback : (input) => { - - if(selected_product == null) - { - let message = [ - `A product should be selected ` - ]; - return message; - } - selected_product.setName(input['product-name']); - let message = [ - 'The product name has been set to '+ selected_product.name - ]; - - return message; - } + callback : () => logic.onSetProductName() }), new Command({ //User command command: `wg product cost `, //function to be executed - callback : (input) => { - - if(selected_product == null) - { - let message = [ - `A product should be selected ` - ]; - return message; - } - selected_product.setCost(input['product-cost']); - let message = [ - 'The product cost has been set to '+ selected_product.cost - ]; - - return message; - } + callback : () => logic.onSetProductCost() }), new Command({ //User command command: 'wg product desc ', //function to be executed - callback : (input) => { - - if(selected_product == null) - { - let message = [ - `A product should be selected ` - ]; - return message; - } - selected_product.setDesc(input['product-desc']); - let message = [ - 'The product description has been set to : '+ selected_product.desc - ]; - - return message; - } + callback : () => logic.onSetProductDesc() }), new Command({ //User command command: 'wg product image ', //function to be executed - callback : (input) => { - - if(selected_product == null) - { - let message = [ - `A product should be selected ` - ]; - return message; - } - selected_product.setImage(input['product-image']); - let message = [ - 'The product image has been set to : '+ selected_product.image - ]; - - return message; - } + callback : () => logic.onSetProductImage() }), ]; @@ -281,8 +114,12 @@ module.exports= onMessage = (message, client) => { if(command.requireInput){ input = command.getInput(message.body); } + + logic.setUser(message.id.remote); + logic.setInput(input); + + let messageToBeSent = command.callback(); - let messageToBeSent = command.callback(input); for (let msg of messageToBeSent) { client.sendMessage(message.from, msg, new Object());