Struttura Scheda Prodotti...

This commit is contained in:
Surya Paolo
2023-11-30 14:27:37 +01:00
parent e895e7dae2
commit e3ed2934ee
6 changed files with 181 additions and 117 deletions

View File

@@ -9,16 +9,16 @@ const CartSchema = new Schema({
idapp: {
type: String,
},
userId: {type: Schema.Types.ObjectId, ref: 'User'},
totalQty: {type: Number, default: 0},
totalPrice: {type: Number, default: 0},
userId: { type: Schema.Types.ObjectId, ref: 'User' },
totalQty: { type: Number, default: 0 },
totalPrice: { type: Number, default: 0 },
department: {
type: String, ref: 'Department',
},
items: [
{
order:
{type: Schema.Types.ObjectId, ref: 'Order'},
{ type: Schema.Types.ObjectId, ref: 'Order' },
},
],
note: {
@@ -31,57 +31,61 @@ const CartSchema = new Schema({
var Cart = module.exports = mongoose.model('Cart', CartSchema);
module.exports.findAllIdApp = async function(idapp, userId) {
const myfind = {idapp, userId};
module.exports.findAllIdApp = async function (idapp, userId) {
const myfind = { idapp, userId };
return await Cart.findOne(myfind).lean();
};
module.exports.getCartByUserId = async function(uid, idapp) {
let query = {userId: uid, idapp};
const mycart = await Cart.findOne(query).lean();
module.exports.getCartByUserId = async function (uid, idapp) {
try {
let query = { userId: uid, idapp };
const mycart = await Cart.findOne(query).lean();
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();
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].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) {
let query = {userId: userId};
Cart.find(query, function(err, c) {
module.exports.updateCartByUserId = function (userId, newCart, callback) {
let query = { userId: userId };
Cart.find(query, function (err, c) {
if (err) throw err;
//exist cart in databse
if (c.length > 0) {
Cart.findOneAndUpdate(
{userId: userId},
{
$set: {
items: newCart.items,
totalQty: newCart.totalQty,
totalPrice: newCart.totalPrice,
userId: userId,
},
{ userId: userId },
{
$set: {
items: newCart.items,
totalQty: newCart.totalQty,
totalPrice: newCart.totalPrice,
userId: userId,
},
{new: true},
callback,
},
{ new: true },
callback,
);
} else {
//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;
const items = newCart.items;
const totalQty = newCart.totalQty;
@@ -98,14 +102,14 @@ module.exports.updateCartByCartId = async function(cartId, newCart) {
const modify_at = new Date();
return await Cart.findOneAndUpdate({_id: cartId}, {
return await Cart.findOneAndUpdate({ _id: cartId }, {
$set: {
items,
totalPrice,
totalQty,
modify_at,
},
}, {new: false}).lean().then((ris) => {
}, { new: false }).lean().then((ris) => {
return ris;
}).catch(err => {
console.log('err', err);
@@ -114,11 +118,11 @@ module.exports.updateCartByCartId = async function(cartId, newCart) {
};
module.exports.deleteCartByCartId = async function(cartId) {
return await Cart.remove({_id: cartId});
module.exports.deleteCartByCartId = async function (cartId) {
return await Cart.remove({ _id: cartId });
};
module.exports.createCart = async function(newCart) {
module.exports.createCart = async function (newCart) {
return await newCart.save();
};

View File

@@ -51,6 +51,9 @@ const orderSchema = new Schema({
weight: {
type: Number
},
unit: {
type: Number
},
stars: {
type: Number
},

View File

@@ -61,6 +61,9 @@ const productSchema = new Schema({
weight: {
type: Number
},
unit: {
type: Number
},
quantityAvailable: {
type: Number
},