-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonSupervisor.js
More file actions
112 lines (99 loc) · 3.49 KB
/
bamazonSupervisor.js
File metadata and controls
112 lines (99 loc) · 3.49 KB
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
var inquirer = require("inquirer");
var mysql = require("mysql");
// const cTable = require('console.table');
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2
});
var connectionData = {
// debug: true,
host: "localhost",
port: 3306,
user: "root",
password: "password",
database: "bamazon"
};
var connection = mysql.createConnection(connectionData);
// connect to the mysql server and sql database
connection.connect(function (err) {
if (err) throw err;
// console.log(`Connected to ${dbName}`);
});
// connection.end();
function salesByDepartment() {
// Alias A = departments table and B = proucts table.
// Right join A and B on department_name.
// Take department_id, department_name, and over_head_costs from departments table.
// Sum up the product sales column from products table. If it's null return 0.
// Calculate total_profit by subtracting over_head_costs from the product sales sum. If it's null return 0.
// Group it all by department_id, department_name, and over_head_costs
// Pass it to the npm console.table for proper display.
connection.query(" SELECT A.department_id, A.department_name, A.over_head_costs, " +
"FORMAT(SUM(IFNULL(B.product_sales, 0)),2) as product_sales, " +
"FORMAT( (SUM(IFNULL(B.product_sales, 0)) - A.over_head_costs),2) as total_profit " +
"FROM products B RIGHT JOIN departments A ON B.department_name = A.department_name " +
"GROUP BY A.department_id, A.department_name, A.over_head_costs", function (err, results) {
console.table(results);
prompt();
}
);
}
function createNewDepartment() {
inquirer
.prompt([
{
name: "department",
type: "input",
message: `\nDepartment Name?`
}
])
.then(function (answer) {
connection.query(
// "UPDATE products SET ? WHERE ?",
`INSERT INTO departments (department_name) VALUES (\"${answer.department.trim()}\")`,
function (error) {
if (error) {
if (error.code === 'ER_DUP_ENTRY') {
console.log(`\nDepartment \'${answer.department.trim()}\' already exists.\n`);
prompt();
} else {
throw error;
}
} else {
console.log(`\nAdded Department: \'${answer.department}\' to departments table.\n`);
prompt();
}
}
);
});
}
function prompt() {
inquirer.prompt([
{
name: "task",
type: "list",
message: "\nWhat would you like to do?",
choices: ["View Product Sales by Department", "Create New Department", "Exit"]
}
]).then(answer => {
// console.log(`Task was ${answer.task}`);
switch (answer.task) {
case "Create New Department":
createNewDepartment();
break;
case "View Product Sales by Department":
salesByDepartment();
break;
case "Exit":
default:
connection.end();
return;
}
});
}
function start() {
process.stdout.write('\033c'); // Refresh the screen
prompt();
}
start();