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

534 lines
10 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');
2023-04-04 15:26:56 +02:00
const { ObjectID } = require('mongodb');
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true;
});
const MyGoodSchema = new Schema({
_id: {
2023-04-04 15:26:56 +02:00
type: String,
},
idapp: {
type: String,
required: true,
},
2023-04-04 15:26:56 +02:00
userId: { type: Schema.Types.ObjectId, ref: 'User' },
idSectorGood: {
type: Number,
},
idGood: {
type: Number,
default: 0,
},
idShipping: [
{
type: Number,
}],
idContribType: [
{
type: String,
}],
idCity: [
{
type: Number,
}],
pub_to_share: {
type: Number, // PUB_TO_SHARE_ALL, PUB_TO_SHARE_ONLY_TABLE_FOLLOW
},
numLevel: {
type: Number,
default: 0,
},
adType: {
type: Number,
},
otherfilters: [{
type: Number,
}],
photos: [
{
imagefile: {
type: String,
},
alt: {
type: String,
},
description: {
type: String,
},
}],
note: {
type: String,
default: '',
},
descr: {
type: String,
},
//**ADDFIELD_MyGood
website: {
type: String,
},
date_created: {
type: Date,
default: Date.now,
},
date_updated: {
type: Date,
},
});
2023-04-04 15:26:56 +02:00
MyGoodSchema.pre('save', async function (next) {
if (this.isNew) {
2023-04-04 15:26:56 +02:00
const myrec = await MyGood.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();
});
2023-04-04 15:26:56 +02:00
MyGoodSchema.statics.findAllIdApp = async function (idapp) {
const MyGood = this;
const query = [
2023-04-04 15:26:56 +02:00
{ $match: { idapp } },
{ $sort: { descr: 1 } },
];
return await MyGood.aggregate(query).then((arrrec) => {
return arrrec;
});
};
2023-04-04 15:26:56 +02:00
MyGoodSchema.statics.getFieldsForSearch = function () {
return [];
};
2023-04-04 15:26:56 +02:00
MyGoodSchema.statics.getFieldsLastForSearch = function () {
return [
2023-04-04 15:26:56 +02:00
{ field: 'note', type: tools.FieldType.string },
{ field: 'descr', type: tools.FieldType.string },
{ field: 'recGood.descr', type: tools.FieldType.string },
{ field: 'MyGood.descr', type: tools.FieldType.string },
];
};
2023-04-04 15:26:56 +02:00
MyGoodSchema.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: {
idGood: 1,
idShipping: 1,
MyGood: 1,
idStatusGood: 1,
idContribType: 1,
2023-04-04 15:26:56 +02:00
'profile.username_telegram': 1,
idCity: 1,
pub_to_share: 1,
numLevel: 1,
adType: 1,
otherfilters: 1,
photos: 1,
note: 1,
website: 1,
//**ADDFIELD_MyGood
descr: 1,
date_created: 1,
date_updated: 1,
userId: 1,
username: 1,
name: 1,
surname: 1,
2022-12-10 02:01:17 +01:00
lasttimeonline: 1,
'profile.img': 1,
"profile.mygroups": 1,
"profile.mycircuits": 1,
'profile.qualifica': 1,
2023-04-04 15:26:56 +02:00
'profile.resid_province': 1,
reported: 1,
},
},
};
2023-04-04 15:26:56 +02:00
params = { ...params, ...otherparams };
return tools.executeQueryTable(this, idapp, params, user);
};
2023-04-04 15:26:56 +02:00
MyGoodSchema.statics.getMyRecById = function (idapp, idGood) {
const MyGood = this;
2023-04-04 15:26:56 +02:00
let myparsid = {
$or: [
{
'_id': parseInt(idGood)
},
{
'_id': idGood,
}]
};
const query = [
{
'$match': {
'$and': [
2023-04-04 15:26:56 +02:00
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': {
'recGood': 1,
'sectorGood': 1,
'idSectorGood': 1,
'idGood': 1,
'idShipping': 1,
'idStatusGood': 1,
'idContribType': 1,
'idCity': 1,
pub_to_share: 1,
'numLevel': 1,
adType: 1,
otherfilters: 1,
'photos': 1,
note: 1,
website: 1,
//**ADDFIELD_MyGood
'descr': 1,
'date_created': 1,
'date_updated': 1,
'userId': 1,
'username': 1,
'name': 1,
'surname': 1,
'comune': 1,
'mycities': 1,
'profile.img': 1,
"profile.mygroups": 1,
"profile.mycircuits": 1,
'profile.qualifica': 1,
2023-04-04 15:26:56 +02:00
'profile.resid_province': 1,
reported: 1,
},
},
{
'$lookup': {
'from': 'goods',
'localField': 'idGood',
'foreignField': '_id',
'as': 'recGood',
},
},
{
'$replaceRoot': {
'newRoot': {
'$mergeObjects': [
{
'$arrayElemAt': [
'$recGood',
0,
],
},
'$$ROOT',
],
},
},
},
{
'$project': {
'recGood': 1,
'sectorGood': 1,
'idSectorGood': 1,
'idGood': 1,
'idShipping': 1,
'idStatusGood': 1,
'idContribType': 1,
'idCity': 1,
pub_to_share: 1,
'numLevel': 1,
adType: 1,
otherfilters: 1,
'photos': 1,
'note': 1,
website: 1,
//**ADDFIELD_MyGood
'descr': 1,
'date_created': 1,
'date_updated': 1,
'userId': 1,
'username': 1,
'name': 1,
'surname': 1,
'comune': 1,
'mycities': 1,
'profile.img': 1,
"profile.mygroups": 1,
"profile.mycircuits": 1,
'profile.qualifica': 1,
2023-04-04 15:26:56 +02:00
'profile.resid_province': 1,
reported: 1,
},
},
{
'$lookup': {
'from': 'sectorgoods',
'localField': 'idSectorGood',
// 'localField': 'recGood.idSectorGood',
'foreignField': '_id',
'as': 'sectorGood',
},
},
{
'$replaceRoot': {
'newRoot': {
'$mergeObjects': [
{
'$arrayElemAt': [
'$sectorgood',
0,
],
},
'$$ROOT',
],
},
},
},
{
'$project': {
'recGood': 1,
'sectorGood': 1,
'idSectorGood': 1,
'idGood': 1,
'idShipping': 1,
'idStatusGood': 1,
'idContribType': 1,
'idCity': 1,
pub_to_share: 1,
'numLevel': 1,
adType: 1,
otherfilters: 1,
'photos': 1,
'note': 1,
website: 1,
//**ADDFIELD_MyGood
'descr': 1,
'date_created': 1,
'date_updated': 1,
'userId': 1,
'username': 1,
'name': 1,
'surname': 1,
'comune': 1,
'mycities': 1,
'profile.img': 1,
"profile.mygroups": 1,
"profile.mycircuits": 1,
'profile.qualifica': 1,
2023-04-04 15:26:56 +02:00
'profile.resid_province': 1,
reported: 1,
},
},
{
'$lookup': {
'from': 'subgoods',
'localField': 'idShipping',
'foreignField': '_id',
'as': 'MyGood',
},
},
{
'$replaceRoot': {
'newRoot': {
'$mergeObjects': [
{
'$arrayElemAt': [
'$MyGood',
0,
],
},
'$$ROOT',
],
},
},
},
{
'$project': {
'recGood': 1,
'sectorGood': 1,
'idSectorGood': 1,
'idGood': 1,
'idShipping': 1,
'idStatusGood': 1,
'idContribType': 1,
'idCity': 1,
pub_to_share: 1,
'numLevel': 1,
adType: 1,
otherfilters: 1,
'photos': 1,
'note': 1,
website: 1,
//**ADDFIELD_MyGood
'descr': 1,
'date_created': 1,
'date_updated': 1,
'userId': 1,
'username': 1,
'name': 1,
'surname': 1,
'comune': 1,
'mycities': 1,
'profile.img': 1,
"profile.mygroups": 1,
"profile.mycircuits": 1,
'profile.qualifica': 1,
2023-04-04 15:26:56 +02:00
'profile.resid_province': 1,
reported: 1,
},
},
{
'$lookup': {
'from': 'cities',
'localField': 'idCity',
'foreignField': '_id',
'as': 'mycities',
},
},
{
'$replaceRoot': {
'newRoot': {
'$mergeObjects': [
{
'$arrayElemAt': [
'$mycities',
0,
],
},
'$$ROOT',
],
},
},
},
{
'$project': {
'recGood': 1,
'sectorGood': 1,
'idSectorGood': 1,
'idGood': 1,
'idShipping': 1,
'idStatusGood': 1,
'idContribType': 1,
'idCity': 1,
pub_to_share: 1,
'numLevel': 1,
adType: 1,
otherfilters: 1,
'photos': 1,
'note': 1,
website: 1,
//**ADDFIELD_MyGood
'descr': 1,
'date_created': 1,
'date_updated': 1,
'userId': 1,
'username': 1,
'name': 1,
'surname': 1,
'comune': 1,
'mycities': 1,
'profile.img': 1,
"profile.mygroups": 1,
"profile.mycircuits": 1,
'profile.qualifica': 1,
2023-04-04 15:26:56 +02:00
'profile.resid_province': 1,
reported: 1,
},
},
];
return MyGood.aggregate(query).then((rec) => {
return rec ? rec[0] : null;
});
};
2023-04-04 15:26:56 +02:00
MyGoodSchema.statics.getCompleteRecord = function (idapp, id) {
const MyGood = this;
return MyGood.getMyRecById(idapp, id);
};
const MyGood = mongoose.model('MyGood', MyGoodSchema);
2023-04-04 15:26:56 +02:00
module.exports = { MyGood };