- corretto import XLS...
This commit is contained in:
@@ -13,6 +13,8 @@ const Product = require('../models/product');
|
||||
const ProductInfo = require('../models/productInfo');
|
||||
const Author = require('../models/author');
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
router.post('/test-lungo', authenticate, (req, res) => {
|
||||
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
|
||||
router.post('/search-books', authenticate, async (req, res) => {
|
||||
const { books } = req.body;
|
||||
@@ -349,44 +356,94 @@ router.post('/search-books', authenticate, async (req, res) => {
|
||||
// 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 trovatoISBN = false;
|
||||
let trovato = false;
|
||||
for (let field of book) {
|
||||
field = field.trim();
|
||||
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,
|
||||
});
|
||||
if (!trovatoISBN) {
|
||||
let productInfoarrISBN = await ProductInfo.find({
|
||||
code: field.toUpperCase(),
|
||||
$or: [{ deleted: false }, { deleted: { $exists: false } }],
|
||||
}).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) {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user