- fixed quantità

- creazione mappa numero utenti per provincia !
This commit is contained in:
Surya Paolo
2024-03-19 00:21:54 +01:00
parent 44b25fdf33
commit 3e0d0bf018
6 changed files with 209 additions and 52 deletions

View File

@@ -1,11 +1,11 @@
DATABASE=test_PiuCheBuono
DATABASE=test_FreePlanet
UDB=paofreeplanet
PDB=mypassword@1A
SEND_EMAIL=0
SEND_EMAIL_ORDERS=1
PORT=3000
appTelegram_TEST=["1","17"]
appTelegram=["1","17"]
appTelegram_TEST=["1","13"]
appTelegram=["1","13"]
DOMAIN=mongodb://localhost:27017/
AUTH_MONGODB=true
MONGODB_USER=admin

View File

@@ -192,13 +192,14 @@ CitySchema.statics.executeQueryPickup = async function(idapp, params) {
};
CitySchema.statics.findAllIdApp = async function (idapp) {
const myfind = {};
return await City.find(myfind);
};
const City = mongoose.model('City', CitySchema);
City.createIndexes((err) => {

View File

@@ -605,7 +605,8 @@ module.exports.getTotalOrderById = async function (id) {
];
return await Order.aggregate(query);
const ris = await Order.aggregate(query);
return ris;
}
module.exports.RemoveDeletedOrdersInOrderscart = async function () {

View File

@@ -42,6 +42,13 @@ const ProvinceSchema = new Schema({
link_telegram: {
type: String,
},
lat: {
type: Number,
},
long: {
type: Number,
},
}, { _id: false });
ProvinceSchema.pre('save', async function (next) {
@@ -125,6 +132,24 @@ ProvinceSchema.statics.findAllIdApp = async function(idapp) {
return Province.find(myfind).sort({ descr: 1 });
};
ProvinceSchema.statics.setCoordinatesOnDB = async function () {
const arrprov = await Province.find({}).lean();
// Funzione per ottenere le coordinate di tutte le città
for (const prov of arrprov) {
if (!prov.lat) {
let coord = await tools.getCityCoordinates(prov);
if (coord) {
let ris = await Province.findOneAndUpdate({ _id: prov._id }, { $set: { lat: coord.lat, long: coord.long } }, { new: true });
console.log(' *** Update ', prov.descr, 'lat', ris.lat, 'long', ris.long);
}
}
}
};
const Province = mongoose.model('Province', ProvinceSchema);
Province.createIndexes((err) => {

View File

@@ -46,6 +46,7 @@ const TypedError = require('../modules/ErrorHandler');
const { MyGroup } = require('../models/mygroup');
const { Circuit } = require('../models/circuit');
const { Province } = require('../models/province');
const { Account } = require('../models/account');
const mongoose = require('mongoose').set('debug', false);
@@ -1253,6 +1254,8 @@ async function eseguiDbOp(idapp, mydata, locale, req, res) {
await Circuit.CheckTransazioniCircuiti(false);
} else if (mydata.dbop === 'CorreggiTransazioniCircuiti') {
await Circuit.CheckTransazioniCircuiti(true);
} else if (mydata.dbop === 'UpdateCoordProv') {
await Province.setCoordinatesOnDB();
} else if (mydata.dbop === 'AbilitaNewsletterALL') {
await User.updateMany({
$or: [
@@ -1708,6 +1711,113 @@ router.post('/dbopuser', authenticate, async (req, res) => {
});
router.post('/infomap', authenticate, async (req, res) => {
const idapp = req.body.idapp;
const raggruppa = true;
try {
let myquery = [
{
$match: {
idapp,
$or: [
{ deleted: { $exists: false } },
{ deleted: { $exists: true, $eq: false } }
]
}
},
{
$lookup: {
from: "provinces", // Collezione delle province
localField: "profile.resid_province", // Campo nella collezione User che contiene l'ID della provincia
foreignField: "prov", // Campo nella collezione Province che identifica l'ID della provincia
as: "provinceInfo" // Nome del campo in cui verranno memorizzate le informazioni della provincia
}
},
{
$addFields: {
"provinceInfo": { $arrayElemAt: ["$provinceInfo", 0] } // Estrae il primo elemento dell'array provinceInfo
}
},
{
$project: {
username: 1,
name: 1,
surname: 1,
email: 1,
verified_by_aportador: 1,
aportador_solidario: 1,
lasttimeonline: 1,
'profile.img': 1,
'profile.resid_province': 1,
lat: "$provinceInfo.lat", // Aggiunge il campo lat preso dalla provincia
long: "$provinceInfo.long" // Aggiunge il campo long preso dalla provincia
}
}
];
let ris = null;
if (raggruppa) {
const myquery = [
{
$lookup: {
from: "users", // Collezione degli utenti
localField: "prov", // Campo nella collezione Province che identifica l'ID della provincia
foreignField: "profile.resid_province", // Campo nella collezione User che contiene l'ID della provincia
as: "users" // Nome del campo in cui verranno memorizzati gli utenti della provincia
}
},
{
$addFields: {
userCount: { $size: "$users" } // Aggiunge il numero di utenti nella provincia
}
},
{
$lookup: {
from: "provinces", // Collezione delle province
localField: "prov", // Campo nella collezione Province che identifica l'ID della provincia
foreignField: "prov", // Campo nella collezione Province che identifica l'ID della provincia
as: "provinceInfo" // Nome del campo in cui verranno memorizzate le informazioni della provincia
}
},
{
$addFields: {
provinceDescr: { $arrayElemAt: ["$provinceInfo.descr", 0] } // Aggiunge il campo descr preso dalla provincia
}
},
{
$project: {
_id: 0, // Esclude il campo _id
province: "$prov", // Rinomina il campo prov come province
descr: "$provinceDescr",
userCount: 1,
lat: 1, // Include il campo lat
long: 1 // Include il campo long
}
}
];
ris = await Province.aggregate(myquery);
} else {
ris = await User.aggregate(myquery);
}
if (!ris) {
ris = {};
}
res.send({ code: server_constants.RIS_CODE_OK, ris });
} catch (e) {
res.status(400).send({ code: server_constants.RIS_CODE_ERR, msg: e });
console.log(e.message);
}
});
router.post('/mgt', authenticate, async (req, res) => {
const mydata = req.body.mydata;

View File

@@ -7,6 +7,8 @@ require('../models/subscribers');
const printf = require('util').format;
const axios = require('axios');
const CryptoJS = require('crypto-js');
const Url = require('url-parse');
@@ -4879,4 +4881,22 @@ module.exports = {
}
},
// Funzione per ottenere le coordinate di una singola città
async getCityCoordinates(city) {
try {
const response = await axios.get(`https://nominatim.openstreetmap.org/search?format=json&q=${city.descr},${city.prov},Italia`);
if (response.data.length > 0) {
city.lat = parseFloat(response.data[0].lat);
city.long = parseFloat(response.data[0].lon);
// console.log(`${city.descr}: Lat ${city.lat}, Long ${city.long}`);
return city;
} else {
console.error(`Coordinate non trovate per ${city.descr}, ${city.prov}`);
}
return null;
} catch (error) {
console.error(`Errore durante il recupero delle coordinate per ${city.descr}, ${city.prov}:`, error.message);
}
},
};