266 lines
5.6 KiB
JavaScript
266 lines
5.6 KiB
JavaScript
|
|
var bcrypt = require('bcryptjs');
|
||
|
|
const mongoose = require('mongoose');
|
||
|
|
const validator = require('validator');
|
||
|
|
const jwt = require('jsonwebtoken');
|
||
|
|
const _ = require('lodash');
|
||
|
|
|
||
|
|
const tools = require('../tools/general');
|
||
|
|
|
||
|
|
const shared_consts = require('../tools/shared_nodejs');
|
||
|
|
const queryclass = require('../classes/queryclass');
|
||
|
|
|
||
|
|
mongoose.Promise = global.Promise;
|
||
|
|
|
||
|
|
mongoose.level = "F";
|
||
|
|
// Resolving error Unknown modifier: $pushAll
|
||
|
|
mongoose.plugin(schema => {
|
||
|
|
schema.options.usePushEach = true
|
||
|
|
});
|
||
|
|
|
||
|
|
mongoose.set('debug', process.env.DEBUG);
|
||
|
|
|
||
|
|
var ExtraListSchema = new mongoose.Schema({
|
||
|
|
idapp: {
|
||
|
|
type: String,
|
||
|
|
required: true,
|
||
|
|
},
|
||
|
|
ind_order: {
|
||
|
|
type: Number,
|
||
|
|
},
|
||
|
|
date_reg: {
|
||
|
|
type: Date,
|
||
|
|
},
|
||
|
|
name_complete: {
|
||
|
|
type: String,
|
||
|
|
trim: true,
|
||
|
|
},
|
||
|
|
username: {
|
||
|
|
type: String,
|
||
|
|
},
|
||
|
|
name: {
|
||
|
|
type: String,
|
||
|
|
trim: true,
|
||
|
|
},
|
||
|
|
surname: {
|
||
|
|
type: String,
|
||
|
|
trim: true,
|
||
|
|
},
|
||
|
|
num_invitati: {
|
||
|
|
type: Number,
|
||
|
|
},
|
||
|
|
is_in_whatsapp: {
|
||
|
|
type: Boolean,
|
||
|
|
},
|
||
|
|
is_in_telegram: {
|
||
|
|
type: Boolean,
|
||
|
|
},
|
||
|
|
cell_complete: {
|
||
|
|
type: String
|
||
|
|
},
|
||
|
|
nationality: {
|
||
|
|
type: String
|
||
|
|
},
|
||
|
|
aportador_solidario_name_surname: {
|
||
|
|
type: String,
|
||
|
|
},
|
||
|
|
aportador_solidario_ind_order: {
|
||
|
|
type: Number,
|
||
|
|
},
|
||
|
|
aportador_solidario_originale_name_surname: {
|
||
|
|
type: String,
|
||
|
|
},
|
||
|
|
note: {
|
||
|
|
type: String,
|
||
|
|
},
|
||
|
|
col_b: {
|
||
|
|
type: Number,
|
||
|
|
},
|
||
|
|
col_h: {
|
||
|
|
type: Number,
|
||
|
|
},
|
||
|
|
registered: {
|
||
|
|
type: Boolean,
|
||
|
|
default: false
|
||
|
|
}
|
||
|
|
|
||
|
|
});
|
||
|
|
|
||
|
|
ExtraListSchema.methods.toJSON = function () {
|
||
|
|
var user = this;
|
||
|
|
var userObject = user.toObject();
|
||
|
|
|
||
|
|
return _.pick(userObject, ['_id', ...shared_consts.fieldsUserToChange()]);
|
||
|
|
};
|
||
|
|
|
||
|
|
ExtraListSchema.statics.findByUsername = function (idapp, username) {
|
||
|
|
const User = this;
|
||
|
|
|
||
|
|
return User.findOne({
|
||
|
|
'idapp': idapp,
|
||
|
|
'username': username,
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
ExtraListSchema.statics.findByCellAndNameSurname = function (idapp, cell_complete, name, surname) {
|
||
|
|
var User = this;
|
||
|
|
|
||
|
|
return User.findOne({
|
||
|
|
'idapp': idapp,
|
||
|
|
'cell_complete': cell_complete,
|
||
|
|
'name': name,
|
||
|
|
'surname': surname,
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
ExtraListSchema.statics.findByIndOrder = function (idapp, ind_order) {
|
||
|
|
const User = this;
|
||
|
|
|
||
|
|
try {
|
||
|
|
return User.findOne({
|
||
|
|
'idapp': idapp,
|
||
|
|
'ind_order': ind_order,
|
||
|
|
});
|
||
|
|
} catch (e) {
|
||
|
|
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
ExtraListSchema.statics.getUsersList = function (idapp) {
|
||
|
|
const User = this;
|
||
|
|
|
||
|
|
return User.find({ 'idapp': idapp }, {
|
||
|
|
username: 1,
|
||
|
|
name: 1,
|
||
|
|
surname: 1,
|
||
|
|
date_reg: 1,
|
||
|
|
})
|
||
|
|
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
ExtraListSchema.statics.getFieldsForSearch = function () {
|
||
|
|
return ['name', 'surname', 'cell_complete', 'aportador_solidario_name_surname', 'aportador_solidario_originale_name_surname']
|
||
|
|
};
|
||
|
|
|
||
|
|
ExtraListSchema.statics.executeQueryTable = function (idapp, params) {
|
||
|
|
params.fieldsearch = this.getFieldsForSearch();
|
||
|
|
return tools.executeQueryTable(this, idapp, params);
|
||
|
|
};
|
||
|
|
|
||
|
|
ExtraListSchema.statics.findAllIdApp = function (idapp) {
|
||
|
|
const ExtraList = this;
|
||
|
|
|
||
|
|
const myfind = { idapp };
|
||
|
|
|
||
|
|
return ExtraList.find(myfind, (err, arrrec) => {
|
||
|
|
return arrrec
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
ExtraListSchema.statics.DuplicateAllRecords = async function (idapporig, idappdest) {
|
||
|
|
|
||
|
|
return tools.DuplicateAllRecords(this, idapporig, idappdest);
|
||
|
|
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
ExtraListSchema.statics.ImportData = async function (locale, idapp, strdata) {
|
||
|
|
|
||
|
|
const ExtraList = this;
|
||
|
|
|
||
|
|
try {
|
||
|
|
let numadded = 0;
|
||
|
|
let numtot = 0;
|
||
|
|
let numalreadyexisted = 0;
|
||
|
|
|
||
|
|
// Convert to array
|
||
|
|
let arrusers = strdata.split('\n');
|
||
|
|
let sep = ',';
|
||
|
|
|
||
|
|
// console.log('arrusers', arrusers);
|
||
|
|
|
||
|
|
try {
|
||
|
|
for (const row of arrusers) {
|
||
|
|
console.log('row', row);
|
||
|
|
if (sep !== '' && row !== '') {
|
||
|
|
let col = row.split(sep);
|
||
|
|
if (col) {
|
||
|
|
if (col.length > 0) {
|
||
|
|
let user = null;
|
||
|
|
try {
|
||
|
|
user = new ExtraList({
|
||
|
|
idapp: idapp,
|
||
|
|
ind_order: col[0],
|
||
|
|
name_complete: col[2],
|
||
|
|
num_invitati: col[3],
|
||
|
|
is_in_whatsapp: col[4] !== '',
|
||
|
|
is_in_telegram: col[5] !== '',
|
||
|
|
cell_complete: col[6],
|
||
|
|
nationality: col[7],
|
||
|
|
aportador_solidario_name_surname: col[8],
|
||
|
|
aportador_solidario_ind_order: col[9],
|
||
|
|
aportador_solidario_originale_name_surname: col[10],
|
||
|
|
note: col[11],
|
||
|
|
col_b: col[12],
|
||
|
|
col_h: col[13]
|
||
|
|
});
|
||
|
|
|
||
|
|
try {
|
||
|
|
user.date_reg = col[1];
|
||
|
|
} catch (e) {
|
||
|
|
console.log('error ', e);
|
||
|
|
}
|
||
|
|
|
||
|
|
namesurname = tools.extractNameAndSurnameByComplete(user.name_complete);
|
||
|
|
user.name = namesurname.name;
|
||
|
|
user.surname = namesurname.surname;
|
||
|
|
|
||
|
|
if (user.name && user.surname && user.cell_complete) {
|
||
|
|
// Save into db
|
||
|
|
await user.save();
|
||
|
|
numadded++;
|
||
|
|
}
|
||
|
|
|
||
|
|
} catch (e) {
|
||
|
|
console.log('error ', e, col);
|
||
|
|
}
|
||
|
|
|
||
|
|
numtot++;
|
||
|
|
}
|
||
|
|
// numalreadyexisted++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
} catch (e) {
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
ris = { numadded, numtot, numalreadyexisted };
|
||
|
|
|
||
|
|
console.log(ris);
|
||
|
|
return ris
|
||
|
|
|
||
|
|
} catch (e) {
|
||
|
|
console.err(e);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
if (tools.INITDB_FIRSTIME) {
|
||
|
|
console.log(' createIndex User Index...');
|
||
|
|
// ExtraListSchema.index({ username: 'text', name: 'text', surname: 'text', email: 'text' });
|
||
|
|
// ExtraListSchema.index({ name: 'name' });
|
||
|
|
// ExtraListSchema.index({ name: 1 });
|
||
|
|
// ExtraListSchema.index({ surname: 1 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const ExtraList = mongoose.model('ExtraList', ExtraListSchema);
|
||
|
|
|
||
|
|
|
||
|
|
module.exports = { ExtraList };
|
||
|
|
|
||
|
|
|