const mongoose = require('mongoose'); const _ = require('lodash'); const tools = require('../tools/general'); const { ListaIngresso } = require('./listaingresso'); const { Settings } = require('./settings'); const { User } = require('./user'); const { ObjectID } = require('mongodb'); const shared_consts = require('../tools/shared_nodejs'); 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); const FlottaSchema = new mongoose.Schema({ idapp: { type: String, required: true, }, index: { type: Number }, riga: { type: Number, }, col_prima: { type: Number, }, col_ultima: { type: Number, }, date_start: { type: Date }, date_close: { type: Date }, provvisoria: { type: Boolean, }, DoniAttesaDiConferma: { type: Number, }, DoniMancanti: { type: Number, }, DoniConfermati: { type: Number, }, DoniTotali: { type: Number, }, note_interne: { type: String }, sognatore: { type: String }, sognatore_nomecognome: { type: String }, link_superchat: { type: String, }, link_payment: { type: String, }, email_paypal: { type: String, }, revolut: { type: String, }, note_payment: { type: String, }, tutor1: { type: String, }, tutor2: { type: String, }, tutor3: { type: String, }, tutorslo: { type: String, }, msg_inviato: { type: Boolean, }, }); function getQueryProj(myfilter) { myobjField = { _id: 1, idapp: 1, lang: 1, ind_order: 1, name: 1, surname: 1, username: 1, 'profile.paymenttypes': 1, 'profile.email_paypal': 1, 'profile.revolut': 1, 'profile.cell': 1, made_gift: 1, commento_al_sognatore: 1, sent_msg_howto_make_gift: 1, date_made_gift: 1, note: 1, received_gift: 1, date_received_gift: 1, num_tess: 1, parent_id: 1, riga: 1, col: 1, created: 1, // date_start: 1, // date_gift_chat_open: 1, // link_chat: 1, // provvisoria: 1, // note_bot: 1, // note_interne: 1, // tutor: 1, // tutor_namesurname: 1, }; const query = [ { $match: myfilter }, { $lookup: { from: "listaingressos", localField: "ind_order", foreignField: "ind_order", // field in the user collection as: "mylista" } }, { $replaceRoot: { newRoot: { $mergeObjects: [ { $arrayElemAt: [ "$mylista", 0 ] }, "$$ROOT" ] } } }, { $lookup: { from: "users", as: "user", let: {username: '$username' }, pipeline: [ { $match: { $expr: { $and: [ { $eq: ['$username', '$$username'] }, { $eq: ['$idapp', myfilter.idapp] }, ] } } } ] } }, { $replaceRoot: { newRoot: { $mergeObjects: [{ $arrayElemAt: ["$user", 0] }, "$$ROOT"] } } // $replaceRoot: { newRoot: { $mergeObjects: [{ $arrayElemAt: ["$user", 0] },] } } }, { $project: myobjField } ]; return query; } FlottaSchema.statics.findById = function (idapp, id) { const Flotta = this; const myquery = getQueryProj({ idapp, '_id': ObjectID(id) }); return Flotta.aggregate(myquery); }; FlottaSchema.statics.getFieldsForSearch = function () { return [{ field: 'ind_order', type: tools.FieldType.number }, { field: 'col', type: tools.FieldType.number }] }; FlottaSchema.statics.executeQueryTable = function (idapp, params) { params.fieldsearch = this.getFieldsForSearch(); return tools.executeQueryTable(this, idapp, params); }; FlottaSchema.statics.findAllIdApp = function (idapp) { const Flotta = this; const myfind = { idapp }; return Flotta.find(myfind).sort({ riga: 1, col: 1 }); }; FlottaSchema.statics.getListaFlotta = function (idapp) { const Flotta = this; const myfind = { idapp }; return Flotta.find(myfind).sort({ riga: 1, col_prima: 1 }); }; FlottaSchema.statics.getLastFlotta = function (idapp) { const Flotta = this; const myfind = { idapp, date_start: { $lte: tools.IncDateNow(0) }, provvisoria: false }; return Flotta.findOne(myfind).sort({ riga: -1, col_prima: -1 }).limit(1); }; FlottaSchema.statics.findByRigaColByDonatore = function (idapp, riga, col, offset) { const Flotta = this; mypos = { riga, col, numup: 3 + offset, }; tools.getRigaColByPosUp(mypos); return Flotta.findOne({ idapp, riga: mypos.riga, col_prima: mypos.col }); }; FlottaSchema.statics.findByRigaCol = function (idapp, riga, col) { const Flotta = this; return Flotta.findOne({ idapp, riga, col }); }; FlottaSchema.statics.getLastRigaCol = async function (idapp) { return Flotta.findOne({ idapp }).sort({ riga: -1, col_prima: -1 }); }; FlottaSchema.statics.getLastRigaColDefinitiva = async function (idapp) { return Flotta.findOne({ idapp, provvisoria: false }).sort({ riga: -1, col_prima: -1 }); }; FlottaSchema.pre('save', async function (next) { if (this.isNew) { const myrec = await Flotta.findOne().limit(1).sort({ _id: -1 }); if (!!myrec) { this.index = myrec._doc.index + 1; } else { this.index = 1; } } next(); }); FlottaSchema.statics.addRecordFlottaByParams = async function (params) { // Check if Exist: const giapresente = await Flotta.findOne({ idapp: params.idapp, riga: params.riga, col: params.col }); if (!giapresente) { // Prende la nave prima: const lastnave = await Flotta.findOne({idapp}).sort({riga: -1, col: -1}); let nextgiftchat = null; if (((params.col - 1) % 8 === 0)) { nextgiftchat = getNextDayNave(lastnave.date_gift_chat_open); } else { nextgiftchat = lastnave.date_gift_chat_open; } const next_date_start = tools.AddDate(nextgiftchat, 7); let myFlotta = new Flotta({ idapp: params.idapp, riga: params.riga, col: params.col, riga1don: params.riga1don, col1don: params.col1don, date_gift_chat_open: nextgiftchat, date_start: next_date_start, provvisoria: true, }); return await myFlotta.save(); } return false; }; FlottaSchema.statics.getFlottaByNavePersistente = async function (idapp, navepers) { const Flotta = this; // const indcolflottaprima = tools.getPrimaColFlotta(navepers.col1don + 7); const myflotta = await Flotta.findOne({ idapp, riga: navepers.riga, $and: [ { col_prima: { $lte: navepers.col1don } }, { col_ultima: { $gte: navepers.col1don } } ] }); return myflotta; }; FlottaSchema.statics.getStrFlotta = function (flotta) { return `Flotta ${flotta.riga}.${Math.ceil(flotta.col_prima / 8)} - ${flotta.riga}.${Math.ceil(flotta.col_ultima / 8)}: Sognatore: ${flotta.sognatore_nomecognome}`; }; FlottaSchema.statics.getFlottaByRigaColDonatore = async function (idapp, riga, col) { const Flotta = this; let mypos = { idapp, riga, col, numup: 3 }; tools.getRigaColByPosUp(mypos); const myflotta = await Flotta.findOne({ idapp, riga: mypos.riga, $and: [ { col_prima: { $lte: col } }, { col_ultima: { $gte: col } } ] }); return myflotta; }; const Flotta = mongoose.model('Flotta', FlottaSchema); module.exports = { Flotta };