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

85
src/server/models/cart.js Executable file → Normal file
View File

@@ -1,24 +1,59 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Order = require('../models/order');
const CartSchema = new Schema({
idapp: {
type: String
},
userId: { type: Schema.Types.ObjectId, ref: 'User' },
totalQty: { type: Number, default: 0 },
totalPrice: { type: Number, default: 0 },
items: [{
item: { type: Schema.Types.ObjectId, ref: 'Product' },
quantity: { type: Number, default: 1 },
price: { type: Number, default: 0 }
}]
items: [
{
order:
{ type: Schema.Types.ObjectId, ref: 'Order' }
}
],
modify_at: {
type: Date
},
});
module.exports.getCartByUserId = function (uid, callback) {
let query = { userId: uid }
Cart.find(query, callback)
}
var Cart = module.exports = mongoose.model('Cart', CartSchema);
module.exports.findAllIdApp = async function (idapp, userId) {
const myfind = { idapp, userId };
return await Cart.findOne(myfind);
};
module.exports.getCartByUserId = async function (uid, idapp) {
let query = { userId: uid, idapp }
const mycart = await Cart.findOne(query);
if (!!mycart) {
for (const idkey in mycart.items) {
try {
idorder = mycart.items[idkey]._id.toString();
const myorder = mycart.items[idkey].order;
if (!!myorder) {
idorder = mycart.items[idkey].order._id.toString();
}
const myord = await Order.getTotalOrderById(idorder);
if (myord.length > 0) {
mycart.items[idkey]._doc.order = myord[0];
}
} catch (e) {
console.log('err', e);
}
}
return mycart;
}
return null;
module.exports.getCartById = function (id, callback) {
Cart.findById(id, callback)
}
module.exports.updateCartByUserId = function (userId, newCart, callback) {
@@ -48,20 +83,24 @@ module.exports.updateCartByUserId = function (userId, newCart, callback) {
})
}
module.exports.updateCartByCartId = function (cartId, newCart, callback) {
Cart.findById(
{ _id: cartId },
{
$set: newCart
},
callback
)
module.exports.updateCartByCartId = async function (cartId, newCart) {
// delete newCart._doc._id;
const items = newCart._doc.items;
const totalQty = newCart.totalQty;
const totalPrice = newCart.totalPrice;
return await Cart.findOneAndUpdate({ _id: cartId }, { $set: { items, totalPrice, totalQty } }, { new: false })
.then((ris) => {
return ris;
}).catch(err => {
console.log('err', err);
return null
})
}
module.exports.createCart = function (newCart, callback) {
newCart.save(callback)
module.exports.createCart = async function (newCart) {
return await newCart.save()
}
module.exports = mongoose.model('Cart', CartSchema);

181
src/server/models/order.js Executable file
View File

@@ -0,0 +1,181 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const tools = require('../tools/general');
const { ObjectID } = require('mongodb');
mongoose.Promise = global.Promise;
mongoose.level = "F";
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
});
const orderSchema = new Schema({
idapp: {
type: String,
},
userId: {
type: String,
},
status: {
type: Number,
},
idProduct: {
type: String
},
idProducer: {
type: String
},
idStorehouse: {
type: String
},
price: {
type: Number
},
color: {
type: String
},
size: {
type: String
},
quantity: {
type: Number
},
weight: {
type: Number
},
stars: {
type: Number
},
date_created: {
type: Date
},
date_checkout: {
type: Date
},
date_payment: {
type: Date
},
date_shipping: {
type: Date
},
date_delivered: {
type: Date
},
notes: {
type: String
}
});
var Order = module.exports = mongoose.model('Order', orderSchema);
module.exports.getFieldsForSearch = function () {
return []
};
module.exports.executeQueryTable = function (idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, idapp, params);
};
module.exports.findAllIdApp = async function (idapp) {
const query = [
{ $match: { idapp } },
{ "$addFields": { "myidProd": { "$toObjectId": "$idProduct" } } },
{ "$addFields": { "myidProducer": { "$toObjectId": "$idProducer" } } },
{
$lookup: {
from: 'products',
localField: 'myidProd',
foreignField: '_id',
as: 'product'
}
},
{
$lookup: {
from: 'producers',
localField: 'myidProducer',
foreignField: '_id',
as: 'producer'
}
},
{ $unwind: '$product' },
{ $unwind: '$producer' },
];
return await Order.aggregate(query)
};
module.exports.getAllOrders = function (query, sort, callback) {
Order.find(query, null, sort, callback)
}
module.exports.getOrderByUserId = function (userId, sort, callback) {
Order.find({ userId }, null, sort, callback)
}
module.exports.getOrderByID = function (id, callback) {
Order.findById(id, callback);
}
module.exports.createOrder = async function (order) {
const orderModel = new Order(order);
return await orderModel.save(order)
.then((ris) => {
if (!!ris)
return ris._id;
return null;
});
}
module.exports.getTotalOrderById = async function (id) {
const query = [
{ $match: { _id: ObjectID(id) } },
{ "$addFields": { "myidProd": { "$toObjectId": "$idProduct" } } },
{ "$addFields": { "myidProducer": { "$toObjectId": "$idProducer" } } },
{ "$addFields": { "myidStore": { "$toObjectId": "$idStorehouse" } } },
{
$lookup: {
from: 'products',
localField: 'myidProd',
foreignField: '_id',
as: 'product'
}
},
{
$lookup: {
from: 'producers',
localField: 'myidProducer',
foreignField: '_id',
as: 'producer'
}
},
{
$lookup: {
from: 'storehouses',
localField: 'myidStore',
foreignField: '_id',
as: 'storehouse'
}
},
{ $unwind: '$product' },
{ $unwind: '$producer' },
{ $unwind: '$storehouse' },
];
return await Order.aggregate(query);
}
// const Order = mongoose.model('Order', OrderSchema);
// module.exports = { Order };

