Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2aae4fe6e9 | ||
|
|
e71e9f372b | ||
|
|
595941341d | ||
|
|
ff7eb53eef | ||
|
|
eeb04e1615 | ||
|
|
d53380616e | ||
|
|
416deb477c | ||
|
|
d4be542a91 | ||
|
|
90a14a9e1b | ||
|
|
0b0569b783 | ||
|
|
c58bc1ce91 | ||
|
|
6288eefa99 | ||
|
|
11063d120d | ||
|
|
507e62e049 | ||
|
|
2a1ade4f25 | ||
|
|
4ca9d1418c | ||
|
|
2f653468e6 | ||
|
|
60367476d8 | ||
|
|
ca46a66283 | ||
|
|
7b0779d552 | ||
|
|
596f56b708 | ||
|
|
1535d4cec0 | ||
|
|
29db39c642 | ||
|
|
9c7f90a102 | ||
|
|
401aa1a17d | ||
|
|
07f3c114e0 | ||
|
|
4924eaef2c | ||
|
|
1513186a45 | ||
|
|
23ceb9fe14 |
30
README.md
30
README.md
@@ -9,21 +9,45 @@ In this project we will illustrate how to build a static web page using commands
|
|||||||
## Installation
|
## Installation
|
||||||
Requirements : Node js and Npm
|
Requirements : Node js and Npm
|
||||||
|
|
||||||
### To start web generator
|
### Install web generator
|
||||||
|
|
||||||
|
First, go to the folder named **website-generator**.
|
||||||
|
Then install all the node modules.
|
||||||
```
|
```
|
||||||
cd website-generation
|
cd website-generation
|
||||||
|
npm install
|
||||||
```
|
```
|
||||||
Installation : https://github.com/Tech-club/Challenge-1/tree/master/website-generator#installation
|
|
||||||
|
|
||||||
|
### Install whatsapp bot
|
||||||
|
|
||||||
### To start whatsapp bot
|
First, go to the folder named **whatsapp-web.js**.
|
||||||
|
Then install all the node modules.
|
||||||
|
|
||||||
```
|
```
|
||||||
cd whatsapp-web.js
|
cd whatsapp-web.js
|
||||||
npm install
|
npm install
|
||||||
|
```
|
||||||
|
|
||||||
|
### Start the website generator
|
||||||
|
Make sure you are in **website-generator** directory. Then type the following command :
|
||||||
|
```
|
||||||
npm start
|
npm start
|
||||||
```
|
```
|
||||||
|
For more information on deploying the server in IPFS, please click the following link:
|
||||||
|
https://github.com/Tech-club/Challenge-1/tree/master/website-generator#installation
|
||||||
|
|
||||||
|
### Start whatsapp bot
|
||||||
|
|
||||||
|
Make sure you are in **whatsapp-web.js** directory. Then type the following command :
|
||||||
|
```
|
||||||
|
npm start
|
||||||
|
```
|
||||||
|
|
||||||
|
### Start generating websites with whatsapp
|
||||||
|
|
||||||
|
You need to start both website-generator and whatsapp-bot.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Contributors
|
### Contributors
|
||||||
1. Akilan Selvacoumar : as251@hw.ac.uk
|
1. Akilan Selvacoumar : as251@hw.ac.uk
|
||||||
|
|||||||
6
website-generator/package-lock.json
generated
6
website-generator/package-lock.json
generated
@@ -1132,9 +1132,9 @@
|
|||||||
"integrity": "sha1-ZMTwJfF/1Tv7RXY/rrFvAVp0dVA="
|
"integrity": "sha1-ZMTwJfF/1Tv7RXY/rrFvAVp0dVA="
|
||||||
},
|
},
|
||||||
"lodash": {
|
"lodash": {
|
||||||
"version": "4.17.15",
|
"version": "4.17.19",
|
||||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
|
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz",
|
||||||
"integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A=="
|
"integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ=="
|
||||||
},
|
},
|
||||||
"lodash.debounce": {
|
"lodash.debounce": {
|
||||||
"version": "4.0.8",
|
"version": "4.0.8",
|
||||||
|
|||||||
970
whatsapp-web.js/package-lock.json
generated
970
whatsapp-web.js/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -4,7 +4,8 @@
|
|||||||
"description": "",
|
"description": "",
|
||||||
"main": "test.js",
|
"main": "test.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "node website_test.js http://localhost:8004",
|
"test-web": "node website_test.js http://localhost:8004",
|
||||||
|
"test" : "mocha",
|
||||||
"start": "node index.js"
|
"start": "node index.js"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
@@ -12,5 +13,9 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"request": "^2.88.2",
|
"request": "^2.88.2",
|
||||||
"whatsapp-web.js": "^1.7.0"
|
"whatsapp-web.js": "^1.7.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"chai": "^4.2.0",
|
||||||
|
"mocha": "^8.0.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
const { Website } = require("../models/website");
|
const { Website } = require("../models/website");
|
||||||
const { data } = require("../models/data");
|
const { data } = require("../models/data");
|
||||||
|
const { Product } = require('../models/product');
|
||||||
|
|
||||||
module.exports.onWG = () => {
|
module.exports.onWG = () => {
|
||||||
console.log('hello world');
|
console.log('hello world');
|
||||||
@@ -18,3 +19,26 @@ module.exports.onCreate = (id, companyName) => {
|
|||||||
//add the website with the company name
|
//add the website with the company name
|
||||||
user.addWebsite(new Website(companyName));
|
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.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.deleteProduct(id);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,47 +1,41 @@
|
|||||||
|
|
||||||
# Commands available for user interaction
|
# Commands available for user interaction
|
||||||
|
|
||||||
*wg* -> Command wg help to get the avaialabe instructions
|
`wg` -> Command wg help to get the avaialabe instructions.
|
||||||
*wg help* -> The following are the available commands:
|
`wg help` -> All the commands will be made be displayed to the user.
|
||||||
*wg create <company name> -> creates a website without details
|
`wg website <company name>` -> if a website exists with the same **company name**, then that website is selected, else it creates a new website.
|
||||||
|
|
||||||
# Note:
|
### Note:
|
||||||
After the user uses *wg create <company name>*. The bot should instruct the user to enter the rest of the details.
|
After the user uses `wg create <company name>`. The bot should instruct the user to enter the rest of the details.
|
||||||
To see the details to be entered, please check *website.js* under models folder.
|
To see the details to be entered, please check [website.js](https://github.com/HWTechClub/Custom-Static-Website-Generator/blob/master/whatsapp-web.js/src/models/website.js) under models folder.
|
||||||
|
|
||||||
The commands should follow the rules below :
|
The commands should follow the rules below :
|
||||||
*wg website firstName*
|
`wg website firstName`, `wg website lastName`, etc..
|
||||||
*wg website lastName*
|
|
||||||
...etc
|
|
||||||
|
|
||||||
|
### Following commands only works when a website is created
|
||||||
|
|
||||||
|
`wg product all` -> shows all the products that are created.
|
||||||
|
`wg product new` -> creates a new product with generated Id.
|
||||||
|
`wg product <id>` -> if the id exists, the product with *id* is selected, else, create a new product with the id given.
|
||||||
|
|
||||||
|
### NOTE :
|
||||||
# Following commands only works when a website is created
|
|
||||||
|
|
||||||
*wg product all* -> shows all the products that are created
|
|
||||||
*wg product new* -> creates a new product with generated Id
|
|
||||||
*wg product <id>* -> if the id exists, the product with “id“ is selected, else, create a new product with the id given
|
|
||||||
|
|
||||||
# NOTE :
|
|
||||||
User should be given feedback on their commands. For example:
|
User should be given feedback on their commands. For example:
|
||||||
If a user created a product, the bot should reply, "a product has been created with the id : <id>"
|
If a user created a product, the bot should reply, *a product has been created with the id : <id>*.
|
||||||
If a user created a product without a website, the bot should handle the error and reply, "A website should be selected or created before creating a product"
|
If a user created a product without a website, the bot should handle the error and reply, *A website should be selected or created before creating a product*.
|
||||||
|
|
||||||
# Following command only works if a product is selected
|
### Following command only works if a product is selected
|
||||||
*wg product info* -> shows the information about the product. Ie name, cost, description, image
|
`wg product info` -> shows the information about the product. Ie name, cost, description, image.
|
||||||
*wg product name* <product-name> -> set the name of the product
|
`wg product name <product-name>` -> set the name of the product.
|
||||||
*wg product cost* <product-cost> -> set the cost of the product
|
`wg product cost <product-cost>` -> set the cost of the product.
|
||||||
*wg product desc* <product-desc> -> set the description of the product
|
`wg product desc <product-desc>` -> set the description of the product.
|
||||||
*wg product image* <product-image> -> sets the image of the product. The caption of the image should be the command.
|
`wg product image <product-image>` -> sets the image of the product. The caption of the image should be the command.
|
||||||
*wg finished* -> creates the website and gives a link to the website maker
|
|
||||||
|
|
||||||
# delete
|
|
||||||
Users should be able to delete products and website
|
|
||||||
*wg delete product <id>*
|
|
||||||
*wg delete website <company name>*
|
|
||||||
|
|
||||||
|
### Create the website
|
||||||
|
`wg website finished` -> creates the website and gives a link to the website maker.
|
||||||
|
|
||||||
|
# Delete
|
||||||
|
Users should be able to delete products and website.
|
||||||
|
`wg delete product <id>`, `wg delete website <company name>`
|
||||||
|
|
||||||
# Note:
|
# Note:
|
||||||
If you think these commands are not enough, please add them in a seperate section with a clear defination.
|
If you think these commands are not enough, please add them in a seperate section with a clear defination.
|
||||||
@@ -1,10 +1,100 @@
|
|||||||
class Command{
|
class Command{
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {{string, function()}} param0
|
||||||
|
*/
|
||||||
constructor({command,callback}){
|
constructor({command,callback}){
|
||||||
this.command = command;
|
this.command = command;
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
|
this.requireInput = false;
|
||||||
|
this.inputPos = [];
|
||||||
|
this._requireInput();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} message
|
||||||
|
*/
|
||||||
|
equals(message){
|
||||||
|
let msg = message.trim();
|
||||||
|
|
||||||
|
//check if the command is exactly equals to the msg
|
||||||
|
//if mathces then return true
|
||||||
|
//else if we check if the command requires an input
|
||||||
|
//if not, then we return false
|
||||||
|
if(this.command == msg){
|
||||||
|
return true
|
||||||
|
}else if(this.requireInput){
|
||||||
|
//the command requires input
|
||||||
|
|
||||||
|
let splitCommand = this.command.split(' ');
|
||||||
|
let splitMsg = msg.split(' ');
|
||||||
|
|
||||||
|
//check the number of words both has
|
||||||
|
//if not same then return false
|
||||||
|
//else iterate through all the words
|
||||||
|
|
||||||
|
|
||||||
|
if(splitCommand.length == splitMsg.length)
|
||||||
|
{
|
||||||
|
for(let i = 0; i < splitMsg.length; i++)
|
||||||
|
{
|
||||||
|
//if words matches then continue
|
||||||
|
//if the word is a input then continue
|
||||||
|
//else return false
|
||||||
|
if(splitMsg[i] == splitCommand[i]){
|
||||||
|
continue;
|
||||||
|
}else if(splitCommand[i].match(/[<>]/g) != null){
|
||||||
|
continue;
|
||||||
|
}else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// all the words are matched to each other or to an input field
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//the message is not the same as the command
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Only used in class
|
||||||
|
*
|
||||||
|
* This determines if the command requires input
|
||||||
|
* if the command has <inputName> in the string
|
||||||
|
* then the command requires input
|
||||||
|
*/
|
||||||
|
_requireInput(){
|
||||||
|
let msg = this.command;
|
||||||
|
|
||||||
|
let matched = msg.match(/[<>]/g);
|
||||||
|
|
||||||
|
this.requireInput = matched != null && matched.length > 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns an array of string representing the inputs of the command
|
||||||
|
* @param {string} message
|
||||||
|
*/
|
||||||
|
getInput(message){
|
||||||
|
|
||||||
|
let splitMsg = message.split(' ');
|
||||||
|
let splitCommand = this.command.split(' ');
|
||||||
|
let input = {};
|
||||||
|
|
||||||
|
for(let i = 0; i < splitMsg.length; i++){
|
||||||
|
if(splitCommand[i].startsWith('<')){
|
||||||
|
let key = splitCommand[i].slice(1, splitCommand[i].length-1);
|
||||||
|
input[key] = splitMsg[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.Command = Command;
|
module.exports.Command = Command;
|
||||||
|
|||||||
@@ -1,22 +1,18 @@
|
|||||||
|
const { Entity } = require("./entity");
|
||||||
const { User } = require("./user");
|
const { User } = require("./user");
|
||||||
const { Website } = require("./website");
|
|
||||||
|
|
||||||
module.exports.data = new StructuredData();
|
|
||||||
|
|
||||||
class StructuredData {
|
class StructuredData {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.data={};
|
this.entity = new Entity();
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
addUser(id){
|
* Adds a user
|
||||||
if(this.data[id] != undefined)
|
* @param {User} user
|
||||||
{
|
*/
|
||||||
this.data[id] = new User(id);
|
addUser(user){
|
||||||
return true
|
return this.entity.addData(user.id, user);
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,22 +21,37 @@ class StructuredData {
|
|||||||
* @returns {User}
|
* @returns {User}
|
||||||
*/
|
*/
|
||||||
getUser(id){
|
getUser(id){
|
||||||
return this.data[id];
|
return this.entity.getData(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete the user with a specific id
|
||||||
|
* @param {string} id
|
||||||
|
*/
|
||||||
deleteUser(id){
|
deleteUser(id){
|
||||||
if(this.data[id] != undefined)
|
return this.entity.deleteData(id)
|
||||||
{
|
|
||||||
return delete this.data[id];
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
/**
|
||||||
|
* Check if a user with the given id exists
|
||||||
|
* @param {string} id
|
||||||
|
*/
|
||||||
|
isUser(id){
|
||||||
|
return this.entity.isData(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears all the data stored
|
||||||
|
*/
|
||||||
|
clearData(){
|
||||||
|
this.entity.clearData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports.data = new StructuredData();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
data structured:
|
data structured:
|
||||||
data = {
|
data = {
|
||||||
|
|||||||
51
whatsapp-web.js/src/models/entity.js
Normal file
51
whatsapp-web.js/src/models/entity.js
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
|
||||||
|
|
||||||
|
class Entity {
|
||||||
|
|
||||||
|
constructor(){
|
||||||
|
this.data = new Map();
|
||||||
|
this.count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
addData(key ,value){
|
||||||
|
if(!this.isData(key)){
|
||||||
|
this.data.set(key, value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check if key exists
|
||||||
|
* @param {string} key
|
||||||
|
*/
|
||||||
|
isData(key){
|
||||||
|
return this.data.has(key) ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteData(key){
|
||||||
|
if(this.isData(key)){
|
||||||
|
this.data.delete(key);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
clearData(){
|
||||||
|
this.data = new Map();
|
||||||
|
this.count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
getData(key){
|
||||||
|
if(this.isData(key)){
|
||||||
|
return this.data.get(key);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
getAllData(){
|
||||||
|
return this.data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.Entity = Entity;
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
class Product {
|
class Product {
|
||||||
|
|
||||||
constructor({ id = '', name = '', desc = '', cost = 0, image = null }) {
|
constructor({ id = '', name = '', desc = '', cost = 0, image = null } = {}) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.desc = desc;
|
this.desc = desc;
|
||||||
@@ -9,40 +9,40 @@ class Product {
|
|||||||
this.image = image;
|
this.image = image;
|
||||||
}
|
}
|
||||||
|
|
||||||
getId(){
|
/**
|
||||||
return this.id;
|
*
|
||||||
|
* @param {Number} id
|
||||||
|
*/
|
||||||
|
setId(id){
|
||||||
|
return this.id = id;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {String} name
|
||||||
|
*/
|
||||||
setName(name){
|
setName(name){
|
||||||
this.name = name;
|
return this.name = name;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
getName(){
|
*
|
||||||
return this.name;
|
* @param {String} description
|
||||||
|
*/
|
||||||
|
setDesc(description){
|
||||||
|
return this.desc = description;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
setDesc(desc){
|
*
|
||||||
this.desc = desc;
|
* @param {Number} cost
|
||||||
}
|
*/
|
||||||
|
|
||||||
getDesc(){
|
|
||||||
return this.desc;
|
|
||||||
}
|
|
||||||
|
|
||||||
setCost(cost){
|
setCost(cost){
|
||||||
this.cost = cost;
|
return this.cost = cost;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
getCost(){
|
*
|
||||||
return this.cost;
|
* @param {String} image
|
||||||
}
|
*/
|
||||||
|
|
||||||
setImage(image){
|
setImage(image){
|
||||||
this.imageUrl = image;
|
return this.image = image;
|
||||||
}
|
|
||||||
|
|
||||||
getImage(){
|
|
||||||
return this.imageUrl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,16 @@
|
|||||||
|
|
||||||
|
const { Entity } = require('./entity');
|
||||||
const {Website} = require('./website');
|
const {Website} = require('./website');
|
||||||
|
|
||||||
class User{
|
class User{
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} id
|
||||||
|
*/
|
||||||
constructor(id){
|
constructor(id){
|
||||||
this.id = id;
|
this.id = id;
|
||||||
//uses key value pair to store website
|
this.websites = new Entity();
|
||||||
//key is the companyName of the website
|
|
||||||
//value is the website itself
|
|
||||||
this.websites = {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,26 +18,15 @@ class User{
|
|||||||
* @param {Website} website
|
* @param {Website} website
|
||||||
*/
|
*/
|
||||||
addWebsite(website){
|
addWebsite(website){
|
||||||
if(this.websites[website.companyName] != undefined)
|
return this.websites.addData(website.companyName, website);
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}else{
|
|
||||||
this.websites[website.companyName] = website;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteWebsite(companyName){
|
deleteWebsite(companyName){
|
||||||
if(this.websites[companyName] != undefined)
|
return this.websites.deleteData(companyName);
|
||||||
{
|
|
||||||
return delete this.websites[companyName];
|
|
||||||
}else{
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getWebsite(companyName){
|
getWebsite(companyName){
|
||||||
return this.websites[companyName];
|
return this.websites.getData(companyName);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
const { Entity } = require('./entity');
|
||||||
const {Product} = require('./product');
|
const {Product} = require('./product');
|
||||||
|
|
||||||
class Website {
|
class Website {
|
||||||
@@ -16,7 +17,7 @@ class Website {
|
|||||||
//uses key value pair to store product
|
//uses key value pair to store product
|
||||||
//key is the productId of the product
|
//key is the productId of the product
|
||||||
//value is the product itself
|
//value is the product itself
|
||||||
this.products = {};
|
this.products = new Entity();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -24,13 +25,7 @@ class Website {
|
|||||||
* @param {Product} product
|
* @param {Product} product
|
||||||
*/
|
*/
|
||||||
addProduct(product){
|
addProduct(product){
|
||||||
if(this.products[product.id] == undefined)
|
return this.products.addData(product.id, product);
|
||||||
{
|
|
||||||
this.products[product.id] = product;
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,7 +34,15 @@ class Website {
|
|||||||
* @returns {Product}
|
* @returns {Product}
|
||||||
*/
|
*/
|
||||||
getProduct(productId){
|
getProduct(productId){
|
||||||
return this.products[productId];
|
return this.products.getData(productId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @returns {[]}
|
||||||
|
*/
|
||||||
|
getAllProduct(){
|
||||||
|
return this.products.getAllData().values();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -47,12 +50,8 @@ class Website {
|
|||||||
* @param {string} productId
|
* @param {string} productId
|
||||||
*/
|
*/
|
||||||
deleteProduct(productId){
|
deleteProduct(productId){
|
||||||
if(this.products[productId] != undefined)
|
|
||||||
{
|
|
||||||
return delete this.products[productId];
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return this.products.deleteData(productId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
|
|
||||||
const { Command } = require("../models/commands");
|
const { Command } = require("../models/commands");
|
||||||
const logic = require("../businessLogic/logic");
|
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 = null; // Selected website.
|
||||||
|
|
||||||
///////////
|
///////////
|
||||||
//add the commands here
|
//add the commands here
|
||||||
@@ -11,7 +16,7 @@ const commands = [
|
|||||||
command: 'wg',
|
command: 'wg',
|
||||||
|
|
||||||
//function to be executed
|
//function to be executed
|
||||||
callback : () => {
|
callback : (input) => {
|
||||||
|
|
||||||
logic.onWG();
|
logic.onWG();
|
||||||
//mesage to be sent to the user
|
//mesage to be sent to the user
|
||||||
@@ -23,12 +28,240 @@ const commands = [
|
|||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
new Command({
|
||||||
|
//User command
|
||||||
|
command: `wg product new <id>`,
|
||||||
|
|
||||||
|
//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 <product-name> \n3. wg product cost <product-cost> \n4. wg product desc <product-desc> \n5. wg product image <product-image>`
|
||||||
|
];
|
||||||
|
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
new Command({
|
||||||
|
//User command
|
||||||
|
command: `wg product delete <id>`,
|
||||||
|
|
||||||
|
//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;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
new Command({
|
||||||
|
//User command
|
||||||
|
command: `wg product select <id>`,
|
||||||
|
|
||||||
|
//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 <product-name> \n3. wg product cost <product-cost> \n4. wg product desc <product-desc> \n5. wg product image <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 <product-name> \n3. wg product cost <product-cost> \n4. wg product desc <product-desc> \n5. wg product image <product-image>'
|
||||||
|
|
||||||
|
];
|
||||||
|
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
new Command({
|
||||||
|
//User command
|
||||||
|
command: `wg product name <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;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
new Command({
|
||||||
|
//User command
|
||||||
|
command: `wg product cost <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;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
new Command({
|
||||||
|
//User command
|
||||||
|
command: 'wg product desc <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;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
new Command({
|
||||||
|
//User command
|
||||||
|
command: 'wg product image <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;
|
||||||
|
}
|
||||||
|
}),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const onMessage = (message, client) => {
|
module.exports= onMessage = (message, client) => {
|
||||||
|
|
||||||
let command = null;
|
let command = null;
|
||||||
|
|
||||||
@@ -36,7 +269,7 @@ const onMessage = (message, client) => {
|
|||||||
if(message.body.startsWith("wg"))
|
if(message.body.startsWith("wg"))
|
||||||
{
|
{
|
||||||
for (let c of commands){
|
for (let c of commands){
|
||||||
if(c.command == message.body)
|
if(c.equals(message.body))
|
||||||
{
|
{
|
||||||
command = c;
|
command = c;
|
||||||
break;
|
break;
|
||||||
@@ -44,7 +277,12 @@ const onMessage = (message, client) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(command != null){
|
if(command != null){
|
||||||
let messageToBeSent = command.callback();
|
let input = {};
|
||||||
|
if(command.requireInput){
|
||||||
|
input = command.getInput(message.body);
|
||||||
|
}
|
||||||
|
|
||||||
|
let messageToBeSent = command.callback(input);
|
||||||
for (let msg of messageToBeSent)
|
for (let msg of messageToBeSent)
|
||||||
{
|
{
|
||||||
client.sendMessage(message.from, msg, new Object());
|
client.sendMessage(message.from, msg, new Object());
|
||||||
@@ -54,6 +292,6 @@ const onMessage = (message, client) => {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.onMessage = onMessage;
|
|
||||||
|
|
||||||
//export default onMessage;
|
//export default onMessage;
|
||||||
169
whatsapp-web.js/test/test_command.js
Normal file
169
whatsapp-web.js/test/test_command.js
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
const { Command } = require('../src/models/commands');
|
||||||
|
|
||||||
|
const expect = require('chai').expect;
|
||||||
|
|
||||||
|
describe('Command', () => {
|
||||||
|
|
||||||
|
it('Test : requireInput() without input', () => {
|
||||||
|
let a = new Command({
|
||||||
|
command: "wg create",
|
||||||
|
callback: () => {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(a.requireInput).to.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Test : requireInput() with input', () => {
|
||||||
|
let a = new Command({
|
||||||
|
command: "wg create <id>",
|
||||||
|
callback: () => {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(a.requireInput).to.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Test : requireInput() with 2 inputs', () => {
|
||||||
|
let a = new Command({
|
||||||
|
command: "wg create <id> <name>",
|
||||||
|
callback: () => {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(a.requireInput).to.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Test : equals() without input', () => {
|
||||||
|
|
||||||
|
let message = 'wg website';
|
||||||
|
|
||||||
|
let a = new Command({
|
||||||
|
command: "wg create <id>",
|
||||||
|
callback: () => {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let b = new Command({
|
||||||
|
command: "wg website",
|
||||||
|
callback: () => {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let c = new Command({
|
||||||
|
command: "wg product p12",
|
||||||
|
callback: () => {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let d = new Command({
|
||||||
|
command: "wg",
|
||||||
|
callback: () => {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(a.equals(message)).to.false;
|
||||||
|
expect(b.equals(message)).to.true;
|
||||||
|
expect(c.equals(message)).to.false;
|
||||||
|
expect(d.equals(message)).to.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('Test : equals() with input', () => {
|
||||||
|
|
||||||
|
let message = 'wg create p12';
|
||||||
|
|
||||||
|
let a = new Command({
|
||||||
|
command: "wg create <id>",
|
||||||
|
callback: () => {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(a.equals(message)).to.true;
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Test : equals() with different message', () => {
|
||||||
|
|
||||||
|
let message = 'wg website web';
|
||||||
|
|
||||||
|
let a = new Command({
|
||||||
|
command: "wg create <id>",
|
||||||
|
callback: () => {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(a.equals(message)).to.false;
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Test : equals() with spelling error', () => {
|
||||||
|
|
||||||
|
let message = 'wg websit';
|
||||||
|
|
||||||
|
let b = new Command({
|
||||||
|
command: "wg website",
|
||||||
|
callback: () => {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(b.equals(message)).to.false;
|
||||||
|
|
||||||
|
});
|
||||||
|
it('Test : equals() with extra inputs', () => {
|
||||||
|
|
||||||
|
let message = 'wg create p12 pr';
|
||||||
|
|
||||||
|
let a = new Command({
|
||||||
|
command: "wg create <id>",
|
||||||
|
callback: () => {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
expect(a.equals(message)).to.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Test : getInput() with 1 input', () => {
|
||||||
|
|
||||||
|
let message = 'wg create p12'
|
||||||
|
|
||||||
|
let a = new Command({
|
||||||
|
command: "wg create <id>",
|
||||||
|
callback: () => {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(a.getInput(message)['id']).to.equal('p12');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Test : getInput() with 2 input', () => {
|
||||||
|
|
||||||
|
let message = 'wg create p12 pikachu'
|
||||||
|
|
||||||
|
let a = new Command({
|
||||||
|
command: "wg create <id> <name>",
|
||||||
|
callback: () => {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(a.getInput(message)['id']).to.equal('p12');
|
||||||
|
expect(a.getInput(message)['name']).to.equal('pikachu');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
68
whatsapp-web.js/test/test_entity.js
Normal file
68
whatsapp-web.js/test/test_entity.js
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
const expect = require('chai').expect;
|
||||||
|
const {data} = require('../src/models/data')
|
||||||
|
const {Entity} = require('../src/models/entity');
|
||||||
|
const { Website } = require('../src/models/website');
|
||||||
|
|
||||||
|
describe('Entity', () => {
|
||||||
|
|
||||||
|
it('Test : addData', () => {
|
||||||
|
let a = new Entity();
|
||||||
|
a.addData('hello', new Website('hello'));
|
||||||
|
|
||||||
|
expect(a.isData('hello')).to.true;
|
||||||
|
expect(a.count).to.equal(0);
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Test : 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;
|
||||||
|
});
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Test: deleteData', () => {
|
||||||
|
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.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);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user