-
Notifications
You must be signed in to change notification settings - Fork 0
/
bamazonSupervisor.js
130 lines (120 loc) · 3.65 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
const inquirer = require("inquirer");
const mysql = require("mysql");
const Table = require("cli-table");
const connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "yuffie",
database: "bamazonDB",
});
connection.connect(function (err) {
if (err) throw err;
console.log("connected as id " + connection.threadId + "\n");
menu();
});
const table = new Table({
head: ["Dept ID", "Department", "Over Head", "Sales", "Profit"],
colWidths: [10, 20, 15, 10, 20],
});
// run
// [x] display menu (list)
// [x] View Product Sales by Department
// [x] Create New Department
function menu() {
inquirer
.prompt({
name: "action",
type: "list",
message: "What would you like to do?",
choices: [
"View Product Sales by Department",
"Create a New Department",
"exit",
],
})
.then(function (answer) {
switch (answer.action) {
case "View Product Sales by Department":
productDep();
break;
case "Create a New Department":
newDep();
break;
case "exit":
connection.end();
break;
}
});
}
// View Product Sales by Department
// [x] display table in terminal
// [ ] total_profit (calculated on run, not a stored value, use custom alias )
function productDep() {
let query = `SELECT
departments.department_id,
departments.department_name,
departments.over_head_costs,
intermediate.product_sales
FROM
departments
LEFT JOIN (
SELECT
departments.department_name,
Sum(products.product_sales) AS product_sales
FROM
products
JOIN departments ON products.department_name = departments.department_name
GROUP BY
departments.department_name
) AS intermediate ON departments.department_name = intermediate.department_name`;
connection.query(query, function(err, res){
if (err) throw err;
console.log("Department List");
res.forEach((res)=> {
//in Customer app add sale count
//Total profit is calculated
let profit = res.over_head_costs - res.product_sales;
table.push([
`${res.department_id}`,
`${res.department_name}`,
`${res.over_head_costs}`,
`${res.product_sales}`,
`${profit.toFixed(2)}`
]);
});
console.log(table.toString());
connection.end();
});
}
// Create New Department
// [x] creates a new department, adds to the department database table
function newDep() {
inquirer
.prompt([
{
name: "deptName",
type: "input",
message: "What is the name of the department?",
},
{
name: "overHead",
type: "number",
message: "What is the over head for this department?"
}
])
.then(function (answer) {
connection.query(
"INSERT INTO departments SET ?",
{
department_name: answer.deptName,
over_head_costs: answer.overHead
},
function (err) {
if (err) throw err;
console.log("Department added!");
}
);
connection.end();
});
}