2019-10-20 01:21:54 +02:00
|
|
|
const mongoose = require('mongoose');
|
|
|
|
|
const Schema = mongoose.Schema;
|
|
|
|
|
|
|
|
|
|
mongoose.Promise = global.Promise;
|
|
|
|
|
mongoose.level = "F";
|
|
|
|
|
|
|
|
|
|
const tools = require('../tools/general');
|
|
|
|
|
|
|
|
|
|
const { ObjectID } = require('mongodb');
|
|
|
|
|
|
2019-12-04 18:20:50 +01:00
|
|
|
const { Settings } = require('./settings');
|
|
|
|
|
|
2019-10-20 01:21:54 +02:00
|
|
|
// Resolving error Unknown modifier: $pushAll
|
|
|
|
|
mongoose.plugin(schema => {
|
|
|
|
|
schema.options.usePushEach = true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const MyEventSchema = new Schema({
|
|
|
|
|
idapp: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
typol: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
short_tit: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
title: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
details: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2019-11-06 22:29:29 +01:00
|
|
|
bodytext: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2019-10-21 20:38:10 +02:00
|
|
|
dateTimeStart: {
|
|
|
|
|
type: Date,
|
2019-10-20 01:21:54 +02:00
|
|
|
},
|
2019-10-21 20:38:10 +02:00
|
|
|
dateTimeEnd: {
|
2019-10-20 01:21:54 +02:00
|
|
|
type: Date,
|
|
|
|
|
},
|
|
|
|
|
bgcolor: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
icon: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
img_small: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
img: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2019-10-21 20:38:10 +02:00
|
|
|
wherecode: {
|
2019-10-20 01:21:54 +02:00
|
|
|
type: String,
|
|
|
|
|
},
|
2019-10-23 23:47:21 +02:00
|
|
|
contribtype: {
|
|
|
|
|
type: String,
|
2019-10-20 01:21:54 +02:00
|
|
|
},
|
|
|
|
|
price: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
infoafterprice: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2019-10-23 23:47:21 +02:00
|
|
|
teacher: {
|
2019-10-20 01:21:54 +02:00
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
teacher2: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2021-06-04 10:07:57 +02:00
|
|
|
teacher3: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
teacher4: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2019-10-20 01:21:54 +02:00
|
|
|
infoextra: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
linkpage: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
linkpdf: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
nobookable: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
},
|
|
|
|
|
news: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
},
|
|
|
|
|
canceled: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
},
|
2021-02-18 12:19:35 +01:00
|
|
|
lunchAvailable: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
},
|
|
|
|
|
dinnerAvailable: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
},
|
2021-04-30 01:31:12 +02:00
|
|
|
dinnerSharedAvailable: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
},
|
2021-02-18 12:19:35 +01:00
|
|
|
lunchType: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
dinnerType: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
lunchPrice: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
dinnerPrice: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
internal: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
},
|
2021-05-10 01:50:40 +02:00
|
|
|
note: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2021-06-04 10:07:57 +02:00
|
|
|
pagefooter: [{
|
|
|
|
|
type: String,
|
|
|
|
|
}],
|
2019-10-20 01:21:54 +02:00
|
|
|
deleted: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
},
|
|
|
|
|
dupId: {
|
|
|
|
|
type: mongoose.Schema.Types.ObjectId,
|
|
|
|
|
},
|
2021-06-04 10:07:57 +02:00
|
|
|
facebook: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2019-10-20 01:21:54 +02:00
|
|
|
modified: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2021-02-18 12:19:35 +01:00
|
|
|
MyEventSchema.statics.findAllIdApp = function (socioresidente, idapp) {
|
2019-10-20 01:21:54 +02:00
|
|
|
const Event = this;
|
|
|
|
|
|
2021-02-18 12:19:35 +01:00
|
|
|
let query = []
|
|
|
|
|
|
|
|
|
|
if (socioresidente) {
|
|
|
|
|
query = [
|
|
|
|
|
{
|
|
|
|
|
$match: {
|
|
|
|
|
idapp,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
} else {
|
|
|
|
|
query = [
|
|
|
|
|
{
|
|
|
|
|
$match: {
|
|
|
|
|
idapp,
|
|
|
|
|
$or: [
|
|
|
|
|
{ $or: [{ internal: { $exists: false } }, { internal: { $exists: true, $eq: false } }] },
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query.push({ $sort: { dateTimeStart: 1 } })
|
2019-11-04 20:30:09 +01:00
|
|
|
|
|
|
|
|
return Event
|
|
|
|
|
.aggregate(query)
|
|
|
|
|
.then((arrrec) => {
|
|
|
|
|
return arrrec
|
|
|
|
|
})
|
2019-10-20 01:21:54 +02:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-04 18:20:50 +01:00
|
|
|
MyEventSchema.statics.getLastEvents = async function (idapp) {
|
2019-11-21 00:18:40 +01:00
|
|
|
const Event = this;
|
|
|
|
|
|
2019-12-04 18:20:50 +01:00
|
|
|
const lastn = await Settings.getValDbSettings(idapp, 'SHOW_LAST_N_EV', 1);
|
2019-11-21 00:18:40 +01:00
|
|
|
|
|
|
|
|
const query = [
|
2020-03-21 10:28:26 +01:00
|
|
|
{ $match: { idapp, dateTimeStart: { $gte: tools.IncDateNow(-1000 * 60 * 60 * 24) } } },
|
2019-12-04 02:03:44 +01:00
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'operators',
|
|
|
|
|
localField: 'teacher',
|
|
|
|
|
foreignField: 'username',
|
|
|
|
|
as: 'op1'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'operators',
|
|
|
|
|
localField: 'teacher2',
|
|
|
|
|
foreignField: 'username',
|
|
|
|
|
as: 'op2'
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-06-04 10:07:57 +02:00
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'operators',
|
|
|
|
|
localField: 'teacher3',
|
|
|
|
|
foreignField: 'username',
|
|
|
|
|
as: 'op3'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'operators',
|
|
|
|
|
localField: 'teacher4',
|
|
|
|
|
foreignField: 'username',
|
|
|
|
|
as: 'op4'
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-12-04 02:03:44 +01:00
|
|
|
{ "$addFields": { "contribtype": { "$toObjectId": "$contribtype" } } },
|
|
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'contribtypes',
|
|
|
|
|
localField: 'contribtype',
|
|
|
|
|
foreignField: '_id',
|
|
|
|
|
as: 'contrib'
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-11-21 00:18:40 +01:00
|
|
|
{ $sort: { dateTimeStart: 1 } }
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return Event
|
|
|
|
|
.aggregate(query)
|
|
|
|
|
.then((arrrec) => {
|
2019-12-04 02:03:44 +01:00
|
|
|
// console.table(arrrec);
|
2019-12-04 18:20:50 +01:00
|
|
|
if (lastn > 0) {
|
|
|
|
|
return arrrec.slice(0, lastn);
|
|
|
|
|
} else {
|
|
|
|
|
return arrrec
|
|
|
|
|
}
|
2019-11-21 00:18:40 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-20 01:21:54 +02:00
|
|
|
|
2019-10-28 16:01:28 +01:00
|
|
|
MyEventSchema.statics.getFieldsForSearch = function () {
|
2020-03-21 10:28:26 +01:00
|
|
|
return [{ field: 'short_tit', type: tools.FieldType.string },
|
|
|
|
|
{ field: 'title', type: tools.FieldType.string },
|
|
|
|
|
{ field: 'teacher', type: tools.FieldType.string },
|
|
|
|
|
{ field: 'details', type: tools.FieldType.string }]
|
2019-10-28 16:01:28 +01:00
|
|
|
};
|
|
|
|
|
|
2019-10-20 01:21:54 +02:00
|
|
|
MyEventSchema.statics.executeQueryTable = function (idapp, params) {
|
2019-10-28 16:01:28 +01:00
|
|
|
params.fieldsearch = this.getFieldsForSearch();
|
2019-10-20 01:21:54 +02:00
|
|
|
return tools.executeQueryTable(this, idapp, params);
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-28 16:01:28 +01:00
|
|
|
if (tools.INITDB_FIRSTIME) {
|
|
|
|
|
// console.log(' createIndex MyEvent Index...');
|
|
|
|
|
// MyEventSchema.index({ short_tit: 'text', title: 'text', teacher: 'text', details: 'text' });
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 01:21:54 +02:00
|
|
|
|
|
|
|
|
const MyEvent = mongoose.model('MyEvent', MyEventSchema);
|
|
|
|
|
|
|
|
|
|
module.exports = { MyEvent };
|