- Modifiche a ProductInfo... Continua
This commit is contained in:
@@ -10,7 +10,6 @@ const XLSX = require('xlsx');
|
||||
const upload = multer({ dest: 'uploads/' });
|
||||
|
||||
const Product = require('../models/product');
|
||||
const ProductInfo = require('../models/productInfo');
|
||||
const Author = require('../models/author');
|
||||
|
||||
const tools = require('../tools/general');
|
||||
@@ -370,8 +369,8 @@ router.post('/search-books', authenticate, async (req, res) => {
|
||||
// Cerca il primo record che corrisponde per ISBN o titolo
|
||||
if (true) {
|
||||
if (!trovatoISBN) {
|
||||
let productInfoarrISBN = await ProductInfo.find({
|
||||
code: field.toUpperCase(),
|
||||
let productInfoarrISBN = await Product.find({
|
||||
'productInfo.code': field.toUpperCase(),
|
||||
$or: [{ deleted: false }, { deleted: { $exists: false } }],
|
||||
}).exec();
|
||||
|
||||
@@ -384,33 +383,33 @@ router.post('/search-books', authenticate, async (req, res) => {
|
||||
}
|
||||
if (!trovatoISBN && !trovato) {
|
||||
// Prima cerca se è esattamente cosi
|
||||
let productInfoarrTitle = await ProductInfo.find({
|
||||
let productarrTitle = await Product.find({
|
||||
$or: [{ deleted: false }, { deleted: { $exists: false } }],
|
||||
name: field,
|
||||
'productInfo.name': field,
|
||||
}).exec();
|
||||
if (productInfoarrTitle.length === 1) {
|
||||
productInfo = productInfoarrTitle[0];
|
||||
if (productarrTitle.length === 1) {
|
||||
productInfo = productarrTitle[0];
|
||||
trovato = true;
|
||||
} else {
|
||||
if (productInfoarrTitle.length > 1) {
|
||||
if (productarrTitle.length > 1) {
|
||||
// Prendi l'Ultimo !
|
||||
productInfo = productInfoarrTitle[productInfoarrTitle.length - 1];
|
||||
productInfo = productarrTitle[productarrTitle.length - 1];
|
||||
trovato = true;
|
||||
}
|
||||
}
|
||||
if (!trovato) {
|
||||
// Altrimenti per Titolo
|
||||
productInfoarrTitle = await ProductInfo.find({
|
||||
productarrTitle = await Product.find({
|
||||
$or: [{ deleted: false }, { deleted: { $exists: false } }],
|
||||
name: new RegExp(`.*${escapeRegExp(tools.removeAccents(field.toUpperCase()))}.*`, 'i'),
|
||||
'productInfo.name': new RegExp(`.*${escapeRegExp(tools.removeAccents(field.toUpperCase()))}.*`, 'i'),
|
||||
}).exec();
|
||||
if (productInfoarrTitle.length === 1) {
|
||||
productInfo = productInfoarrTitle[0];
|
||||
if (productarrTitle.length === 1) {
|
||||
productInfo = productarrTitle[0];
|
||||
trovato = true;
|
||||
} else {
|
||||
if (productInfoarrTitle.length > 1) {
|
||||
if (productarrTitle.length > 1) {
|
||||
// Prendi l'Ultimo !
|
||||
productInfo = productInfoarrTitle[productInfoarrTitle.length - 1];
|
||||
productInfo = productarrTitle[productarrTitle.length - 1];
|
||||
trovato = true;
|
||||
}
|
||||
}
|
||||
@@ -426,15 +425,14 @@ router.post('/search-books', authenticate, async (req, res) => {
|
||||
if (product) {
|
||||
const existingResult = results.find((r) => r._id.toString() === product._id.toString());
|
||||
if (!existingResult) {
|
||||
let titolo = productInfo.name;
|
||||
let titolo = product.productInfo.name;
|
||||
results.push({
|
||||
...product,
|
||||
productInfo,
|
||||
_id: product._id,
|
||||
title: titolo,
|
||||
isbn: product.isbn,
|
||||
authors: await Promise.all(
|
||||
productInfo.idAuthors.map(async (authorId) => {
|
||||
product.productInfo.idAuthors.map(async (authorId) => {
|
||||
const author = await Author.findById(authorId).exec();
|
||||
return author ? `${author.name} ${author.surname}`.trim() : '';
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user