Struttura Scheda Prodotti...
This commit is contained in:
@@ -9,16 +9,16 @@ const CartSchema = new Schema({
|
|||||||
idapp: {
|
idapp: {
|
||||||
type: String,
|
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 },
|
||||||
department: {
|
department: {
|
||||||
type: String, ref: 'Department',
|
type: String, ref: 'Department',
|
||||||
},
|
},
|
||||||
items: [
|
items: [
|
||||||
{
|
{
|
||||||
order:
|
order:
|
||||||
{type: Schema.Types.ObjectId, ref: 'Order'},
|
{ type: Schema.Types.ObjectId, ref: 'Order' },
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
note: {
|
note: {
|
||||||
@@ -31,57 +31,61 @@ const CartSchema = new Schema({
|
|||||||
|
|
||||||
var Cart = module.exports = mongoose.model('Cart', CartSchema);
|
var Cart = module.exports = mongoose.model('Cart', CartSchema);
|
||||||
|
|
||||||
module.exports.findAllIdApp = async function(idapp, userId) {
|
module.exports.findAllIdApp = async function (idapp, userId) {
|
||||||
const myfind = {idapp, userId};
|
const myfind = { idapp, userId };
|
||||||
|
|
||||||
return await Cart.findOne(myfind).lean();
|
return await Cart.findOne(myfind).lean();
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.getCartByUserId = async function(uid, idapp) {
|
module.exports.getCartByUserId = async function (uid, idapp) {
|
||||||
let query = {userId: uid, idapp};
|
try {
|
||||||
const mycart = await Cart.findOne(query).lean();
|
let query = { userId: uid, idapp };
|
||||||
|
const mycart = await Cart.findOne(query).lean();
|
||||||
|
|
||||||
if (!!mycart) {
|
if (!!mycart) {
|
||||||
for (const idkey in mycart.items) {
|
for (const idkey in mycart.items) {
|
||||||
try {
|
try {
|
||||||
idorder = mycart.items[idkey]._id.toString();
|
idorder = mycart.items[idkey]._id.toString();
|
||||||
const myorder = mycart.items[idkey].order;
|
const myorder = mycart.items[idkey].order;
|
||||||
if (!!myorder) {
|
if (!!myorder) {
|
||||||
idorder = mycart.items[idkey].order._id.toString();
|
idorder = mycart.items[idkey].order._id.toString();
|
||||||
|
}
|
||||||
|
const myord = await Order.getTotalOrderById(idorder);
|
||||||
|
if (myord.length > 0) {
|
||||||
|
mycart.items[idkey].order = myord[0];
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log('err', e);
|
||||||
}
|
}
|
||||||
const myord = await Order.getTotalOrderById(idorder);
|
|
||||||
if (myord.length > 0) {
|
|
||||||
mycart.items[idkey].order = myord[0];
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.log('err', e);
|
|
||||||
}
|
}
|
||||||
|
return mycart;
|
||||||
}
|
}
|
||||||
return mycart;
|
return null;
|
||||||
|
} catch (e) {
|
||||||
|
console.log('getCartByUserId err', e);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.updateCartByUserId = function(userId, newCart, callback) {
|
module.exports.updateCartByUserId = function (userId, newCart, callback) {
|
||||||
let query = {userId: userId};
|
let query = { userId: userId };
|
||||||
Cart.find(query, function(err, c) {
|
Cart.find(query, function (err, c) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
//exist cart in databse
|
//exist cart in databse
|
||||||
if (c.length > 0) {
|
if (c.length > 0) {
|
||||||
Cart.findOneAndUpdate(
|
Cart.findOneAndUpdate(
|
||||||
{userId: userId},
|
{ userId: userId },
|
||||||
{
|
{
|
||||||
$set: {
|
$set: {
|
||||||
items: newCart.items,
|
items: newCart.items,
|
||||||
totalQty: newCart.totalQty,
|
totalQty: newCart.totalQty,
|
||||||
totalPrice: newCart.totalPrice,
|
totalPrice: newCart.totalPrice,
|
||||||
userId: userId,
|
userId: userId,
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{new: true},
|
},
|
||||||
callback,
|
{ new: true },
|
||||||
|
callback,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
//no cart in database
|
//no cart in database
|
||||||
@@ -90,7 +94,7 @@ module.exports.updateCartByUserId = function(userId, newCart, callback) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.updateCartByCartId = async function(cartId, newCart) {
|
module.exports.updateCartByCartId = async function (cartId, newCart) {
|
||||||
// delete newCart._doc._id;
|
// delete newCart._doc._id;
|
||||||
const items = newCart.items;
|
const items = newCart.items;
|
||||||
const totalQty = newCart.totalQty;
|
const totalQty = newCart.totalQty;
|
||||||
@@ -98,14 +102,14 @@ module.exports.updateCartByCartId = async function(cartId, newCart) {
|
|||||||
|
|
||||||
const modify_at = new Date();
|
const modify_at = new Date();
|
||||||
|
|
||||||
return await Cart.findOneAndUpdate({_id: cartId}, {
|
return await Cart.findOneAndUpdate({ _id: cartId }, {
|
||||||
$set: {
|
$set: {
|
||||||
items,
|
items,
|
||||||
totalPrice,
|
totalPrice,
|
||||||
totalQty,
|
totalQty,
|
||||||
modify_at,
|
modify_at,
|
||||||
},
|
},
|
||||||
}, {new: false}).lean().then((ris) => {
|
}, { new: false }).lean().then((ris) => {
|
||||||
return ris;
|
return ris;
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
console.log('err', err);
|
console.log('err', err);
|
||||||
@@ -114,11 +118,11 @@ module.exports.updateCartByCartId = async function(cartId, newCart) {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.deleteCartByCartId = async function(cartId) {
|
module.exports.deleteCartByCartId = async function (cartId) {
|
||||||
return await Cart.remove({_id: cartId});
|
return await Cart.remove({ _id: cartId });
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.createCart = async function(newCart) {
|
module.exports.createCart = async function (newCart) {
|
||||||
return await newCart.save();
|
return await newCart.save();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,9 @@ const orderSchema = new Schema({
|
|||||||
weight: {
|
weight: {
|
||||||
type: Number
|
type: Number
|
||||||
},
|
},
|
||||||
|
unit: {
|
||||||
|
type: Number
|
||||||
|
},
|
||||||
stars: {
|
stars: {
|
||||||
type: Number
|
type: Number
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -61,6 +61,9 @@ const productSchema = new Schema({
|
|||||||
weight: {
|
weight: {
|
||||||
type: Number
|
type: Number
|
||||||
},
|
},
|
||||||
|
unit: {
|
||||||
|
type: Number
|
||||||
|
},
|
||||||
quantityAvailable: {
|
quantityAvailable: {
|
||||||
type: Number
|
type: Number
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -30,17 +30,20 @@ const Cart = require('../models/cart');
|
|||||||
const OrdersCart = require('../models/orderscart');
|
const OrdersCart = require('../models/orderscart');
|
||||||
|
|
||||||
//GET cart
|
//GET cart
|
||||||
router.get('/:userId', authenticate, function (req, res, next) {
|
router.get('/:userId', authenticate, async function (req, res, next) {
|
||||||
let userId = req.body.userId
|
let userId = req.params.userId
|
||||||
let idapp = req.body.idapp
|
let idapp = req.user.idapp
|
||||||
Cart.getCartByUserId(userId, idapp, function (err, cart) {
|
return await Cart.getCartByUserId(userId, idapp)
|
||||||
if (err) return next(err)
|
.then((cart) => {
|
||||||
|
if (cart)
|
||||||
|
return res.send({ code: server_constants.RIS_CODE_OK, cart });
|
||||||
|
else
|
||||||
|
return res.status(400).send(e);
|
||||||
|
}).catch((err) => {
|
||||||
|
console.error('Err', err);
|
||||||
|
return res.send({ code: server_constants.RIS_CODE_ERR, cart: null });
|
||||||
|
});
|
||||||
|
|
||||||
if (cart)
|
|
||||||
res.send({ code: server_constants.RIS_CODE_OK, cart });
|
|
||||||
else
|
|
||||||
res.status(400).send(e);
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
//POST cart
|
//POST cart
|
||||||
@@ -51,46 +54,54 @@ router.post('/:userId', authenticate, async function (req, res, next) {
|
|||||||
let subqty = req.body.subqty;
|
let subqty = req.body.subqty;
|
||||||
let order = req.body.order;
|
let order = req.body.order;
|
||||||
|
|
||||||
const mycart = await Cart.getCartByUserId(userId, idapp);
|
try {
|
||||||
|
|
||||||
// const myorder = Order.getOrderByID(order._id);
|
const mycart = await Cart.getCartByUserId(userId, idapp);
|
||||||
if (!addqty && !subqty)
|
|
||||||
order._id = await Order.createOrder(order);
|
|
||||||
|
|
||||||
let cart = null;
|
// const myorder = Order.getOrderByID(order._id);
|
||||||
// no cart save empty cart to database then return response
|
if (!addqty && !subqty)
|
||||||
let myqty = 0;
|
order._id = await Order.createOrder(order);
|
||||||
if (!mycart) {
|
|
||||||
let oldCart = new CartClass(order)
|
let cart = null;
|
||||||
cart = await Cart.createCart(oldCart.generateModel());
|
// no cart save empty cart to database then return response
|
||||||
} else {
|
let myqty = 0;
|
||||||
let newCart = CartClass.constructByCart(mycart);
|
if (!mycart) {
|
||||||
if (addqty) {
|
let oldCart = new CartClass(order)
|
||||||
myqty = await newCart.addqty(order);
|
cart = await Cart.createCart(oldCart.generateModel());
|
||||||
} else if (subqty) {
|
|
||||||
myqty = await newCart.subqty(order);
|
|
||||||
} else {
|
} else {
|
||||||
const ind = newCart.addItem(order);
|
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());
|
||||||
}
|
}
|
||||||
cart = await Cart.updateCartByCartId(mycart._id, newCart.generateModel());
|
if (cart) {
|
||||||
}
|
const carttot = await Cart.getCartByUserId(userId, idapp);
|
||||||
if (cart) {
|
return res.send({ code: server_constants.RIS_CODE_OK, cart: carttot, qty: myqty });
|
||||||
const carttot = await Cart.getCartByUserId(userId, idapp);
|
} else {
|
||||||
return res.send({ code: server_constants.RIS_CODE_OK, cart: carttot, qty: myqty });
|
return res.send({ code: server_constants.RIS_CODE_ERR, cart: null });
|
||||||
} 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 })
|
||||||
|
})
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
return res.send({ code: server_constants.RIS_CODE_ERR, cart: 0 });
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
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) {
|
router.delete('/:userId', authenticate, async function (req, res) {
|
||||||
@@ -122,47 +133,56 @@ router.delete('/:userId', authenticate, async function (req, res) {
|
|||||||
|
|
||||||
|
|
||||||
//PUT cart
|
//PUT cart
|
||||||
router.put('/:userId', authenticate, function (req, res, next) {
|
router.put('/:userId', authenticate, async function (req, res, next) {
|
||||||
let userId = req.params.userId
|
let userId = req.params.userId
|
||||||
let requestProduct = req.body
|
let requestProduct = req.body
|
||||||
let { productId, color, size } = requestProduct.product
|
let { productId, color, size } = requestProduct.product
|
||||||
|
|
||||||
Cart.getCartByUserId(userId, function (err, c) {
|
try {
|
||||||
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
|
await Cart.getCartByUserId(userId, function (err, c) {
|
||||||
if (c.length > 0) {
|
if (err) return next(err)
|
||||||
Cart.updateCartByUserId(
|
let oldCart = new CartClass(c[0] || {})
|
||||||
userId,
|
|
||||||
{
|
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,
|
items: newCart.items,
|
||||||
totalQty: newCart.totalQty,
|
totalQty: newCart.totalQty,
|
||||||
totalPrice: newCart.totalPrice,
|
totalPrice: newCart.totalPrice,
|
||||||
userId: userId
|
userId: userId
|
||||||
},
|
|
||||||
function (err, result) {
|
|
||||||
if (err) return next(err)
|
|
||||||
res.json(result)
|
|
||||||
})
|
})
|
||||||
} else {
|
Cart.createCart(newCart, function (err, resultCart) {
|
||||||
//no cart in database
|
if (err) return next(err)
|
||||||
newCart = new Cart({
|
res.status(201).json(resultCart)
|
||||||
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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
return res.send({ code: server_constants.RIS_CODE_OK });
|
||||||
|
} catch (e) {
|
||||||
|
return res.send({ code: server_constants.RIS_CODE_ERR, status: 0 });
|
||||||
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
//POST cart
|
//POST cart
|
||||||
|
|||||||
@@ -1573,6 +1573,8 @@ function load(req, res, version) {
|
|||||||
myelems: arrdata[38],
|
myelems: arrdata[38],
|
||||||
categories: arrdata[39],
|
categories: arrdata[39],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const prova = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
|
|||||||
@@ -326,6 +326,38 @@ module.exports = {
|
|||||||
OPZ1_2: 2,
|
OPZ1_2: 2,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
UNITS_OF_MEASURE: {
|
||||||
|
NESSUNO: 0,
|
||||||
|
GRAMMI: 1,
|
||||||
|
CHILI: 2,
|
||||||
|
LITRI: 3,
|
||||||
|
PEZZI: 4,
|
||||||
|
},
|
||||||
|
|
||||||
|
Units_Of_Measure_ListBox: [
|
||||||
|
{
|
||||||
|
label: '[Nessuno]',
|
||||||
|
value: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Grammi (g)',
|
||||||
|
value: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Chili (kg)',
|
||||||
|
value: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Litri (l)',
|
||||||
|
value: 3,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Pezzi (p)',
|
||||||
|
value: 4,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
|
||||||
|
|
||||||
CallFunz: {
|
CallFunz: {
|
||||||
SOSTITUISCI: 345,
|
SOSTITUISCI: 345,
|
||||||
AGGIUNGI_NUOVO_IMBARCO: 380,
|
AGGIUNGI_NUOVO_IMBARCO: 380,
|
||||||
|
|||||||
Reference in New Issue
Block a user