- fix: resizing image uploaded

- add: mybachecas
This commit is contained in:
paoloar77
2022-02-16 19:21:12 +01:00
parent bae30504f7
commit 04f76c61a7
10 changed files with 785 additions and 174 deletions

469
src/server/models/mybacheca.js Executable file
View File

@@ -0,0 +1,469 @@
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 MyBachecaSchema = new Schema({
_id: {
type: Number,
},
idapp: {
type: String,
required: true,
},
userId: {type: Schema.Types.ObjectId, ref: 'User'},
idSector: {
type: Number,
},
idSkill: {
type: Number,
default: 0,
},
idSubSkill: [
{
type: Number,
default: 0,
}],
idStatusSkill: [
{
type: Number,
}],
idContribType: [
{
type: String,
}],
idCity: [
{
type: Number,
}],
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,
},
date_created: {
type: Date,
default: Date.now,
},
date_updated: {
type: Date,
default: Date.now,
},
});
MyBachecaSchema.pre('save', async function(next) {
if (this.isNew) {
const myrec = await MyBacheca.findOne().limit(1).sort({_id: -1});
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();
});
MyBachecaSchema.statics.findAllIdApp = async function(idapp) {
const MyBacheca = this;
const query = [
{$match: {idapp}},
{$sort: {descr: 1}},
];
return MyBacheca.aggregate(query).then((arrrec) => {
return arrrec;
});
};
MyBachecaSchema.statics.getFieldsForSearch = function() {
return [];
};
MyBachecaSchema.statics.getFieldsLastForSearch = function() {
return [
{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},
];
};
MyBachecaSchema.statics.executeQueryTable = function(idapp, params) {
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: {
idSkill: 1,
idSubSkill: 1,
MyBacheca: 1,
idStatusSkill: 1,
idContribType: 1,
idCity: 1,
numLevel: 1,
adType: 1,
photos: 1,
note: 1,
descr: 1,
date_created: 1,
date_updated: 1,
userId: 1,
username: 1,
name: 1,
surname: 1,
'profile.img': 1,
'profile.qualifica': 1,
},
},
};
params = {...params, ...otherparams};
return tools.executeQueryTable(this, idapp, params);
};
MyBachecaSchema.statics.getMyRecById = function(idapp, id) {
const MyBacheca = this;
const query = [
{
'$match': {
'$and': [
{
'_id': parseInt(id),
},
],
},
},
{
'$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,
'idSector': 1,
'idSkill': 1,
'idSubSkill': 1,
'idStatusSkill': 1,
'idContribType': 1,
'idCity': 1,
'numLevel': 1,
adType: 1,
'photos': 1,
'note': 1,
'descr': 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,
'idSector': 1,
'idSkill': 1,
'idSubSkill': 1,
'idStatusSkill': 1,
'idContribType': 1,
'idCity': 1,
'numLevel': 1,
adType: 1,
'photos': 1,
'note': 1,
'descr': 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,
'idSector': 1,
'idSkill': 1,
'idSubSkill': 1,
'idStatusSkill': 1,
'idContribType': 1,
'idCity': 1,
'numLevel': 1,
adType: 1,
'photos': 1,
'note': 1,
'descr': 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': 'MyBacheca',
},
},
{
'$replaceRoot': {
'newRoot': {
'$mergeObjects': [
{
'$arrayElemAt': [
'$MyBacheca',
0,
],
},
'$$ROOT',
],
},
},
},
{
'$project': {
'recSkill': 1,
'sector': 1,
'idSector': 1,
'idSkill': 1,
'idSubSkill': 1,
'idStatusSkill': 1,
'idContribType': 1,
'idCity': 1,
'numLevel': 1,
adType: 1,
'photos': 1,
'note': 1,
'descr': 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,
'idSector': 1,
'idSkill': 1,
'idSubSkill': 1,
'idStatusSkill': 1,
'idContribType': 1,
'idCity': 1,
'numLevel': 1,
adType: 1,
'photos': 1,
'note': 1,
'descr': 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 MyBacheca.aggregate(query).then((rec) => {
return rec ? rec[0] : null;
});
};
MyBachecaSchema.statics.getCompleteRecord = function(idapp, id) {
const MyBacheca = this;
return MyBacheca.getMyRecById(idapp, id);
};
const MyBacheca = mongoose.model('MyBacheca', MyBachecaSchema);
module.exports = {MyBacheca};