const mongoose = require('mongoose').set('debug', false); const Schema = mongoose.Schema; const tools = require('../tools/general'); mongoose.Promise = global.Promise; mongoose.level = 'F'; // Resolving error Unknown modifier: $pushAll mongoose.plugin(schema => { schema.options.usePushEach = true; }); const IscrittiArcadeiSchema = new Schema({ idapp: { type: String, }, userId: { type: String, }, numTesseraInterna: { type: Number, default: 0, }, name: { type: String, trim: true, }, surname: { type: String, trim: true, }, email: { type: String, trim: true, }, email2: { type: String, trim: true, }, residency_address: { type: String, }, residency_city: { type: String, trim: true, }, residency_province: { type: String, trim: true, }, residency_country: { type: String, trim: true, }, residency_zipcode: { type: String, trim: true, }, dateofbirth: { type: Date, }, born_city: { type: String, trim: true, }, born_province: { type: String, trim: true, }, born_country: { type: String, trim: true, }, cell_phone: { type: String, }, cell_phone2: { type: String, }, newsletter_on: { type: Boolean, }, terms: { type: Boolean, }, metodo_pagamento: { type: Number, }, doctype: { type: String, }, documentnumber: { type: String, }, ha_pagato: { type: Boolean, }, iscrizione_compilata: { type: Boolean, }, dateofreg: { type: Date, }, dateofapproved: { type: Date, }, codiceArcadei: { type: String, }, annoTesseramento: { type: Number, }, motivazioni: { type: String, }, categorie_interesse: [ { type: Number, }], altre_comunicazioni: { type: String, }, come_ci_hai_conosciuto: { type: String, }, note: { type: String, }, quota_versata: { type: Number, }, }); IscrittiArcadeiSchema.pre('save', async function(next) { if (this.isNew) { const myrec = await IscrittiArcadei.findOne().limit(1).sort({numTesseraInterna: -1}); if (!!myrec) { this.numTesseraInterna = myrec._doc.numTesseraInterna + 1; } else { this.numTesseraInterna = 0; } } next(); }); var IscrittiArcadei = module.exports = mongoose.model('IscrittiArcadei', IscrittiArcadeiSchema); module.exports.getFieldsForSearch = function() { return [ {field: 'name', type: tools.FieldType.string}, {field: 'surname', type: tools.FieldType.string}, {field: 'email', type: tools.FieldType.string}]; }; module.exports.executeQueryTable = function(idapp, params) { params.fieldsearch = this.getFieldsForSearch(); return tools.executeQueryTable(this, idapp, params); }; module.exports.getLastRec = async function(idapp) { const lastrec = await IscrittiArcadei.find({idapp}).sort({dateofreg: -1}).limit(1); if (!!lastrec) { return lastrec[0]; } else { return null; } }; module.exports.getNameSurnameByEmail = async function(idapp, email) { return await IscrittiArcadei.findOne({ idapp, email, }, {name: 1, surname: 1}).then((rec) => { return (!!rec) ? `${rec.name} ${rec.surname}` : ''; }).catch((e) => { console.error('getNameSurnameByUsername', e); }); }; module.exports.findByEmail = function(idapp, email) { return IscrittiArcadei.findOne({ 'idapp': idapp, 'email': email, $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], }); }; module.exports.findAllIdApp = async function(idapp) { const myfind = {idapp}; return await IscrittiArcadei.find(myfind, (err, arrrec) => { return arrrec; }); };