Files
freeplanet_serverside/src/server/models/mybacheca.js

355 lines
6.4 KiB
JavaScript
Raw Normal View History

2022-09-14 11:32:04 +02:00
const mongoose = require('mongoose').set('debug', false);
const Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
mongoose.level = 'F';
const tools = require('../tools/general');
2022-12-10 02:01:17 +01:00
const { ObjectID } = require('mongodb');
const shared_consts = require('../tools/shared_nodejs');
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true;
});
const MyBachecaSchema = new Schema({
_id: {
2023-04-04 15:26:56 +02:00
type: String,
default: function () {
return new ObjectID().toString();
},
},
idapp: {
type: String,
required: true,
},
2022-12-10 02:01:17 +01:00
userId: { type: Schema.Types.ObjectId, ref: 'User' },
idSector: {
type: Number,
},
idSkill: {
type: Number,
default: 0,
},
idStatusSkill: [
{
type: Number,
}],
idContribType: [
{
type: String,
}],
idCity: [
{
type: Number,
}],
dateTimeStart: {
type: Date,
},
dateTimeEnd: {
type: Date,
},
numLevel: {
type: Number,
default: 0,
},
adType: {
type: Number,
},
photos: [
{
imagefile: {
type: String,
},
alt: {
type: String,
},
description: {
type: String,
},
}],
note: {
type: String,
default: '',
},
descr: {
type: String,
},
//**ADDFIELD_MYBACHECAS
website: {
type: String,
},
date_created: {
type: Date,
},
date_updated: {
type: Date,
},
});
2022-12-10 02:01:17 +01:00
MyBachecaSchema.pre('save', async function (next) {
if (this.isNew) {
if (!this.date_created)
this.date_created = new Date();
}
next();
});
2022-12-10 02:01:17 +01:00
MyBachecaSchema.statics.findAllIdApp = async function (idapp) {
const MyBacheca = this;
const query = [
2022-12-10 02:01:17 +01:00
{ $match: { idapp } },
{ $sort: { descr: 1 } },
];
return await MyBacheca.aggregate(query).then((arrrec) => {
return arrrec;
});
};
2022-12-10 02:01:17 +01:00
MyBachecaSchema.statics.getFieldsForSearch = function () {
return [];
};
2022-12-10 02:01:17 +01:00
MyBachecaSchema.statics.getFieldsLastForSearch = function () {
return [
2022-12-10 02:01:17 +01:00
{ field: 'note', type: tools.FieldType.string },
{ field: 'descr', type: tools.FieldType.string },
{ field: 'recSkill.descr', type: tools.FieldType.string },
{ field: 'MyBacheca.descr', type: tools.FieldType.string },
];
};
2022-12-10 02:01:17 +01:00
MyBachecaSchema.statics.executeQueryTable = function (idapp, params, user) {
params.fieldsearch = this.getFieldsForSearch();
params.fieldsearch_last = this.getFieldsLastForSearch();
const otherparams = {
lookup1: {
lk_tab: 'users',
lk_LF: 'userId',
lk_FF: '_id',
lk_as: 'user',
af_objId_tab: 'myId',
lk_proj: this.getProject(),
},
};
2022-12-10 02:01:17 +01:00
params = { ...params, ...otherparams };
return tools.executeQueryTable(this, idapp, params, user);
};
2022-12-10 02:01:17 +01:00
MyBachecaSchema.statics.getMyRecById = function (idapp, id) {
const MyBacheca = this;
2023-04-04 15:26:56 +02:00
let myparsid = {
'_id': id,
2023-04-04 15:26:56 +02:00
};
let query = [
{
'$match':
myparsid,
},
{
'$match': {
'idapp': idapp,
},
},
{
'$sort': {
'desc': 1,
},
},
{
'$addFields': {
'myId1': {
'$toObjectId': '$userId',
},
},
},
{
'$lookup': {
'from': 'users',
'localField': 'myId1',
'foreignField': '_id',
'as': 'user',
},
},
{
'$replaceRoot': {
'newRoot': {
'$mergeObjects': [
{
'$arrayElemAt': [
'$user',
0,
],
},
'$$ROOT',
],
},
},
},
{
'$project': this.getProject(),
},
{
'$lookup': {
'from': 'skills',
'localField': 'idSkill',
'foreignField': '_id',
'as': 'recSkill',
},
},
{
'$replaceRoot': {
'newRoot': {
'$mergeObjects': [
{
'$arrayElemAt': [
'$recSkill',
0,
],
},
'$$ROOT',
],
},
},
},
{
'$project': this.getProject(),
},
{
'$lookup': {
'from': 'sectors',
'localField': 'idSector',
'foreignField': '_id',
'as': 'sector',
},
},
{
'$replaceRoot': {
'newRoot': {
'$mergeObjects': [
{
'$arrayElemAt': [
'$sector',
0,
],
},
'$$ROOT',
],
},
},
},
{
'$project': this.getProject(),
},
/*{
'$lookup': {
'from': 'subskills',
'localField': 'idSubSkill',
'foreignField': '_id',
'as': 'MyBacheca',
},
},*/
{
'$replaceRoot': {
'newRoot': {
'$mergeObjects': [
{
'$arrayElemAt': [
'$MyBacheca',
0,
],
},
'$$ROOT',
],
},
},
},
{
'$project': this.getProject(),
},
{
'$lookup': {
'from': 'cities',
'localField': 'idCity',
'foreignField': '_id',
'as': 'mycities',
},
},
{
'$replaceRoot': {
'newRoot': {
'$mergeObjects': [
{
'$arrayElemAt': [
'$mycities',
0,
],
},
'$$ROOT',
],
},
},
},
];
let numtab = tools.getNumTabByTable(shared_consts.TABLES_MYBACHECAS);
const objadd = tools.addNumFavoriteAndBookmarkToQuery(idapp, numtab);
query = [...query, ...objadd.query];
query = query.push(
{
$project: this.getProject(objadd.proj),
}
);
return MyBacheca.aggregate(query).then((rec) => {
return rec ? rec[0] : null;
});
};
MyBachecaSchema.statics.getProject = function (proj_add2) {
let proj = {
recSkill: 1,
sector: 1,
idSector: 1,
idSkill: 1,
// 'idSubSkill': 1,
idStatusSkill: 1,
idContribType: 1,
dateTimeStart: 1,
dateTimeEnd: 1,
website: 1,
//**ADDFIELD_MYBACHECAS
};
const proj_add = shared_consts.getProjectForAll(proj_add2)
return Object.assign({}, proj, proj_add);
}
2022-12-10 02:01:17 +01:00
MyBachecaSchema.statics.getCompleteRecord = function (idapp, id) {
const MyBacheca = this;
return MyBacheca.getMyRecById(idapp, id);
};
const MyBacheca = mongoose.model('MyBacheca', MyBachecaSchema);
2022-12-10 02:01:17 +01:00
module.exports = { MyBacheca };