- Import di un file XLS contenente una lista di libri, all'interno di un catalogo.
This commit is contained in:
@@ -9,6 +9,9 @@ const XLSX = require('xlsx');
|
|||||||
|
|
||||||
const upload = multer({ dest: 'uploads/' });
|
const upload = multer({ dest: 'uploads/' });
|
||||||
|
|
||||||
|
const Product = require('../models/product');
|
||||||
|
const ProductInfo = require('../models/productInfo');
|
||||||
|
const Author = require('../models/author');
|
||||||
|
|
||||||
router.post('/test-lungo', authenticate, (req, res) => {
|
router.post('/test-lungo', authenticate, (req, res) => {
|
||||||
const timeout = req.body.timeout;
|
const timeout = req.body.timeout;
|
||||||
@@ -304,7 +307,7 @@ router.get('/pageviews/weekly-top-pages', authenticate_noerror, async (req, res)
|
|||||||
router.post('/api/convert-csv-to-xls', upload.single('csv'), (req, res) => {
|
router.post('/api/convert-csv-to-xls', upload.single('csv'), (req, res) => {
|
||||||
try {
|
try {
|
||||||
const csvFilePath = req.file.path;
|
const csvFilePath = req.file.path;
|
||||||
|
|
||||||
// Leggi il CSV con SheetJS
|
// Leggi il CSV con SheetJS
|
||||||
const csvData = fs.readFileSync(csvFilePath, 'utf-8');
|
const csvData = fs.readFileSync(csvFilePath, 'utf-8');
|
||||||
const worksheet = XLSX.utils.csv_to_sheet(csvData);
|
const worksheet = XLSX.utils.csv_to_sheet(csvData);
|
||||||
@@ -323,15 +326,74 @@ router.post('/api/convert-csv-to-xls', upload.single('csv'), (req, res) => {
|
|||||||
console.error('Errore nel download del file:', err);
|
console.error('Errore nel download del file:', err);
|
||||||
res.status(500).send('Errore nel download del file');
|
res.status(500).send('Errore nel download del file');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pulisci il file temporaneo
|
// Pulisci il file temporaneo
|
||||||
fs.unlinkSync(csvFilePath);
|
fs.unlinkSync(csvFilePath);
|
||||||
fs.unlinkSync(xlsFilePath);
|
fs.unlinkSync(xlsFilePath);
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Errore nella conversione del file:", error);
|
console.error('Errore nella conversione del file:', error);
|
||||||
res.status(500).send('Errore nella conversione del file');
|
res.status(500).send('Errore nella conversione del file');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// API per la ricerca dei libri
|
||||||
|
router.post('/search-books', authenticate, async (req, res) => {
|
||||||
|
const { books } = req.body;
|
||||||
|
|
||||||
|
if (!books || books.length === 0) {
|
||||||
|
return res.status(400).json({ error: 'Nessun dato fornito per la ricerca' });
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Crea un array per raccogliere i risultati
|
||||||
|
let results = [];
|
||||||
|
|
||||||
|
let productInfo = null;
|
||||||
|
let product = null;
|
||||||
|
|
||||||
|
for (const book of books) {
|
||||||
|
for (const field of book) {
|
||||||
|
let valido = typeof field === 'string' && field.length > 4 && field.length < 50;
|
||||||
|
if (valido) {
|
||||||
|
// Cerca il primo record che corrisponde per ISBN o titolo
|
||||||
|
if (true) {
|
||||||
|
productInfo = await ProductInfo.findOne({
|
||||||
|
$or: [{ code: field.toUpperCase() }, { name: new RegExp(`.*${field.toUpperCase()}.*`, 'i') }],
|
||||||
|
}).exec();
|
||||||
|
if (productInfo) {
|
||||||
|
product = await Product.findOne({ idProductInfo: productInfo._id }).exec();
|
||||||
|
if (product) {
|
||||||
|
const existingResult = results.find((r) => r._id.toString() === product._id.toString());
|
||||||
|
if (!existingResult) {
|
||||||
|
let titolo = productInfo.name;
|
||||||
|
results.push({
|
||||||
|
...product,
|
||||||
|
productInfo,
|
||||||
|
_id: product._id,
|
||||||
|
title: titolo,
|
||||||
|
isbn: product.isbn,
|
||||||
|
authors: await Promise.all(
|
||||||
|
productInfo.idAuthors.map(async (authorId) => {
|
||||||
|
const author = await Author.findById(authorId).exec();
|
||||||
|
return author ? `${author.name} ${author.surname}`.trim() : '';
|
||||||
|
})
|
||||||
|
),
|
||||||
|
select: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).json(results); // Restituisci i risultati trovati
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Errore durante la ricerca dei libri:', err);
|
||||||
|
res.status(500).json({ error: 'Errore interno del server' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
1.2.63
|
1.2.65
|
||||||
Reference in New Issue
Block a user