import ownConn from "../dbConfig.js"; //get single department by id export const getDepartmentById = async (id, result) => { try { let sql = `SELECT * FROM customerdepartment WHERE primaryID = ?`; const results = await ownConn.execute(sql, [id]) result(null, results[0]); } catch (err) { // Manage Errors console.log("SQL error : ", err); result(err, null); } }; //get departments by customer id export const getDepartmentsByCustomerId = async (customerId, result) => { try { let sql = `SELECT * FROM customerdepartment WHERE customerID = ? ORDER BY name ASC`; const results = await ownConn.query(sql, [customerId]) result(null, results); } catch (err) { // Manage Errors console.log("SQL error : ", err); result(err, null); } }; // Update department to Database export const updateDepartmentById = async (data, result) => { const id = data.primaryID; try { let sql = `UPDATE customerdepartment SET name = ?, head = ?, notes = ? WHERE primaryID = ?`; const results = await ownConn.query(sql, [data.name, data.head, data.notes, id]); results.insertId = results.insertId.toString(); result(null, results); } catch (err) { // Manage Errors console.log("SQL error : ", err); result(err, null); } }; //insert department to databased export const insertDepartment = async (data, result) => { try { const results = await ownConn.query(`INSERT INTO customerdepartment(customerID, customername, name, head, notes) VALUES(?, ?, ?, ?, ?)`, [data.customerID, data.customername, data.name, data.head, data.notes]) results.insertId = results.insertId.toString(); result(null, results); } catch (err) { // Manage Errors console.log("SQL error : ", err); result(err, null); } }; //get all selected departments by name export const getSelectedDepartmentsByName = async (selected, result) => { try { let sql = `SELECT * FROM customerdepartment WHERE LOWER(name) LIKE '%${selected.toLowerCase()}%' ORDER BY name ASC`; const results = await ownConn.execute(sql) result(null, results); } catch (err) { // Manage Errors console.log("SQL error : ", err); result(err, null); } }; // Delete department to Database export const deleteDepartmentById = async (id, result) => { try { let sql = `DELETE FROM customerdepartment WHERE primaryID = ?`; const results = await ownConn.query(sql, [id]) results.insertId = results.insertId.toString(); result(null, results); } catch (err) { // Manage Errors console.log("SQL error : ", err); result(err, null); } }; //get single department by name export const getDepartmentByName = async (id, result) => { try { let sql = `SELECT * FROM customerdepartment WHERE name = ?`; const results = await ownConn.execute(sql, [id]) result(null, results[0]); } catch (err) { // Manage Errors console.log("SQL error : ", err); result(err, null); } };