X - Mettere anche la email del sognatore, per chi è abituato ad inviarla in quel modo... X - Controllare che sul sito compaiano le informazioni del Sognatore...
495 lines
12 KiB
JavaScript
Executable File
495 lines
12 KiB
JavaScript
Executable File
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 { Flotta } = require('./flotta');
|
|
|
|
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 NavePersistenteSchema = new mongoose.Schema({
|
|
idapp: {
|
|
type: String,
|
|
required: true,
|
|
},
|
|
index: {
|
|
type: Number
|
|
},
|
|
riga: {
|
|
type: Number,
|
|
},
|
|
col: {
|
|
type: Number,
|
|
},
|
|
riga1don: {
|
|
type: Number,
|
|
},
|
|
col1don: {
|
|
type: Number,
|
|
},
|
|
date_start: {
|
|
type: Date
|
|
},
|
|
date_gift_chat_open: {
|
|
type: Date
|
|
},
|
|
link_chat: {
|
|
type: String,
|
|
},
|
|
provvisoria: {
|
|
type: Boolean,
|
|
},
|
|
DoniAttesaDiConferma: {
|
|
type: Number,
|
|
},
|
|
DoniMancanti: {
|
|
type: Number,
|
|
},
|
|
DoniConfermati: {
|
|
type: Number,
|
|
},
|
|
DoniTotali: {
|
|
type: Number,
|
|
},
|
|
note_bot: {
|
|
type: String
|
|
},
|
|
note_interne: {
|
|
type: String
|
|
},
|
|
tutor: {
|
|
type: String
|
|
},
|
|
tutor_namesurname: {
|
|
type: String
|
|
}
|
|
});
|
|
|
|
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.link_payment': 1,
|
|
'profile.note_payment': 1,
|
|
'profile.cell': 1,
|
|
made_gift: 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;
|
|
}
|
|
|
|
|
|
NavePersistenteSchema.statics.findById = function (idapp, id) {
|
|
const NavePersistente = this;
|
|
|
|
const myquery = getQueryProj({ idapp, '_id': ObjectID(id) });
|
|
|
|
return NavePersistente.aggregate(myquery);
|
|
|
|
};
|
|
|
|
NavePersistenteSchema.statics.getFieldsForSearch = function () {
|
|
return [{ field: 'ind_order', type: tools.FieldType.number },
|
|
{ field: 'col', type: tools.FieldType.number }]
|
|
};
|
|
|
|
NavePersistenteSchema.statics.executeQueryTable = function (idapp, params) {
|
|
params.fieldsearch = this.getFieldsForSearch();
|
|
return tools.executeQueryTable(this, idapp, params);
|
|
};
|
|
|
|
NavePersistenteSchema.statics.findAllIdApp = function (idapp) {
|
|
const NavePersistente = this;
|
|
|
|
const myfind = { idapp };
|
|
|
|
return NavePersistente.find(myfind).sort({ riga: 1, col: 1 });
|
|
};
|
|
|
|
NavePersistenteSchema.statics.getListaNavi = function (idapp) {
|
|
const NavePersistente = this;
|
|
|
|
const myfind = { idapp };
|
|
|
|
return NavePersistente.find(myfind,
|
|
{
|
|
index: 1,
|
|
riga: 1,
|
|
col: 1,
|
|
riga1don: 1,
|
|
col1don: 1,
|
|
date_gift_chat_open: 1,
|
|
date_start: 1,
|
|
provvisoria: 1,
|
|
DoniConfermati: 1,
|
|
DoniTotali: 1,
|
|
DoniMancanti: 1,
|
|
}
|
|
).sort({ riga: 1, col: 1 });
|
|
};
|
|
|
|
NavePersistenteSchema.statics.getLastNave = function (idapp) {
|
|
const NavePersistente = this;
|
|
|
|
const myfind = { idapp, date_start: { $lte: tools.IncDateNow(0) }, provvisoria: false };
|
|
|
|
return NavePersistente.findOne(myfind,
|
|
{
|
|
index: 1,
|
|
riga: 1,
|
|
col: 1,
|
|
riga1don: 1,
|
|
col1don: 1,
|
|
date_start: 1,
|
|
provvisoria: 1,
|
|
DoniConfermati: 1,
|
|
DoniTotali: 1,
|
|
DoniMancanti: 1,
|
|
}
|
|
).sort({ riga: -1, col: -1 }).limit(1);
|
|
};
|
|
|
|
NavePersistenteSchema.statics.findByRigaColByDonatore = function (idapp, riga, col, offset) {
|
|
const NavePersistente = this;
|
|
|
|
mypos = {
|
|
riga,
|
|
col,
|
|
numup: 3 + offset,
|
|
};
|
|
tools.getRigaColByPosUp(mypos);
|
|
|
|
return NavePersistente.findOne({ idapp, riga: mypos.riga, col: mypos.col });
|
|
|
|
};
|
|
|
|
NavePersistenteSchema.statics.findByRigaCol = function (idapp, riga, col) {
|
|
const NavePersistente = this;
|
|
|
|
return NavePersistente.findOne({ idapp, riga, col });
|
|
|
|
};
|
|
|
|
|
|
NavePersistenteSchema.statics.getLastRigaCol = async function (idapp) {
|
|
const NavePersistente = this;
|
|
return NavePersistente.findOne({ idapp }).sort({ riga: -1, col: -1 });
|
|
};
|
|
|
|
NavePersistenteSchema.statics.getLastRigaColDefinitiva = async function (idapp) {
|
|
const NavePersistente = this;
|
|
|
|
return NavePersistente.findOne({ idapp, provvisoria: false }).sort({ riga: -1, col: -1 });
|
|
};
|
|
|
|
NavePersistenteSchema.pre('save', async function (next) {
|
|
if (this.isNew) {
|
|
const myrec = await NavePersistente.findOne().limit(1).sort({ _id: -1 });
|
|
if (!!myrec) {
|
|
this.index = myrec._doc.index + 1;
|
|
} else {
|
|
this.index = 1;
|
|
}
|
|
}
|
|
|
|
next();
|
|
});
|
|
|
|
function getNextDayNave(miadata) {
|
|
|
|
// const dayofweek = [1, 2, 4, 5]; // LUNEDI, MARTEDI', GIOVEDI, VENERDI
|
|
const dayofweek = [1, 4]; // LUNEDI, GIOVEDI
|
|
|
|
let mydate = tools.AddDate(miadata, 1);
|
|
|
|
while (!dayofweek.includes(mydate.getDay())) {
|
|
mydate = tools.AddDate(mydate, 1);
|
|
}
|
|
|
|
return mydate
|
|
|
|
}
|
|
|
|
NavePersistenteSchema.statics.addRecordNavePersistenteByParams = async function (params) {
|
|
const NavePersistente = this;
|
|
|
|
// Check if Exist:
|
|
const giapresente = await NavePersistente.findOne({
|
|
idapp: params.idapp,
|
|
riga: params.riga,
|
|
col: params.col
|
|
});
|
|
|
|
if (!giapresente) {
|
|
// Prende la nave prima:
|
|
const lastnave = await NavePersistente.findOne({ idapp: params.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 myNavePersistente = new NavePersistente({
|
|
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 myNavePersistente.save();
|
|
}
|
|
|
|
return false;
|
|
};
|
|
|
|
NavePersistenteSchema.statics.generaFlotte = async function (idapp) {
|
|
const NavePersistente = this;
|
|
|
|
// await Flotta.deleteMany({ idapp });
|
|
|
|
const arrnavi = await NavePersistente.find({ idapp, col: { $mod: [8, 1] } }).sort({ riga: 1, col: 1 });
|
|
|
|
let num = 0;
|
|
|
|
for (const navepers of arrnavi) {
|
|
|
|
const ris = await NavePersistente.aggiornaFlottaByNavePersistente(idapp, navepers);
|
|
if (ris)
|
|
num++;
|
|
}
|
|
|
|
return { num }
|
|
|
|
};
|
|
|
|
NavePersistenteSchema.statics.changeField = async function (idapp, flotta, fieldvalue) {
|
|
|
|
let myval = {};
|
|
|
|
if ('date_close' in fieldvalue) {
|
|
myval['date_gift_chat_open'] = fieldvalue['date_close'];
|
|
} else {
|
|
myval = fieldvalue;
|
|
}
|
|
|
|
const ris = await NavePersistente.updateMany({
|
|
idapp,
|
|
riga: flotta.riga,
|
|
$and: [
|
|
{ col1don: { $gte: flotta.col_prima } },
|
|
{ col1don: { $lte: flotta.col_ultima } },
|
|
]
|
|
}, { $set: myval });
|
|
|
|
return ris;
|
|
};
|
|
|
|
NavePersistenteSchema.statics.aggiornaFlottaByNavePersistente = async function (idapp, naveinput) {
|
|
|
|
const { Nave } = require('../models/nave');
|
|
const { User } = require('./user');
|
|
|
|
let num = 0;
|
|
|
|
try {
|
|
let nuovo = false;
|
|
// let primacol = false;
|
|
|
|
indcolflottaprima = tools.getPrimaColFlotta(naveinput.col1don + 7);
|
|
|
|
console.log(num, ' -> [', naveinput.riga, '.', naveinput.col, '] indcolflottaprima=', indcolflottaprima);
|
|
|
|
let ini = Math.ceil(indcolflottaprima / 8);
|
|
let fine = ini + 7;
|
|
|
|
const arrnavi = await NavePersistente.find({
|
|
idapp,
|
|
riga: naveinput.riga,
|
|
$and: [{ col: { $gte: ini } }, { col: { $lte: fine } }]
|
|
}).sort({ riga: 1, col: 1 });
|
|
|
|
let primogiro = true;
|
|
|
|
let myflotta = await Flotta.findOne({ idapp, riga: naveinput.riga, col_prima: indcolflottaprima });
|
|
|
|
if (!myflotta) {
|
|
myflotta = new Flotta({
|
|
_id: new ObjectID(),
|
|
idapp,
|
|
});
|
|
nuovo = true;
|
|
}
|
|
|
|
for (const navepers of arrnavi) {
|
|
|
|
if (primogiro) {
|
|
myflotta.DoniAttesaDiConferma = 0;
|
|
myflotta.DoniMancanti = 0;
|
|
myflotta.DoniConfermati = 0;
|
|
myflotta.DoniTotali = 0;
|
|
primogiro = false;
|
|
}
|
|
|
|
if (nuovo) {
|
|
myflotta.riga = navepers.riga;
|
|
myflotta.col_prima = indcolflottaprima;
|
|
myflotta.col_ultima = indcolflottaprima + 63;
|
|
|
|
myflotta.sognatore = '';
|
|
myflotta.sognatore_nomecognome = '';
|
|
myflotta.link_superchat = '';
|
|
myflotta.msg_inviato = false;
|
|
}
|
|
|
|
myflotta.date_start = navepers.date_start;
|
|
myflotta.date_close = navepers.date_gift_chat_open;
|
|
|
|
let sognatore = await Nave.getSognatoreByRigaColMediatore(idapp, { riga: navepers.riga, col: navepers.col });
|
|
if (myflotta.sognatore_nomecognome === '') {
|
|
myflotta.sognatore = sognatore.username;
|
|
if (!!sognatore.username)
|
|
myflotta.sognatore_nomecognome = await User.getNameSurnameByUsername(idapp, sognatore.username);
|
|
}
|
|
|
|
if (nuovo) {
|
|
myflotta.email_paypal = '';
|
|
myflotta.link_payment = '';
|
|
myflotta.note_payment = '';
|
|
if (!!sognatore) {
|
|
myflotta.email_paypal = sognatore.profile.email_paypal;
|
|
myflotta.link_payment = sognatore.profile.link_payment;
|
|
myflotta.note_payment = sognatore.profile.note_payment;
|
|
}
|
|
}
|
|
|
|
if (!!sognatore) {
|
|
if (!myflotta.email_paypal)
|
|
myflotta.email_paypal = sognatore.profile.email_paypal;
|
|
if (!myflotta.link_payment)
|
|
myflotta.link_payment = sognatore.profile.link_payment;
|
|
if (!myflotta.note_payment)
|
|
myflotta.note_payment = sognatore.profile.note_payment;
|
|
}
|
|
|
|
myflotta.provvisoria = navepers.provvisoria;
|
|
|
|
if (!!navepers.DoniAttesaDiConferma && !isNaN(navepers.DoniAttesaDiConferma))
|
|
myflotta.DoniAttesaDiConferma += navepers.DoniAttesaDiConferma;
|
|
if (!!navepers.DoniMancanti && !isNaN(navepers.DoniMancanti))
|
|
myflotta.DoniMancanti += navepers.DoniMancanti;
|
|
if (!!navepers.DoniConfermati && !isNaN(navepers.DoniConfermati))
|
|
myflotta.DoniConfermati += navepers.DoniConfermati;
|
|
if (!!navepers.DoniTotali && !isNaN(navepers.DoniTotali))
|
|
myflotta.DoniTotali += navepers.DoniTotali;
|
|
|
|
|
|
}
|
|
|
|
await myflotta.save();
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
console.log(e.message);
|
|
return false;
|
|
}
|
|
|
|
};
|
|
|
|
|
|
const NavePersistente = mongoose.model('NavePersistente', NavePersistenteSchema);
|
|
|
|
module.exports = { NavePersistente };
|
|
|
|
|