Carrello Spesa
This commit is contained in:
181
src/server/models/order.js
Executable file
181
src/server/models/order.js
Executable 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 };
|
||||
Reference in New Issue
Block a user