2020-03-10 21:44:14 +01:00
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 NaveSchema = new mongoose . Schema ( {
idapp : {
type : String ,
required : true ,
} ,
riga : {
type : Number ,
} ,
col : {
type : Number ,
} ,
indprimario : {
type : Number ,
} ,
ind _order : {
type : Number ,
} ,
parent _id : {
type : mongoose . Schema . Types . ObjectId ,
} ,
date _start : {
type : Date
} ,
2020-03-21 10:28:26 +01:00
date _gift _chat _open : {
type : Date
} ,
2020-03-10 21:44:14 +01:00
link _chat : {
type : String ,
} ,
sent _msg _howto _make _gift : {
type : Boolean ,
default : false ,
} ,
made _gift : {
type : Boolean ,
default : false ,
} ,
date _made _gift : {
type : Date ,
} ,
received _gift : {
type : Boolean ,
default : false ,
} ,
date _received _gift : {
type : Date ,
} ,
created : {
type : Date ,
} ,
num _tess : { // numero di tessitura
type : Number ,
default : 1
2020-03-21 10:28:26 +01:00
} ,
provvisoria : {
type : Boolean
2020-03-10 21:44:14 +01:00
}
} ) ;
NaveSchema . statics . getTotInLista = async function ( idapp ) {
const Nave = this ;
const myfind = { idapp } ;
return await Nave . count ( myfind ) ;
} ;
NaveSchema . statics . findByIndOrder = function ( idapp , ind _order ) {
const Nave = this ;
try {
return Nave . findOne ( {
'idapp' : idapp ,
'ind_order' : ind _order ,
} ) ;
} catch ( e ) {
}
} ;
NaveSchema . statics . findById = function ( idapp , id ) {
const Nave = this ;
const myquery = getQueryProj ( { idapp , '_id' : ObjectID ( id ) } ) ;
return Nave . aggregate ( myquery ) ;
} ;
NaveSchema . statics . findByIndPrimario = function ( idapp , indprimario ) {
const Nave = this ;
try {
return Nave . findOne ( {
idapp ,
indprimario ,
} ) ;
} catch ( e ) {
}
} ;
NaveSchema . statics . getFieldsForSearch = function ( ) {
2020-03-21 10:28:26 +01:00
return [ { field : 'ind_order' , type : tools . FieldType . number } ,
{ field : 'col' , type : tools . FieldType . number } ]
2020-03-10 21:44:14 +01:00
} ;
NaveSchema . statics . executeQueryTable = function ( idapp , params ) {
params . fieldsearch = this . getFieldsForSearch ( ) ;
return tools . executeQueryTable ( this , idapp , params ) ;
} ;
NaveSchema . statics . findAllIdApp = function ( idapp ) {
const Nave = this ;
const myfind = { idapp } ;
return Nave . find ( myfind , ( err , arrrec ) => {
return arrrec
} ) ;
} ;
NaveSchema . statics . findByRigaCol = function ( idapp , riga , col , nullifnotexist ) {
const Nave = this ;
myrigacol = {
idapp ,
riga ,
col ,
nullifnotexist
} ;
return Nave . findGeneric ( myrigacol ) ;
} ;
NaveSchema . statics . findDonatoreByNave = function ( idapp , nave ) {
try {
const arrrigacol = nave . split ( "." ) ;
if ( arrrigacol . length <= 0 )
return ;
let riganave = parseInt ( arrrigacol [ 0 ] ) ;
let colnave = parseInt ( arrrigacol [ 1 ] ) ;
let rigadonatore = riganave + 3 ;
if ( rigadonatore < 1 )
rigadonatore = 1 ;
coldonatoreIni = calcval ( riganave , colnave , 8 ) + ( 1 ) ;
coldonatoreFine = calcval ( riganave , colnave , 8 ) + ( 8 ) ;
const miaquery = {
idapp ,
riga : rigadonatore ,
$and : [ { col : { $gte : { coldonatoreIni } } } , { col : { $lte : { coldonatoreFine } } } ]
} ;
return Nave . findOne ( miaquery ) ;
} catch ( e ) {
}
} ;
function getIndColonneByNave ( navemediatore ) {
let riga = navemediatore . riga ;
let col = navemediatore . col ;
let col _ini = calcval ( riga , col , 8 ) + 1 ;
2020-03-21 10:28:26 +01:00
let col _fine = col _ini + 7 ;
2020-03-10 21:44:14 +01:00
return { riga : riga + 3 , col _ini , col _fine }
}
NaveSchema . statics . getusersByNave = function ( idapp , navemediatore ) {
2020-03-21 10:28:26 +01:00
if ( ( ! navemediatore . riga ) || ( ! navemediatore . col ) )
return [ ] ;
2020-03-10 21:44:14 +01:00
const obj = getIndColonneByNave ( navemediatore ) ;
const miacol _ini = obj . col _ini ;
const miacol _fine = obj . col _fine ;
const miaquery = {
idapp ,
riga : obj . riga ,
$and : [
{ col : { $gte : miacol _ini } } ,
{ col : { $lte : miacol _fine } }
] ,
} ;
return Nave . find ( miaquery ) ;
} ;
NaveSchema . statics . findRecByRigaColParent = async function ( idapp , riga , col , numparentUp ) {
const Nave = this ;
const { User } = require ( './user' ) ;
const myrigacol = {
idapp ,
riga ,
col
} ;
if ( numparentUp === 0 ) {
return await Nave . findGeneric ( myrigacol ) ;
}
let parentup = 0 ;
let myrec = null ;
let lastrec = null ;
while ( parentup < numparentUp ) {
myrec = await Nave . findGeneric ( myrigacol ) ;
if ( ! ! myrec ) {
if ( myrec . parent _id === "0" ) {
break ;
}
recfind = await Nave . findById ( idapp , myrec . parent _id ) ;
if ( ! ! recfind ) {
if ( recfind . length > 0 ) {
recfind = recfind [ 0 ] ;
lastrec = recfind ;
myrigacol . riga = lastrec . riga ;
myrigacol . col = lastrec . col ;
} else {
2020-03-21 10:28:26 +01:00
//lastrec = await User.findByIndOrder(idapp, 0);
lastrec = await Nave . findGeneric ( { idapp , riga : 0 , col : 0 } ) ;
2020-03-10 21:44:14 +01:00
break ;
}
}
}
parentup ++ ;
}
if ( ! lastrec ) {
2020-03-21 10:28:26 +01:00
// return await User.findByIndOrder(idapp, 0);
return await Nave . findGeneric ( { idapp , riga : 0 , col : 0 } ) ;
2020-03-10 21:44:14 +01:00
}
return lastrec ;
} ;
function getlimiti ( mypos ) {
if ( mypos . col < 0 ) {
mypos . col = 0 ;
}
if ( mypos . riga < 0 ) {
mypos . riga = 0 ;
mypos . col = 0 ;
}
return mypos ;
}
function getRigaColGenerica ( idapp , riga , col , numup ) {
mypos = {
idapp ,
riga ,
col ,
numup
} ;
if ( idapp === tools . AYNI ) {
2020-03-21 10:28:26 +01:00
tools . getRigaColByPosUp ( mypos ) ;
2020-03-10 21:44:14 +01:00
ris = getlimiti ( mypos ) ;
}
return ris ;
}
function getRigaColSognatoreByFuoco ( idapp , riga , col ) {
return getRigaColGenerica ( idapp , riga , col , 6 ) ;
}
function getRigaColMediatoreByFuoco ( idapp , riga , col ) {
return getRigaColGenerica ( idapp , riga , col , 3 ) ;
}
NaveSchema . statics . findGeneric = function ( myrigacol ) {
const { ListaIngresso } = require ( './listaingresso' ) ;
const myquery = getQueryProj ( { idapp : myrigacol . idapp , riga : myrigacol . riga , col : myrigacol . col } ) ;
return Nave . aggregate ( myquery )
. then ( async ( rec ) => {
if ( ! rec || rec . length === 0 ) {
if ( myrigacol . nullifnotexist )
return null ;
const ris = await ListaIngresso . findByIndOrder ( myrigacol . idapp , 0 ) ;
const arr = [ ] ;
arr . push ( ris ) ;
return arr ;
}
else {
return rec ;
}
}
) . then ( async ( rec ) => {
const { User } = require ( './user' ) ;
if ( ! ! rec ) {
if ( rec . length > 0 ) {
// console.log('rec', rec);
// console.table('myrigacol', myrigacol);
if ( ! ! rec [ 0 ] ) {
const newrec = await User . getSmallRecByIndOrder ( myrigacol . idapp , rec [ 0 ] . ind _order ) ;
if ( ! ! newrec ) {
let myarr = { } ;
if ( rec [ 0 ] . _doc === undefined )
myarr = { ... newrec . _doc , ... rec [ 0 ] } ;
else
myarr = { ... newrec . _doc , ... rec [ 0 ] . _doc } ;
return myarr ;
} else {
let myarr = null ;
if ( rec [ 0 ] . _doc === undefined )
myarr = { ... newrec . _doc , ... rec [ 0 ] } ;
else
myarr = { ... newrec . _doc , ... rec [ 0 ] . _doc } ;
return myarr ;
}
} else {
const ris = await ListaIngresso . findByIndOrder ( myrigacol . idapp , 0 ) ;
if ( ! ! ris )
return ris [ 0 ]
}
}
}
return null ;
} ) . catch ( ( e ) => {
console . error ( e ) ;
} ) ;
}
;
NaveSchema . statics . findSognatoriByFuoco = async function ( idapp , riga , col , offset ) {
const Nave = this ;
// const myrigacol = getRigaColSognatoreByFuoco(idapp, riga, col);
// return Nave.findGeneric(myrigacol);
return [ await Nave . findRecByRigaColParent ( idapp , riga , col , 6 - offset ) ,
await Nave . findRecByRigaColParent ( idapp , riga , col , 5 - offset ) ,
await Nave . findRecByRigaColParent ( idapp , riga , col , 4 - offset )
]
} ;
NaveSchema . statics . findMediatoreByFuoco = async function ( idapp , riga , col , offset ) {
const Nave = this ;
// const myrigacol = getRigaColMediatoreByFuoco(idapp, riga, col);
// return Nave.findGeneric(myrigacol);
return await Nave . findRecByRigaColParent ( idapp , riga , col , 3 - offset ) ;
} ;
function getQueryProj ( myfilter ) {
myobjField = {
_id : 1 ,
2020-03-21 10:28:26 +01:00
idapp : 1 ,
2020-03-10 21:44:14 +01:00
lang : 1 ,
ind _order : 1 ,
name : 1 ,
surname : 1 ,
username : 1 ,
2020-03-21 10:28:26 +01:00
'profile.paymenttypes' : 1 ,
'profile.email_paypal' : 1 ,
2020-03-10 21:44:14 +01:00
date _start : 1 ,
2020-03-21 10:28:26 +01:00
date _gift _chat _open : 1 ,
2020-03-10 21:44:14 +01:00
made _gift : 1 ,
link _chat : 1 ,
date _made _gift : 1 ,
2020-03-21 10:28:26 +01:00
provvisoria : 1 ,
2020-03-10 21:44:14 +01:00
received _gift : 1 ,
date _received _gift : 1 ,
num _tess : 1 ,
indprimario : 1 ,
parent _id : 1 ,
riga : 1 ,
col : 1 ,
created : 1 ,
} ;
const query = [
{ $match : myfilter } ,
{
$lookup : {
from : "users" ,
localField : "ind_order" ,
foreignField : "ind_order" , // field in the user collection
as : "user"
}
} ,
{
$replaceRoot : { newRoot : { $mergeObjects : [ { $arrayElemAt : [ "$user" , 0 ] } , "$$ROOT" ] } }
// $replaceRoot: { newRoot: { $mergeObjects: [{ $arrayElemAt: ["$user", 0] },] } }
} ,
{ $project : myobjField }
] ;
return query ;
}
NaveSchema . statics . setRiga = function ( idapp , riga ) {
return Settings . setKeyNum ( idapp , 'riga' , riga ) ;
} ;
NaveSchema . statics . getRiga = function ( idapp ) {
return Settings . getKeyNum ( idapp , 'riga' , 1 ) ;
} ;
NaveSchema . statics . setCol = function ( idapp , col ) {
return Settings . setKeyNum ( idapp , 'col' , col ) ;
} ;
NaveSchema . statics . getCol = function ( idapp ) {
return Settings . getKeyNum ( idapp , 'col' , 1 ) ;
} ;
function getmaxcol ( riga ) {
return Math . pow ( 2 , riga - 1 ) ;
}
function getPrimoFuocoByIndCol ( col ) {
2020-03-21 10:28:26 +01:00
// let ris = Math.ceil(col - (col % 8)) + 1;
let ris = ( ( Math . ceil ( col / 8 ) - 1 ) * 8 ) + 1 ;
if ( ris <= 0 )
ris = 1 ;
return ris
2020-03-10 21:44:14 +01:00
}
async function getusertextbyrec ( myrec , symbol , lettera , riga , col , ind ) {
let visu _test = false ;
if ( ! ! myrec )
visu _test = await Settings . getValDbSettings ( myrec . idapp , 'VISU_TEST' ) ;
if ( ! visu _test ) {
if ( myrec )
return lettera + ind + ' - ' + symbol + ' ' + myrec . name + ' ' + tools . getFirst2Car ( myrec . surname ) + ' (' + myrec . username + ')' ;
else
return lettera + ind + ' - ' + symbol ;
} else {
if ( myrec )
2020-03-21 10:28:26 +01:00
return lettera + ind + ' - ' + symbol + '[' + riga + ',' + col + ']' + ' ' + myrec . name + ' ' + myrec . surname + '' ;
2020-03-10 21:44:14 +01:00
else
return lettera + ind + ' - ' + symbol ;
}
}
2020-03-21 10:28:26 +01:00
function checkifNullThenEmpty ( rec , riga , col ) {
if ( rec === null ) {
return {
name : '' ,
surname : '' ,
username : '' ,
riga ,
col ,
}
} else {
return rec ;
}
}
2020-03-10 21:44:14 +01:00
NaveSchema . statics . getPlaccaGenerica = async function ( idapp , riga , col , offset , solorecord ) {
const Nave = this ;
try {
let recsognatori = await Nave . findSognatoriByFuoco ( idapp , riga , col , offset ) ;
let recmediatore = await Nave . findMediatoreByFuoco ( idapp , riga , col , offset ) ;
let primofuoco = getPrimoFuocoByIndCol ( col ) ;
mystr = tools . ACAPO ;
mystr += 'GIFTING CHAT (' + riga + '.' + col + ') ' + tools . ACAPO + tools . ACAPO ;
if ( offset === tools . Placca . SONOFUOCO ) {
mystr += tools . Placca . SOGNATORE + tools . Placca . STR _SOGNATORE + ': ' + await getusertextbyrec ( recsognatori [ 0 ] , '' , '' , riga , col , '' ) + tools . ACAPO + tools . ACAPO ;
mystr += tools . Placca . MEDIATORE + tools . Placca . STR _MEDIATORE + ': ' + await getusertextbyrec ( recmediatore , '' , '' , riga , col , '' ) + tools . ACAPO + tools . ACAPO ;
}
if ( offset === tools . Placca . SONOACQUA ) {
mystr += tools . Placca . SOGNATORE + tools . Placca . STR _SOGNATORE + ': ' + await getusertextbyrec ( recmediatore , '' , '' , riga , col , '' ) + tools . ACAPO + tools . ACAPO ;
}
let symb = {
car : 'D' ,
icona : '🔥'
} ;
if ( offset === tools . Placca . SONOFUOCO ) {
mystr += tools . Placca . STR _DONATORI + ':' + tools . ACAPO ;
} else if ( offset === tools . Placca . SONOACQUA ) {
mystr += tools . Placca . STR _MEDIATORI + ':' + tools . ACAPO ;
symb = {
car : 'A' ,
icona : '💦'
} ;
}
// mystr += '🔥🌏💦💨🔥🌏💦💨' + tools.ACAPO;
let arrdonatori = [ ] ;
2020-03-21 10:28:26 +01:00
// let numcol = Math.pow(2, indriga - 1);
primofuoco = getPrimoFuocoByIndCol ( col * Math . pow ( 2 , offset ) ) ;
2020-03-10 21:44:14 +01:00
let ind = 1 ;
for ( let indcol = primofuoco ; indcol < primofuoco + 8 ; indcol ++ ) {
let indr = riga + offset ;
let indc = indcol + ( offset * indr ) ;
indr = riga + offset ;
2020-03-21 10:28:26 +01:00
// indc = calcval(riga, col, 8) + (indcol - primofuoco + 1);
if ( offset === 0 )
indc = indcol ;
else
indc = indcol ;
2020-03-10 21:44:14 +01:00
// miacol = calcval(riga, col, 8) + (indfuoco);
let recfuoco = await Nave . findByRigaCol ( idapp , indr , indc , true ) ;
2020-03-21 10:28:26 +01:00
recfuoco = checkifNullThenEmpty ( recfuoco , indr , indc ) ;
2020-03-10 21:44:14 +01:00
arrdonatori . push ( recfuoco ) ;
mystr += await getusertextbyrec ( recfuoco , symb . icona , symb . car , indr , indc , ind ) + tools . ACAPO ;
ind ++ ;
}
if ( solorecord ) {
res = {
recsognatori ,
recmediatore ,
arrdonatori
} ;
return res ;
} else {
return mystr ;
}
} catch ( e ) {
console . error ( e ) ;
}
} ;
2020-03-21 10:28:26 +01:00
NaveSchema . statics . getPlaccaPerDonatore = async function ( idapp , riga , col , solorecord , rec ) {
const Nave = this ;
if ( solorecord ) {
rec . donatore = await Nave . getPlaccaGenerica ( idapp , riga , col , tools . Placca . SONOFUOCO , solorecord ) ;
newcol = Math . ceil ( col / ( Math . pow ( 2 , 3 ) ) ) ;
rec . donatore . arrterra = await Nave . getArrTerra ( idapp , riga - 3 , newcol ) ;
rec . donatore . arraria = await Nave . getArrAria ( idapp , riga - 3 , newcol ) ;
return rec ;
} else {
return await Nave . getPlaccaGenerica ( idapp , riga , col , tools . Placca . SONOFUOCO , solorecord ) ;
}
} ;
NaveSchema . statics . getPlaccaPerMediatore = async function ( idapp , riga , col , solorecord , rec ) {
2020-03-10 21:44:14 +01:00
const Nave = this ;
2020-03-21 10:28:26 +01:00
rec . mediatore = await Nave . getPlaccaGenerica ( idapp , riga , col , tools . Placca . SONOACQUA , solorecord ) ;
rec . mediatore . arrterra = await Nave . getArrTerra ( idapp , riga , col ) ;
rec . mediatore . arraria = await Nave . getArrAria ( idapp , riga , col ) ;
return rec ;
2020-03-10 21:44:14 +01:00
} ;
2020-03-21 10:28:26 +01:00
NaveSchema . statics . getArrTerra = async function ( idapp , riga , col ) {
2020-03-10 21:44:14 +01:00
const Nave = this ;
2020-03-21 10:28:26 +01:00
let arrterra = [ ] ;
for ( let indterra = 1 ; indterra <= 2 ; indterra ++ ) {
miacol = calcval ( riga , col , 2 ) + ( indterra ) ;
miariga = riga + 1 ;
let rec = await Nave . findByRigaCol ( idapp , miariga , miacol , true ) ;
rec = checkifNullThenEmpty ( rec , miariga , miacol ) ;
arrterra . push ( rec ) ;
}
return arrterra ;
} ;
NaveSchema . statics . getArrAria = async function ( idapp , riga , col ) {
const Nave = this ;
let arraria = [ ] ;
for ( let indaria = 1 ; indaria <= 4 ; indaria ++ ) {
miacol = calcval ( riga , col , 4 ) + ( indaria ) ;
miariga = riga + 2 ;
let rec = await Nave . findByRigaCol ( idapp , miariga , miacol , true ) ;
rec = checkifNullThenEmpty ( rec , miariga , miacol ) ;
arraria . push ( rec ) ;
}
return arraria ;
2020-03-10 21:44:14 +01:00
} ;
function calcval ( riga , col , quanti ) {
return ( quanti * ( col - 1 ) ) ;
}
NaveSchema . statics . getNavePos = async function ( idapp , riga , col , solorecord ) {
const Nave = this ;
try {
let recsognatori = [ await Nave . findRecByRigaColParent ( idapp , riga , col , 3 ) ,
await Nave . findRecByRigaColParent ( idapp , riga , col , 2 ) ,
await Nave . findRecByRigaColParent ( idapp , riga , col , 1 ) ] ;
let recmediatore = await Nave . findByRigaCol ( idapp , riga , col , true ) ;
mystr = '🌈SOGNATORE: ' ;
for ( let indsogn = 0 ; indsogn < recsognatori . length ; indsogn ++ ) {
mystr += 'A' + ( 3 - indsogn ) + await getusertextbyrec ( recsognatori [ indsogn ] , '' , '' , riga , col , '' ) + tools . ACAPO ;
}
mystr += tools . ACAPO + '🌀 MEDIATORE:' + await getusertextbyrec ( recmediatore , '' , '' , riga , col , '' ) + tools . ACAPO + tools . ACAPO ;
for ( let indterra = 1 ; indterra <= 2 ; indterra ++ ) {
miacol = calcval ( riga , col , 2 ) + ( indterra ) ;
miariga = riga + 1 ;
let recterra = await Nave . findByRigaCol ( idapp , miariga , miacol , true ) ;
mystr += await getusertextbyrec ( recterra , '' , 'B' , miariga , miacol , indterra ) + tools . ACAPO ;
}
mystr += tools . ACAPO ;
for ( let indaria = 1 ; indaria <= 4 ; indaria ++ ) {
miacol = calcval ( riga , col , 4 ) + ( indaria ) ;
miariga = riga + 2 ;
let recaria = await Nave . findByRigaCol ( idapp , miariga , miacol , true ) ;
mystr += await getusertextbyrec ( recaria , '' , 'C' , miariga , miacol , indaria ) + tools . ACAPO ;
}
mystr += tools . ACAPO ;
let primofuoco = null ;
mystr += '🎁 DONATORI:' + tools . ACAPO ;
for ( let indfuoco = 1 ; indfuoco <= 8 ; indfuoco ++ ) {
miacol = calcval ( riga , col , 8 ) + ( indfuoco ) ;
miariga = riga + 3 ;
// miariga = riga + 2;
let recfuoco = await Nave . findByRigaCol ( idapp , miariga , miacol , true ) ;
if ( indfuoco === 1 )
primofuoco = recfuoco ;
mystr += await getusertextbyrec ( recfuoco , '' , 'D' , miariga , miacol , indfuoco ) + tools . ACAPO ;
}
let data = "" ;
2020-03-21 10:28:26 +01:00
let mystrtemp = '' ;
if ( primofuoco ) {
data = '💬 Giorno di Apertura GIFT CHAT: ' + tools . getstrDateLong ( primofuoco . date _gift _chat _open ) + tools . ACAPO ;
data += "🎁 Giorno in cui Inviare il DONO : " + tools . getstrDateLong ( primofuoco . date _start ) + tools . ACAPO ;
data += tools . ACAPO ;
data += 'Il Mediatore sarà contattato 1 giorno prima dal Tutor' + tools . ACAPO ;
if ( primofuoco . provvisoria )
mystrtemp = 'TEMPORANEA ' ;
} else
2020-03-10 21:44:14 +01:00
data = "" ;
2020-03-21 10:28:26 +01:00
mystr = tools . ACAPO + 'NAVE ' + '[' + riga + '.' + col + '] ' + mystrtemp + tools . ACAPO + data + tools . ACAPO + mystr ;
2020-03-10 21:44:14 +01:00
return mystr ;
} catch ( e ) {
console . error ( e ) ;
}
} ;
async function Fuochi8Completati ( idapp , params ) {
// Inviare un msg al Mediatore che può aprire la Chat con gli 8 fuochi
const telegrambot = require ( '../telegram/telegrambot' ) ;
let text = '\nCompletata NAVE [riga=' + params . riga + ',col=' + params . col + ', ind_order=' + params . ind _order + ']' ;
console . log ( text ) ;
tools . writeNaveLog ( text ) ;
const { User } = require ( './user' ) ;
const rec = await User . findByIndOrder ( idapp , params . ind _order ) ;
text = await Nave . getNaveByUser ( idapp , params . ind _order , rec . lang , true ) ;
// Inviare un msg a questi 8 Fuochi, che la loro placca è Pronta !
if ( tools . isAbilitaNave ( idapp ) ) {
// .............
}
if ( false ) {
await telegrambot . sendMsgTelegram ( idapp , rec . username , text , true ) ;
} else {
await telegrambot . sendMsgTelegramToTheAdmin ( idapp , text ) ;
}
// Inviare la placca a Managers
// await telegrambot.sendMsgTelegramToTheManagers(idapp, txt);
}
NaveSchema . statics . getArrPosizioniByIndOrder = async function ( idapp , ind _order ) {
const Nave = this ;
arrposizioni = await Nave . find ( { 'idapp' : idapp , ind _order } ) . sort ( { riga : 1 , col : 1 } ) ;
return arrposizioni ;
} ;
NaveSchema . statics . getPrimaNaveByRiga = async function ( idapp , riga ) {
const Nave = this ;
2020-03-21 10:28:26 +01:00
nave = await Nave . findOne ( { 'idapp' : idapp , riga : ( riga + 3 ) , col : 1 } ) ;
2020-03-10 21:44:14 +01:00
return nave ;
} ;
NaveSchema . statics . showListaOrd = async function ( idapp ) {
const Nave = this ;
const myquery = getQueryProj ( { idapp } ) ;
arrrec = await Nave . aggregate ( myquery ) . sort ( { riga : 1 , col : 1 } ) ;
let mystr = '' ;
let conta = 1 ;
for ( const rec of arrrec ) {
mystr += '[' + conta + '] [' + rec . riga + '.' + rec . col + '] ' + rec . ind _order + ' ' + rec . name + ' ' + rec . surname + ' (' + tools . getstrDateShort ( rec . date _start ) + ')\n' ;
conta ++ ;
}
return mystr ;
} ;
2020-03-21 10:28:26 +01:00
NaveSchema . statics . getLastRigaCol = async function ( idapp ) {
return Nave . findOne ( { idapp } ) . sort ( { _id : - 1 } ) ;
} ;
2020-03-10 21:44:14 +01:00
async function addRecordNaveByParams ( params , siRitesse ) {
const { ListaIngresso } = require ( './listaingresso' ) ;
if ( ! siRitesse ) {
// Check if Exist:
2020-03-21 10:28:26 +01:00
const giapresente = await Nave . findOne ( {
idapp : params . idapp ,
ind _order : params . ind _order ,
num _tess : params . num _tess
} ) ;
2020-03-10 21:44:14 +01:00
if ( ! ! giapresente ) {
let fields _to _update = { added : true } ;
await ListaIngresso . findOneAndUpdate ( { _id : params . id . toString ( ) } , { $set : fields _to _update } , { new : false } ) ;
return false ;
}
}
let myNave = new Nave ( {
idapp : params . idapp ,
indprimario : params . indprimario ,
ind _order : params . ind _order ,
riga : params . riga ,
col : params . col ,
date _start : params . date _start ,
2020-03-21 10:28:26 +01:00
date _gift _chat _open : params . date _gift _chat _open ,
2020-03-10 21:44:14 +01:00
num _tess : params . num _tess ,
2020-03-21 10:28:26 +01:00
provvisoria : params . provvisoria ,
2020-03-10 21:44:14 +01:00
} ) ;
myNave . created = new Date ( ) ;
console . log ( '[' + params . riga + ',' + params . col + ']' ) ;
const recfindparent = await Nave . findByRigaCol ( idapp , params . rigaparent , params . colparent , false ) ;
if ( ! ! recfindparent ) {
myNave . parent _id = recfindparent . _id ;
} else {
myNave . parent _id = "0" ;
}
console . log ( 'addRecordNaveByParams (' + myNave . parent _id + ')' ) ;
return await myNave . save ( )
. then ( async ( result ) => {
if ( ! ! result ) {
let fields _to _update = { added : true } ;
await ListaIngresso . findOneAndUpdate ( { _id : params . id . toString ( ) } , { $set : fields _to _update } , { new : false } ) ;
params . conta ++ ;
// Check if the Ship has Completed
if ( idapp === tools . AYNI ) {
if ( ( params . col % 8 ) === 0 ) {
// Completed 8 people
if ( ! params . primavolta ) {
await Fuochi8Completati ( idapp , params ) ;
}
}
}
// Next
if ( params . col === params . maxcol ) {
params . riga ++ ;
params . rigaparent = params . riga - 1 ;
params . col = 1 ;
params . maxcol = getmaxcol ( params . riga ) ;
} else {
params . col ++ ;
}
if ( params . riga === 2 )
params . colparent = 1 ;
else
params . colparent = Math . ceil ( params . col / 2 ) ;
await Nave . setRiga ( idapp , params . riga ) ;
await Nave . setCol ( idapp , params . col ) ;
return true ;
}
return false ;
} ) . catch ( ( e ) => {
console . error ( e ) ;
} ) ;
}
NaveSchema . statics . checkifDeveRitessersi = async function ( recuser ) {
const Nave = this ;
let deveritessersi = true ;
arrrec = await Nave . find ( { idapp : recuser . idapp , ind _order : recuser . ind _order } ) ;
if ( ! ! arrrec ) {
if ( arrrec . length > 2 ) {
// deveritessersi = await User.deveRitessersi(recuser.idapp, recuser.ind_order)
deveritessersi = false ;
}
}
return { deveritessersi , num _tess : arrrec . length } ;
} ;
NaveSchema . statics . addUserFromListaIngresso _IntoNave = async function ( init , idapp , params , addednowreal ) {
const Nave = this ;
const { User } = require ( './user' ) ;
if ( init ) {
params . idapp = idapp ;
params . conta = 0 ;
}
params . primavolta = ( params . riga === 1 ) && ( params . col === 1 ) ;
params . riga = await Nave . getRiga ( idapp ) ;
params . col = await Nave . getCol ( idapp ) ;
params . maxcol = getmaxcol ( params . riga ) ;
params . colparent = Math . ceil ( params . col / 2 ) ;
params . rigaparent = params . riga - 1 ;
myriga = params . riga ;
mycol = params . col ;
const inserito = await addRecordNaveByParams ( params , false ) ;
if ( inserito ) {
if ( idapp === tools . AYNI ) {
if ( ( ( params . col ) % 8 ) === 0 ) {
// Trova se la persona di 3 livelli sopra, si deve ritessere
let recmediatore = await Nave . findMediatoreByFuoco ( idapp , myriga , mycol , 0 ) ;
if ( ! ! recmediatore ) {
const ris = await Nave . checkifDeveRitessersi ( recmediatore ) ;
2020-03-21 10:28:26 +01:00
if ( ris . num _tess >= 2 ) {
2020-03-10 21:44:14 +01:00
console . log ( 'E\' arrivato alla TERZA RITESSITURA, PERTANTO LO METTIAMO IN CODA... [riga=' , params . riga , 'col' , params . col , '] indorder=' + recmediatore . ind _order ) ;
// E' arrivato alla TERZA RITESSITURA, PERTANTO LO METTIAMO IN CODA...
2020-03-21 10:28:26 +01:00
//++Todo: TESSITURA????
//const risultato = await ListaIngresso.addUserInListaIngresso(idapp, recmediatore.ind_order, rec.lang, false, ris.num_tess);
2020-03-10 21:44:14 +01:00
} else {
console . log ( 'Si deve ritessere: [riga=' , params . riga , 'col' , params . col , ']' ) ;
if ( ris . deveritessersi ) {
params . indprimario = recmediatore . indprimario ;
params . ind _order = recmediatore . ind _order ;
params . id = recmediatore . _id ;
params . num _tess = ris . num _tess ;
await addRecordNaveByParams ( params , true ) ;
}
}
}
}
if ( ( params . riga === 4 + 3 ) && ( params . col === ( 8 * 2 ) + 3 ) ) {
// Si ritesse il Fondo AYNI nella Nave 3.3
const userFondo = await User . findByIndOrder ( idapp , 0 ) ;
params . indprimario = userFondo . indprimario ;
params . ind _order = userFondo . ind _order ;
params . id = userFondo . _id ;
params . num _tess = userFondo . num _tess ;
await addRecordNaveByParams ( params , true ) ;
}
}
}
// Se ho completato 8 persone, allora
if ( addednowreal ) {
}
} ;
NaveSchema . statics . generaNave = async function ( idapp , mydata ) {
const Nave = this ;
const { User } = require ( './user' ) ;
const { ListaIngresso } = require ( './listaingresso' ) ;
let params = {
idapp ,
riga : await Nave . getRiga ( idapp ) ,
col : await Nave . getCol ( idapp ) ,
date _start : mydata . date _start ,
2020-03-21 10:28:26 +01:00
date _gift _chat _open : tools . AddDate ( mydata . date _start , - 7 ) ,
2020-03-10 21:44:14 +01:00
numpersone : mydata . numpersone ,
} ;
params . primavolta = ( params . riga === 1 ) && ( params . col === 1 ) ;
// params.primavolta = false;
const recfindFondo = await Nave . findByRigaCol ( params . idapp , 0 , 0 , true ) ;
if ( ! recfindFondo ) {
let myNave = new Nave ( { idapp , indprimario : 0 , ind _order : 0 , riga : 0 , col : 0 } ) ;
myNave . created = new Date ( ) ;
myNave . parent _id = ObjectID ( "5e592aecbfd0b75f3021d9c9" ) ;
myNave . date _start = params . date _start ;
await myNave . save ( ) ;
const userFondo = await User . findByIndOrder ( idapp , 0 ) ;
if ( ! userFondo || userFondo === undefined ) {
await telegrambot . sendMsgTelegramToTheAdmin ( idapp , 'Devi creare l\'utente FONDO , con ind_order = 0 ! ' ) ;
}
}
const arrlistaingresso = await ListaIngresso . getProssimiInLista ( idapp , true ) ;
let index = 0 ;
for ( const reclista of arrlistaingresso ) {
params . indprimario = reclista . indprimario ;
params . ind _order = reclista . ind _order ;
params . id = reclista . _id ;
params . num _tess = reclista . num _tess ;
2020-03-21 10:28:26 +01:00
params . provvisoria = mydata . provvisoria ;
2020-03-10 21:44:14 +01:00
await Nave . addUserFromListaIngresso _IntoNave ( index === 0 , idapp , params , false ) ;
index ++ ;
if ( index >= params . numpersone )
break ;
}
return params . conta ;
} ;
async function addUserToNave ( idapp , rec ) {
let params = { } ;
params . indprimario = rec . indprimario ;
params . ind _order = rec . ind _order ;
params . id = rec . _id ;
params . num _tess = rec . num _tess ;
2020-03-21 10:28:26 +01:00
params . provvisoria = false ;
2020-03-10 21:44:14 +01:00
return await Nave . addUserFromListaIngresso _IntoNave ( true , idapp , params , true ) ;
}
NaveSchema . statics . checkIfDevoAggiungereInNave = async function ( idapp ) {
const { ListaIngresso } = require ( './listaingresso' ) ;
// Ottieni la lista Ordinata in base al numero d'invitati
arrlista = await ListaIngresso . getProssimiInLista ( idapp , false ) ;
for ( const rec of arrlista ) {
if ( rec . numinvitatiattivi >= 2 ) {
await addUserToNave ( idapp , rec ) ;
}
}
} ;
2020-03-21 10:28:26 +01:00
NaveSchema . statics . pulisciNonPresenzeInNave = async function ( idapp ) {
const Nave = this ;
let numrec = 0 ;
const { ListaIngresso } = require ( './listaingresso' ) ;
const myquery = getQueryProj ( {
idapp ,
ind _order : { $gt : 0 } ,
added : true ,
deleted : false ,
} ) ;
arrrec = await ListaIngresso . aggregate ( myquery )
. then ( async ( arrlista ) => {
for ( const rec of arrlista ) {
const trovato = await Nave . findByIndOrder ( idapp , rec . ind _order ) ;
if ( ! trovato ) {
let fields _to _update = { added : false } ;
await ListaIngresso . findOneAndUpdate ( { _id : rec . _id } , { $set : fields _to _update } , { new : false } ) ;
numrec ++ ;
}
}
} ) ;
return numrec ;
} ;
2020-03-10 21:44:14 +01:00
NaveSchema . statics . getNaveByUser = async function ( idapp , ind _order , lang , fuoco ) {
const Nave = this ;
let mystr = '' ;
2020-03-21 10:28:26 +01:00
let rec = { } ;
2020-03-10 21:44:14 +01:00
const arrposiz = await Nave . getArrPosizioniByIndOrder ( idapp , ind _order ) ;
if ( ! ! arrposiz ) {
for ( const pos of arrposiz ) {
mystr += await Nave . getNavePos ( idapp , pos . riga - 3 , Math . floor ( pos . col / ( 2 * 4 ) ) , false ) ;
2020-03-21 10:28:26 +01:00
mystr += await Nave . getPlaccaPerDonatore ( idapp , pos . riga , pos . col , false , rec ) ;
mystr += await Nave . getPlaccaPerMediatore ( idapp , pos . riga , pos . col , false , rec ) ;
2020-03-10 21:44:14 +01:00
}
}
if ( mystr === '' ) {
mystr = tools . gettranslate ( 'NO_PROG' , lang ) ;
}
return mystr ;
} ;
2020-03-21 10:28:26 +01:00
NaveSchema . statics . getNaveByRigaCol = async function ( idapp , riganave , colnave ) {
2020-03-10 21:44:14 +01:00
const Nave = this ;
2020-03-21 10:28:26 +01:00
const rigaprec = riganave ;
2020-03-10 21:44:14 +01:00
rec = { } ;
2020-03-21 10:28:26 +01:00
rec = await Nave . getPlaccaPerMediatore ( idapp , riganave , colnave , true , rec ) ;
if ( riganave < 4 ) {
riganave = 4 ;
colnave = 1 ;
}
rec = await Nave . getPlaccaPerDonatore ( idapp , riganave , colnave , true , rec ) ;
2020-03-10 21:44:14 +01:00
return rec ;
} ;
2020-03-21 10:28:26 +01:00
NaveSchema . statics . getDonatoridelSognatore = async function ( idapp , riganave , colnave ) {
const Nave = this ;
coldonatoreIni = calcval ( riganave , colnave , 8 ) + ( 1 ) ;
coldonatoreFine = calcval ( riganave , colnave , 8 ) + ( 64 ) ;
2020-03-10 21:44:14 +01:00
2020-03-21 10:28:26 +01:00
const myquery = getQueryProj ( {
idapp ,
riga : riganave + 6 ,
col : { $gte : coldonatoreIni , $lte : coldonatoreFine }
} ) ;
const arrnaviout = [ ] ;
const arrnavi = await Nave . aggregate ( myquery ) ;
for ( const nave of arrnavi ) {
// Se è il mediatore, allora non includerlo nei Donatori!
mypos = {
riga : nave . riga ,
col : nave . col ,
numup : 3 ,
} ;
2020-03-10 21:44:14 +01:00
2020-03-21 10:28:26 +01:00
tools . getRigaColByPosUp ( mypos ) ;
const navemediatore = await Nave . findByRigaCol ( idapp , mypos . riga , mypos . col , true ) ;
if ( ! ! navemediatore ) {
if ( navemediatore . ind _order !== nave . ind _order ) {
arrnaviout . push ( nave ) ;
}
} else {
arrnaviout . push ( nave ) ;
}
}
return arrnaviout ;
} ;
const Nave = mongoose . model ( 'Nave' , NaveSchema ) ;
2020-03-10 21:44:14 +01:00
module . exports = { Nave } ;