2022-09-14 11:32:04 +02: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');
|
|
|
|
|
|
2024-03-19 00:21:54 +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,
|
2022-02-12 02:20:07 +01:00
|
|
|
maxlength: 3,
|
2021-10-08 00:38:35 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-03-19 00:21:54 +01:00
|
|
|
CitySchema.pre('save', async function (next) {
|
2021-10-08 00:38:35 +02:00
|
|
|
if (this.isNew) {
|
2024-03-19 00:21:54 +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();
|
|
|
|
|
});
|
|
|
|
|
|
2024-03-19 00:21:54 +01:00
|
|
|
CitySchema.statics.getProvinceByIdCity = async function (idcity) {
|
|
|
|
|
const myrec = await City.findOne({ _id: idcity }).lean();
|
2022-07-23 17:48:33 +02:00
|
|
|
if (myrec) {
|
|
|
|
|
return myrec.prov;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-19 00:21:54 +01:00
|
|
|
CitySchema.statics.getCircuitNameBystrProv = async function (strProv) {
|
2023-06-07 18:46:04 +02:00
|
|
|
const { Circuit } = require('../models/circuit');
|
|
|
|
|
|
2024-03-19 00:21:54 +01:00
|
|
|
const myrec = await Circuit.findOne({ strProv }).lean();
|
2023-06-07 18:46:04 +02:00
|
|
|
if (myrec) {
|
|
|
|
|
return myrec.name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-19 00:21:54 +01:00
|
|
|
CitySchema.statics.getRegionByIdCity = async function (idcity) {
|
|
|
|
|
const myrec = await City.findOne({ _id: idcity }).lean();
|
2022-07-23 17:48:33 +02:00
|
|
|
if (myrec) {
|
|
|
|
|
return myrec.reg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-19 00:21:54 +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 = [
|
2024-03-19 00:21:54 +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
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-19 00:21:54 +01:00
|
|
|
CitySchema.statics.getFieldsForSearch = function () {
|
2022-01-20 00:39:06 +01:00
|
|
|
return [
|
2024-03-19 00:21:54 +01:00
|
|
|
{ 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 },
|
2022-01-20 00:39:06 +01:00
|
|
|
];
|
2021-10-08 00:38:35 +02:00
|
|
|
};
|
|
|
|
|
|
2024-03-19 00:21:54 +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);
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-19 00:21:54 +01:00
|
|
|
CitySchema.statics.executeQueryPickup = async function (idapp, params) {
|
2022-01-20 00:39:06 +01:00
|
|
|
|
|
|
|
|
const strfind = params.search;
|
|
|
|
|
|
2022-02-21 13:12:27 +01:00
|
|
|
if (strfind === '' && !params.filter) {
|
2022-01-20 00:39:06 +01:00
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-21 13:12:27 +01:00
|
|
|
let filterfindexact = {};
|
2022-02-26 17:35:50 +01:00
|
|
|
if (strfind) {
|
2024-03-19 00:21:54 +01:00
|
|
|
filterfindexact = { comune: strfind };
|
2022-02-21 13:12:27 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-26 17:35:50 +01:00
|
|
|
let limit = 10;
|
|
|
|
|
let risexact = [];
|
2022-01-23 23:25:34 +01:00
|
|
|
|
2024-03-19 00:21:54 +01:00
|
|
|
let filterfind = { comune: { $regex: '^' + strfind, $options: 'i' } };
|
2022-01-23 23:25:34 +01:00
|
|
|
|
2022-02-26 17:35:50 +01:00
|
|
|
let aggr1 = [
|
|
|
|
|
{
|
2024-03-19 00:21:54 +01:00
|
|
|
$match: { comune: strfind },
|
2022-02-26 17:35:50 +01:00
|
|
|
},
|
2024-03-19 00:21:54 +01:00
|
|
|
{ $limit: 1 },
|
2022-02-26 17:35:50 +01:00
|
|
|
{
|
|
|
|
|
$project: {
|
|
|
|
|
comune: { $concat: ["$comune", " (", "$prov", ")"] },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2022-02-21 13:12:27 +01:00
|
|
|
if (params.filter) {
|
2024-03-19 00:21:54 +01:00
|
|
|
filterfind = { ...params.filter, ...filterfind };
|
2022-02-26 17:35:50 +01:00
|
|
|
limit = 200;
|
|
|
|
|
} else {
|
|
|
|
|
// risexact = await City.find(filterfindexact, {comune: 1, prov: 1, reg: 1}).lean();
|
|
|
|
|
risexact = await City.aggregate(aggr1);
|
2022-02-21 13:12:27 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-26 17:35:50 +01:00
|
|
|
let aggr2 = [
|
|
|
|
|
{
|
|
|
|
|
$match: filterfind,
|
|
|
|
|
},
|
2024-03-19 00:21:54 +01:00
|
|
|
{ $limit: limit },
|
2022-02-26 17:35:50 +01:00
|
|
|
{
|
|
|
|
|
$project: {
|
|
|
|
|
comune: { $concat: ["$comune", " (", "$prov", ")"] },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// let ris = await City.find(filterfind, {comune: 1, prov: 1, reg: 1}).lean().limit(limit);
|
|
|
|
|
let ris = await City.aggregate(aggr2).limit(limit);
|
2022-01-23 23:25:34 +01:00
|
|
|
|
|
|
|
|
return [...risexact, ...ris];
|
2022-01-20 00:39:06 +01:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-19 00:21:54 +01:00
|
|
|
CitySchema.statics.findAllIdApp = async function (idapp) {
|
2021-10-28 00:38:10 +02:00
|
|
|
const myfind = {};
|
|
|
|
|
|
2022-09-11 11:45:33 +02:00
|
|
|
return await City.find(myfind);
|
2021-10-28 00:38:10 +02:00
|
|
|
};
|
|
|
|
|
|
2024-03-19 00:21:54 +01:00
|
|
|
|
|
|
|
|
|
2021-10-08 00:38:35 +02:00
|
|
|
const City = mongoose.model('City', CitySchema);
|
|
|
|
|
|
2023-12-09 11:55:58 +01:00
|
|
|
City.createIndexes((err) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
});
|
|
|
|
|
|
2024-03-19 00:21:54 +01:00
|
|
|
module.exports = { City };
|