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');
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
},
|
2022-03-09 14:53:09 +01:00
|
|
|
reg: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2022-01-23 23:25:34 +01:00
|
|
|
prov: {
|
|
|
|
|
type: String,
|
2022-03-09 14:53:09 +01:00
|
|
|
// unique: true,
|
2022-01-23 23:25:34 +01:00
|
|
|
maxlength: 3,
|
|
|
|
|
},
|
|
|
|
|
descr: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2022-03-09 14:53:09 +01:00
|
|
|
link_grp: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2023-11-24 17:52:17 +01:00
|
|
|
card : {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
|
2023-06-01 11:39:53 +02:00
|
|
|
link_telegram: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2022-01-23 23:25:34 +01:00
|
|
|
}, { _id : false });
|
|
|
|
|
|
2023-11-24 17:52:17 +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();
|
|
|
|
|
});
|
|
|
|
|
|
2022-07-23 17:48:33 +02:00
|
|
|
ProvinceSchema.statics.getRegionByStrProvince = async function(strprovince) {
|
|
|
|
|
const myrec = await Province.findOne({prov: strprovince}).lean();
|
|
|
|
|
if (myrec) {
|
|
|
|
|
return myrec.reg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 18:11:56 +01:00
|
|
|
ProvinceSchema.statics.getStrProvinceByProv = async function(prov) {
|
|
|
|
|
const myrec = await Province.findOne({prov}).lean();
|
|
|
|
|
if (myrec) {
|
|
|
|
|
return myrec.descr;
|
|
|
|
|
}
|
2022-07-23 17:48:33 +02:00
|
|
|
|
2023-03-21 18:11:56 +01:00
|
|
|
return '';
|
|
|
|
|
}
|
2022-01-23 23:25:34 +01:00
|
|
|
|
|
|
|
|
ProvinceSchema.statics.getFieldsForSearch = function() {
|
|
|
|
|
return [
|
|
|
|
|
{field: 'prov', type: tools.FieldType.string},
|
|
|
|
|
{field: 'descr', type: tools.FieldType.string},
|
|
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ProvinceSchema.statics.executeQueryTable = function(idapp, params) {
|
|
|
|
|
params.fieldsearch = this.getFieldsForSearch();
|
|
|
|
|
|
|
|
|
|
const strfind = params.search;
|
|
|
|
|
|
|
|
|
|
if (strfind === '') {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tools.executeQueryTable(this, 0, params);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ProvinceSchema.statics.executeQueryPickup = async function(idapp, params) {
|
|
|
|
|
|
|
|
|
|
const strfind = params.search;
|
|
|
|
|
|
|
|
|
|
if (strfind === '') {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let filterfindexact = {descr: strfind};
|
|
|
|
|
const risexact = await Province.find(filterfindexact).lean();
|
|
|
|
|
|
|
|
|
|
let filterfind = {};
|
|
|
|
|
|
|
|
|
|
filterfind = {descr: {$regex: '^' + strfind, $options: 'i'}};
|
|
|
|
|
|
|
|
|
|
const ris = await Province.find(filterfind).lean().limit(10);
|
|
|
|
|
|
|
|
|
|
return [...risexact, ...ris];
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ProvinceSchema.statics.findAllIdApp = async function(idapp) {
|
|
|
|
|
const myfind = {};
|
|
|
|
|
|
2023-03-11 01:01:11 +01:00
|
|
|
return Province.find(myfind).sort({descr: 1});
|
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;
|
|
|
|
|
});
|
|
|
|
|
|
2022-01-23 23:25:34 +01:00
|
|
|
module.exports = {Province};
|