aggiornamento scontistica, corretto errori

This commit is contained in:
Surya Paolo
2023-12-16 18:40:17 +01:00
parent ab3a31d4fb
commit 4290895a97
11 changed files with 132 additions and 44 deletions

View File

@@ -25,6 +25,7 @@ const orderSchema = new Schema({
idProduct: { type: Schema.Types.ObjectId, ref: 'Product' },
idProducer: { type: Schema.Types.ObjectId, ref: 'Producer' },
idStorehouse: { type: Schema.Types.ObjectId, ref: 'StoreHouse' },
idScontisticas: [{ type: Schema.Types.ObjectId, ref: 'Scontistica' }],
idProvider: { type: Schema.Types.ObjectId, ref: 'Provider' },
price: {
type: Number
@@ -167,13 +168,23 @@ module.exports.findAllIdApp = async function (idapp) {
},
{
$unwind: {
path: '$scontistica',
path: '$product',
preserveNullAndEmptyArrays: true,
},
},
{ $unwind: '$product' },
{ $unwind: '$producer' },
{ $unwind: '$provider' },
{
$unwind: {
path: '$producer',
preserveNullAndEmptyArrays: true,
},
},
{
$unwind: {
path: '$provider',
preserveNullAndEmptyArrays: true,
},
}
];
return await Order.aggregate(query)
@@ -263,19 +274,33 @@ module.exports.getTotalOrderById = async function (id) {
from: 'scontisticas',
localField: 'idScontisticas',
foreignField: '_id',
as: 'scontistica'
as: 'scontisticas'
}
},
{
$unwind: {
path: '$scontistica',
path: '$product',
preserveNullAndEmptyArrays: true,
},
},
{ $unwind: '$product' },
{ $unwind: '$producer' },
{ $unwind: '$storehouse' },
{ $unwind: '$provider' },
{
$unwind: {
path: '$producer',
preserveNullAndEmptyArrays: true,
},
},
{
$unwind: {
path: '$storehouse',
preserveNullAndEmptyArrays: true,
},
},
{
$unwind: {
path: '$provider',
preserveNullAndEmptyArrays: true,
},
}
];
return await Order.aggregate(query);

View File

@@ -223,6 +223,13 @@ module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
model: 'Storehouse'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idScontisticas',
model: 'Scontistica'
},
})
.populate({
path: 'userId',
model: 'User',
@@ -237,13 +244,15 @@ module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
if (item.order) {
try {
item.order.product = item.order.idProduct;
item.order.idProduct = item.order.product._id;
item.order.idProduct = item.order.product ? item.order.product._id : '';
item.order.producer = item.order.idProducer;
item.order.idProducer = item.order.producer._id;
item.order.idProducer = item.order.producer ? item.order.producer._id : '';
item.order.storehouse = item.order.idStorehouse;
item.order.idStorehouse = item.order.storehouse._id;
item.order.idStorehouse = item.order.storehouse ? item.order.storehouse._id : '';
item.order.provider = item.order.idProvider;
item.order.idProvider = item.order.provider._id;
item.order.idProvider = item.order.provider ? item.order.provider._id : '';
item.order.scontisticas = item.order.scontisticas;
item.order.idScontisticas = item.order.idScontisticas ? item.order.idScontisticas._id : '';
} catch (e) {
console.error('Err: ', e);
}

View File

@@ -206,6 +206,9 @@ module.exports.findAllIdApp = async function (idapp, code, id) {
query.push(myqueryadd);
}
// DA TOGLIEREE
// myfind = { ...myfind, code: '4012824406094' };
// return await Product.find(myfind);
query.push(
@@ -243,15 +246,9 @@ module.exports.findAllIdApp = async function (idapp, code, id) {
from: 'scontisticas',
localField: 'idScontisticas',
foreignField: '_id',
as: 'scontistica'
as: 'scontisticas'
}
},
{
$unwind: {
path: '$scontistica',
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'storehouses',
@@ -372,9 +369,9 @@ module.exports.convertAfterImport = async function (idapp, dataObjects) {
let recproducer = await Producer.findOne({ idapp, name: prod.producer_name }).lean();
if (!recproducer) {
// Non esiste questo produttore, quindi lo creo !
recproducer = await Producer.create({ idapp, name: prod.producer_name }, (err, recordCreato) => {
return recordCreato
})
recproducer = new Producer({ idapp, name: prod.producer_name });
ris = await recproducer.save();
recproducer = await Producer.findOne({ idapp, name: prod.producer_name }).lean();
}
if (recproducer) {
@@ -391,9 +388,9 @@ module.exports.convertAfterImport = async function (idapp, dataObjects) {
let recstorehouse = await Storehouse.findOne({ idapp, name: prod.magazzino_name }).lean();
if (!recstorehouse) {
// Non esiste questo produttore, quindi lo creo !
recstorehouse = await Storehouse.create({ idapp, name: prod.magazzino_name }, (err, recordCreato) => {
return recordCreato
})
recstorehouse = new Storehouse({ idapp, name: prod.magazzino_name });
ris = await recstorehouse.save();
recstorehouse = await Storehouse.findOne({ idapp, name: prod.magazzino_name }).lean();
}
if (recstorehouse) {
@@ -409,10 +406,10 @@ module.exports.convertAfterImport = async function (idapp, dataObjects) {
// Cerca il produttore
let recprovider = await Provider.findOne({ idapp, name: prod.provider_name }).lean();
if (!recprovider) {
recprovider = new Provider({ idapp, name: prod.provider_name });
// Non esiste questo produttore, quindi lo creo !
recprovider = await Provider.create({ idapp, name: prod.provider_name }, (err, recordCreato) => {
return recordCreato
})
ris = await recprovider.save();
recprovider = await Provider.findOne({ idapp, name: prod.provider_name }).lean();
}
if (recprovider) {

View File

@@ -40,7 +40,10 @@ const scontisticaSchema = new Schema({
var Scontistica = module.exports = mongoose.model('Scontistica', scontisticaSchema);
module.exports.getFieldsForSearch = function () {
return [{ field: 'name', type: tools.FieldType.string }]
return [
{ field: 'code', type: tools.FieldType.string },
{ field: 'description', type: tools.FieldType.string }
]
};
module.exports.executeQueryTable = function (idapp, params) {

View File

@@ -338,7 +338,8 @@ module.exports.createFirstUserAdmin = async function () {
const numusers = await User.countDocuments({ idapp: mysite.idapp });
if (numusers === 0) {
// Non esistono utenti, quindi creo quello di Admin
const utenteadmin = await User.findOne({ idapp: '13', username: telegrambot.ADMIN_USER_SERVER }).lean()
const utenteadmin = { idapp: '13', username: telegrambot.ADMIN_USER_SERVER };
const newuser = new User(utenteadmin);
newuser._id = new ObjectID();

View File

@@ -5378,6 +5378,7 @@ UserSchema.statics.addNewSite = async function (idappPass, body) {
// cerca un IdApp Libero
let idapp = await Site.generateNewSite_IdApp(idappPass, body, true);
if (idapp) {
let arrSite = await Site.find({ idapp }).lean();
let numutenti = 0;
@@ -5385,7 +5386,7 @@ UserSchema.statics.addNewSite = async function (idappPass, body) {
numutenti = await User.countDocuments({ idapp });
};
if (arrSite && arrSite.length === 1 && numutenti === 0) {
if (arrSite && arrSite.length === 1 && numutenti < 2) {
// Nessun Sito Installato e Nessun Utente installato !
let myuser = new User();
myuser._id = new ObjectID();