-
Notifications
You must be signed in to change notification settings - Fork 0
/
bamazonSupervisor.js
145 lines (145 loc) · 5.13 KB
/
bamazonSupervisor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env node
/**
* BAMazon is a command line application created with NodeJS and MySQL. There three separate
* programs included in this project. Each program is designed for a different BAMazon user,
* one for customers, one for managers, and one for supervisors. Data about the store's
* inventory is stored in a local MySQL database which can be recreated by any user with the
* provided sql files in this project.
*
* @summary BAMazon supervisor interface used for checking department status and creating new departments.
* @since 1.0.0
* @version 1.0.0
*
* Inquirer npm module
* @link https://www.npmjs.com/package/inquirer
* MySQL npm module
* @link https://www.npmjs.com/package/mysql
*
* @author Aaron Michael McNulty
*/
// Include depended files and libraries.
require('dotenv').config();
require('console.table');
const inquirer = require('inquirer');
const MYSQL = require('mysql');
/**
* All properties and methods involving supervisor interactions are stored in this object.
*
* @since 1.0.0
*/
var supervisor = {
/**
* A handle for the connection to the MySQL database.
*/
connection: MYSQL.createConnection({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: "bamazon"
}),
/**
* A map for different user interactions gathered from user input.
* @type {Object}
*/
actionsMap: {
'View Product Sales by Department': 'showDepartmentSales',
'Create New Department': 'createNewDepartment',
'Exit App': 'exitApp'
},
/**
* Displays the main menu to the user and prompts for input.
*
* @since 1.0.0
*/
showMenu: function() {
inquirer.prompt([
{
type: 'list',
message: "\nBAMazon Supervisor Portal\n\nPlease choose an action from the list below.",
choices: [
'View Product Sales by Department',
'Create New Department',
'Exit App'
],
name: 'action'
}
]).then(function(answers) {
supervisor[supervisor.actionsMap[answers.action]]();
});
},
/**
* Displays the total sales and profit per department.
*
* @since 1.0.0
* @fires supervisor.showMenu()
*/
showDepartmentSales: function() {
supervisor.connection.query('SELECT department_id, departments.department_name, over_head_costs, SUM(product_sales) AS product_sales, (SUM(product_sales) - over_head_costs) AS total_profit FROM products INNER JOIN departments ON products.department_name = departments.department_name GROUP BY departments.department_name;', function(err, results) {
if (err) throw err;
var myTable = [];
for (var i = 0; i < results.length; i++) {
myTable.push(
{
department_id: results[i].department_id,
department_name: results[i].department_name,
over_head_costs: results[i].over_head_costs.toFixed(2),
product_sales: results[i].product_sales.toFixed(2),
total_profit: results[i].total_profit.toFixed(2)
}
);
}
console.table(myTable);
supervisor.showMenu();
});
},
/**
* Allows user to create a new department in the database.
*
* @since 1.0.0
* @fires supervisor.showMenu()
*/
createNewDepartment: function() {
inquirer.prompt([
{
type: 'input',
message: "\nEnter the name of the new department.",
name: 'departmentName'
},
{
type: 'input',
message: "\nEnter the overhead cost for this department.",
validate: function(overhead) {
if (Number.isNaN(parseFloat(overhead)) || parseFloat(overhead) <= 0) return "\nPlease enter a valid number greater than 0.";
else return true;
},
name: 'departmentOverhead'
}
]).then(function(answers) {
supervisor.connection.query("INSERT INTO departments (department_name, over_head_costs) VALUES('" + answers.departmentName + "', " + answers.departmentOverhead + ")", function(err, results) {
if (err) throw err;
console.log("\n" + answers.departmentName + " department created successfully!!");
supervisor.showMenu();
});
});
},
/**
* Ends database connection, thanks customer, and exits application.
*
* @since 1.0.0
*/
exitApp: function() {
supervisor.connection.end(function(err) {
if (err) throw err;
console.log("\nThank you for using BAMazon!")
process.exit();
})
}
}
/**
* Connects this app to the database then displays the main menu.
*/
supervisor.connection.connect(function(err) {
if (err) throw err;
supervisor.showMenu();
});