From 2f653468e6b397b79290abc9a90ea26812a24526 Mon Sep 17 00:00:00 2001 From: Baber-Jan Date: Thu, 23 Jul 2020 20:02:16 +0400 Subject: [PATCH 01/13] Added all the product methods, now have to figure out how to use id in call back function --- .../src/userInteraction/messages.js | 269 +++++++++++++++++- 1 file changed, 268 insertions(+), 1 deletion(-) diff --git a/whatsapp-web.js/src/userInteraction/messages.js b/whatsapp-web.js/src/userInteraction/messages.js index 5884fee..4e569f3 100644 --- a/whatsapp-web.js/src/userInteraction/messages.js +++ b/whatsapp-web.js/src/userInteraction/messages.js @@ -1,6 +1,11 @@ const { Command } = require("../models/commands"); const logic = require("../businessLogic/logic"); +const { Website } = require("../models/website"); +const { Product } = require('../models/product'); + +var selected_product = null;// Will keep track of the selected product +var selected_website = new Website; // Selected website. /////////// //add the commands here @@ -23,7 +28,269 @@ const commands = [ return message; } }), + new Command({ + //User command + command: `wg product new `, + //function to be executed + callback : () => { + + logic.onWG(); + var id; + if(selected_website==null) + { + let message = [ + `A website should be selected or created before creating a product ` + ]; + return message; + } + // Create a product and select it. At this point all the product properties will be default including the id will be empty + selected_product = new Product(); + if(id==null) + { + // Setting up the product id if not given + id = Math.floor((Math.random() * 100000) + 1); + while(selected_website.products.isData(id))// This is will check if the generated id is already used + { + id = Math.floor((Math.random() * 100000) + 1); + } + } + selected_product.id =id; + // Add a product to the selected website. We will add the id based on the count of the products on the website + selected_website.addProduct(selected_product) + //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; + } + }), + new Command({ + //User command + command: `wg delete product `, + + //function to be executed + callback : () => { + + logic.onWG(); + var id ; + if(selected_website==null) + { + let message = [ + `A website should be selected or created before deleting a product ` + ]; + return message; + } + // Find the product with the given id + selected_product = selected_website.products.deleteData(id); + if(selected_product == false) + { + let message = [ + `No product with the given id` + ]; + return message; + } + + let message = [ + `The product has been deleted with the id : ` + id + ]; + selected_product =null; + return message; + } + }), + new Command({ + //User command + command: `wg product `, + + //function to be executed + callback : () => { + + logic.onWG(); + var id ; + 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.products.getData(id); + if(selected_product == null) + { + selected_product = new Product(); + selected_product.id =id; + // Add a product to the selected website. We will add the id based on the count of the products on the website + selected_website.addProduct(selected_product) + //mesage to be sent to the user + 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 : ` + 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; + } + }), + new Command({ + //User command + command: `wg product all`, + + //function to be executed + callback : () => { + + logic.onWG(); + 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.products.getAllData().values()) + { + 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; + } + }), + new Command({ + //User command + command: `wg product info`, + + //function to be executed + callback : () => { + + logic.onWG(); + if(selected_product == null) + { + let message = [ + `A product should be selected ` + ]; + return message; + } + let message = [ + 'Product id: ' + p.id, + 'Product Name : ' + p.name , + 'Product description : '+ p.desc , + 'Product Cost: ' + p.cost , + 'Product Image : '+ p.image + ]; + + return message; + } + }), + new Command({ + //User command + command: `wg product name `, + + //function to be executed + callback : () => { + + logic.onWG(); + given_name= null; + if(selected_product == null) + { + let message = [ + `A product should be selected ` + ]; + return message; + } + selected_product.name= given_name; + let message = [ + 'The product name has been set to '+ given_name + ]; + + return message; + } + }), + new Command({ + //User command + command: `wg product cost `, + + //function to be executed + callback : () => { + + logic.onWG(); + given_cost= null; + if(selected_product == null) + { + let message = [ + `A product should be selected ` + ]; + return message; + } + selected_product.cost= given_cost; + let message = [ + 'The product cost has been set to '+ given_cost + ]; + + return message; + } + }), + new Command({ + //User command + command: 'wg product desc ', + + //function to be executed + callback : () => { + + logic.onWG(); + given_desc= null; + if(selected_product == null) + { + let message = [ + `A product should be selected ` + ]; + return message; + } + selected_product.desc= given_desc; + let message = [ + 'The product description has been set to : '+ given_desc + ]; + + return message; + } + }), + new Command({ + //User command + command: 'wg product image ', + + //function to be executed + callback : () => { + + logic.onWG(); + given_image= null; + if(selected_product == null) + { + let message = [ + `A product should be selected ` + ]; + return message; + } + selected_product.image = given_image; + let message = [ + 'The product image has been set to : '+ given_image + ]; + + return message; + } + }), ]; @@ -52,7 +319,7 @@ module.exports= onMessage = (message, client) => { } } -}; +}; From 4ca9d1418c25e7b114d5b0b20ecb42981393778c Mon Sep 17 00:00:00 2001 From: Baber-Jan Date: Thu, 23 Jul 2020 20:23:15 +0400 Subject: [PATCH 02/13] moved some code to logic.j --- whatsapp-web.js/src/businessLogic/logic.js | 28 +++++++++++++++++++ .../src/userInteraction/messages.js | 27 +++--------------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/whatsapp-web.js/src/businessLogic/logic.js b/whatsapp-web.js/src/businessLogic/logic.js index 483cb10..28c0495 100644 --- a/whatsapp-web.js/src/businessLogic/logic.js +++ b/whatsapp-web.js/src/businessLogic/logic.js @@ -1,5 +1,6 @@ const { Website } = require("../models/website"); const { data } = require("../models/data"); +const { Product } = require('../models/product'); module.exports.onWG = () => { console.log('hello world'); @@ -17,4 +18,31 @@ module.exports.onCreate = (id, companyName) => { //add the website with the company name user.addWebsite(new Website(companyName)); +} +module.exports.addProduct = (id,website) => { + + if(id==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 + { + id = Math.floor((Math.random() * 100000) + 1); + } + } + + // 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.id =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; +} +module.exports.deleteProduct = (id,website) => { + + return website.products.deleteData(id); +} + +module.exports.deleteProduct = (id,website) => { + return website.products.deleteData(id); } \ No newline at end of file diff --git a/whatsapp-web.js/src/userInteraction/messages.js b/whatsapp-web.js/src/userInteraction/messages.js index 4e569f3..157b1f4 100644 --- a/whatsapp-web.js/src/userInteraction/messages.js +++ b/whatsapp-web.js/src/userInteraction/messages.js @@ -44,20 +44,7 @@ const commands = [ ]; return message; } - // Create a product and select it. At this point all the product properties will be default including the id will be empty - selected_product = new Product(); - if(id==null) - { - // Setting up the product id if not given - id = Math.floor((Math.random() * 100000) + 1); - while(selected_website.products.isData(id))// This is will check if the generated id is already used - { - id = Math.floor((Math.random() * 100000) + 1); - } - } - selected_product.id =id; - // Add a product to the selected website. We will add the id based on the count of the products on the website - selected_website.addProduct(selected_product) + selected_product= logic.addProduct(id,selected_website); //mesage to be sent to the user let message = [ `The product has been created with the id : ` + selected_product.id , @@ -83,9 +70,8 @@ const commands = [ ]; return message; } - // Find the product with the given id - selected_product = selected_website.products.deleteData(id); - if(selected_product == false) + + if(logic.deleteProduct == false) { let message = [ `No product with the given id` @@ -96,7 +82,6 @@ const commands = [ let message = [ `The product has been deleted with the id : ` + id ]; - selected_product =null; return message; } }), @@ -120,11 +105,7 @@ const commands = [ selected_product = selected_website.products.getData(id); if(selected_product == null) { - selected_product = new Product(); - selected_product.id =id; - // Add a product to the selected website. We will add the id based on the count of the products on the website - selected_website.addProduct(selected_product) - //mesage to be sent to the user + logic.addProduct(id,selected_website); 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 ` From 0b0569b7835bbc85b50fba912c902afeb28117d9 Mon Sep 17 00:00:00 2001 From: Mohammed Ashab Uddin Date: Sat, 25 Jul 2020 10:15:33 +0400 Subject: [PATCH 03/13] deleted all the logic.onWg() --- whatsapp-web.js/src/userInteraction/messages.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/whatsapp-web.js/src/userInteraction/messages.js b/whatsapp-web.js/src/userInteraction/messages.js index c3fd555..b4a86dc 100644 --- a/whatsapp-web.js/src/userInteraction/messages.js +++ b/whatsapp-web.js/src/userInteraction/messages.js @@ -35,7 +35,6 @@ const commands = [ //function to be executed callback : (input) => { - logic.onWG(); if(selected_website==null) { let message = [ @@ -60,7 +59,6 @@ const commands = [ //function to be executed callback : (input) => { - logic.onWG(); if(selected_website==null) { let message = [ @@ -90,7 +88,6 @@ const commands = [ //function to be executed callback : (input) => { - logic.onWG(); if(selected_website==null) { let message = [ @@ -127,7 +124,6 @@ const commands = [ //function to be executed callback : () => { - logic.onWG(); if(selected_website==null) { let message = [ @@ -154,7 +150,6 @@ const commands = [ //function to be executed callback : () => { - logic.onWG(); if(selected_product == null) { let message = [ @@ -180,7 +175,6 @@ const commands = [ //function to be executed callback : (input) => { - logic.onWG(); if(selected_product == null) { let message = [ @@ -203,7 +197,6 @@ const commands = [ //function to be executed callback : (input) => { - logic.onWG(); if(selected_product == null) { let message = [ @@ -226,7 +219,6 @@ const commands = [ //function to be executed callback : (input) => { - logic.onWG(); if(selected_product == null) { let message = [ @@ -249,7 +241,6 @@ const commands = [ //function to be executed callback : (input) => { - logic.onWG(); if(selected_product == null) { let message = [ From 90a14a9e1b43aa4d7d9dd132781de48b7283a509 Mon Sep 17 00:00:00 2001 From: Mohammed Ashab Uddin Date: Sat, 25 Jul 2020 10:46:30 +0400 Subject: [PATCH 04/13] added default value to the object parameter --- whatsapp-web.js/src/models/product.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/whatsapp-web.js/src/models/product.js b/whatsapp-web.js/src/models/product.js index 0878b60..8a01ddb 100644 --- a/whatsapp-web.js/src/models/product.js +++ b/whatsapp-web.js/src/models/product.js @@ -1,7 +1,7 @@ class Product { - constructor({ id = '', name = '', desc = '', cost = 0, image = null }) { + constructor({ id = '', name = '', desc = '', cost = 0, image = null } = {}) { this.id = id; this.name = name; this.desc = desc; From d4be542a91098a7bb7d699a5cae6b1465d0b39c1 Mon Sep 17 00:00:00 2001 From: Mohammed Ashab Uddin Date: Sat, 25 Jul 2020 21:17:11 +0400 Subject: [PATCH 05/13] fixed bug in command.js --- whatsapp-web.js/src/models/commands.js | 2 +- whatsapp-web.js/test/test_command.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/whatsapp-web.js/src/models/commands.js b/whatsapp-web.js/src/models/commands.js index ab084c2..674a54b 100644 --- a/whatsapp-web.js/src/models/commands.js +++ b/whatsapp-web.js/src/models/commands.js @@ -45,7 +45,7 @@ class Command{ //else return false if(splitMsg[i] == splitCommand[i]){ continue; - }else if(splitCommand[i].match(/[<>]/g).length > 1){ + }else if(splitCommand[i].match(/[<>]/g) != null){ continue; }else{ return false; diff --git a/whatsapp-web.js/test/test_command.js b/whatsapp-web.js/test/test_command.js index 1fca64a..b0786b7 100644 --- a/whatsapp-web.js/test/test_command.js +++ b/whatsapp-web.js/test/test_command.js @@ -91,6 +91,21 @@ describe('Command', () => { }); + it.only('Test : equals() with different message', () => { + + let message = 'wg website web'; + + let a = new Command({ + command: "wg create ", + callback: () => { + + } + }); + + expect(a.equals(message)).to.false; + + }); + it('Test : equals() with spelling error', () => { let message = 'wg websit'; From 416deb477c7e460a593e6ff6afff1dd8614b54e6 Mon Sep 17 00:00:00 2001 From: Mohammed Ashab Uddin Date: Sat, 25 Jul 2020 21:19:59 +0400 Subject: [PATCH 06/13] fixed bug in command.js --- whatsapp-web.js/src/models/commands.js | 2 +- whatsapp-web.js/test/test_command.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/whatsapp-web.js/src/models/commands.js b/whatsapp-web.js/src/models/commands.js index ab084c2..674a54b 100644 --- a/whatsapp-web.js/src/models/commands.js +++ b/whatsapp-web.js/src/models/commands.js @@ -45,7 +45,7 @@ class Command{ //else return false if(splitMsg[i] == splitCommand[i]){ continue; - }else if(splitCommand[i].match(/[<>]/g).length > 1){ + }else if(splitCommand[i].match(/[<>]/g) != null){ continue; }else{ return false; diff --git a/whatsapp-web.js/test/test_command.js b/whatsapp-web.js/test/test_command.js index 1fca64a..405a04d 100644 --- a/whatsapp-web.js/test/test_command.js +++ b/whatsapp-web.js/test/test_command.js @@ -91,6 +91,21 @@ describe('Command', () => { }); + it('Test : equals() with different message', () => { + + let message = 'wg website p12'; + + let a = new Command({ + command: "wg create ", + callback: () => { + + } + }); + + expect(a.equals(message)).to.false; + + }); + it('Test : equals() with spelling error', () => { let message = 'wg websit'; From eeb04e1615874820fcdf71b97ea2d0d6c93d1c94 Mon Sep 17 00:00:00 2001 From: Baber-Jan Date: Sun, 26 Jul 2020 19:12:51 +0400 Subject: [PATCH 07/13] Did the required changes --- whatsapp-web.js/src/businessLogic/logic.js | 4 +- whatsapp-web.js/src/models/product.js | 37 ++++++++++++++++++ whatsapp-web.js/src/models/website.js | 8 ++++ .../src/userInteraction/messages.js | 38 +++++++++---------- 4 files changed, 66 insertions(+), 21 deletions(-) diff --git a/whatsapp-web.js/src/businessLogic/logic.js b/whatsapp-web.js/src/businessLogic/logic.js index f96eca2..489aad5 100644 --- a/whatsapp-web.js/src/businessLogic/logic.js +++ b/whatsapp-web.js/src/businessLogic/logic.js @@ -33,12 +33,12 @@ module.exports.addProduct = (id,website) => { // 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.id =id; + 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; } module.exports.deleteProduct = (id,website) => { - return website.products.deleteData(id); + return website.deleteProduct(id); } diff --git a/whatsapp-web.js/src/models/product.js b/whatsapp-web.js/src/models/product.js index 8a01ddb..ebbefca 100644 --- a/whatsapp-web.js/src/models/product.js +++ b/whatsapp-web.js/src/models/product.js @@ -8,6 +8,43 @@ class Product { this.cost = cost; this.image = image; } + + /** + * + * @param {Number} id + */ + setId(id){ + return this.id = id; + } + /** + * + * @param {String} name + */ + setName(name){ + return this.name = name; + } + /** + * + * @param {String} description + */ + setDesc(description){ + return this.desc = description; + } + /** + * + * @param {Number} cost + */ + setCost(cost){ + return this.cost = cost; + } + /** + * + * @param {String} image + */ + setImage(image){ + return this.image = image; + } + } module.exports.Product = Product; diff --git a/whatsapp-web.js/src/models/website.js b/whatsapp-web.js/src/models/website.js index 9584f76..86e0f35 100644 --- a/whatsapp-web.js/src/models/website.js +++ b/whatsapp-web.js/src/models/website.js @@ -37,6 +37,14 @@ class Website { return this.products.getData(productId); } + /** + * + * @returns {[]} + */ + getAllProduct(){ + return this.products.getAllData().values(); + } + /** * * @param {string} productId diff --git a/whatsapp-web.js/src/userInteraction/messages.js b/whatsapp-web.js/src/userInteraction/messages.js index 9e32940..cf19e39 100644 --- a/whatsapp-web.js/src/userInteraction/messages.js +++ b/whatsapp-web.js/src/userInteraction/messages.js @@ -4,7 +4,7 @@ const logic = require("../businessLogic/logic"); const { Website } = require("../models/website"); const { Product } = require('../models/product'); -var selected_product = null;// Will keep track of the selected product +var selected_product = new Product;// Will keep track of the selected product var selected_website = null; // Selected website. /////////// @@ -85,7 +85,7 @@ const commands = [ }), new Command({ //User command - command: `wg product `, + command: `wg select product `, //function to be executed callback : (input) => { @@ -99,7 +99,7 @@ const commands = [ return message; } // Find the product with the given id - selected_product = selected_website.products.getData(input['id']); + selected_product = selected_website.getProduct(input['id']); if(selected_product == null) { logic.addProduct(id,selected_website); @@ -112,7 +112,7 @@ const commands = [ } let message = [ - `The product has been selected with the id : ` + id , + `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 ' ]; @@ -136,9 +136,9 @@ const commands = [ return message; } let message = [ - 'Your website has following products' + 'Your website has following products: ' ]; - for(let p of selected_website.products.getAllData().values()) + 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); @@ -163,11 +163,11 @@ const commands = [ return message; } let message = [ - 'Product id: ' + p.id, - 'Product Name : ' + p.name , - 'Product description : '+ p.desc , - 'Product Cost: ' + p.cost , - 'Product Image : '+ p.image + '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; @@ -188,9 +188,9 @@ const commands = [ ]; return message; } - selected_product.name= input['product-name']; + selected_product.setName(input['product-name']); let message = [ - 'The product name has been set to '+ given_name + 'The product name has been set to '+ selected_product.name ]; return message; @@ -211,9 +211,9 @@ const commands = [ ]; return message; } - selected_product.cost= input['product-cost']; + selected_product.setCost(input['product-cost']); let message = [ - 'The product cost has been set to '+ given_cost + 'The product cost has been set to '+ selected_product.cost ]; return message; @@ -234,9 +234,9 @@ const commands = [ ]; return message; } - selected_product.desc = input['product-desc']; + selected_product.setDesc(input['product-desc']); let message = [ - 'The product description has been set to : '+ given_desc + 'The product description has been set to : '+ selected_product.desc ]; return message; @@ -257,9 +257,9 @@ const commands = [ ]; return message; } - selected_product.image = input['product-image']; + selected_product.setImage(input['product-image']); let message = [ - 'The product image has been set to : '+ given_image + 'The product image has been set to : '+ selected_product.image ]; return message; From ff7eb53eef18eb8a57351d4bfd7f1c6b997bdf2d Mon Sep 17 00:00:00 2001 From: Baber-Jan Date: Sun, 26 Jul 2020 20:10:58 +0400 Subject: [PATCH 08/13] Single change --- whatsapp-web.js/src/userInteraction/messages.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/whatsapp-web.js/src/userInteraction/messages.js b/whatsapp-web.js/src/userInteraction/messages.js index cf19e39..c5cefbe 100644 --- a/whatsapp-web.js/src/userInteraction/messages.js +++ b/whatsapp-web.js/src/userInteraction/messages.js @@ -4,7 +4,7 @@ const logic = require("../businessLogic/logic"); const { Website } = require("../models/website"); const { Product } = require('../models/product'); -var selected_product = new Product;// Will keep track of the selected product +var selected_product = null;// Will keep track of the selected product var selected_website = null; // Selected website. /////////// From e71e9f372b1a88d0ec271f137d0c8d01f648e774 Mon Sep 17 00:00:00 2001 From: Mohammed Ashab Uddin Date: Tue, 28 Jul 2020 10:44:40 +0400 Subject: [PATCH 09/13] fixed entity map problems --- whatsapp-web.js/src/models/entity.js | 9 +++++---- whatsapp-web.js/test/test_command.js | 2 +- whatsapp-web.js/test/test_entity.js | 13 +++++++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/whatsapp-web.js/src/models/entity.js b/whatsapp-web.js/src/models/entity.js index 8297cb3..0bcce8f 100644 --- a/whatsapp-web.js/src/models/entity.js +++ b/whatsapp-web.js/src/models/entity.js @@ -9,7 +9,7 @@ class Entity { addData(key ,value){ if(!this.isData(key)){ - this.data[key] = value; + this.data.set(key, value); return true; } return false; @@ -20,12 +20,13 @@ class Entity { * @param {string} key */ isData(key){ - return this.data[key] != undefined ? true : false; + return this.data.has(key) ? true : false; } deleteData(key){ if(this.isData(key)){ - return delete this.data[key]; + this.data.delete(key); + return true; } return false; } @@ -37,7 +38,7 @@ class Entity { getData(key){ if(this.isData(key)){ - return this.data[key]; + return this.data.get(key); } return null; } diff --git a/whatsapp-web.js/test/test_command.js b/whatsapp-web.js/test/test_command.js index b0786b7..fff3df5 100644 --- a/whatsapp-web.js/test/test_command.js +++ b/whatsapp-web.js/test/test_command.js @@ -91,7 +91,7 @@ describe('Command', () => { }); - it.only('Test : equals() with different message', () => { + it('Test : equals() with different message', () => { let message = 'wg website web'; diff --git a/whatsapp-web.js/test/test_entity.js b/whatsapp-web.js/test/test_entity.js index f42eb2e..56a3e22 100644 --- a/whatsapp-web.js/test/test_entity.js +++ b/whatsapp-web.js/test/test_entity.js @@ -52,4 +52,17 @@ describe('Entity', () => { expect(a.getData('gada')).to.null; }) + it('Test: getAllData', () => { + let a = new Entity(); + a.addData('hello world', new Website('hello world')); + a.addData('gada', new Website('gada')); + a.addData('pagol', new Website('pagol')); + + let websitegen = a.getAllData().values(); + for(let t of websitegen) + { + console.log(t.companyName); + } + }) + }) \ No newline at end of file From 2aae4fe6e917251e804aa50658a2361baeef66fe Mon Sep 17 00:00:00 2001 From: Mohammed Ashab Uddin Date: Tue, 28 Jul 2020 10:46:02 +0400 Subject: [PATCH 10/13] small fixes --- whatsapp-web.js/src/userInteraction/messages.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/whatsapp-web.js/src/userInteraction/messages.js b/whatsapp-web.js/src/userInteraction/messages.js index 1fd12e0..d75293c 100644 --- a/whatsapp-web.js/src/userInteraction/messages.js +++ b/whatsapp-web.js/src/userInteraction/messages.js @@ -83,7 +83,7 @@ const commands = [ }), new Command({ //User command - command: `wg select product `, + command: `wg product select `, //function to be executed callback : (input) => { @@ -99,7 +99,8 @@ const commands = [ selected_product = selected_website.getProduct(input['id']); if(selected_product == null) { - logic.addProduct(id,selected_website); + 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 ` @@ -136,7 +137,7 @@ const commands = [ ]; 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 + 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); } @@ -278,7 +279,7 @@ module.exports= onMessage = (message, client) => { if(command != null){ let input = {}; if(command.requireInput){ - input = command.getInput(); + input = command.getInput(message.body); } let messageToBeSent = command.callback(input); From 3bf8cf6d03721da10e1b2a352a86ac80441dc3c9 Mon Sep 17 00:00:00 2001 From: Mohammed Ashab Uddin Date: Thu, 30 Jul 2020 09:29:02 +0400 Subject: [PATCH 11/13] added selection methods --- whatsapp-web.js/src/models/entity.js | 29 ++++++++++++++ whatsapp-web.js/test/test_entity.js | 57 ++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/whatsapp-web.js/src/models/entity.js b/whatsapp-web.js/src/models/entity.js index 0bcce8f..38abfcc 100644 --- a/whatsapp-web.js/src/models/entity.js +++ b/whatsapp-web.js/src/models/entity.js @@ -4,12 +4,14 @@ class Entity { constructor(){ this.data = new Map(); + this.selectedData = null; this.count = 0; } addData(key ,value){ if(!this.isData(key)){ this.data.set(key, value); + this.selectedData = key; return true; } return false; @@ -26,16 +28,40 @@ class Entity { deleteData(key){ if(this.isData(key)){ this.data.delete(key); + if(this.selectedData == key){ + this.selectedData = null; + } return true; } return false; } clearData(){ + this.selectedData = null; this.data = new Map(); this.count = 0; } + /** + * Select a specific data with its key + * @param {string} key + * @returns true if a data is selected + * @returns false if data is null + */ + selectData(key){ + if(this.isData(key)) + { + this.selectedData = key; + return true; + } + return false; + } + + /** + * + * @param {string} key + * @returns the value mapped to the key + */ getData(key){ if(this.isData(key)){ return this.data.get(key); @@ -43,6 +69,9 @@ class Entity { return null; } + /** + * @returns a map of all data + */ getAllData(){ return this.data; } diff --git a/whatsapp-web.js/test/test_entity.js b/whatsapp-web.js/test/test_entity.js index 56a3e22..46ea027 100644 --- a/whatsapp-web.js/test/test_entity.js +++ b/whatsapp-web.js/test/test_entity.js @@ -29,13 +29,13 @@ describe('Entity', () => { expect(a.isData('pagol')).to.false; expect(a.isData('hello world')).to.false; }); - + it('Test: getData', () => { let a = new Entity(); a.addData('hello world', new Website('hello world')); a.addData('gada', new Website('gada')); a.addData('pagol', new Website('pagol')); - + expect(a.getData('hello world').companyName).to.equal('hello world'); expect(a.getData('gada').companyName).to.equal('gada'); expect(a.getData('chu chu')).to.null; @@ -51,18 +51,67 @@ describe('Entity', () => { a.deleteData('gada'); expect(a.getData('gada')).to.null; }) - + it('Test: getAllData', () => { let a = new Entity(); a.addData('hello world', new Website('hello world')); a.addData('gada', new Website('gada')); a.addData('pagol', new Website('pagol')); - + let websitegen = a.getAllData().values(); for(let t of websitegen) { console.log(t.companyName); } }) + + it('Test: selectData()', () => { + let a = new Entity(); + a.addData('hello world', new Website('hello world')); + a.addData('gada', new Website('gada')); + a.addData('pagol', new Website('pagol')); + + a.selectData('gada'); + + expect(a.selectedData).to.equals('gada'); + }); + + it.only('Test: selectedData() = false', () => { + let a = new Entity(); + a.addData('hello world', new Website('hello world')); + a.addData('gada', new Website('gada')); + a.addData('pagol', new Website('pagol')); + a.selectData('tara'); + expect(a.selectedData).to.equals('pagol'); + }); + + it('Test: selectedData when addData() is used', () => { + let a = new Entity(); + a.addData('hello world', new Website('hello world')); + a.addData('gada', new Website('gada')); + a.addData('pagol', new Website('pagol')); + + expect(a.selectedData).to.equals('pagol'); + }); + + it('Test : selectedData when clearData()', () => { + let a = new Entity(); + a.addData('hello world', new Website('hello world')); + a.addData('gada', new Website('gada')); + a.addData('pagol', new Website('pagol')); + + a.clearData(); + + expect(a.count).to.equal(0); + expect(a.isData("kruta")).to.false; + expect(a.isData("hello")).to.false; + expect(a.isData('pagol')).to.false; + expect(a.isData('hello world')).to.false; + expect(a.selectedData).to.null; + }); + + + + }) \ No newline at end of file From e936c4d3120503fb6b081422b3f1e572a1fd4ffa Mon Sep 17 00:00:00 2001 From: Mohammed Ashab Uddin Date: Thu, 30 Jul 2020 09:29:19 +0400 Subject: [PATCH 12/13] added selection methods --- whatsapp-web.js/src/models/user.js | 9 +++++++++ whatsapp-web.js/src/models/website.js | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/whatsapp-web.js/src/models/user.js b/whatsapp-web.js/src/models/user.js index 2c915c6..0bac638 100644 --- a/whatsapp-web.js/src/models/user.js +++ b/whatsapp-web.js/src/models/user.js @@ -29,6 +29,15 @@ class User{ return this.websites.getData(companyName); } + selectWebsite(companyName) + { + return this.websites.selectData(companyName); + } + + getSelectedWebsite(){ + return this.websites.selectedData; + } + } module.exports.User = User; \ No newline at end of file diff --git a/whatsapp-web.js/src/models/website.js b/whatsapp-web.js/src/models/website.js index 86e0f35..5e3198b 100644 --- a/whatsapp-web.js/src/models/website.js +++ b/whatsapp-web.js/src/models/website.js @@ -54,6 +54,15 @@ class Website { return this.products.deleteData(productId); } + selectProduct(productId){ + return this.products.selectData(productId); + } + + getSelectedProduct() + { + return this.products.selectedData; + } + } From d0655737f1ea6a878bdff86e8f20984d342f598b Mon Sep 17 00:00:00 2001 From: Mohammed Ashab Uddin Date: Thu, 30 Jul 2020 11:27:37 +0400 Subject: [PATCH 13/13] structured the logic.js --- whatsapp-web.js/src/businessLogic/logic.js | 279 ++++++++++++++++-- whatsapp-web.js/src/models/user.js | 3 + whatsapp-web.js/src/models/website.js | 5 +- .../src/userInteraction/messages.js | 191 +----------- 4 files changed, 276 insertions(+), 202 deletions(-) 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());