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 IscrittiConacreisSchema = 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, }, fiscalcode: { 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, }, newsletter_on: { type: Boolean, }, terms: { type: Boolean, }, metodo_pagamento: { type: Number, }, ha_pagato: { type: Boolean, }, iscrizione_compilata: { type: Boolean, }, dateofreg: { type: Date, }, dateofapproved: { type: Date, }, codiceConacreis: { type: String, }, annoTesseramento: { type: Number }, motivazioni: { type: String, }, competenze_professionalita: { type: String, }, cosa_potrei_offrire: { type: String, }, cosa_vorrei_ricevere: { type: String, }, altre_comunicazioni: { type: String, }, come_ci_hai_conosciuto: { type: String, }, note: { type: String, }, }); IscrittiConacreisSchema.pre('save', async function (next) { if (this.isNew) { const myrec = await IscrittiConacreis.findOne().limit(1).sort({ numTesseraInterna: -1 }); if (!!myrec) { this.numTesseraInterna = myrec._doc.numTesseraInterna + 1; } else { this.numTesseraInterna = 0; } } next(); }); var IscrittiConacreis = module.exports = mongoose.model('IscrittiConacreis', IscrittiConacreisSchema); 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 IscrittiConacreis.find({ idapp }).sort({ dateofreg: -1 }).limit(1); if (!!lastrec) { return lastrec[0]; } else { return null; } }; module.exports.getNameSurnameByEmail = async function (idapp, email) { return await IscrittiConacreis.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 IscrittiConacreis.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 IscrittiConacreis.find(myfind, (err, arrrec) => { return arrrec }); };