Carrello Spesa

This commit is contained in:
Paolo Arena
2020-12-25 03:54:16 +01:00
parent 67d2872e61
commit 142380e54b
12 changed files with 736 additions and 214 deletions

View File

@@ -1,4 +1,4 @@
mongoose = require('mongoose');
mongoose = require('mongoose');
const Schema = mongoose.Schema;
const tools = require('../tools/general');
@@ -19,6 +19,9 @@ const productSchema = new Schema({
idProducer: {
type: String
},
idStorehouses: [
{ type: Schema.Types.ObjectId, ref: 'Storehouse' }
],
name: {
type: String,
},
@@ -26,10 +29,10 @@ const productSchema = new Schema({
type: String,
},
department: {
type: String
type: String, ref: 'Department'
},
category: {
type: mongoose.Schema.Types.ObjectId, ref: 'Category'
type: String, ref: 'Category'
// type: String
},
price: {
@@ -41,12 +44,18 @@ const productSchema = new Schema({
size: {
type: String
},
quantity: {
weight: {
type: Number
},
date: {
quantityAvailable: {
type: Number
},
stars: {
type: Number
},
dateAvailableFrom: {
type: Date
},
icon: {
type: String,
},
@@ -58,7 +67,7 @@ const productSchema = new Schema({
var Product = module.exports = mongoose.model('Product', productSchema);
module.exports.getFieldsForSearch = function () {
return [{field: 'name', type: tools.FieldType.string}]
return [{ field: 'name', type: tools.FieldType.string }]
};
module.exports.executeQueryTable = function (idapp, params) {
@@ -69,22 +78,50 @@ module.exports.executeQueryTable = function (idapp, params) {
module.exports.findAllIdApp = async function (idapp) {
const myfind = { idapp };
return await Product.find(myfind);
// 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;
};
module.exports.getAllProducts = function (query, sort, callback) {
Product.find(query, null, sort, callback)
}
module.exports.getProductByDepartment = function (query,sort, callback) {
module.exports.getProductByDepartment = function (query, sort, callback) {
Product.find(query, null, sort, callback)
}
module.exports.getProductByCategory = function (query,sort, callback) {
module.exports.getProductByCategory = function (query, sort, callback) {
Product.find(query, null, sort, callback)
}
module.exports.getProductByTitle = function (query,sort, callback) {
module.exports.getProductByTitle = function (query, sort, callback) {
Product.find(query, null, sort, callback)
}