133 lines
4.2 KiB
JavaScript
133 lines
4.2 KiB
JavaScript
import ownConn from "../dbConfig.js";
|
|
|
|
//get all customers
|
|
export const getCustomers = async (result) => {
|
|
try {
|
|
let sql = `SELECT * FROM customers ORDER BY customername ASC`;
|
|
const results = await ownConn.execute(sql)
|
|
result(null, results);
|
|
}
|
|
catch (err) {
|
|
// Manage Errors
|
|
console.log("SQL error : ", err);
|
|
result(err, null);
|
|
}
|
|
};
|
|
|
|
//get all selected customers
|
|
export const getSelectedCustomers = async (selected, result) => {
|
|
try {
|
|
let sql = `SELECT * FROM customers WHERE customername LIKE '%${selected}%' ORDER BY customername ASC`;
|
|
const results = await ownConn.execute(sql)
|
|
result(null, results);
|
|
}
|
|
catch (err) {
|
|
// Manage Errors
|
|
console.log("SQL error : ", err);
|
|
result(err, null);
|
|
}
|
|
};
|
|
|
|
//get single customer by production order id
|
|
export const getCustomerByProductionOrderId = async (id, result) => {
|
|
try {
|
|
let sql = `SELECT * FROM productionOrders WHERE ticketNumber = ?`;
|
|
const results = await ownConn.execute(sql, [id])
|
|
let sql1 = `SELECT * FROM customers WHERE customerID LIKE '%${results[0].customerID}%'`;
|
|
const results1 = await ownConn.execute(sql1)
|
|
result(null, results1[0]);
|
|
}
|
|
catch (err) {
|
|
// Manage Errors
|
|
console.log("SQL error : ", err);
|
|
result(err, null);
|
|
}
|
|
};
|
|
|
|
//get single customer by maintenance visit id
|
|
export const getCustomerByMaintenanceVisitId = async (id, result) => {
|
|
try {
|
|
let sql = `SELECT * FROM maintenanceVisits WHERE primaryID = ?`;
|
|
const results = await ownConn.execute(sql, [id])
|
|
let sql1 = `SELECT * FROM customers WHERE customerID LIKE '%${results[0].customerID}%'`;
|
|
const results1 = await ownConn.execute(sql1)
|
|
result(null, results1[0]);
|
|
}
|
|
catch (err) {
|
|
// Manage Errors
|
|
console.log("SQL error : ", err);
|
|
result(err, null);
|
|
}
|
|
};
|
|
|
|
//insert customer to databased
|
|
export const insertCustomer = async (data, result) => {
|
|
try {
|
|
const results = await ownConn.query(`INSERT INTO customers(customername, contactperson, EMail, phonenumber, address, postcode, city, notes) VALUES(?, ?, ?, ?, ?, ?, ?, ?)`, [data.customername, data.contactperson, data.EMail, data.phonenumber, data.address, data.postcode, data.city, data.notes])
|
|
result(null, results);
|
|
}
|
|
catch (err) {
|
|
// Manage Errors
|
|
console.log("SQL error : ", err);
|
|
result(err, null);
|
|
}
|
|
};
|
|
|
|
//get single customer by name
|
|
export const getCustomerByCustomerId = async (id, result) => {
|
|
try {
|
|
let sql = `SELECT * FROM customers WHERE customerID = ?`;
|
|
const results = await ownConn.execute(sql, [id])
|
|
result(null, results[0]);
|
|
}
|
|
catch (err) {
|
|
// Manage Errors
|
|
console.log("SQL error : ", err);
|
|
result(err, null);
|
|
}
|
|
};
|
|
|
|
//get single customer by name
|
|
export const getCustomerByName = async (id, result) => {
|
|
try {
|
|
let sql = `SELECT * FROM customers WHERE customername = ?`;
|
|
const results = await ownConn.execute(sql, [id])
|
|
result(null, results[0]);
|
|
}
|
|
catch (err) {
|
|
// Manage Errors
|
|
console.log("SQL error : ", err);
|
|
result(err, null);
|
|
}
|
|
};
|
|
|
|
// Update customer to Database
|
|
export const updateCustomerById = async (data, result) => {
|
|
const id = data.customerID;
|
|
try {
|
|
let sql = `UPDATE customers SET customername = ?, contactperson = ?, EMail = ?, phonenumber = ?, address = ?, postcode = ?, city = ?, notes = ? WHERE customerID = ?`;
|
|
const results = await ownConn.query(sql, [data.customername, data.contactperson, data.EMail, data.phonenumber, data.address, data.postcode, data.city, data.notes, id]);
|
|
result(null, results);
|
|
}
|
|
catch (err) {
|
|
// Manage Errors
|
|
console.log("SQL error : ", err);
|
|
result(err, null);
|
|
}
|
|
};
|
|
|
|
|
|
// Delete customer to Database
|
|
export const deleteCustomerById = async (id, result) => {
|
|
try {
|
|
let sql = `DELETE FROM customers WHERE customerID = ?`;
|
|
const results = await ownConn.query(sql, [id])
|
|
result(null, results);
|
|
}
|
|
catch (err) {
|
|
// Manage Errors
|
|
console.log("SQL error : ", err);
|
|
result(err, null);
|
|
}
|
|
};
|