- 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

56
src/server/models/catprod.js Executable file
View File

@@ -0,0 +1,56 @@
const mongoose = require('mongoose').set('debug', false)
const Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
mongoose.level = "F";
const tools = require('../tools/general');
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
});
const CatProdSchema = new Schema({
idapp: {
type: String,
},
name: {
type: String,
},
img: {
type: String,
},
});
CatProdSchema.statics.getAllCategories = function (callback) {
CatProd.find(callback)
}
CatProdSchema.statics.getCatProdById = function (id, callback) {
CatProd.findById(id, callback);
}
CatProdSchema.statics.getFieldsForSearch = function () {
return [{ field: 'name', type: tools.FieldType.string }]
};
CatProdSchema.statics.executeQueryTable = function (idapp, params) {
return tools.executeQueryTable(this, idapp, params);
};
CatProdSchema.statics.findAllIdApp = async function (idapp) {
const myfind = { idapp };
return await CatProd.find(myfind).sort({ name: 1 });
};
const CatProd = mongoose.model('CatProd', CatProdSchema);
CatProd.createIndexes((err) => {
if (err) throw err;
});
module.exports = CatProd;

View File

@@ -34,6 +34,7 @@ const gasordineSchema = new Schema({
img: {
type: String,
},
dataora_chiusura_ordini: {
type: Date,
},
@@ -43,6 +44,7 @@ const gasordineSchema = new Schema({
dataora_termine_pagamento: {
type: Date,
},
});
var Gasordine = module.exports = mongoose.model('Gasordine', gasordineSchema);

View File

@@ -154,10 +154,24 @@ module.exports.findAllIdApp = async function (idapp) {
as: 'product'
}
},
{
$lookup: {
from: 'productinfos',
localField: 'product.idProduct',
foreignField: '_id',
as: 'product.productInfo'
}
},
{
$unwind: {
path: '$product.productInfo',
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'producers',
localField: 'idProducer',
localField: 'product.idProducer',
foreignField: '_id',
as: 'producer'
}
@@ -165,7 +179,7 @@ module.exports.findAllIdApp = async function (idapp) {
{
$lookup: {
from: 'providers',
localField: 'idProvider',
localField: 'product.idProvider',
foreignField: '_id',
as: 'provider'
}
@@ -178,15 +192,24 @@ module.exports.findAllIdApp = async function (idapp) {
as: 'gasordine'
}
},
{
$unwind: {
path: '$gasordine',
preserveNullAndEmptyArrays: true,
},
},
{
$match: {
'gasordine.active': true
$or: [
{ 'gasordine': { $exists: false } },
{ 'gasordine.active': true }
]
}
},
{
$lookup: {
from: 'scontisticas',
localField: 'idScontisticas',
localField: 'product.idScontisticas',
foreignField: '_id',
as: 'scontistica'
}
@@ -209,12 +232,6 @@ module.exports.findAllIdApp = async function (idapp) {
preserveNullAndEmptyArrays: true,
},
},
{
$unwind: {
path: '$gasordine',
preserveNullAndEmptyArrays: true,
},
}
];
@@ -270,9 +287,9 @@ module.exports.updateTotals = function (order) {
try {
if (!order) {
return;
return;
}
let mypricecalc = 0;
order.TotalPriceProduct = 0;
@@ -342,10 +359,30 @@ module.exports.getTotalOrderById = async function (id) {
as: 'product'
}
},
{
$unwind: {
path: '$product',
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'productinfos',
localField: 'product.idProductInfo',
foreignField: '_id',
as: 'product.productInfo'
}
},
{
$unwind: {
path: '$product.productInfo',
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'producers',
localField: 'idProducer',
localField: 'product.idProducer',
foreignField: '_id',
as: 'producer'
}
@@ -361,7 +398,7 @@ module.exports.getTotalOrderById = async function (id) {
{
$lookup: {
from: 'providers',
localField: 'idProvider',
localField: 'product.idProvider',
foreignField: '_id',
as: 'provider'
}
@@ -374,25 +411,28 @@ module.exports.getTotalOrderById = async function (id) {
as: 'gasordine'
}
},
{
$unwind: {
path: '$gasordine',
preserveNullAndEmptyArrays: true,
},
},
{
$match: {
'gasordine.active': true
$or: [
{ 'gasordine': { $exists: false } },
{ 'gasordine.active': true }
]
}
},
{
$lookup: {
from: 'scontisticas',
localField: 'idScontisticas',
localField: 'product.idScontisticas',
foreignField: '_id',
as: 'scontisticas'
}
},
{
$unwind: {
path: '$product',
preserveNullAndEmptyArrays: true,
},
},
{
$unwind: {
path: '$producer',
@@ -411,12 +451,6 @@ module.exports.getTotalOrderById = async function (id) {
preserveNullAndEmptyArrays: true,
},
},
{
$unwind: {
path: '$gasordine',
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'orders',

View File

@@ -10,6 +10,7 @@ const Storehouse = require('../models/storehouse');
const Provider = require('../models/provider');
const Gasordine = require('../models/gasordine');
const Product = require('../models/product');
const ProductInfo = require('../models/productinfo');
const tools = require('../tools/general');
@@ -178,90 +179,6 @@ module.exports.getOrdersCartById = async function (id) {
}
module.exports.getOLDOrdersCartByQuery = async function (query) {
let myorderscart = await OrdersCart.find(query)
.populate('items.order')
.populate({
path: 'items.order',
populate: {
path: 'idProduct',
model: 'Product'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idProducer',
model: 'Producer'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idProvider',
model: 'Provider'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idGasordine',
model: 'Gasordine'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idStorehouse',
model: 'Storehouse'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idScontisticas',
model: 'Scontistica'
},
})
.populate({
path: 'userId',
model: 'User',
select: '_id name surname username profile', // Specify the fields you want to retrieve
})
.lean();
myorderscart = myorderscart.map(order => {
order.user = order.userId
order.userId = order.user._id
order.items = order.items.map(item => {
if (item.order) {
try {
item.order.product = item.order.idProduct;
item.order.idProduct = item.order.product ? item.order.product._id : '';
item.order.producer = item.order.idProducer;
item.order.idProducer = item.order.producer ? item.order.producer._id : '';
item.order.storehouse = item.order.idStorehouse;
item.order.idStorehouse = item.order.storehouse ? item.order.storehouse._id : '';
item.order.provider = item.order.idProvider;
item.order.idProvider = item.order.provider ? item.order.provider._id : '';
item.order.gasordine = item.order.idGasordine;
item.order.idGasordine = item.order.gasordine ? item.order.gasordine._id : '';
item.order.scontisticas = item.order.scontisticas;
item.order.idScontisticas = item.order.idScontisticas ? item.order.idScontisticas._id : '';
} catch (e) {
console.error('Err: ', e);
}
}
return item;
});
return order;
});
return myorderscart;
}
module.exports.getOrdersCartByQuery = async function (query) {
@@ -274,6 +191,16 @@ module.exports.getOrdersCartByQuery = async function (query) {
model: 'Product'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idProduct',
populate: {
path: 'idProductInfo',
model: 'ProductInfo'
},
},
})
.populate({
path: 'items.order',
populate: {
@@ -324,6 +251,10 @@ module.exports.getOrdersCartByQuery = async function (query) {
try {
item.order.product = item.order.idProduct;
item.order.idProduct = item.order.product ? item.order.product._id : '';
if (item.order.product.idProductInfo) {
item.order.product.productInfo = item.order.product.idProductInfo;
item.order.product.idProductInfo = item.order.product ? item.order.product.productInfo._id : '';
}
item.order.producer = item.order.idProducer;
item.order.idProducer = item.order.producer ? item.order.producer._id : '';
item.order.storehouse = item.order.idStorehouse;
@@ -653,7 +584,7 @@ module.exports.getmsgorderTelegram = async function (ordersCart) {
qtystr += 'Ordinate: ' + ord.order.quantity
if (ord.order.quantitypreordered > 0)
qtystr += ' Pre-Ordinate: ' + ord.order.quantitypreordered
msg += '✅ [' + qtystr + '] ' + ord.order.product.name + ' (' + ord.order.price + ' € ' + (ord.order.after_price ? ord.order.after_price : '') + ' Tot=' + ord.order.TotalPriceProduct + '€ )';
msg += '✅ [' + qtystr + '] ' + ord.order.product.productInfo.name + ' (' + ord.order.price + ' € ' + (ord.order.after_price ? ord.order.after_price : '') + ' Tot=' + ord.order.TotalPriceProduct + '€ )';
}
msg += '<br>';

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);

156
src/server/models/productInfo.js Executable file
View File

@@ -0,0 +1,156 @@
mongoose = require('mongoose').set('debug', false)
const Schema = mongoose.Schema;
const tools = require('../tools/general');
mongoose.Promise = global.Promise;
mongoose.level = "F";
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
});
const productInfoSchema = new Schema({
idapp: {
type: String,
},
department: {
type: String, ref: 'Department'
},
code: {
type: String,
unique: true,
required: true,
},
codice_EAN: {
type: String,
},
barcode: {
type: String,
},
name: {
type: String,
},
description: {
type: String,
},
idCatProds: [{ type: Schema.Types.ObjectId, ref: 'CatProd' }],
color: {
type: String
},
size: {
type: String
},
weight: {
type: Number
},
vegan: {
type: Boolean
},
unit: {
type: Number
},
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,
},
});
var productInfo = module.exports = mongoose.model('ProductInfo', productInfoSchema);
module.exports.getFieldsForSearch = function () {
return [{ field: 'name', type: tools.FieldType.string }]
};
module.exports.executeQueryTable = function (idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, idapp, params);
};
module.exports.findAllIdApp = async function (idapp, code, id) {
let myfind = {};
let myqueryadd = {};
let query = [];
try {
if (idapp)
myfind = { idapp };
if (code) {
myfind = { ...myfind, code }
}
if (id) {
myqueryadd = {
$addFields: {
myId1: {
$toObjectId: id,
},
},
}
myfind = {
$expr: {
$eq: ["$_id", "$myId1"],
},
}
query.push(myqueryadd);
}
query.push(
{ $match: myfind },
{
$lookup: {
from: 'catprods',
localField: 'idCatProds',
foreignField: '_id',
as: 'catprods'
}
},
{
$sort: {
name: 1 // 1 for ascending order, -1 for descending order
}
},
);
let ris = await productInfo.aggregate(query)
return ris;
} catch (e) {
console.error('E', e);
}
};
module.exports.getProductByCode = function (idapp, code) {
return productInfo.findAllIdApp(idapp, code);
}
module.exports.createIndexes((err) => {
if (err) throw err;
});

View File

@@ -40,12 +40,21 @@ const storehouseSchema = new Schema({
website: {
type: String,
},
stockQty: { // in magazzino
type: Number,
default: 0,
},
bookableQty: { // Quantità prenotabili
type: Number,
default: 0,
},
});
var Storehouse = module.exports = mongoose.model('Storehouse', storehouseSchema);
module.exports.getFieldsForSearch = function () {
return [{field: 'name', type: tools.FieldType.string}]
return [{ field: 'name', type: tools.FieldType.string }]
};
module.exports.executeQueryTable = function (idapp, params) {