Files
freeplanet_serverside/src/server/router/admin_router.js

116 lines
3.2 KiB
JavaScript
Raw Normal View History

const express = require('express');
const router = express.Router();
2022-09-14 11:32:04 +02:00
const mongoose = require('mongoose').set('debug', false);
2023-12-14 15:20:21 +01:00
const { CfgServer } = require('../models/cfgserver');
const shared_consts = require('../tools/shared_nodejs');
const tools = require('../tools/general');
2023-12-14 15:20:21 +01:00
const { City } = require('../models/city');
const Product = require('../models/product');
2023-12-14 15:20:21 +01:00
var { authenticate } = require('../middleware/authenticate');
2020-04-24 10:29:25 +02:00
router.post('/updateval', authenticate, async (req, res) => {
console.log('/updateval', req.body.pairval);
2020-04-24 10:29:25 +02:00
idapp = req.body.idapp;
pair = req.body.pairval;
return await CfgServer.findOneAndUpdate(
2023-12-14 15:20:21 +01:00
{ chiave: pair.chiave, idapp, userId: pair.userId }, { $set: pair },
{ new: false }).then((item) => {
// CfgServer.find({ chiave: pair.chiave }, (err, item) => {
if (!!item) {
res.status(200).send();
} else {
res.status(400).send();
}
}).catch(err => {
console.log('ERR:', err);
res.status(400).send();
2023-12-14 15:20:21 +01:00
});
});
router.post('/import', authenticate, async (req, res) => {
const cmd = req.body.cmd;
const idapp = req.body.idapp;
2023-12-14 15:20:21 +01:00
const data = req.body.data;
try {
const liste = require('../data/liste');
if (cmd === shared_consts.Cmd.CITIES_SERVER) {
return await City.insertMany(liste.Comuni).then((ris) => {
return res.status(200).send(true);
});
2023-12-14 15:20:21 +01:00
} else if (cmd === shared_consts.Cmd.PRODUCTS) {
let dataObjects = JSON.parse(`[${data}]`);
2023-12-18 12:11:12 +01:00
let updated = 0;
let imported = 0;
let errors = 0;
for (const product of dataObjects) {
if (!product.hasOwnProperty('active')) {
product.active = true;
}
let risrec = await Product.findOneAndUpdate({ code: product.code }, { $set: product }, { new: true, upsert: true });
let recnew = await Product.findOne({ code: product.code }).lean();
if (risrec) {
if (risrec._id) {
// Record existed, so it was updated
let arrfieldchange = tools.differentObjects(product, recnew);
if (arrfieldchange.length > 0) {
updated++;
console.log('Changed: ', product.name + ': ' + arrfieldchange);
}
} else {
// Record didn't exist, so it was created
imported++;
}
} else {
// risrec is null or undefined, indicating an error
console.error('Error: ', product.name);
errors++;
}
await Product.singlerecconvert_AfterImport(idapp, recnew);
}
// L'opzione ordered: false gestisce gli errori senza interrompere l'inserimento
2023-12-18 12:11:12 +01:00
/*return await Product.insertMany(dataObjects, { ordered: false })
.then((ris) => {
2023-12-14 15:20:21 +01:00
Product.convertAfterImport(idapp, dataObjects).then((ris) => {
return res.status(200).send(true);
});
2023-12-14 15:20:21 +01:00
})
.catch((errors) => {
console.error(errors);
Product.convertAfterImport(idapp).then((ris) => {
return res.status(200).send(true);
});
2023-12-18 12:11:12 +01:00
});*/
return res.status(200).send({ updated, imported, errors });
2023-12-14 15:20:21 +01:00
}
2023-12-14 15:20:21 +01:00
} catch (e) {
console.error('e', e);
res.status(400).send();
}
res.status(400).send();
});
module.exports = router;