99
src/server/models/producer.js Executable file
View File

@@ -0,0 +1,99 @@
mongoose = require('mongoose');
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 producerSchema = new Schema({
idapp: {
type: String,
},
name: {
type: String,
},
description: {
type: String,
},
referent: {
type: String,
},
region: {
type: String,
},
city: {
type: String,
},
img: {
type: String,
},
website: {
type: String,
}
});
var Producer = module.exports = mongoose.model('Producer', producerSchema);
module.exports.getFieldsForSearch = function () {
return [{field: 'name', type: tools.FieldType.string}]
};
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 };
return await Producer.find(myfind);
};
module.exports.getAllProducers = function (query, sort, callback) {
Producer.find(query, null, sort, callback)
}
module.exports.getProducerByDepartment = function (query,sort, callback) {
Producer.find(query, null, sort, callback)
}
module.exports.getProducerByCategory = function (query,sort, callback) {
Producer.find(query, null, sort, callback)
}
module.exports.getProducerByTitle = function (query,sort, callback) {
Producer.find(query, null, sort, callback)
}
module.exports.filterProducerByDepartment = function (department, callback) {
let regexp = new RegExp(`${department}`, 'i')
var query = { department: { $regex: regexp } };
Producer.find(query, callback)
}
module.exports.filterProducerByCategory = function (category, callback) {
let regexp = new RegExp(`${category}`, 'i')
var query = { category: { $regex: regexp } };
Producer.find(query, callback);
}
module.exports.filterProducerByTitle = function (title, callback) {
let regexp = new RegExp(`${title}`, 'i')
var query = { title: { $regex: regexp } };
Producer.find(query, callback);
}
module.exports.getProducerByID = function (id, callback) {
Producer.findById(id, callback);
}
// const Producer = mongoose.model('Producer', producerSchema);
// module.exports = { Producer };

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)
}

61
src/server/models/storehouse.js Executable file
View File

@@ -0,0 +1,61 @@
mongoose = require('mongoose');
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 storehouseSchema = new Schema({
idapp: {
type: String,
},
name: {
type: String,
},
description: {
type: String,
},
referent: {
type: String,
},
address: {
type: String,
},
city: {
type: String,
},
region: {
type: String,
},
img: {
type: String,
},
website: {
type: String,
},
});
var Storehouse = module.exports = mongoose.model('Storehouse', storehouseSchema);
module.exports.getFieldsForSearch = function () {
return [{field: 'name', type: tools.FieldType.string}]
};
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 };
return await Storehouse.find(myfind);
};