- corretto import XLS...
This commit is contained in:
@@ -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,44 +356,94 @@ 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({
|
||||||
}).exec();
|
code: field.toUpperCase(),
|
||||||
if (productInfo) {
|
$or: [{ deleted: false }, { deleted: { $exists: false } }],
|
||||||
product = await Product.findOne({ idProductInfo: productInfo._id }).exec();
|
}).exec();
|
||||||
if (product) {
|
|
||||||
const existingResult = results.find((r) => r._id.toString() === product._id.toString());
|
// Priorità se lo trovo per ISBN:
|
||||||
if (!existingResult) {
|
if (productInfoarrISBN.length === 1) {
|
||||||
let titolo = productInfo.name;
|
productInfo = productInfoarrISBN[0];
|
||||||
results.push({
|
trovatoISBN = true;
|
||||||
...product,
|
trovato = true;
|
||||||
productInfo,
|
}
|
||||||
_id: product._id,
|
}
|
||||||
title: titolo,
|
if (!trovatoISBN && !trovato) {
|
||||||
isbn: product.isbn,
|
// Prima cerca se è esattamente cosi
|
||||||
authors: await Promise.all(
|
let productInfoarrTitle = await ProductInfo.find({
|
||||||
productInfo.idAuthors.map(async (authorId) => {
|
$or: [{ deleted: false }, { deleted: { $exists: false } }],
|
||||||
const author = await Author.findById(authorId).exec();
|
name: field,
|
||||||
return author ? `${author.name} ${author.surname}`.trim() : '';
|
}).exec();
|
||||||
})
|
if (productInfoarrTitle.length === 1) {
|
||||||
),
|
productInfo = productInfoarrTitle[0];
|
||||||
select: true,
|
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
|
res.status(200).json(results); // Restituisci i risultati trovati
|
||||||
|
|||||||
Reference in New Issue
Block a user