I Think the product functions are done.
This commit is contained in:
6
website-generator/package-lock.json
generated
6
website-generator/package-lock.json
generated
@@ -1132,9 +1132,9 @@
|
||||
"integrity": "sha1-ZMTwJfF/1Tv7RXY/rrFvAVp0dVA="
|
||||
},
|
||||
"lodash": {
|
||||
"version": "4.17.15",
|
||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
|
||||
"integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A=="
|
||||
"version": "4.17.19",
|
||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz",
|
||||
"integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ=="
|
||||
},
|
||||
"lodash.debounce": {
|
||||
"version": "4.0.8",
|
||||
|
||||
@@ -42,7 +42,3 @@ module.exports.deleteProduct = (id,website) => {
|
||||
|
||||
return website.products.deleteData(id);
|
||||
}
|
||||
|
||||
module.exports.deleteProduct = (id,website) => {
|
||||
return website.products.deleteData(id);
|
||||
}
|
||||
@@ -1,10 +1,100 @@
|
||||
class Command{
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {{string, function()}} param0
|
||||
*/
|
||||
constructor({command,callback}){
|
||||
this.command = command;
|
||||
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).length > 1){
|
||||
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;
|
||||
|
||||
@@ -16,7 +16,7 @@ const commands = [
|
||||
command: 'wg',
|
||||
|
||||
//function to be executed
|
||||
callback : () => {
|
||||
callback : (input) => {
|
||||
|
||||
logic.onWG();
|
||||
//mesage to be sent to the user
|
||||
@@ -33,10 +33,9 @@ const commands = [
|
||||
command: `wg product new <id>`,
|
||||
|
||||
//function to be executed
|
||||
callback : () => {
|
||||
callback : (input) => {
|
||||
|
||||
logic.onWG();
|
||||
var id;
|
||||
if(selected_website==null)
|
||||
{
|
||||
let message = [
|
||||
@@ -44,7 +43,7 @@ const commands = [
|
||||
];
|
||||
return message;
|
||||
}
|
||||
selected_product= logic.addProduct(id,selected_website);
|
||||
selected_product= logic.addProduct(input,selected_website);
|
||||
//mesage to be sent to the user
|
||||
let message = [
|
||||
`The product has been created with the id : ` + selected_product.id ,
|
||||
@@ -59,10 +58,9 @@ const commands = [
|
||||
command: `wg delete product <id>`,
|
||||
|
||||
//function to be executed
|
||||
callback : () => {
|
||||
callback : (input) => {
|
||||
|
||||
logic.onWG();
|
||||
var id ;
|
||||
if(selected_website==null)
|
||||
{
|
||||
let message = [
|
||||
@@ -71,7 +69,7 @@ const commands = [
|
||||
return message;
|
||||
}
|
||||
|
||||
if(logic.deleteProduct == false)
|
||||
if(logic.deleteProduct(input,selected_website) == false)
|
||||
{
|
||||
let message = [
|
||||
`No product with the given id`
|
||||
@@ -90,10 +88,9 @@ const commands = [
|
||||
command: `wg product <id>`,
|
||||
|
||||
//function to be executed
|
||||
callback : () => {
|
||||
callback : (input) => {
|
||||
|
||||
logic.onWG();
|
||||
var id ;
|
||||
if(selected_website==null)
|
||||
{
|
||||
let message = [
|
||||
@@ -102,7 +99,7 @@ const commands = [
|
||||
return message;
|
||||
}
|
||||
// Find the product with the given id
|
||||
selected_product = selected_website.products.getData(id);
|
||||
selected_product = selected_website.products.getData(input);
|
||||
if(selected_product == null)
|
||||
{
|
||||
logic.addProduct(id,selected_website);
|
||||
@@ -181,10 +178,9 @@ const commands = [
|
||||
command: `wg product name <product-name>`,
|
||||
|
||||
//function to be executed
|
||||
callback : () => {
|
||||
callback : (input) => {
|
||||
|
||||
logic.onWG();
|
||||
given_name= null;
|
||||
if(selected_product == null)
|
||||
{
|
||||
let message = [
|
||||
@@ -192,7 +188,7 @@ const commands = [
|
||||
];
|
||||
return message;
|
||||
}
|
||||
selected_product.name= given_name;
|
||||
selected_product.name= input;
|
||||
let message = [
|
||||
'The product name has been set to '+ given_name
|
||||
];
|
||||
@@ -205,10 +201,9 @@ const commands = [
|
||||
command: `wg product cost <product-cost>`,
|
||||
|
||||
//function to be executed
|
||||
callback : () => {
|
||||
callback : (input) => {
|
||||
|
||||
logic.onWG();
|
||||
given_cost= null;
|
||||
if(selected_product == null)
|
||||
{
|
||||
let message = [
|
||||
@@ -216,7 +211,7 @@ const commands = [
|
||||
];
|
||||
return message;
|
||||
}
|
||||
selected_product.cost= given_cost;
|
||||
selected_product.cost= input;
|
||||
let message = [
|
||||
'The product cost has been set to '+ given_cost
|
||||
];
|
||||
@@ -229,10 +224,9 @@ const commands = [
|
||||
command: 'wg product desc <product-desc>',
|
||||
|
||||
//function to be executed
|
||||
callback : () => {
|
||||
callback : (input) => {
|
||||
|
||||
logic.onWG();
|
||||
given_desc= null;
|
||||
if(selected_product == null)
|
||||
{
|
||||
let message = [
|
||||
@@ -240,7 +234,7 @@ const commands = [
|
||||
];
|
||||
return message;
|
||||
}
|
||||
selected_product.desc= given_desc;
|
||||
selected_product.desc = input;
|
||||
let message = [
|
||||
'The product description has been set to : '+ given_desc
|
||||
];
|
||||
@@ -253,10 +247,9 @@ const commands = [
|
||||
command: 'wg product image <product-image>',
|
||||
|
||||
//function to be executed
|
||||
callback : () => {
|
||||
callback : (input) => {
|
||||
|
||||
logic.onWG();
|
||||
given_image= null;
|
||||
if(selected_product == null)
|
||||
{
|
||||
let message = [
|
||||
@@ -264,7 +257,7 @@ const commands = [
|
||||
];
|
||||
return message;
|
||||
}
|
||||
selected_product.image = given_image;
|
||||
selected_product.image = input;
|
||||
let message = [
|
||||
'The product image has been set to : '+ given_image
|
||||
];
|
||||
@@ -284,7 +277,7 @@ module.exports= onMessage = (message, client) => {
|
||||
if(message.body.startsWith("wg"))
|
||||
{
|
||||
for (let c of commands){
|
||||
if(c.command == message.body)
|
||||
if(c.equals(message.body))
|
||||
{
|
||||
command = c;
|
||||
break;
|
||||
@@ -292,7 +285,12 @@ module.exports= onMessage = (message, client) => {
|
||||
}
|
||||
|
||||
if(command != null){
|
||||
let messageToBeSent = command.callback();
|
||||
let input = {};
|
||||
if(command.requireInput){
|
||||
input = command.getInput();
|
||||
}
|
||||
|
||||
let messageToBeSent = command.callback(input);
|
||||
for (let msg of messageToBeSent)
|
||||
{
|
||||
client.sendMessage(message.from, msg, new Object());
|
||||
|
||||
154
whatsapp-web.js/test/test_command.js
Normal file
154
whatsapp-web.js/test/test_command.js
Normal file
@@ -0,0 +1,154 @@
|
||||
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 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');
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user