-
Notifications
You must be signed in to change notification settings - Fork 1
/
bamazonSupervisor.js
119 lines (106 loc) · 3.76 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
var mysql = require("mysql");
var Table = require('cli-table');
var clear = require('clear');
const chalk = require('chalk');
var figlet = require('figlet');
var inquirer = require("inquirer");
var dotenv = require('dotenv').config();
var connection = mysql.createConnection({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
// Your username
user: process.env.DB_USER,
// Your password
password: process.env.DB_PASS,
database: process.env.DB_DATABASE
});
connection.connect(function (err) {
if (err) throw err;
console.log("connected as id " + connection.threadId + "\n");
clear();
userOptions();
});
function userOptions() {
inquirer
.prompt([
{
name: "action",
type: "list",
message: "What would you like to do?",
choices: [
{
key: 'a',
name: 'View Product Sales by Department',
value: 'view_product_sales'
},
{
key: 'b',
name: 'Create New Department',
value: 'create_department'
}
]
}
])
.then(function (answer) {
//console.log(answer);
if (answer.action === 'view_product_sales') {
displayProductSales();
}
else if (answer.action === 'create_department') {
createDepartment();
}
});
}
function displayProductSales() {
connection.query("SELECT d.*,sum(p.product_sales) as product_sales, (sum(p.product_sales)-d.over_head_costs) as total_profit FROM departments d left join products p on d.department_name=p.department_name group by d.department_id", function (err, res) {
if (err) throw err;
var table = new Table({
chars: {
'top': '═', 'top-mid': '╤', 'top-left': '╔', 'top-right': '╗'
, 'bottom': '═', 'bottom-mid': '╧', 'bottom-left': '╚', 'bottom-right': '╝'
, 'left': '║', 'left-mid': '╟', 'mid': '─', 'mid-mid': '┼'
, 'right': '║', 'right-mid': '╢', 'middle': '│'
},
head: ['Department Id', 'Department Name', 'Over Head Costs', 'Product Sales', 'Total Profit']
});
for (var i = 0; i < res.length; i++) {
table.push([res[i].department_id, res[i].department_name, '$'
+ res[i].over_head_costs, '$'
+ res[i].product_sales, '$'
+ res[i].total_profit]);
}
console.log("\nProduct Sales by Department");
console.log(table.toString());
connection.end();
});
}
function createDepartment() {
inquirer
.prompt([
{
name: "department",
type: "input",
message: "What is the name of the department you would like to add ?"
},
{
name: "overhead",
type: "input",
message: "What is the over head cost of the department?",
validate: function (value) {
var reg = /^\d+$/;
return reg.test(value) || "Over Head Cost should be a number!";
}
}
])
.then(function (answer) {
connection.query("INSERT INTO departments SET ?",
{
department_name: answer.department,
over_head_cost: answer.overhead
}
, function (err, res) {
console.log('New department added');
connection.end();
});
});
}