Carrello Spesa
This commit is contained in:
@@ -4,6 +4,13 @@ var { User } = require('../models/user');
|
|||||||
|
|
||||||
const tools = require('../tools/general');
|
const tools = require('../tools/general');
|
||||||
|
|
||||||
|
const auth_default = (req, res, next) => {
|
||||||
|
|
||||||
|
if (req.body.keyappid === process.env.KEY_APP_ID)
|
||||||
|
next();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
const authenticate = (req, res, next) => {
|
const authenticate = (req, res, next) => {
|
||||||
const token = req.header('x-auth');
|
const token = req.header('x-auth');
|
||||||
|
|
||||||
@@ -71,4 +78,4 @@ const authenticate_noerror = (req, res, next) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = { authenticate, authenticate_noerror };
|
module.exports = { authenticate, authenticate_noerror, auth_default };
|
||||||
|
|||||||
85
src/server/models/cart.js
Executable file → Normal file
85
src/server/models/cart.js
Executable file → Normal file
@@ -1,24 +1,59 @@
|
|||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
const Schema = mongoose.Schema;
|
const Schema = mongoose.Schema;
|
||||||
|
|
||||||
|
const Order = require('../models/order');
|
||||||
|
|
||||||
const CartSchema = new Schema({
|
const CartSchema = new Schema({
|
||||||
|
idapp: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
userId: { type: Schema.Types.ObjectId, ref: 'User' },
|
userId: { type: Schema.Types.ObjectId, ref: 'User' },
|
||||||
totalQty: { type: Number, default: 0 },
|
totalQty: { type: Number, default: 0 },
|
||||||
totalPrice: { type: Number, default: 0 },
|
totalPrice: { type: Number, default: 0 },
|
||||||
items: [{
|
items: [
|
||||||
item: { type: Schema.Types.ObjectId, ref: 'Product' },
|
{
|
||||||
quantity: { type: Number, default: 1 },
|
order:
|
||||||
price: { type: Number, default: 0 }
|
{ type: Schema.Types.ObjectId, ref: 'Order' }
|
||||||
}]
|
}
|
||||||
|
],
|
||||||
|
modify_at: {
|
||||||
|
type: Date
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports.getCartByUserId = function (uid, callback) {
|
var Cart = module.exports = mongoose.model('Cart', CartSchema);
|
||||||
let query = { userId: uid }
|
|
||||||
Cart.find(query, callback)
|
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) {
|
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) {
|
module.exports.updateCartByCartId = async function (cartId, newCart) {
|
||||||
Cart.findById(
|
// delete newCart._doc._id;
|
||||||
{ _id: cartId },
|
const items = newCart._doc.items;
|
||||||
{
|
const totalQty = newCart.totalQty;
|
||||||
$set: newCart
|
const totalPrice = newCart.totalPrice;
|
||||||
},
|
|
||||||
callback
|
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 = async function (newCart) {
|
||||||
module.exports.createCart = function (newCart, callback) {
|
return await newCart.save()
|
||||||
newCart.save(callback)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = mongoose.model('Cart', CartSchema);
|
|
||||||
|
|||||||
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 };
|
||||||
99
src/server/models/producer.js
Executable file
99
src/server/models/producer.js
Executable 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 };
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
mongoose = require('mongoose');
|
mongoose = require('mongoose');
|
||||||
const Schema = mongoose.Schema;
|
const Schema = mongoose.Schema;
|
||||||
|
|
||||||
const tools = require('../tools/general');
|
const tools = require('../tools/general');
|
||||||
@@ -19,6 +19,9 @@ const productSchema = new Schema({
|
|||||||
idProducer: {
|
idProducer: {
|
||||||
type: String
|
type: String
|
||||||
},
|
},
|
||||||
|
idStorehouses: [
|
||||||
|
{ type: Schema.Types.ObjectId, ref: 'Storehouse' }
|
||||||
|
],
|
||||||
name: {
|
name: {
|
||||||
type: String,
|
type: String,
|
||||||
},
|
},
|
||||||
@@ -26,10 +29,10 @@ const productSchema = new Schema({
|
|||||||
type: String,
|
type: String,
|
||||||
},
|
},
|
||||||
department: {
|
department: {
|
||||||
type: String
|
type: String, ref: 'Department'
|
||||||
},
|
},
|
||||||
category: {
|
category: {
|
||||||
type: mongoose.Schema.Types.ObjectId, ref: 'Category'
|
type: String, ref: 'Category'
|
||||||
// type: String
|
// type: String
|
||||||
},
|
},
|
||||||
price: {
|
price: {
|
||||||
@@ -41,12 +44,18 @@ const productSchema = new Schema({
|
|||||||
size: {
|
size: {
|
||||||
type: String
|
type: String
|
||||||
},
|
},
|
||||||
quantity: {
|
weight: {
|
||||||
type: Number
|
type: Number
|
||||||
},
|
},
|
||||||
date: {
|
quantityAvailable: {
|
||||||
type: Number
|
type: Number
|
||||||
},
|
},
|
||||||
|
stars: {
|
||||||
|
type: Number
|
||||||
|
},
|
||||||
|
dateAvailableFrom: {
|
||||||
|
type: Date
|
||||||
|
},
|
||||||
icon: {
|
icon: {
|
||||||
type: String,
|
type: String,
|
||||||
},
|
},
|
||||||
@@ -58,7 +67,7 @@ const productSchema = new Schema({
|
|||||||
var Product = module.exports = mongoose.model('Product', productSchema);
|
var Product = module.exports = mongoose.model('Product', productSchema);
|
||||||
|
|
||||||
module.exports.getFieldsForSearch = function () {
|
module.exports.getFieldsForSearch = function () {
|
||||||
return [{field: 'name', type: tools.FieldType.string}]
|
return [{ field: 'name', type: tools.FieldType.string }]
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.executeQueryTable = function (idapp, params) {
|
module.exports.executeQueryTable = function (idapp, params) {
|
||||||
@@ -69,22 +78,50 @@ module.exports.executeQueryTable = function (idapp, params) {
|
|||||||
module.exports.findAllIdApp = async function (idapp) {
|
module.exports.findAllIdApp = async function (idapp) {
|
||||||
const myfind = { 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) {
|
module.exports.getAllProducts = function (query, sort, callback) {
|
||||||
Product.find(query, null, 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)
|
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)
|
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)
|
Product.find(query, null, sort, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
61
src/server/models/storehouse.js
Executable file
61
src/server/models/storehouse.js
Executable 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);
|
||||||
|
};
|
||||||
|
|
||||||
@@ -1,58 +1,100 @@
|
|||||||
const cartModel = require('../models/cart')
|
const cartModel = require('../models/cart')
|
||||||
|
|
||||||
|
const { ObjectID } = require('mongodb');
|
||||||
|
|
||||||
|
const Order = require('../models/order');
|
||||||
|
|
||||||
class Cart {
|
class Cart {
|
||||||
constructor(oldCart) {
|
constructor(order) {
|
||||||
this.items = oldCart.items || {};
|
this.items = {};
|
||||||
this.totalQty = oldCart.totalQty || 0;
|
if (!!order) {
|
||||||
this.totalPrice = oldCart.totalPrice || 0;
|
this.idapp = order.idapp || 0;
|
||||||
this.userId = oldCart.userId || "";
|
this.items[order._id] = order;
|
||||||
|
this.userId = order.userId || "";
|
||||||
|
}
|
||||||
|
this.modify_at = new Date();
|
||||||
|
|
||||||
|
this.updatetotals();
|
||||||
}
|
}
|
||||||
|
|
||||||
add(item, id) {
|
static constructByCart(cart) {
|
||||||
let storedItem = this.items[id];
|
try {
|
||||||
if (!storedItem) {
|
const mynewcart = new Cart(null);
|
||||||
storedItem = this.items[id] = { item: item, qty: 0, price: 0 };
|
mynewcart.idapp = cart.idapp || 0;
|
||||||
|
mynewcart.items = cart.items;
|
||||||
|
mynewcart.userId = cart.userId || "";
|
||||||
|
mynewcart.modify_at = new Date();
|
||||||
|
|
||||||
|
return mynewcart;
|
||||||
|
} catch (e) {
|
||||||
|
console.log('Error', e);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
storedItem.qty++;
|
|
||||||
storedItem.price = parseFloat((storedItem.item.price * storedItem.qty).toFixed(2));
|
|
||||||
this.items[id]=storedItem
|
|
||||||
this.totalQty++;
|
|
||||||
this.totalPrice += storedItem.item.price;
|
|
||||||
this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
|
|
||||||
return this
|
|
||||||
}
|
}
|
||||||
|
|
||||||
generateModel(){
|
async addqty(itemorder) {
|
||||||
|
const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id)
|
||||||
|
if (!!myitem) {
|
||||||
|
myitem.order.quantity++;
|
||||||
|
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
|
||||||
|
this.updatetotals();
|
||||||
|
return myitem.order.quantity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async subqty(itemorder) {
|
||||||
|
const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id)
|
||||||
|
if (!!myitem) {
|
||||||
|
myitem.order.quantity--;
|
||||||
|
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
|
||||||
|
this.updatetotals();
|
||||||
|
return myitem.order.quantity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
addItem(itemorder) {
|
||||||
|
// this.items.push(itemorder);
|
||||||
|
let ind = this.items.length;
|
||||||
|
this.items[ind] = {};
|
||||||
|
this.items[ind].order = itemorder;
|
||||||
|
this.updatetotals();
|
||||||
|
|
||||||
|
return ind;
|
||||||
|
}
|
||||||
|
|
||||||
|
removeItem(orderId) {
|
||||||
|
// this.items.push(itemorder);
|
||||||
|
this.items = this.items.filter(item => item.order._id.toString() !== orderId.toString());
|
||||||
|
this.updatetotals();
|
||||||
|
}
|
||||||
|
|
||||||
|
generateModel() {
|
||||||
let newCart = new cartModel({
|
let newCart = new cartModel({
|
||||||
items: this.items,
|
idapp: this.idapp,
|
||||||
|
items: this.generateArray(),
|
||||||
totalQty: this.totalQty,
|
totalQty: this.totalQty,
|
||||||
totalPrice: this.totalPrice,
|
totalPrice: this.totalPrice,
|
||||||
userId: this.userId
|
userId: this.userId,
|
||||||
|
modify_at: this.modify_at
|
||||||
})
|
})
|
||||||
return newCart
|
return newCart
|
||||||
}
|
}
|
||||||
|
|
||||||
decreaseQty(id) {
|
updatetotals() {
|
||||||
this.items[id].qty--;
|
this.totalQty = 0;
|
||||||
this.items[id].price -= this.items[id].item.price;
|
this.totalPrice = 0;
|
||||||
this.items[id].price = parseFloat(this.items[id].price.toFixed(2))
|
for (const rec in this.items) {
|
||||||
this.totalQty--;
|
let order = this.items[rec].order;
|
||||||
this.totalPrice -= this.items[id].item.price
|
if (!order) {
|
||||||
this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
|
order = this.items[rec];
|
||||||
if (this.items[id].qty <= 0) {
|
|
||||||
delete this.items[id];
|
|
||||||
}
|
}
|
||||||
return this
|
this.totalQty += order.quantity;
|
||||||
|
this.totalPrice += order.price * order.quantity;
|
||||||
}
|
}
|
||||||
|
|
||||||
increaseQty(id) {
|
|
||||||
this.items[id].qty++;
|
|
||||||
this.items[id].price += this.items[id].item.price;
|
|
||||||
this.items[id].price = parseFloat(this.items[id].price.toFixed(2))
|
|
||||||
this.totalQty++;
|
|
||||||
this.totalPrice += this.items[id].item.price
|
|
||||||
this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
|
this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
|
||||||
return this
|
|
||||||
}
|
}
|
||||||
|
|
||||||
generateArray() {
|
generateArray() {
|
||||||
|
|||||||
170
src/server/router/cart_router.js
Executable file
170
src/server/router/cart_router.js
Executable file
@@ -0,0 +1,170 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
const tools = require('../tools/general');
|
||||||
|
|
||||||
|
var server_constants = require('../tools/server_constants');
|
||||||
|
|
||||||
|
var { Project } = require('../models/project');
|
||||||
|
|
||||||
|
var { authenticate, auth_default } = require('../middleware/authenticate');
|
||||||
|
|
||||||
|
var mongoose = require('mongoose');
|
||||||
|
const Subscription = mongoose.model('subscribers');
|
||||||
|
|
||||||
|
const _ = require('lodash');
|
||||||
|
|
||||||
|
const { ObjectID } = require('mongodb');
|
||||||
|
|
||||||
|
const Product = require('../models/product');
|
||||||
|
const Order = require('../models/order');
|
||||||
|
const Variant = require('../models/variant');
|
||||||
|
|
||||||
|
/*const Department = require('../models/Department')
|
||||||
|
const Category = require('../models/Category')
|
||||||
|
const TypedError = require('../modules/ErrorHandler')
|
||||||
|
const paypal_config = require('../configs/paypal-config')
|
||||||
|
const paypal = require('paypal-rest-sdk')
|
||||||
|
*/
|
||||||
|
|
||||||
|
const CartClass = require('../modules/Cart')
|
||||||
|
const Cart = require('../models/cart');
|
||||||
|
|
||||||
|
//GET cart
|
||||||
|
router.get('/:userId', authenticate, function (req, res, next) {
|
||||||
|
let userId = req.body.userId
|
||||||
|
let idapp = req.body.idapp
|
||||||
|
Cart.getCartByUserId(userId, idapp, function (err, cart) {
|
||||||
|
if (err) return next(err)
|
||||||
|
|
||||||
|
if (cart)
|
||||||
|
res.send({ code: server_constants.RIS_CODE_OK, cart });
|
||||||
|
else
|
||||||
|
res.status(400).send(e);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
//POST cart
|
||||||
|
router.post('/:userId', authenticate, async function (req, res, next) {
|
||||||
|
let idapp = req.body.idapp;
|
||||||
|
let userId = req.params.userId;
|
||||||
|
let addqty = req.body.addqty;
|
||||||
|
let subqty = req.body.subqty;
|
||||||
|
let order = req.body.order;
|
||||||
|
|
||||||
|
const mycart = await Cart.getCartByUserId(userId, idapp);
|
||||||
|
|
||||||
|
// const myorder = Order.getOrderByID(order._id);
|
||||||
|
if (!addqty && !subqty)
|
||||||
|
order._id = await Order.createOrder(order);
|
||||||
|
|
||||||
|
let cart = null;
|
||||||
|
// no cart save empty cart to database then return response
|
||||||
|
let myqty = 0;
|
||||||
|
if (!mycart) {
|
||||||
|
let oldCart = new CartClass(order)
|
||||||
|
cart = await Cart.createCart(oldCart.generateModel());
|
||||||
|
} else {
|
||||||
|
let newCart = CartClass.constructByCart(mycart);
|
||||||
|
if (addqty) {
|
||||||
|
myqty = await newCart.addqty(order);
|
||||||
|
} else if (subqty) {
|
||||||
|
myqty = await newCart.subqty(order);
|
||||||
|
} else {
|
||||||
|
const ind = newCart.addItem(order);
|
||||||
|
}
|
||||||
|
cart = await Cart.updateCartByCartId(mycart._id, newCart.generateModel());
|
||||||
|
}
|
||||||
|
if (cart) {
|
||||||
|
const carttot = await Cart.getCartByUserId(userId, idapp);
|
||||||
|
return res.send({ code: server_constants.RIS_CODE_OK, cart: carttot, qty: myqty });
|
||||||
|
} else {
|
||||||
|
return res.send({ code: server_constants.RIS_CODE_ERR, cart: null });
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Cart.updateCartByUserId(
|
||||||
|
userId,
|
||||||
|
newCart,
|
||||||
|
function (err, result) {
|
||||||
|
if (err) return next(err)
|
||||||
|
return res.status(200).json({ cart: result })
|
||||||
|
})
|
||||||
|
|
||||||
|
*/
|
||||||
|
})
|
||||||
|
|
||||||
|
router.delete('/:userId', authenticate, async function (req, res) {
|
||||||
|
console.log('DELETE Item');
|
||||||
|
let idapp = req.query.idapp;
|
||||||
|
let userId = req.params.userId;
|
||||||
|
let orderId = req.query.orderId;
|
||||||
|
|
||||||
|
const mycart = await Cart.getCartByUserId(userId, idapp);
|
||||||
|
|
||||||
|
// Rimuovere l'Ordine
|
||||||
|
const recremoved = await Order.findByIdAndRemove(orderId);
|
||||||
|
if (recremoved) {
|
||||||
|
// Rimuovere l'id sul Carrello
|
||||||
|
|
||||||
|
let newCart = CartClass.constructByCart(mycart);
|
||||||
|
newCart.removeItem(orderId);
|
||||||
|
let carttot = null;
|
||||||
|
const cart = await Cart.updateCartByCartId(mycart._id, newCart.generateModel());
|
||||||
|
|
||||||
|
carttot = await Cart.getCartByUserId(userId, idapp);
|
||||||
|
|
||||||
|
console.log('carttot', carttot)
|
||||||
|
return res.send({ code: server_constants.RIS_CODE_OK, cart: carttot });
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.send({ code: server_constants.RIS_CODE_ERR, cart: null });
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//PUT cart
|
||||||
|
router.put('/:userId', authenticate, function (req, res, next) {
|
||||||
|
let userId = req.params.userId
|
||||||
|
let requestProduct = req.body
|
||||||
|
let { productId, color, size } = requestProduct.product
|
||||||
|
|
||||||
|
Cart.getCartByUserId(userId, function (err, c) {
|
||||||
|
if (err) return next(err)
|
||||||
|
let oldCart = new CartClass(c[0] || {})
|
||||||
|
Product.getProductByID(productId, function (err, p) {
|
||||||
|
if (err) return next(err)
|
||||||
|
let newCart = oldCart.add(p, productId, { color, size })
|
||||||
|
|
||||||
|
//exist cart in databse
|
||||||
|
if (c.length > 0) {
|
||||||
|
Cart.updateCartByUserId(
|
||||||
|
userId,
|
||||||
|
{
|
||||||
|
items: newCart.items,
|
||||||
|
totalQty: newCart.totalQty,
|
||||||
|
totalPrice: newCart.totalPrice,
|
||||||
|
userId: userId
|
||||||
|
},
|
||||||
|
function (err, result) {
|
||||||
|
if (err) return next(err)
|
||||||
|
res.json(result)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
//no cart in database
|
||||||
|
newCart = new Cart({
|
||||||
|
items: newCart.items,
|
||||||
|
totalQty: newCart.totalQty,
|
||||||
|
totalPrice: newCart.totalPrice,
|
||||||
|
userId: userId
|
||||||
|
})
|
||||||
|
Cart.createCart(newCart, function (err, resultCart) {
|
||||||
|
if (err) return next(err)
|
||||||
|
res.status(201).json(resultCart)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
@@ -52,7 +52,10 @@ const { SendMsg } = require('../models/sendmsg');
|
|||||||
const { Permission } = require('../models/permission');
|
const { Permission } = require('../models/permission');
|
||||||
const { MsgTemplate } = require('../models/msg_template');
|
const { MsgTemplate } = require('../models/msg_template');
|
||||||
const Product = require('../models/product');
|
const Product = require('../models/product');
|
||||||
|
const Producer = require('../models/producer');
|
||||||
|
const Cart = require('../models/cart');
|
||||||
|
const Storehouse = require('../models/storehouse');
|
||||||
|
const Order = require('../models/order');
|
||||||
|
|
||||||
const tools = require('../tools/general');
|
const tools = require('../tools/general');
|
||||||
|
|
||||||
@@ -195,6 +198,14 @@ function getTableByTableName(tablename) {
|
|||||||
mytable = Operator;
|
mytable = Operator;
|
||||||
else if (tablename === 'products')
|
else if (tablename === 'products')
|
||||||
mytable = Product;
|
mytable = Product;
|
||||||
|
else if (tablename === 'storehouses')
|
||||||
|
mytable = Storehouse;
|
||||||
|
else if (tablename === 'orders')
|
||||||
|
mytable = Order;
|
||||||
|
else if (tablename === 'producers')
|
||||||
|
mytable = Producer;
|
||||||
|
else if (tablename === 'carts')
|
||||||
|
mytable = Cart;
|
||||||
else if (tablename === 'sendmsgs')
|
else if (tablename === 'sendmsgs')
|
||||||
mytable = SendMsg;
|
mytable = SendMsg;
|
||||||
else if (tablename === 'wheres')
|
else if (tablename === 'wheres')
|
||||||
@@ -1094,16 +1105,21 @@ router.get('/loadsite/:userId/:idapp/:sall', authenticate_noerror, (req, res) =>
|
|||||||
let mypage = MyPage.findAllIdApp(idapp);
|
let mypage = MyPage.findAllIdApp(idapp);
|
||||||
let calzoom = CalZoom.findAllIdApp(idapp);
|
let calzoom = CalZoom.findAllIdApp(idapp);
|
||||||
let gallery = Gallery.findAllIdApp(idapp);
|
let gallery = Gallery.findAllIdApp(idapp);
|
||||||
|
let producers = Producer.findAllIdApp(idapp);
|
||||||
|
let storehouses = Storehouse.findAllIdApp(idapp);
|
||||||
|
let cart = null;
|
||||||
if (sall) {
|
if (sall) {
|
||||||
newstosent = Newstosent.findAllIdApp(idapp);
|
newstosent = Newstosent.findAllIdApp(idapp);
|
||||||
}
|
}
|
||||||
|
|
||||||
let calcstat = null;
|
let calcstat = null;
|
||||||
if (req.user)
|
if (req.user) {
|
||||||
calcstat = User.calculateStat(idapp, req.user.username);
|
calcstat = User.calculateStat(idapp, req.user.username);
|
||||||
|
cart = Cart.getCartByUserId(req.user.id, idapp);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return Promise.all([bookedevent, eventlist, operators, wheres, contribtype, settings, permissions, disciplines, newstosent, mailinglist, mypage, gallery, paymenttype, calcstat, calzoom])
|
return Promise.all([bookedevent, eventlist, operators, wheres, contribtype, settings, permissions, disciplines, newstosent, mailinglist, mypage, gallery, paymenttype, calcstat, calzoom, producers, cart, storehouses])
|
||||||
.then((arrdata) => {
|
.then((arrdata) => {
|
||||||
// console.table(arrdata);
|
// console.table(arrdata);
|
||||||
const myuser = req.user;
|
const myuser = req.user;
|
||||||
@@ -1127,6 +1143,9 @@ router.get('/loadsite/:userId/:idapp/:sall', authenticate_noerror, (req, res) =>
|
|||||||
gallery: arrdata[11],
|
gallery: arrdata[11],
|
||||||
paymenttypes: arrdata[12],
|
paymenttypes: arrdata[12],
|
||||||
calzoom: arrdata[14],
|
calzoom: arrdata[14],
|
||||||
|
producers: arrdata[15],
|
||||||
|
cart: arrdata[16],
|
||||||
|
storehouses: arrdata[17],
|
||||||
myuser,
|
myuser,
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ var server_constants = require('../tools/server_constants');
|
|||||||
|
|
||||||
var { Project } = require('../models/project');
|
var { Project } = require('../models/project');
|
||||||
|
|
||||||
var { authenticate } = require('../middleware/authenticate');
|
var { authenticate, auth_default } = require('../middleware/authenticate');
|
||||||
|
|
||||||
var mongoose = require('mongoose');
|
var mongoose = require('mongoose');
|
||||||
const Subscription = mongoose.model('subscribers');
|
const Subscription = mongoose.model('subscribers');
|
||||||
@@ -30,8 +30,8 @@ const CartClass = require('../modules/Cart')
|
|||||||
const Cart = require('../models/cart');
|
const Cart = require('../models/cart');
|
||||||
|
|
||||||
//GET /products
|
//GET /products
|
||||||
router.get('/', async function (req, res, next) {
|
router.post('/', auth_default, async function (req, res, next) {
|
||||||
const idapp = req.query.idapp;
|
const idapp = req.body.idapp;
|
||||||
|
|
||||||
var products = await Product.findAllIdApp(idapp);
|
var products = await Product.findAllIdApp(idapp);
|
||||||
|
|
||||||
|
|||||||
@@ -799,140 +799,5 @@ router.post('/dbop', authenticate, async (req, res) => {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//GET cart
|
|
||||||
router.get('/:userId/cart', authenticate, function (req, res, next) {
|
|
||||||
let userId = req.params.userId
|
|
||||||
Cart.getCartByUserId(userId, function (err, cart) {
|
|
||||||
if (err) return next(err)
|
|
||||||
if (cart.length < 1) {
|
|
||||||
let err = new TypedError('cart error', 404, 'not_found', { message: "create a cart first" })
|
|
||||||
return next(err)
|
|
||||||
}
|
|
||||||
return res.json({ cart: cart[0] })
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
//POST cart
|
|
||||||
router.post('/:userId/cart', authenticate, function (req, res, next) {
|
|
||||||
let userId = req.params.userId
|
|
||||||
let { productId, increase, decrease } = req.body
|
|
||||||
|
|
||||||
Cart.getCartByUserId(userId, function (err, c) {
|
|
||||||
if (err) return next(err)
|
|
||||||
let oldCart = new CartClass(c[0] || { userId })
|
|
||||||
// no cart save empty cart to database then return response
|
|
||||||
if (c.length < 1 && !productId) {
|
|
||||||
return Cart.createCart(oldCart.generateModel(), function (err, resultCart) {
|
|
||||||
if (err) return next(err)
|
|
||||||
return res.status(201).json({ cart: resultCart })
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Product.findById(productId, function (e, product) {
|
|
||||||
if (e) {
|
|
||||||
e.status = 406;
|
|
||||||
return next(e);
|
|
||||||
}
|
|
||||||
if (product) {
|
|
||||||
if (decrease) {
|
|
||||||
oldCart.decreaseQty(product.id);
|
|
||||||
} else if (increase) {
|
|
||||||
oldCart.increaseQty(product.id);
|
|
||||||
} else {
|
|
||||||
oldCart.add(product, product.id);
|
|
||||||
}
|
|
||||||
let newCart = oldCart.generateModel()
|
|
||||||
Cart.updateCartByUserId(
|
|
||||||
userId,
|
|
||||||
newCart,
|
|
||||||
function (err, result) {
|
|
||||||
if (err) return next(err)
|
|
||||||
return res.status(200).json({ cart: result })
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
// apply variant
|
|
||||||
Variant.getVariantByID(productId, function (e, variant) {
|
|
||||||
if (e) {
|
|
||||||
e.status = 406;
|
|
||||||
return next(e);
|
|
||||||
}
|
|
||||||
if (variant) {
|
|
||||||
Product.getProductByID(variant.productID, function (e, p) {
|
|
||||||
let color = (variant.color) ? "- " + variant.color : "";
|
|
||||||
let size = (variant.size) ? "- " + variant.size : "";
|
|
||||||
variant.title = p.title + " " + color + size
|
|
||||||
variant.price = p.price
|
|
||||||
if (decrease) {
|
|
||||||
oldCart.decreaseQty(variant.id);
|
|
||||||
} else if (increase) {
|
|
||||||
oldCart.increaseQty(variant.id);
|
|
||||||
} else {
|
|
||||||
oldCart.add(variant, variant.id);
|
|
||||||
}
|
|
||||||
let newCart = oldCart.generateModel()
|
|
||||||
Cart.updateCartByUserId(
|
|
||||||
userId,
|
|
||||||
newCart,
|
|
||||||
function (err, result) {
|
|
||||||
if (err) return next(err)
|
|
||||||
res.status(200).json({ cart: result })
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
// no product and no variant find
|
|
||||||
else {
|
|
||||||
let err = new TypedError('/cart', 400, 'invalid_field', {
|
|
||||||
message: "invalid request body"
|
|
||||||
})
|
|
||||||
return next(err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
//PUT cart
|
|
||||||
router.put('/:userId/cart', authenticate, function (req, res, next) {
|
|
||||||
let userId = req.params.userId
|
|
||||||
let requestProduct = req.body
|
|
||||||
let { productId, color, size } = requestProduct.product
|
|
||||||
|
|
||||||
Cart.getCartByUserId(userId, function (err, c) {
|
|
||||||
if (err) return next(err)
|
|
||||||
let oldCart = new CartClass(c[0] || {})
|
|
||||||
Product.getProductByID(productId, function (err, p) {
|
|
||||||
if (err) return next(err)
|
|
||||||
let newCart = oldCart.add(p, productId, { color, size })
|
|
||||||
|
|
||||||
//exist cart in databse
|
|
||||||
if (c.length > 0) {
|
|
||||||
Cart.updateCartByUserId(
|
|
||||||
userId,
|
|
||||||
{
|
|
||||||
items: newCart.items,
|
|
||||||
totalQty: newCart.totalQty,
|
|
||||||
totalPrice: newCart.totalPrice,
|
|
||||||
userId: userId
|
|
||||||
},
|
|
||||||
function (err, result) {
|
|
||||||
if (err) return next(err)
|
|
||||||
res.json(result)
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
//no cart in database
|
|
||||||
newCart = new Cart({
|
|
||||||
items: newCart.items,
|
|
||||||
totalQty: newCart.totalQty,
|
|
||||||
totalPrice: newCart.totalPrice,
|
|
||||||
userId: userId
|
|
||||||
})
|
|
||||||
Cart.createCart(newCart, function (err, resultCart) {
|
|
||||||
if (err) return next(err)
|
|
||||||
res.status(201).json(resultCart)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ const users_router = require('./router/users_router');
|
|||||||
const site_router = require('./router/site_router');
|
const site_router = require('./router/site_router');
|
||||||
const admin_router = require('./router/admin_router');
|
const admin_router = require('./router/admin_router');
|
||||||
const products_router = require('./router/products_router');
|
const products_router = require('./router/products_router');
|
||||||
|
const cart_router = require('./router/cart_router');
|
||||||
|
|
||||||
|
|
||||||
const { ListaIngresso } = require('./models/listaingresso');
|
const { ListaIngresso } = require('./models/listaingresso');
|
||||||
@@ -130,6 +131,7 @@ app.use('/users', users_router);
|
|||||||
app.use('/site', site_router);
|
app.use('/site', site_router);
|
||||||
app.use('/admin', admin_router);
|
app.use('/admin', admin_router);
|
||||||
app.use('/products', products_router);
|
app.use('/products', products_router);
|
||||||
|
app.use('/cart', cart_router);
|
||||||
|
|
||||||
// catch 404 and forward to error handler
|
// catch 404 and forward to error handler
|
||||||
// app.use(function (req, res, next) {
|
// app.use(function (req, res, next) {
|
||||||
|
|||||||
Reference in New Issue
Block a user