2021-10-08 00:38:35 +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');
|
|
|
|
|
|
|
|
|
|
const {ObjectID} = require('mongodb');
|
|
|
|
|
|
|
|
|
|
// Resolving error Unknown modifier: $pushAll
|
|
|
|
|
mongoose.plugin(schema => {
|
|
|
|
|
schema.options.usePushEach = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const MySkillSchema = new Schema({
|
|
|
|
|
_id: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
idapp: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
2021-10-28 00:38:10 +02:00
|
|
|
userId: {type: Schema.Types.ObjectId, ref: 'User'},
|
2022-01-28 00:57:39 +01:00
|
|
|
idSector: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
2021-10-08 00:38:35 +02:00
|
|
|
idSkill: {
|
|
|
|
|
type: Number,
|
2021-12-02 10:13:27 +01:00
|
|
|
default: 0,
|
2021-10-08 00:38:35 +02:00
|
|
|
},
|
2021-12-31 01:44:28 +01:00
|
|
|
idSubSkill: [
|
|
|
|
|
{
|
|
|
|
|
type: Number,
|
|
|
|
|
default: 0,
|
|
|
|
|
}],
|
2021-10-28 00:38:10 +02:00
|
|
|
idStatusSkill: [
|
|
|
|
|
{
|
|
|
|
|
type: Number,
|
|
|
|
|
}],
|
2021-12-21 01:27:45 +01:00
|
|
|
idContribType: [
|
|
|
|
|
{
|
|
|
|
|
type: String,
|
|
|
|
|
}],
|
2021-10-28 00:38:10 +02:00
|
|
|
idCity: [
|
|
|
|
|
{
|
|
|
|
|
type: Number,
|
|
|
|
|
}],
|
2021-10-08 00:38:35 +02:00
|
|
|
numLevel: {
|
|
|
|
|
type: Number,
|
2021-12-02 10:13:27 +01:00
|
|
|
default: 0,
|
2021-10-08 00:38:35 +02:00
|
|
|
},
|
2021-10-28 00:38:10 +02:00
|
|
|
photos: [
|
|
|
|
|
{
|
|
|
|
|
imagefile: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
alt: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
description: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
}],
|
2021-10-08 00:38:35 +02:00
|
|
|
note: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2021-10-28 00:38:10 +02:00
|
|
|
subTitle: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2022-01-28 00:57:39 +01:00
|
|
|
descr: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2021-10-08 00:38:35 +02:00
|
|
|
date_created: {
|
|
|
|
|
type: Date,
|
|
|
|
|
default: Date.now,
|
|
|
|
|
},
|
|
|
|
|
date_updated: {
|
|
|
|
|
type: Date,
|
|
|
|
|
default: Date.now,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2021-10-28 00:38:10 +02:00
|
|
|
MySkillSchema.pre('save', async function(next) {
|
2021-10-08 00:38:35 +02:00
|
|
|
if (this.isNew) {
|
2021-10-28 00:38:10 +02:00
|
|
|
const myrec = await MySkill.findOne().limit(1).sort({_id: -1});
|
2021-10-08 00:38:35 +02:00
|
|
|
if (!!myrec) {
|
|
|
|
|
if (myrec._doc._id === 0)
|
|
|
|
|
this._id = 1;
|
|
|
|
|
else
|
|
|
|
|
this._id = myrec._doc._id + 1;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
this._id = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.date_created = new Date();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2021-12-23 14:13:40 +01:00
|
|
|
MySkillSchema.statics.findAllIdApp = async function(idapp) {
|
2022-01-26 01:31:22 +01:00
|
|
|
const MySkill = this;
|
2021-10-08 00:38:35 +02:00
|
|
|
|
|
|
|
|
const query = [
|
2021-10-28 00:38:10 +02:00
|
|
|
{$match: {idapp}},
|
2021-10-08 00:38:35 +02:00
|
|
|
{$sort: {descr: 1}},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return MySkill.aggregate(query).then((arrrec) => {
|
|
|
|
|
return arrrec;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MySkillSchema.statics.getFieldsForSearch = function() {
|
2022-01-23 23:25:34 +01:00
|
|
|
return [
|
|
|
|
|
{field: 'idSkill', type: tools.FieldType.Number}
|
|
|
|
|
, {field: 'note', type: tools.FieldType.String}
|
|
|
|
|
, {field: 'subTitle', type: tools.FieldType.String},
|
|
|
|
|
];
|
2021-10-08 00:38:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MySkillSchema.statics.executeQueryTable = function(idapp, params) {
|
|
|
|
|
params.fieldsearch = this.getFieldsForSearch();
|
2021-12-11 00:25:54 +01:00
|
|
|
|
|
|
|
|
const otherparams = {
|
2021-12-21 01:27:45 +01:00
|
|
|
lookup1: {
|
|
|
|
|
lk_tab: 'users',
|
|
|
|
|
lk_LF: 'userId',
|
|
|
|
|
lk_FF: '_id',
|
|
|
|
|
lk_as: 'user',
|
|
|
|
|
af_objId_tab: 'myId',
|
|
|
|
|
lk_proj: {
|
|
|
|
|
idSkill: 1,
|
|
|
|
|
idStatusSkill: 1,
|
|
|
|
|
idContribType: 1,
|
|
|
|
|
idCity: 1,
|
|
|
|
|
numLevel: 1,
|
|
|
|
|
photos: 1,
|
|
|
|
|
note: 1,
|
|
|
|
|
subTitle: 1,
|
|
|
|
|
date_created: 1,
|
|
|
|
|
date_updated: 1,
|
|
|
|
|
userId: 1,
|
|
|
|
|
username: 1,
|
|
|
|
|
name: 1,
|
|
|
|
|
surname: 1,
|
2022-01-20 00:39:06 +01:00
|
|
|
'profile.img': 1,
|
|
|
|
|
'profile.qualifica': 1,
|
2021-12-21 01:27:45 +01:00
|
|
|
},
|
2021-12-11 00:25:54 +01:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-23 23:25:34 +01:00
|
|
|
params = {...params, ...otherparams};
|
2021-12-11 00:25:54 +01:00
|
|
|
|
2021-10-08 00:38:35 +02:00
|
|
|
return tools.executeQueryTable(this, idapp, params);
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-26 01:31:22 +01:00
|
|
|
MySkillSchema.statics.getMySkillByIdkill = function (idapp, idSkill) {
|
|
|
|
|
const MySkill = this;
|
|
|
|
|
|
|
|
|
|
const query = [
|
|
|
|
|
{
|
|
|
|
|
"$match": {
|
|
|
|
|
"$and": [
|
|
|
|
|
{
|
|
|
|
|
"_id": parseInt(idSkill)
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"$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": {
|
|
|
|
|
"recSkill": 1,
|
|
|
|
|
"sector": 1,
|
2022-01-28 00:57:39 +01:00
|
|
|
"idSector": 1,
|
2022-01-26 01:31:22 +01:00
|
|
|
"idSkill": 1,
|
|
|
|
|
"idSubSkill": 1,
|
|
|
|
|
"idStatusSkill": 1,
|
|
|
|
|
"idContribType": 1,
|
|
|
|
|
"idCity": 1,
|
|
|
|
|
"numLevel": 1,
|
|
|
|
|
"photos": 1,
|
|
|
|
|
"note": 1,
|
|
|
|
|
"subTitle": 1,
|
|
|
|
|
"date_created": 1,
|
|
|
|
|
"date_updated": 1,
|
|
|
|
|
"userId": 1,
|
|
|
|
|
"username": 1,
|
|
|
|
|
"name": 1,
|
|
|
|
|
"surname": 1,
|
|
|
|
|
"comune": 1,
|
|
|
|
|
"mycities": 1,
|
|
|
|
|
"profile.img": 1,
|
|
|
|
|
"profile.qualifica": 1
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"$lookup": {
|
|
|
|
|
"from": "skills",
|
|
|
|
|
"localField": "idSkill",
|
|
|
|
|
"foreignField": "_id",
|
|
|
|
|
"as": "recSkill"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"$replaceRoot": {
|
|
|
|
|
"newRoot": {
|
|
|
|
|
"$mergeObjects": [
|
|
|
|
|
{
|
|
|
|
|
"$arrayElemAt": [
|
|
|
|
|
"$recSkill",
|
|
|
|
|
0
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"$$ROOT"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"$project": {
|
|
|
|
|
"recSkill": 1,
|
|
|
|
|
"sector": 1,
|
2022-01-28 00:57:39 +01:00
|
|
|
"idSector": 1,
|
2022-01-26 01:31:22 +01:00
|
|
|
"idSkill": 1,
|
|
|
|
|
"idSubSkill": 1,
|
|
|
|
|
"idStatusSkill": 1,
|
|
|
|
|
"idContribType": 1,
|
|
|
|
|
"idCity": 1,
|
|
|
|
|
"numLevel": 1,
|
|
|
|
|
"photos": 1,
|
|
|
|
|
"note": 1,
|
|
|
|
|
"subTitle": 1,
|
|
|
|
|
"date_created": 1,
|
|
|
|
|
"date_updated": 1,
|
|
|
|
|
"userId": 1,
|
|
|
|
|
"username": 1,
|
|
|
|
|
"name": 1,
|
|
|
|
|
"surname": 1,
|
|
|
|
|
"comune": 1,
|
|
|
|
|
"mycities": 1,
|
|
|
|
|
"profile.img": 1,
|
|
|
|
|
"profile.qualifica": 1
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"$lookup": {
|
|
|
|
|
"from": "sectors",
|
|
|
|
|
"localField": "recSkill.idSector",
|
|
|
|
|
"foreignField": "_id",
|
|
|
|
|
"as": "sector"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"$replaceRoot": {
|
|
|
|
|
"newRoot": {
|
|
|
|
|
"$mergeObjects": [
|
|
|
|
|
{
|
|
|
|
|
"$arrayElemAt": [
|
|
|
|
|
"$sector",
|
|
|
|
|
0
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"$$ROOT"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"$project": {
|
|
|
|
|
"recSkill": 1,
|
|
|
|
|
"sector": 1,
|
2022-01-28 00:57:39 +01:00
|
|
|
"idSector": 1,
|
2022-01-26 01:31:22 +01:00
|
|
|
"idSkill": 1,
|
|
|
|
|
"idSubSkill": 1,
|
|
|
|
|
"idStatusSkill": 1,
|
|
|
|
|
"idContribType": 1,
|
|
|
|
|
"idCity": 1,
|
|
|
|
|
"numLevel": 1,
|
|
|
|
|
"photos": 1,
|
|
|
|
|
"note": 1,
|
|
|
|
|
"subTitle": 1,
|
|
|
|
|
"date_created": 1,
|
|
|
|
|
"date_updated": 1,
|
|
|
|
|
"userId": 1,
|
|
|
|
|
"username": 1,
|
|
|
|
|
"name": 1,
|
|
|
|
|
"surname": 1,
|
|
|
|
|
"comune": 1,
|
|
|
|
|
"mycities": 1,
|
|
|
|
|
"profile.img": 1,
|
|
|
|
|
"profile.qualifica": 1
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"$lookup": {
|
|
|
|
|
"from": "subskills",
|
|
|
|
|
"localField": "idSubSkill",
|
|
|
|
|
"foreignField": "_id",
|
|
|
|
|
"as": "myskill"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"$replaceRoot": {
|
|
|
|
|
"newRoot": {
|
|
|
|
|
"$mergeObjects": [
|
|
|
|
|
{
|
|
|
|
|
"$arrayElemAt": [
|
|
|
|
|
"$myskill",
|
|
|
|
|
0
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"$$ROOT"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"$project": {
|
|
|
|
|
"recSkill": 1,
|
|
|
|
|
"sector": 1,
|
2022-01-28 00:57:39 +01:00
|
|
|
"idSector": 1,
|
2022-01-26 01:31:22 +01:00
|
|
|
"idSkill": 1,
|
|
|
|
|
"idSubSkill": 1,
|
|
|
|
|
"idStatusSkill": 1,
|
|
|
|
|
"idContribType": 1,
|
|
|
|
|
"idCity": 1,
|
|
|
|
|
"numLevel": 1,
|
|
|
|
|
"photos": 1,
|
|
|
|
|
"note": 1,
|
|
|
|
|
"subTitle": 1,
|
|
|
|
|
"date_created": 1,
|
|
|
|
|
"date_updated": 1,
|
|
|
|
|
"userId": 1,
|
|
|
|
|
"username": 1,
|
|
|
|
|
"name": 1,
|
|
|
|
|
"surname": 1,
|
|
|
|
|
"comune": 1,
|
|
|
|
|
"mycities": 1,
|
|
|
|
|
"profile.img": 1,
|
|
|
|
|
"profile.qualifica": 1
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"$lookup": {
|
|
|
|
|
"from": "cities",
|
|
|
|
|
"localField": "idCity",
|
|
|
|
|
"foreignField": "_id",
|
|
|
|
|
"as": "mycities"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"$replaceRoot": {
|
|
|
|
|
"newRoot": {
|
|
|
|
|
"$mergeObjects": [
|
|
|
|
|
{
|
|
|
|
|
"$arrayElemAt": [
|
|
|
|
|
"$mycities",
|
|
|
|
|
0
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"$$ROOT"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"$project": {
|
|
|
|
|
"recSkill": 1,
|
|
|
|
|
"sector": 1,
|
2022-01-28 00:57:39 +01:00
|
|
|
"idSector": 1,
|
2022-01-26 01:31:22 +01:00
|
|
|
"idSkill": 1,
|
|
|
|
|
"idSubSkill": 1,
|
|
|
|
|
"idStatusSkill": 1,
|
|
|
|
|
"idContribType": 1,
|
|
|
|
|
"idCity": 1,
|
|
|
|
|
"numLevel": 1,
|
|
|
|
|
"photos": 1,
|
|
|
|
|
"note": 1,
|
|
|
|
|
"subTitle": 1,
|
|
|
|
|
"date_created": 1,
|
|
|
|
|
"date_updated": 1,
|
|
|
|
|
"userId": 1,
|
|
|
|
|
"username": 1,
|
|
|
|
|
"name": 1,
|
|
|
|
|
"surname": 1,
|
|
|
|
|
"comune": 1,
|
|
|
|
|
"mycities": 1,
|
|
|
|
|
"profile.img": 1,
|
|
|
|
|
"profile.qualifica": 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
return MySkill.aggregate(query).then((rec) => {
|
|
|
|
|
return rec ? rec[0] : null;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2021-10-08 00:38:35 +02:00
|
|
|
const MySkill = mongoose.model('MySkill', MySkillSchema);
|
|
|
|
|
|
|
|
|
|
module.exports = {MySkill};
|