Files
freeplanet_serverside/src/server/models/province.js

160 lines
3.3 KiB
JavaScript
Raw Normal View History

2022-09-14 11:32:04 +02:00
const mongoose = require('mongoose').set('debug', false);
2022-01-23 23:25:34 +01:00
const Schema = mongoose.Schema;
const escapeStringRegexp = require('escape-string-regexp');
mongoose.Promise = global.Promise;
mongoose.level = 'F';
const tools = require('../tools/general');
const { ObjectId } = require('mongodb');
2022-01-23 23:25:34 +01:00
const shared_consts = require('../tools/shared_nodejs');
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true;
});
const ProvinceSchema = new Schema({
_id: {
type: Number,
},
reg: {
type: String,
},
2022-01-23 23:25:34 +01:00
prov: {
type: String,
// unique: true,
2022-01-23 23:25:34 +01:00
maxlength: 3,
},
descr: {
type: String,
},
link_grp: {
type: String,
},
card: {
type: String,
},
link_telegram: {
type: String,
},
lat: {
type: Number,
},
long: {
type: Number,
},
}, { _id: false });
2022-01-23 23:25:34 +01:00
ProvinceSchema.pre('save', async function (next) {
if (this.isNew) {
const myrec = await Province.findOne().limit(1).sort({ _id: -1 });
if (!!myrec) {
if (myrec._doc._id === 0)
this._id = 1;
else
this._id = myrec._doc._id + 1;
} else {
this._id = 1;
}
}
next();
});
ProvinceSchema.statics.getRegionByStrProvince = async function (strprovince) {
const myrec = await Province.findOne({ prov: strprovince }).lean();
if (myrec) {
return myrec.reg;
}
return '';
}
ProvinceSchema.statics.getStrProvinceByProv = async function (prov) {
const myrec = await Province.findOne({ prov }).lean();
if (myrec) {
return myrec.descr;
}
return '';
}
2022-01-23 23:25:34 +01:00
ProvinceSchema.statics.getFieldsForSearch = function () {
2022-01-23 23:25:34 +01:00
return [
{ field: 'prov', type: tools.FieldType.string },
{ field: 'descr', type: tools.FieldType.string },
2022-01-23 23:25:34 +01:00
];
};
ProvinceSchema.statics.executeQueryTable = function (idapp, params) {
2022-01-23 23:25:34 +01:00
params.fieldsearch = this.getFieldsForSearch();
const strfind = params.search;
if (strfind === '') {
return [];
}
return tools.executeQueryTable(this, 0, params);
};
ProvinceSchema.statics.executeQueryPickup = async function (idapp, params) {
2022-01-23 23:25:34 +01:00
const strfind = params.search;
if (strfind === '') {
return [];
}
let filterfindexact = { descr: strfind };
2022-01-23 23:25:34 +01:00
const risexact = await Province.find(filterfindexact).lean();
let filterfind = {};
filterfind = { descr: { $regex: '^' + strfind, $options: 'i' } };
2022-01-23 23:25:34 +01:00
const ris = await Province.find(filterfind).lean().limit(10);
return [...risexact, ...ris];
};
ProvinceSchema.statics.findAllIdApp = async function (idapp) {
2022-01-23 23:25:34 +01:00
const myfind = {};
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);
}
}
}
2022-01-23 23:25:34 +01:00
};
2022-01-23 23:25:34 +01:00
const Province = mongoose.model('Province', ProvinceSchema);
2023-12-09 11:55:58 +01:00
Province.createIndexes((err) => {
if (err) throw err;
});
module.exports = { Province };