aggiornamento ordini

This commit is contained in:
Surya Paolo
2023-12-14 15:20:21 +01:00
parent d0bf7e1b6a
commit ea2b7095c9
7 changed files with 246 additions and 130 deletions

View File

@@ -3,6 +3,8 @@ const Schema = mongoose.Schema;
const tools = require('../tools/general');
const Producer = require('../models/producer');
const shared_consts = require('../tools/shared_nodejs');
mongoose.Promise = global.Promise;
@@ -58,6 +60,10 @@ const productSchema = new Schema({
type: Number,
required: true,
},
price_acquistato: {
type: Number,
required: true,
},
after_price: {
type: String
},
@@ -113,6 +119,9 @@ const productSchema = new Schema({
img: {
type: String,
},
link: {
type: String,
},
img2: {
type: String,
},
@@ -128,6 +137,9 @@ const productSchema = new Schema({
note: {
type: String,
},
producer_name: {
type: String,
}
});
var Product = module.exports = mongoose.model('Product', productSchema);
@@ -198,7 +210,7 @@ module.exports.findAllIdApp = async function (idapp, code) {
},
{
$addFields: {
totalQty: {
QuantitaOrdinateInAttesa: {
$cond: {
if: { $isArray: '$productOrders' },
then: { $arrayElemAt: ['$productOrders.totalQty', 0] },
@@ -266,6 +278,25 @@ module.exports.createIndexes((err) => {
if (err) throw err;
});
module.exports.convertAfterImport = async function () {
const arrprod = await Product.find({}).lean();
for (const prod of arrprod) {
if (prod.producer_name) {
// Cerca il produttore
const recproducer = await Producer.findOne({ name: prod.producer_name }).lean();
const campodarimuovere = 'producer_name';
if (recproducer) {
await Product.findOneAndUpdate({ _id: prod._id }, { $set: { idProducer: recproducer._id } })
await Product.findOneAndUpdate({ _id: prod._id }, { $unset: { [campodarimuovere]: 1 } })
}
}
}
}
// const Product = mongoose.model('Product', ProductSchema);