2020-12-25 03:54:16 +01:00
|
|
|
mongoose = require('mongoose');
|
2020-12-21 02:16:42 +01:00
|
|
|
const Schema = mongoose.Schema;
|
|
|
|
|
|
|
|
|
|
const tools = require('../tools/general');
|
|
|
|
|
|
|
|
|
|
mongoose.Promise = global.Promise;
|
|
|
|
|
mongoose.level = "F";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Resolving error Unknown modifier: $pushAll
|
|
|
|
|
mongoose.plugin(schema => {
|
|
|
|
|
schema.options.usePushEach = true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const productSchema = new Schema({
|
|
|
|
|
idapp: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
idProducer: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
2020-12-25 03:54:16 +01:00
|
|
|
idStorehouses: [
|
|
|
|
|
{ type: Schema.Types.ObjectId, ref: 'Storehouse' }
|
|
|
|
|
],
|
2020-12-21 02:16:42 +01:00
|
|
|
name: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
description: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
department: {
|
2020-12-25 03:54:16 +01:00
|
|
|
type: String, ref: 'Department'
|
2020-12-21 02:16:42 +01:00
|
|
|
},
|
|
|
|
|
category: {
|
2020-12-25 03:54:16 +01:00
|
|
|
type: String, ref: 'Category'
|
2020-12-21 02:16:42 +01:00
|
|
|
// type: String
|
|
|
|
|
},
|
|
|
|
|
price: {
|
|
|
|
|
type: Number
|
|
|
|
|
},
|
|
|
|
|
color: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
|
|
|
|
size: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
2020-12-25 03:54:16 +01:00
|
|
|
weight: {
|
|
|
|
|
type: Number
|
|
|
|
|
},
|
|
|
|
|
quantityAvailable: {
|
2020-12-21 02:16:42 +01:00
|
|
|
type: Number
|
|
|
|
|
},
|
2020-12-25 03:54:16 +01:00
|
|
|
stars: {
|
2020-12-21 02:16:42 +01:00
|
|
|
type: Number
|
|
|
|
|
},
|
2020-12-25 03:54:16 +01:00
|
|
|
dateAvailableFrom: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
2020-12-21 02:16:42 +01:00
|
|
|
icon: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
img: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var Product = module.exports = mongoose.model('Product', productSchema);
|
|
|
|
|
|
|
|
|
|
module.exports.getFieldsForSearch = function () {
|
2020-12-25 03:54:16 +01:00
|
|
|
return [{ field: 'name', type: tools.FieldType.string }]
|
2020-12-21 02:16:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports.executeQueryTable = function (idapp, params) {
|
|
|
|
|
params.fieldsearch = this.getFieldsForSearch();
|
|
|
|
|
return tools.executeQueryTable(this, idapp, params);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports.findAllIdApp = async function (idapp) {
|
|
|
|
|
const myfind = { idapp };
|
|
|
|
|
|
2020-12-25 03:54:16 +01:00
|
|
|
// return await Product.find(myfind);
|
|
|
|
|
|
|
|
|
|
const query = [
|
|
|
|
|
{ $match: { idapp } },
|
|
|
|
|
{ "$addFields": { "myidProd": { "$toObjectId": "$idProducer" } } },
|
|
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'producers',
|
|
|
|
|
localField: 'myidProd',
|
|
|
|
|
foreignField: '_id',
|
|
|
|
|
as: 'producer'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{ $unwind: '$producer' },
|
|
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'storehouses',
|
|
|
|
|
localField: 'idStorehouses',
|
|
|
|
|
foreignField: '_id',
|
|
|
|
|
as: 'storehouses'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let ris = await Product.aggregate(query)
|
|
|
|
|
|
|
|
|
|
return ris;
|
|
|
|
|
|
|
|
|
|
|
2020-12-21 02:16:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports.getAllProducts = function (query, sort, callback) {
|
|
|
|
|
Product.find(query, null, sort, callback)
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 03:54:16 +01:00
|
|
|
module.exports.getProductByDepartment = function (query, sort, callback) {
|
2020-12-21 02:16:42 +01:00
|
|
|
Product.find(query, null, sort, callback)
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 03:54:16 +01:00
|
|
|
module.exports.getProductByCategory = function (query, sort, callback) {
|
2020-12-21 02:16:42 +01:00
|
|
|
Product.find(query, null, sort, callback)
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 03:54:16 +01:00
|
|
|
module.exports.getProductByTitle = function (query, sort, callback) {
|
2020-12-21 02:16:42 +01:00
|
|
|
Product.find(query, null, sort, callback)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports.filterProductByDepartment = function (department, callback) {
|
|
|
|
|
let regexp = new RegExp(`${department}`, 'i')
|
|
|
|
|
var query = { department: { $regex: regexp } };
|
|
|
|
|
Product.find(query, callback)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports.filterProductByCategory = function (category, callback) {
|
|
|
|
|
let regexp = new RegExp(`${category}`, 'i')
|
|
|
|
|
var query = { category: { $regex: regexp } };
|
|
|
|
|
Product.find(query, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports.filterProductByTitle = function (title, callback) {
|
|
|
|
|
let regexp = new RegExp(`${title}`, 'i')
|
|
|
|
|
var query = { title: { $regex: regexp } };
|
|
|
|
|
Product.find(query, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports.getProductByID = function (id, callback) {
|
|
|
|
|
Product.findById(id, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// const Product = mongoose.model('Product', ProductSchema);
|
|
|
|
|
|
|
|
|
|
// module.exports = { Product };
|