floranet/api/db/db.js

98 lines
2.6 KiB
JavaScript

async function connect() {
if (global.connection && global.connection.state !== 'disconnected')
return global.connection;
const host = process.env.HOST;
const port = process.env.PORT;
const database = process.env.DATABASE;
const user = process.env.DB_USER;
const password = process.env.DB_PASSWORD;
const mysql = require("mysql2/promise");
const connection = await mysql.createConnection({host: host,port: port,user: user,password: password,database: database});
global.connection = connection;
return connection;
}
//Procedure for get products
async function getProducts(dateExpired, postalCode) {
const conn = await connect();
const [rows] = await conn.query(`CALL catalogue_get("${dateExpired}", "${postalCode}")`);
return rows;
}
async function getProductById(id) {
const conn = await connect();
const rows = await conn.query(`CALL catalogue_findById(${id})`);
return rows;
}
//Procedure for create transactions, do not carry out any manipulation at the bank
async function orderData_put(jsonOrderData) {
const conn = await connect();
const [rows] = await conn.query(`CALL order_put(?)`, [jsonOrderData], (err, results) => {
if (err) {
console.error(err);
} else {
}
});
return rows;
}
async function order_confirm(orderFk) {
const conn = await connect();
const [rows] = await conn.query(`CALL order_confirm(${orderFk})`);
return rows;
}
//Procedure for get transactions, do not carry out any manipulation at the bank
async function orderData_get() {
const conn = await connect();
const [rows] = await conn.query(`CALL orderData_get()`);
return rows;
}
async function getProvinces() {
const conn = await connect();
const [rows] = await conn.query(`SELECT p.id, p.name, c.code, c.country
FROM vn.province p
JOIN vn.country c ON c.id = p.countryFk
WHERE c.country IN('España', 'Francia', 'Portugal')`);
return rows;
}
async function deliveryDate_get(postalCode) {
const conn = await connect();
const [rows] = await conn.query(`CALL deliveryDate_get("${postalCode}")`);
return rows;
}
async function contact_Request(name, phone, email, message) {
const conn = await connect();
const [rows] = await conn.query(`CALL contact_request("${name}", "${phone}", "${email}", "${message}")`);
return rows;
}
async function findAll_banner() {
const conn = await connect();
const [rows] = await conn.query(`CALL sliders_get`);
return rows;
}
module.exports = {
getProducts,
getProductById,
orderData_get,
orderData_put,
getProvinces,
deliveryDate_get,
contact_Request,
order_confirm,
findAll_banner
}