Aggiornamento cataloghi...
This commit is contained in:
@@ -4,6 +4,8 @@ const server_constants = require('../tools/server_constants');
|
||||
|
||||
const shared_consts = require('../tools/shared_nodejs');
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
const SERVER_A_URL = process.env.SERVER_A_URL || "http://IP_DI_SERVER_A:3000";
|
||||
const API_KEY = process.env.API_KEY_MSSQL;
|
||||
|
||||
@@ -18,7 +20,9 @@ const getArticlesSales = async () => {
|
||||
COALESCE(o.totVen, 0) as totVen, COALESCE(u.totFat, 0) as totFat,
|
||||
COALESCE(p.rank3M, 0) as rank3M, COALESCE(t.fatrank3M, 0) as fatrank3M,
|
||||
COALESCE(q.rank6M, 0) as rank6M, COALESCE(r.rank1Y, 0) as rank1Y,
|
||||
COALESCE(t.fat3mesi, 0) as fatLast3M, COALESCE(p.venduti3mesi, 0) as vLast3M,
|
||||
COALESCE(t.fat3mesi, 0) as fatLast3M,
|
||||
COALESCE(t.fat6mesi, 0) as fatLast6M,
|
||||
COALESCE(p.venduti3mesi, 0) as vLast3M,
|
||||
COALESCE(q.venduti6mesi, 0) as vLast6M, COALESCE(r.venduti1anno, 0) as vLastY,
|
||||
s.ultimoOrdine as dataUltimoOrdine
|
||||
FROM T_WEB_Articoli a
|
||||
@@ -82,7 +86,7 @@ const getTableContent = async (options) => {
|
||||
const checkResponse = await axios.post(SERVER_A_URL + '/query', { query: checkTableQuery }, {
|
||||
headers: { 'x-api-key': API_KEY }
|
||||
});
|
||||
if (!checkResponse.data || checkResponse.data.length === 0 || checkResponse.data[0].tableExists === 0) {
|
||||
if (!checkResponse?.data || checkResponse?.data.length === 0 || checkResponse?.data[0].tableExists === 0) {
|
||||
return `La tabella '${options.nameTable}' non esiste.`;
|
||||
}
|
||||
|
||||
@@ -115,7 +119,7 @@ const getTableContent = async (options) => {
|
||||
if (options.nameTable.toLowerCase() === 't_web_articoli') {
|
||||
if (true) {
|
||||
dataQuery = `
|
||||
SELECT TOP ${options.numrec}
|
||||
SELECT TOP ${options.numrec || 10000}
|
||||
${columnsToShow}
|
||||
` + (options.campispeciali ? `
|
||||
,f.DescrizioneStatoProdotto
|
||||
@@ -236,18 +240,20 @@ const getTableContent = async (options) => {
|
||||
`;
|
||||
}
|
||||
} else {
|
||||
dataQuery = `SELECT TOP ${options.numrec} * FROM ${options.nameTable} `;
|
||||
dataQuery = `SELECT TOP ${options.numrec || 10000} * FROM ${options.nameTable} `;
|
||||
}
|
||||
if (options.where && options.where.trim() !== "") {
|
||||
dataQuery += ` WHERE ${options.where} `;
|
||||
}
|
||||
|
||||
console.log('dataQuery', dataQuery);
|
||||
|
||||
// Esegue la query per recuperare i dati
|
||||
// console.log('dataQuery', dataQuery);
|
||||
const dataResponse = await axios.post(SERVER_A_URL + '/query', { query: dataQuery }, {
|
||||
headers: { 'x-api-key': API_KEY }
|
||||
});
|
||||
const records = dataResponse.data;
|
||||
const records = dataResponse?.data;
|
||||
if (!records || records.length === 0) {
|
||||
return `Nessun record trovato nella tabella '${options.nameTable}'.`;
|
||||
}
|
||||
@@ -355,6 +361,7 @@ const getTableContent = async (options) => {
|
||||
return output;
|
||||
|
||||
} catch (error) {
|
||||
output = error.message;
|
||||
console.error("Errore nel recupero della tabella: ", error.message);
|
||||
if (options.outhtml) {
|
||||
output = `
|
||||
@@ -409,6 +416,200 @@ const getTableContent = async (options) => {
|
||||
}
|
||||
};
|
||||
|
||||
const setTableContent = async (options) => {
|
||||
try {
|
||||
// checkPermissions()
|
||||
|
||||
const esegui = true
|
||||
|
||||
if (esegui) {
|
||||
|
||||
// Verifica se la tabella esiste
|
||||
const checkTableQuery = `SELECT COUNT(*) as tableExists FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '${options.nameTable}'`;
|
||||
const checkResponse = await axios.post(SERVER_A_URL + '/query', { query: checkTableQuery }, {
|
||||
headers: { 'x-api-key': API_KEY }
|
||||
});
|
||||
|
||||
if (!checkResponse.data || checkResponse.data.length === 0 || checkResponse.data[0].tableExists === 0) {
|
||||
return `La tabella '${options.nameTable}' non esiste.`;
|
||||
}
|
||||
|
||||
// Costruisce la query per inserire o aggiornare i record
|
||||
let dataQuery = "";
|
||||
if (options.insertMode) {
|
||||
// Modalità INSERT
|
||||
const columns = Object.keys(options.data);
|
||||
const values = columns.map(col => `'${options.data[col]}'`).join(", ");
|
||||
dataQuery = `
|
||||
INSERT INTO ${options.nameTable} (${columns.join(", ")})
|
||||
VALUES (${values});
|
||||
`;
|
||||
} else {
|
||||
// Modalità UPDATE
|
||||
const updateFields = Object.keys(options.data)
|
||||
.map(col => `${col} = '${options.data[col]}'`)
|
||||
.join(", ");
|
||||
const whereClause = options.where ? `WHERE ${options.where}` : "";
|
||||
dataQuery = `
|
||||
UPDATE ${options.nameTable}
|
||||
SET ${updateFields}
|
||||
${whereClause};
|
||||
`;
|
||||
}
|
||||
|
||||
console.log('dataQuery', dataQuery);
|
||||
|
||||
// Esegue la query per inserire o aggiornare i dati
|
||||
const dataResponse = await axios.post(SERVER_A_URL + '/query', { query: dataQuery }, {
|
||||
headers: { 'x-api-key': API_KEY }
|
||||
});
|
||||
|
||||
if (dataResponse.data && dataResponse.data.affectedRows > 0) {
|
||||
return `Operazione completata con successo su '${options.nameTable}'.`;
|
||||
} else {
|
||||
return `Nessun record modificato nella tabella '${options.nameTable}'.`;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Errore nell'inserimento o aggiornamento della tabella: ", error.message);
|
||||
if (options.outhtml) {
|
||||
output = `
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Errore nell'inserimento o aggiornamento della Tabella ${options.nameTable}</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
background-color: #f7f7f7;
|
||||
color: #333;
|
||||
padding: 20px;
|
||||
}
|
||||
.error-container {
|
||||
background-color: #ffe6e6;
|
||||
border: 1px solid #ff4d4d;
|
||||
padding: 20px;
|
||||
border-radius: 5px;
|
||||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||
max-width: 800px;
|
||||
margin: auto;
|
||||
}
|
||||
.error-title {
|
||||
font-size: 24px;
|
||||
font-weight: bold;
|
||||
color: #d8000c;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.error-message {
|
||||
font-size: 16px;
|
||||
white-space: pre-wrap;
|
||||
background: #fff;
|
||||
padding: 10px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 3px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="error-container">
|
||||
<div class="error-title">Errore nell'inserimento o aggiornamento della Tabella ${options.nameTable}</div>
|
||||
<div class="error-message">
|
||||
${error.response.data.error || error.stack || error.message}
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
`;
|
||||
return output;
|
||||
}
|
||||
return "Errore nell'inserimento o aggiornamento della tabella.";
|
||||
}
|
||||
};
|
||||
const checkPermissions = async (options) => {
|
||||
try {
|
||||
const dataQuery = `
|
||||
SELECT
|
||||
dp.name AS UserName,
|
||||
dp.type_desc AS UserType,
|
||||
o.name AS ObjectName,
|
||||
p.permission_name,
|
||||
p.state_desc AS PermissionState
|
||||
FROM
|
||||
sys.database_permissions p
|
||||
JOIN
|
||||
sys.objects o ON p.major_id = o.object_id
|
||||
JOIN
|
||||
sys.database_principals dp ON p.grantee_principal_id = dp.principal_id
|
||||
WHERE
|
||||
o.name = 'T_WEB_Articoli';
|
||||
`;
|
||||
|
||||
console.log('checkPermissions query:', dataQuery);
|
||||
|
||||
// Esegue la query per inserire o aggiornare i dati
|
||||
const dataResponse = await axios.post(SERVER_A_URL + '/query', { query: dataQuery }, {
|
||||
headers: { 'x-api-key': API_KEY }
|
||||
});
|
||||
|
||||
console.log('checkPermissions result:', dataResponse.data);
|
||||
|
||||
if (dataResponse.data && dataResponse.data.affectedRows > 0) {
|
||||
return `Operazione completata con successo.`;
|
||||
} else {
|
||||
return `Nessun permesso.`;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Errore nel check dei Permessi: ", error.message);
|
||||
if (options.outhtml) {
|
||||
output = `
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Errore nell'inserimento o aggiornamento della Tabella ${options.nameTable}</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
background-color: #f7f7f7;
|
||||
color: #333;
|
||||
padding: 20px;
|
||||
}
|
||||
.error-container {
|
||||
background-color: #ffe6e6;
|
||||
border: 1px solid #ff4d4d;
|
||||
padding: 20px;
|
||||
border-radius: 5px;
|
||||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||
max-width: 800px;
|
||||
margin: auto;
|
||||
}
|
||||
.error-title {
|
||||
font-size: 24px;
|
||||
font-weight: bold;
|
||||
color: #d8000c;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.error-message {
|
||||
font-size: 16px;
|
||||
white-space: pre-wrap;
|
||||
background: #fff;
|
||||
padding: 10px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 3px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="error-container">
|
||||
<div class="error-title">Errore nell'inserimento o aggiornamento della Tabella ${options.nameTable}</div>
|
||||
<div class="error-message">
|
||||
${error.response.data.error || error.stack || error.message}
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
`;
|
||||
return output;
|
||||
}
|
||||
return "Errore nell'inserimento o aggiornamento della tabella.";
|
||||
}
|
||||
};
|
||||
|
||||
// Endpoint per mostrare i dati della tabella
|
||||
exports.viewTable = async (req, res) => {
|
||||
try {
|
||||
@@ -429,6 +630,12 @@ exports.viewTable = async (req, res) => {
|
||||
out = tableContent;
|
||||
}
|
||||
|
||||
if (tableContent && tableContent.length > 0) {
|
||||
if (options.updatelocaldb) {
|
||||
this.updateLocalDb(tableContent[0], options)
|
||||
}
|
||||
}
|
||||
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, data: out });
|
||||
|
||||
} catch (error) {
|
||||
@@ -437,6 +644,82 @@ exports.viewTable = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
exports.updateLocalDb = async (tableContent, options) => {
|
||||
try {
|
||||
|
||||
const ProductInfo = require('../models/productInfo');
|
||||
const CatProd = require('../models/catprod');
|
||||
|
||||
let recproductInfo = {
|
||||
code: tableContent.Ean13.trim(),
|
||||
};
|
||||
|
||||
let risrecUpdated = null;
|
||||
|
||||
const recfound = await ProductInfo.findOne({ code: recproductInfo.code }).lean();
|
||||
if (recfound) {
|
||||
|
||||
ListaArgomenti = tableContent.ListaArgomenti;
|
||||
|
||||
let arrayPulito = ListaArgomenti
|
||||
.trim() // Rimuove gli spazi all'inizio e alla fine
|
||||
.replace(/[\(\)]/g, '') // Rimuove le parentesi tonde
|
||||
.split(','); // Divide la stringa in un array usando la virgola come separatore
|
||||
|
||||
if (arrayPulito && arrayPulito.length > 0) {
|
||||
let aggiornacat = false;
|
||||
const precCatProds = recfound.idCatProds;
|
||||
let reccatprods = [];
|
||||
for (let i = 0; i < arrayPulito.length; i++) {
|
||||
const idArgomento = parseInt(arrayPulito[i]);
|
||||
reccateg = await CatProd.findOne({ idArgomento }).lean();
|
||||
|
||||
if (reccateg) {
|
||||
// aggiungi solo se non esiste già
|
||||
if (!reccatprods.includes(reccateg._id)) {
|
||||
reccatprods.push(reccateg._id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ora controlla se l'array reccatprods e' diverso da precCatProds
|
||||
if (reccatprods.length !== precCatProds.length) {
|
||||
aggiornacat = true;
|
||||
} else {
|
||||
for (let i = 0; i < reccatprods.length; i++) {
|
||||
if (reccatprods[i].toString() !== precCatProds[i].toString()) {
|
||||
aggiornacat = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (aggiornacat) {
|
||||
recproductInfo.idCatProds = reccatprods;
|
||||
aggiorna = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (tableContent.DataPubblicazione.trim()) {
|
||||
recproductInfo.date_pub = new Date(tools.convertiDataItaliana(tableContent.DataPubblicazione.trim()).date);
|
||||
// convert data to timestamp
|
||||
recproductInfo.date_pub_ts = recproductInfo.date_pub.getTime();
|
||||
aggiorna = true;
|
||||
}
|
||||
|
||||
if (aggiorna) {
|
||||
risrecUpdated = await ProductInfo.findOneAndUpdate({ code: recproductInfo.code }, { $set: recproductInfo }, { new: true, upsert: true });
|
||||
}
|
||||
|
||||
return risrecUpdated;
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.error('Error: ', e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Endpoint per mostrare i dati della tabella
|
||||
exports.queryTable = async (req, res) => {
|
||||
try {
|
||||
@@ -462,3 +745,30 @@ exports.queryTable = async (req, res) => {
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, error });
|
||||
}
|
||||
};
|
||||
// Endpoint per salvare i dati di una tabella
|
||||
exports.saveTable = async (req, res) => {
|
||||
try {
|
||||
const options = req.body.options;
|
||||
const tableContent = await setTableContent(options);
|
||||
|
||||
let out = {};
|
||||
|
||||
if (options.outhtml) {
|
||||
out = `
|
||||
<h2 class="text-center">Tabella: ${options.nameTable}</h2>
|
||||
<div class="text-h7 row justify-center text-blue">Query: ${options.where}<br></div>
|
||||
<div class="row justify-center">
|
||||
${tableContent}
|
||||
</div>
|
||||
`
|
||||
} else {
|
||||
out = tableContent;
|
||||
}
|
||||
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, data: out });
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error: ', error);
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, error });
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user