structured the logic.js
This commit is contained in:
@@ -1,44 +1,275 @@
|
|||||||
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');
|
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
|
if(user.getSelectedWebsite() == null)
|
||||||
let user = data.getUser(id);
|
|
||||||
if(user == undefined)
|
|
||||||
{
|
{
|
||||||
data.addUser(id);
|
return [
|
||||||
user = data.getUser(id);
|
`A website should be selected or created
|
||||||
|
To create website, please use the following command : `,
|
||||||
|
`*wg create <company-name>*`,
|
||||||
|
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
//add the website with the company name
|
return '';
|
||||||
user.addWebsite(new Website(companyName));
|
|
||||||
}
|
}
|
||||||
module.exports.addProduct = (id,website) => {
|
|
||||||
|
|
||||||
if(id==null)
|
const checkSelectedProductExist = () => {
|
||||||
|
if(user.getSelectedWebsite().getSelectedProduct() == null)
|
||||||
{
|
{
|
||||||
// Setting up the product id if not given
|
return [
|
||||||
id = Math.floor((Math.random() * 100000) + 1);
|
`A product should be created or selected`
|
||||||
while(website.products.isData(id))// This is will check if the generated id is already used
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
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 <product-name>
|
||||||
|
3. wg product cost <product-cost>
|
||||||
|
4. wg product desc <product-desc>
|
||||||
|
5. wg product image <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 <product-name>
|
||||||
|
3. wg product cost <product-cost>
|
||||||
|
4. wg product desc <product-desc>
|
||||||
|
5. wg product image <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
|
return check;
|
||||||
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);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,9 @@ class User{
|
|||||||
return this.websites.selectData(companyName);
|
return this.websites.selectData(companyName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {Website}
|
||||||
|
*/
|
||||||
getSelectedWebsite(){
|
getSelectedWebsite(){
|
||||||
return this.websites.selectedData;
|
return this.websites.selectedData;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ class Website {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @returns {[]}
|
* returns all the products in the website
|
||||||
*/
|
*/
|
||||||
getAllProduct(){
|
getAllProduct(){
|
||||||
return this.products.getAllData().values();
|
return this.products.getAllData().values();
|
||||||
@@ -58,6 +58,9 @@ class Website {
|
|||||||
return this.products.selectData(productId);
|
return this.products.selectData(productId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {Product}
|
||||||
|
*/
|
||||||
getSelectedProduct()
|
getSelectedProduct()
|
||||||
{
|
{
|
||||||
return this.products.selectedData;
|
return this.products.selectedData;
|
||||||
|
|||||||
@@ -31,231 +31,64 @@ const commands = [
|
|||||||
new Command({
|
new Command({
|
||||||
//User command
|
//User command
|
||||||
command: `wg product new <id>`,
|
command: `wg product new <id>`,
|
||||||
|
|
||||||
//function to be executed
|
//function to be executed
|
||||||
callback : (input) => {
|
callback : () => logic.onCreateProduct()
|
||||||
|
|
||||||
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({
|
new Command({
|
||||||
//User command
|
//User command
|
||||||
command: `wg product delete <id>`,
|
command: `wg product delete <id>`,
|
||||||
|
|
||||||
//function to be executed
|
//function to be executed
|
||||||
callback : (input) => {
|
callback : () => logic.onDeleteProduct()
|
||||||
|
|
||||||
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({
|
new Command({
|
||||||
//User command
|
//User command
|
||||||
command: `wg product select <id>`,
|
command: `wg product select <id>`,
|
||||||
|
|
||||||
//function to be executed
|
//function to be executed
|
||||||
callback : (input) => {
|
callback : () => logic.onSelectProduct()
|
||||||
|
|
||||||
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({
|
new Command({
|
||||||
//User command
|
//User command
|
||||||
command: `wg product all`,
|
command: `wg product all`,
|
||||||
|
|
||||||
//function to be executed
|
//function to be executed
|
||||||
callback : () => {
|
callback : () => logic.onGetAllProducts()
|
||||||
|
|
||||||
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({
|
new Command({
|
||||||
//User command
|
//User command
|
||||||
command: `wg product info`,
|
command: `wg product info`,
|
||||||
|
|
||||||
//function to be executed
|
//function to be executed
|
||||||
callback : () => {
|
callback : () => logic.onGetProductInfo()
|
||||||
|
|
||||||
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({
|
new Command({
|
||||||
//User command
|
//User command
|
||||||
command: `wg product name <product-name>`,
|
command: `wg product name <product-name>`,
|
||||||
|
|
||||||
//function to be executed
|
//function to be executed
|
||||||
callback : (input) => {
|
callback : () => logic.onSetProductName()
|
||||||
|
|
||||||
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({
|
new Command({
|
||||||
//User command
|
//User command
|
||||||
command: `wg product cost <product-cost>`,
|
command: `wg product cost <product-cost>`,
|
||||||
|
|
||||||
//function to be executed
|
//function to be executed
|
||||||
callback : (input) => {
|
callback : () => logic.onSetProductCost()
|
||||||
|
|
||||||
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({
|
new Command({
|
||||||
//User command
|
//User command
|
||||||
command: 'wg product desc <product-desc>',
|
command: 'wg product desc <product-desc>',
|
||||||
|
|
||||||
//function to be executed
|
//function to be executed
|
||||||
callback : (input) => {
|
callback : () => logic.onSetProductDesc()
|
||||||
|
|
||||||
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({
|
new Command({
|
||||||
//User command
|
//User command
|
||||||
command: 'wg product image <product-image>',
|
command: 'wg product image <product-image>',
|
||||||
|
|
||||||
//function to be executed
|
//function to be executed
|
||||||
callback : (input) => {
|
callback : () => logic.onSetProductImage()
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}),
|
}),
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -281,8 +114,12 @@ module.exports= onMessage = (message, client) => {
|
|||||||
if(command.requireInput){
|
if(command.requireInput){
|
||||||
input = command.getInput(message.body);
|
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)
|
for (let msg of messageToBeSent)
|
||||||
{
|
{
|
||||||
client.sendMessage(message.from, msg, new Object());
|
client.sendMessage(message.from, msg, new Object());
|
||||||
|
|||||||
Reference in New Issue
Block a user