2022-09-14 11:32:04 +02:00
|
|
|
const mongoose = require('mongoose').set('debug', false);
|
2022-02-21 13:12:27 +01:00
|
|
|
const Schema = mongoose.Schema;
|
|
|
|
|
|
|
|
|
|
mongoose.Promise = global.Promise;
|
|
|
|
|
mongoose.level = 'F';
|
|
|
|
|
|
|
|
|
|
const tools = require('../tools/general');
|
|
|
|
|
|
2023-10-01 01:24:47 +02:00
|
|
|
const { Reaction } = require('./reaction');
|
|
|
|
|
|
2023-04-07 02:45:21 +02:00
|
|
|
const shared_consts = require('../tools/shared_nodejs');
|
|
|
|
|
|
2023-04-04 15:26:56 +02:00
|
|
|
const { ObjectID } = require('mongodb');
|
2022-02-21 13:12:27 +01:00
|
|
|
|
|
|
|
|
// Resolving error Unknown modifier: $pushAll
|
|
|
|
|
mongoose.plugin(schema => {
|
|
|
|
|
schema.options.usePushEach = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const MyGoodSchema = new Schema({
|
2023-10-01 01:24:47 +02:00
|
|
|
...{
|
|
|
|
|
_id: {
|
2022-02-21 13:12:27 +01:00
|
|
|
type: String,
|
2023-10-01 01:24:47 +02:00
|
|
|
},
|
|
|
|
|
idapp: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
userId: { type: Schema.Types.ObjectId, ref: 'User' },
|
|
|
|
|
idSectorGood: {
|
2022-02-21 13:12:27 +01:00
|
|
|
type: Number,
|
2023-10-01 01:24:47 +02:00
|
|
|
},
|
|
|
|
|
idGood: {
|
|
|
|
|
type: Number,
|
|
|
|
|
default: 0,
|
|
|
|
|
},
|
|
|
|
|
idShipping: [
|
|
|
|
|
{
|
|
|
|
|
type: Number,
|
|
|
|
|
}],
|
|
|
|
|
|
|
|
|
|
idContribType: [
|
|
|
|
|
{
|
2022-02-21 13:12:27 +01:00
|
|
|
type: String,
|
2023-10-01 01:24:47 +02:00
|
|
|
}],
|
|
|
|
|
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,
|
2022-02-21 13:12:27 +01:00
|
|
|
}],
|
2023-10-01 01:24:47 +02:00
|
|
|
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,
|
|
|
|
|
},
|
|
|
|
|
date_updated: {
|
|
|
|
|
type: Date,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
...Reaction.getFieldsForReactions()
|
2022-02-21 13:12:27 +01:00
|
|
|
});
|
|
|
|
|
|
2023-04-04 15:26:56 +02:00
|
|
|
MyGoodSchema.pre('save', async function (next) {
|
2022-02-21 13:12:27 +01:00
|
|
|
if (this.isNew) {
|
2023-04-07 02:45:21 +02:00
|
|
|
if (!this.date_created)
|
|
|
|
|
this.date_created = new Date();
|
2022-02-21 13:12:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2023-04-04 15:26:56 +02:00
|
|
|
MyGoodSchema.statics.findAllIdApp = async function (idapp) {
|
2022-02-21 13:12:27 +01:00
|
|
|
const MyGood = this;
|
|
|
|
|
|
|
|
|
|
const query = [
|
2023-04-04 15:26:56 +02:00
|
|
|
{ $match: { idapp } },
|
|
|
|
|
{ $sort: { descr: 1 } },
|
2022-02-21 13:12:27 +01:00
|
|
|
];
|
|
|
|
|
|
2022-09-11 11:45:33 +02:00
|
|
|
return await MyGood.aggregate(query).then((arrrec) => {
|
2022-02-21 13:12:27 +01:00
|
|
|
return arrrec;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-04 15:26:56 +02:00
|
|
|
MyGoodSchema.statics.getFieldsForSearch = function () {
|
2022-02-21 13:12:27 +01:00
|
|
|
return [];
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-04 15:26:56 +02:00
|
|
|
MyGoodSchema.statics.getFieldsLastForSearch = function () {
|
2022-02-21 13:12:27 +01:00
|
|
|
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 },
|
2022-02-21 13:12:27 +01:00
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2023-04-04 15:26:56 +02:00
|
|
|
MyGoodSchema.statics.executeQueryTable = function (idapp, params, user) {
|
2022-02-21 13:12:27 +01:00
|
|
|
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',
|
2023-04-07 02:45:21 +02:00
|
|
|
lk_proj: this.getProject(),
|
2022-02-21 13:12:27 +01:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-04 15:26:56 +02:00
|
|
|
params = { ...params, ...otherparams };
|
2022-02-21 13:12:27 +01:00
|
|
|
|
2022-06-16 20:34:42 +02:00
|
|
|
return tools.executeQueryTable(this, idapp, params, user);
|
2022-02-21 13:12:27 +01:00
|
|
|
};
|
|
|
|
|
|
2023-04-04 15:26:56 +02:00
|
|
|
MyGoodSchema.statics.getMyRecById = function (idapp, idGood) {
|
2022-02-21 13:12:27 +01:00
|
|
|
const MyGood = this;
|
|
|
|
|
|
2023-04-04 15:26:56 +02:00
|
|
|
let myparsid = {
|
2023-04-07 02:45:21 +02:00
|
|
|
'_id': idGood,
|
2023-04-17 00:11:36 +02:00
|
|
|
idapp,
|
2023-04-04 15:26:56 +02:00
|
|
|
};
|
|
|
|
|
|
2023-04-07 02:45:21 +02:00
|
|
|
let query = [
|
2022-02-21 13:12:27 +01:00
|
|
|
{
|
2023-10-01 01:24:47 +02:00
|
|
|
'$match':
|
|
|
|
|
myparsid,
|
2022-02-21 13:12:27 +01:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'$sort': {
|
|
|
|
|
'desc': 1,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'$addFields': {
|
|
|
|
|
'myId1': {
|
|
|
|
|
'$toObjectId': '$userId',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'$lookup': {
|
|
|
|
|
'from': 'users',
|
|
|
|
|
'localField': 'myId1',
|
|
|
|
|
'foreignField': '_id',
|
|
|
|
|
'as': 'user',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'$replaceRoot': {
|
|
|
|
|
'newRoot': {
|
|
|
|
|
'$mergeObjects': [
|
|
|
|
|
{
|
|
|
|
|
'$arrayElemAt': [
|
|
|
|
|
'$user',
|
|
|
|
|
0,
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
'$$ROOT',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-04-07 02:45:21 +02:00
|
|
|
$project: this.getProject(),
|
2022-02-21 13:12:27 +01:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'$lookup': {
|
|
|
|
|
'from': 'goods',
|
|
|
|
|
'localField': 'idGood',
|
|
|
|
|
'foreignField': '_id',
|
|
|
|
|
'as': 'recGood',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'$replaceRoot': {
|
|
|
|
|
'newRoot': {
|
|
|
|
|
'$mergeObjects': [
|
|
|
|
|
{
|
|
|
|
|
'$arrayElemAt': [
|
|
|
|
|
'$recGood',
|
|
|
|
|
0,
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
'$$ROOT',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-04-07 02:45:21 +02:00
|
|
|
$project: this.getProject(),
|
2022-02-21 13:12:27 +01:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'$lookup': {
|
|
|
|
|
'from': 'sectorgoods',
|
2022-08-15 15:10:27 +02:00
|
|
|
'localField': 'idSectorGood',
|
|
|
|
|
// 'localField': 'recGood.idSectorGood',
|
2022-02-21 13:12:27 +01:00
|
|
|
'foreignField': '_id',
|
2022-08-15 15:10:27 +02:00
|
|
|
'as': 'sectorGood',
|
2022-02-21 13:12:27 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'$replaceRoot': {
|
|
|
|
|
'newRoot': {
|
|
|
|
|
'$mergeObjects': [
|
|
|
|
|
{
|
|
|
|
|
'$arrayElemAt': [
|
|
|
|
|
'$sectorgood',
|
|
|
|
|
0,
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
'$$ROOT',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-04-07 02:45:21 +02:00
|
|
|
$project: this.getProject(),
|
2022-02-21 13:12:27 +01:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'$lookup': {
|
|
|
|
|
'from': 'subgoods',
|
|
|
|
|
'localField': 'idShipping',
|
|
|
|
|
'foreignField': '_id',
|
|
|
|
|
'as': 'MyGood',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'$replaceRoot': {
|
|
|
|
|
'newRoot': {
|
|
|
|
|
'$mergeObjects': [
|
|
|
|
|
{
|
|
|
|
|
'$arrayElemAt': [
|
|
|
|
|
'$MyGood',
|
|
|
|
|
0,
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
'$$ROOT',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-04-07 02:45:21 +02:00
|
|
|
$project: this.getProject(),
|
2022-02-21 13:12:27 +01:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'$lookup': {
|
|
|
|
|
'from': 'cities',
|
|
|
|
|
'localField': 'idCity',
|
|
|
|
|
'foreignField': '_id',
|
|
|
|
|
'as': 'mycities',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'$replaceRoot': {
|
|
|
|
|
'newRoot': {
|
|
|
|
|
'$mergeObjects': [
|
|
|
|
|
{
|
|
|
|
|
'$arrayElemAt': [
|
|
|
|
|
'$mycities',
|
|
|
|
|
0,
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
'$$ROOT',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-04-07 02:45:21 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let numtab = tools.getNumTabByTable(shared_consts.TABLES_MYGOODS);
|
|
|
|
|
|
|
|
|
|
const objadd = tools.addNumFavoriteAndBookmarkToQuery(idapp, numtab);
|
|
|
|
|
query = [...query, ...objadd.query];
|
|
|
|
|
|
2023-04-17 00:11:36 +02:00
|
|
|
const toadd = {
|
|
|
|
|
$project: this.getProject(objadd.proj),
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-01 01:24:47 +02:00
|
|
|
query = [...query, { ...toadd }];
|
2022-02-21 13:12:27 +01:00
|
|
|
|
|
|
|
|
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) {
|
2022-02-21 13:12:27 +01:00
|
|
|
const MyGood = this;
|
|
|
|
|
|
2022-03-06 00:48:33 +01:00
|
|
|
return MyGood.getMyRecById(idapp, id);
|
2022-02-21 13:12:27 +01:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-07 02:45:21 +02:00
|
|
|
MyGoodSchema.statics.getProject = function () {
|
|
|
|
|
let proj = {
|
|
|
|
|
'recGood': 1,
|
|
|
|
|
'sectorGood': 1,
|
|
|
|
|
'idSectorGood': 1,
|
|
|
|
|
'idGood': 1,
|
|
|
|
|
'idShipping': 1,
|
|
|
|
|
'idStatusGood': 1,
|
|
|
|
|
//**ADDFIELD_MYGOOD
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const proj_add = shared_consts.getProjectForAll()
|
|
|
|
|
|
|
|
|
|
return Object.assign({}, proj, proj_add);
|
2023-10-01 01:24:47 +02:00
|
|
|
|
2023-04-07 02:45:21 +02:00
|
|
|
}
|
2022-02-21 13:12:27 +01:00
|
|
|
|
|
|
|
|
const MyGood = mongoose.model('MyGood', MyGoodSchema);
|
|
|
|
|
|
2023-04-04 15:26:56 +02:00
|
|
|
module.exports = { MyGood };
|