function byDepartment() {
  var query = "SELECT * FROM departments";
  connection.query(query, function (err, res) {
    if (err) {
      console.log(
        "We're having trouble finding the department list. Check your connection with the database and try again"
      );
    }
    const deptOptions = res.map(function (department) {
      return {
        name: department.name,
        value: department.id,
      };
    });
    inquirer
      .prompt({
        name: "department",
        type: "list",
        message: "Which department?",
        choices: deptOptions,
      })
      .then(function (answer) {
        var query =
          "SELECT first_name, last_name, role_id, roles.title AS 'role', salary FROM employees INNER JOIN roles ON role_id = roles.id INNER JOIN departments ON roles.department_id = departments.id WHERE departments.id = ?";
        connection.query(query, [answer.department], function (err, res) {
          if (err) {
            console.log(
              "We're having trouble finding the department list. Check your connection with the database and try again"
            );
          }
          console.log("-----------DEPARTMENT STAFF LIST-----------");
          for (var i = 0; i < res.length; i++) {
            console.log(
              res[i].role +
                ": " +
                res[i].first_name +
                " " +
                res[i].last_name +
                " || Salary: $" +
                res[i].salary
            );
          }
          console.log("-------------------------------------------");
          runSearch();
        });
      });
  });
}
// VIEW ALL EMPLOYEES IN A DEPARTMENT
VIEW ALL EMPLOYEES IN A DEPARTMENT
VIEW ALL EMPLOYEES IN A DEPARTMENT
function byDepartment() {
  var query = "SELECT * FROM departments";
  connection.query(query, function (err, res) {
    if (err) {
      console.log(
        "We're having trouble finding the department list. Check your connection with the database and try again"
      );
    }
    const deptOptions = res.map(function (department) {
      return {
        name: department.name,
        value: department.id,
      };
    });
    inquirer
      .prompt({
        name: "department",
        type: "list",
        message: "Which department?",
        choices: deptOptions,
      })
      .then(function (answer) {
        var query =
          "SELECT first_name, last_name, role_id, roles.title AS 'role', salary FROM employees INNER JOIN roles ON role_id = roles.id INNER JOIN departments ON roles.department_id = departments.id WHERE departments.id = ?";
        connection.query(query, [answer.department], function (err, res) {
          if (err) {
            console.log(
              "We're having trouble finding the department list. Check your connection with the database and try again"
            );
          }
          console.log("-----------DEPARTMENT STAFF LIST-----------");
          for (var i = 0; i < res.length; i++) {
            console.log(
              res[i].role +
                ": " +
                res[i].first_name +
                " " +
                res[i].last_name +
                " || Salary: $" +
                res[i].salary
            );
          }
          console.log("-------------------------------------------");
          runSearch();
        });
      });
  });
}
function byDepartment ( ) { var query = "SELECT * FROM departments" ; connection . query ( query , function ( err , res ) { if ( err ) { console . log ( "We're having trouble finding the department list. Check your connection with the database and try again" ) ; } const deptOptions = res . map ( function ( department ) { return { name : department . name , value : department . id , } ; } ) ; inquirer . prompt ( { name : "department" , type : "list" , message : "Which department?" , choices : deptOptions , } ) . then ( function ( answer ) { var query = "SELECT first_name, last_name, role_id, roles.title AS 'role', salary FROM employees INNER JOIN roles ON role_id = roles.id INNER JOIN departments ON roles.department_id = departments.id WHERE departments.id = ?" ; connection . query ( query , [ answer . department ] , function ( err , res ) { if ( err ) { console . log ( "We're having trouble finding the department list. Check your connection with the database and try again" ) ; } console . log ( "-----------DEPARTMENT STAFF LIST-----------" ) ; for ( var i = 0 ; i < res . length ; i ++ ) { console . log ( res [ i ] . role + ": " + res [ i ] . first_name + " " + res [ i ] . last_name + " || Salary: $" + res [ i ] . salary ) ; } console . log ( "-------------------------------------------" ) ; runSearch ( ) ; } ) ; } ) ; } ) ; }
VIEW ALL EMPLOYEES IN A DEPARTMENT
VIEW ALL EMPLOYEES IN A DEPARTMENT