- Categorie

- ProductInfo
This commit is contained in:
Surya Paolo
2023-12-27 02:58:15 +01:00
parent f0495d93b3
commit 15d831eecc
16 changed files with 516 additions and 230 deletions

View File

@@ -6,6 +6,7 @@ const tools = require('../tools/general');
const Producer = require('../models/producer');
const Storehouse = require('../models/storehouse');
const Provider = require('../models/provider');
const CatProd = require('../models/catprod');
const Gasordine = require('../models/gasordine');
const Scontistica = require('../models/scontistica');
@@ -31,6 +32,7 @@ const productSchema = new Schema({
type: Boolean,
default: true,
},
idProductInfo: { type: Schema.Types.ObjectId, ref: 'ProductInfo' },
idProducer: { type: Schema.Types.ObjectId, ref: 'Producer' },
idStorehouses: [
{ type: Schema.Types.ObjectId, ref: 'Storehouse' }
@@ -42,29 +44,6 @@ const productSchema = new Schema({
{ type: Schema.Types.ObjectId, ref: 'Scontistica' }
],
idProvider: { type: Schema.Types.ObjectId, ref: 'Provider' },
code: {
type: String,
unique: true,
required: true,
},
codice_EAN: {
type: String,
},
barcode: {
type: String,
},
name: {
type: String,
},
description: {
type: String,
},
department: {
type: String, ref: 'Department'
},
category: {
type: Array,
},
prezzo_ivato: { // Con IVA
type: Number
},
@@ -82,26 +61,15 @@ const productSchema = new Schema({
after_price: {
type: String
},
color: {
type: String
},
size: {
type: String
},
weight: {
type: Number
},
vegan: {
type: Boolean
},
unit: {
type: Number
},
minBuyQty: { // quantità minima acquistabile
type: Number,
default: 1,
required: true,
},
maxBookableQty: { // quantità massima Pre-ordinabile (singolarmente)
type: Number,
default: 0,
},
stockQty: { // in magazzino
type: Number,
default: 0,
@@ -133,30 +101,6 @@ const productSchema = new Schema({
dateAvailableFrom: {
type: Date
},
icon: {
type: String,
},
img: {
type: String,
},
link: {
type: String,
},
img2: {
type: String,
},
img3: {
type: String,
},
ingredienti: {
type: String,
},
valori_nutrizionali: {
type: String,
},
note: {
type: String,
},
producer_name: {
type: String,
},
@@ -166,6 +110,9 @@ const productSchema = new Schema({
magazzino_name: {
type: String,
},
cat_name: {
type: String,
},
});
var Product = module.exports = mongoose.model('Product', productSchema);
@@ -244,6 +191,20 @@ module.exports.findAllIdApp = async function (idapp, code, id) {
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'productinfos',
localField: 'idProductInfo',
foreignField: '_id',
as: 'productInfo'
}
},
{
$unwind: {
path: '$productInfo',
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'gasordines',
@@ -300,6 +261,14 @@ module.exports.findAllIdApp = async function (idapp, code, id) {
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'catprods',
localField: 'idCatProds',
foreignField: '_id',
as: 'catprods'
}
},
{
$lookup: {
from: 'scontisticas',
@@ -410,7 +379,11 @@ module.exports.findAllIdApp = async function (idapp, code, id) {
{
$unset: 'productPreOrders'
},
{
$sort: {
'productInfo.name': 1 // 1 for ascending order, -1 for descending order
}
},
);
// console.log('query=', query);
@@ -436,7 +409,7 @@ module.exports.getProductByDepartment = function (query, sort, callback) {
Product.find(query, null, sort, callback)
}
module.exports.getProductByCategory = function (query, sort, callback) {
module.exports.getProductByCatProd = function (query, sort, callback) {
Product.find(query, null, sort, callback)
}
@@ -450,9 +423,9 @@ module.exports.filterProductByDepartment = function (department, callback) {
Product.find(query, callback)
}
module.exports.filterProductByCategory = function (category, callback) {
let regexp = new RegExp(`^${category}$`, 'i')
var query = { category: { $regex: regexp } };
module.exports.filterProductByCatProd = function (catprod, callback) {
let regexp = new RegExp(`^${catprod}$`, 'i')
var query = { catprod: { $regex: regexp } };
Product.find(query, callback);
}
@@ -487,16 +460,20 @@ module.exports.convertAfterImport = async function (idapp, dataObjects) {
};
module.exports.singlerecconvert_AfterImport = async function (idapp, prod) {
module.exports.singlerecconvert_AfterImport = async function (idapp, prod, isnuovo) {
let setta = false;
try {
let objtoset = {}
// Impostazioni Base:
let objtoset = {
idapp,
img: 'upload/products/' + prod.code + '.jpg',
minBuyQty: 1,
if (isnuovo) {
objtoset = {
idapp,
minBuyQty: 1,
maxBookableQty: 0,
}
}
if (prod.producer_name) {
@@ -569,8 +546,17 @@ module.exports.singlerecconvert_AfterImport = async function (idapp, prod) {
}
}
if (setta) {
const ris = await Product.findOneAndUpdate({ _id: ObjectID(prod._id) }, { $set: objtoset })
if (!tools.isObjectEmpty(objtoset)) {
ris = await Product.findOneAndUpdate({ _id: ObjectID(prod._id) }, { $set: objtoset })
const objDelete = {
cat_name: 1,
producer_name: 1,
provider_name: 1,
magazzino_name: 1,
};
ris = await Product.updateOne({ _id: ObjectID(prod._id) }, { $unset: objDelete })
if (ris) {
console.log('ris', ris);