2022-01-20 00:39:06 +01:00
|
|
|
const mongoose = require('mongoose').set('debug', false);
|
2021-10-08 00:38:35 +02:00
|
|
|
const Schema = mongoose.Schema;
|
|
|
|
|
|
2022-01-20 00:39:06 +01:00
|
|
|
const escapeStringRegexp = require('escape-string-regexp');
|
|
|
|
|
|
2021-10-08 00:38:35 +02:00
|
|
|
mongoose.Promise = global.Promise;
|
2022-01-20 00:39:06 +01:00
|
|
|
mongoose.level = 'F';
|
2021-10-08 00:38:35 +02:00
|
|
|
|
|
|
|
|
const tools = require('../tools/general');
|
|
|
|
|
|
2022-01-20 00:39:06 +01:00
|
|
|
const {ObjectID} = require('mongodb');
|
2021-10-08 00:38:35 +02:00
|
|
|
|
2022-01-23 23:25:34 +01:00
|
|
|
const shared_consts = require('../tools/shared_nodejs');
|
|
|
|
|
|
2021-10-08 00:38:35 +02:00
|
|
|
// Resolving error Unknown modifier: $pushAll
|
|
|
|
|
mongoose.plugin(schema => {
|
2022-01-20 00:39:06 +01:00
|
|
|
schema.options.usePushEach = true;
|
2021-10-08 00:38:35 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const CitySchema = new Schema({
|
|
|
|
|
_id: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
istat: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
comune: {
|
2022-01-20 00:39:06 +01:00
|
|
|
type: String,
|
2021-10-08 00:38:35 +02:00
|
|
|
},
|
|
|
|
|
prov: {
|
|
|
|
|
type: String,
|
|
|
|
|
maxlength: 3,
|
|
|
|
|
},
|
|
|
|
|
reg: {
|
|
|
|
|
type: String,
|
|
|
|
|
maxlength: 3,
|
|
|
|
|
},
|
|
|
|
|
pref: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
cap: {
|
|
|
|
|
type: String,
|
|
|
|
|
maxlength: 6,
|
|
|
|
|
},
|
|
|
|
|
abitanti: {
|
2022-01-20 00:39:06 +01:00
|
|
|
type: String,
|
2021-10-08 00:38:35 +02:00
|
|
|
},
|
|
|
|
|
country: {
|
|
|
|
|
type: String,
|
|
|
|
|
maxlength: 2,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2022-01-20 00:39:06 +01:00
|
|
|
CitySchema.pre('save', async function(next) {
|
2021-10-08 00:38:35 +02:00
|
|
|
if (this.isNew) {
|
2022-01-20 00:39:06 +01:00
|
|
|
const myrec = await City.findOne().limit(1).sort({_id: -1});
|
2021-10-08 00:38:35 +02:00
|
|
|
if (!!myrec) {
|
|
|
|
|
if (myrec._doc._id === 0)
|
|
|
|
|
this._id = 1;
|
|
|
|
|
else
|
|
|
|
|
this._id = myrec._doc._id + 1;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
this._id = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2022-01-20 00:39:06 +01:00
|
|
|
CitySchema.statics.findByCity = function(mycity) {
|
2021-10-08 00:38:35 +02:00
|
|
|
|
|
|
|
|
let myregexp = new RegExp(mycity.trim().replace(' ', '|'), 'ig');
|
|
|
|
|
|
|
|
|
|
const query = [
|
2022-01-20 00:39:06 +01:00
|
|
|
{$match: {comune: {$regex: myregexp}}},
|
|
|
|
|
{$sort: {descr: 1}},
|
2021-10-08 00:38:35 +02:00
|
|
|
];
|
|
|
|
|
|
2022-01-20 00:39:06 +01:00
|
|
|
return City.aggregate(query).then((arrrec) => {
|
|
|
|
|
return arrrec;
|
|
|
|
|
});
|
2021-10-08 00:38:35 +02:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-20 00:39:06 +01:00
|
|
|
CitySchema.statics.getFieldsForSearch = function() {
|
|
|
|
|
return [
|
|
|
|
|
{field: 'comune', type: tools.FieldType.string},
|
|
|
|
|
{field: 'prov', type: tools.FieldType.string},
|
|
|
|
|
{field: 'reg', type: tools.FieldType.string},
|
|
|
|
|
{field: 'pref', type: tools.FieldType.number},
|
|
|
|
|
{field: 'cap', type: tools.FieldType.number},
|
|
|
|
|
];
|
2021-10-08 00:38:35 +02:00
|
|
|
};
|
|
|
|
|
|
2022-01-20 00:39:06 +01:00
|
|
|
CitySchema.statics.executeQueryTable = function(idapp, params) {
|
2021-10-08 00:38:35 +02:00
|
|
|
params.fieldsearch = this.getFieldsForSearch();
|
2022-01-03 21:53:50 +01:00
|
|
|
|
|
|
|
|
const strfind = params.search;
|
|
|
|
|
|
|
|
|
|
if (strfind === '') {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-08 00:38:35 +02:00
|
|
|
return tools.executeQueryTable(this, 0, params);
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-20 00:39:06 +01:00
|
|
|
CitySchema.statics.executeQueryPickup = async function(idapp, params) {
|
|
|
|
|
|
|
|
|
|
const strfind = params.search;
|
|
|
|
|
|
|
|
|
|
if (strfind === '') {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-23 23:25:34 +01:00
|
|
|
let filterfindexact = {comune: strfind};
|
|
|
|
|
const risexact = await City.find(filterfindexact, {comune: 1, prov: 1, reg: 1}).lean();
|
|
|
|
|
|
|
|
|
|
let filterfind = {comune: {$regex: '^' + strfind, $options: 'i'}};
|
|
|
|
|
|
|
|
|
|
const ris = await City.find(filterfind, {comune: 1, prov: 1, reg: 1}).lean().limit(10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return [...risexact, ...ris];
|
2022-01-20 00:39:06 +01:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CitySchema.statics.findAllIdApp = async function(idapp) {
|
2021-10-28 00:38:10 +02:00
|
|
|
const myfind = {};
|
|
|
|
|
|
|
|
|
|
return City.find(myfind);
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-08 00:38:35 +02:00
|
|
|
const City = mongoose.model('City', CitySchema);
|
|
|
|
|
|
2022-01-20 00:39:06 +01:00
|
|
|
module.exports = {City};
|