Carrello Spesa
This commit is contained in:
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 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);
|
||||
|
||||
Reference in New Issue
Block a user