- corretto import XLS...

This commit is contained in:
Surya Paolo
2025-07-11 15:03:01 +02:00
parent efd1d12ba7
commit 87c8bf4c45

View File

@@ -13,6 +13,8 @@ const Product = require('../models/product');
const ProductInfo = require('../models/productInfo'); const ProductInfo = require('../models/productInfo');
const Author = require('../models/author'); const Author = require('../models/author');
const tools = require('../tools/general');
router.post('/test-lungo', authenticate, (req, res) => { router.post('/test-lungo', authenticate, (req, res) => {
const timeout = req.body.timeout; const timeout = req.body.timeout;
@@ -337,6 +339,11 @@ router.post('/api/convert-csv-to-xls', upload.single('csv'), (req, res) => {
} }
}); });
// Funzione per "escapare" i caratteri speciali
const escapeRegExp = (string) => {
return string.replace(/[.*+?^=!:${}()|\[\]\/\\]/g, '\\$&'); // Escape dei caratteri speciali
};
// API per la ricerca dei libri // API per la ricerca dei libri
router.post('/search-books', authenticate, async (req, res) => { router.post('/search-books', authenticate, async (req, res) => {
const { books } = req.body; const { books } = req.body;
@@ -349,18 +356,69 @@ router.post('/search-books', authenticate, async (req, res) => {
// Crea un array per raccogliere i risultati // Crea un array per raccogliere i risultati
let results = []; let results = [];
let productInfo = null;
let product = null; let product = null;
for (const book of books) { for (const book of books) {
for (const field of book) { let trovatoISBN = false;
let trovato = false;
for (let field of book) {
field = field.trim();
let valido = typeof field === 'string' && field.length > 4 && field.length < 50; let valido = typeof field === 'string' && field.length > 4 && field.length < 50;
if (valido) { if (valido) {
// Cerca il primo record che corrisponde per ISBN o titolo // Cerca il primo record che corrisponde per ISBN o titolo
if (true) { if (true) {
productInfo = await ProductInfo.findOne({ if (!trovatoISBN) {
$or: [{ code: field.toUpperCase() }, { name: new RegExp(`.*${field.toUpperCase()}.*`, 'i') }], let productInfoarrISBN = await ProductInfo.find({
code: field.toUpperCase(),
$or: [{ deleted: false }, { deleted: { $exists: false } }],
}).exec(); }).exec();
// Priorità se lo trovo per ISBN:
if (productInfoarrISBN.length === 1) {
productInfo = productInfoarrISBN[0];
trovatoISBN = true;
trovato = true;
}
}
if (!trovatoISBN && !trovato) {
// Prima cerca se è esattamente cosi
let productInfoarrTitle = await ProductInfo.find({
$or: [{ deleted: false }, { deleted: { $exists: false } }],
name: field,
}).exec();
if (productInfoarrTitle.length === 1) {
productInfo = productInfoarrTitle[0];
trovato = true;
} else {
if (productInfoarrTitle.length > 1) {
// Prendi l'Ultimo !
productInfo = productInfoarrTitle[productInfoarrTitle.length - 1];
trovato = true;
}
}
if (!trovato) {
// Altrimenti per Titolo
productInfoarrTitle = await ProductInfo.find({
$or: [{ deleted: false }, { deleted: { $exists: false } }],
name: new RegExp(`.*${escapeRegExp(tools.removeAccents(field.toUpperCase()))}.*`, 'i'),
}).exec();
if (productInfoarrTitle.length === 1) {
productInfo = productInfoarrTitle[0];
trovato = true;
} else {
if (productInfoarrTitle.length > 1) {
// Prendi l'Ultimo !
productInfo = productInfoarrTitle[productInfoarrTitle.length - 1];
trovato = true;
}
}
}
}
}
}
}
if (trovato) {
if (productInfo) { if (productInfo) {
product = await Product.findOne({ idProductInfo: productInfo._id }).exec(); product = await Product.findOne({ idProductInfo: productInfo._id }).exec();
if (product) { if (product) {
@@ -385,8 +443,7 @@ router.post('/search-books', authenticate, async (req, res) => {
} }
} }
} }
} //
}
} }
res.status(200).json(results); // Restituisci i risultati trovati res.status(200).json(results); // Restituisci i risultati trovati