2022-09-11 11:45:33 +02:00
|
|
|
const mongoose = require('mongoose').set('debug', process.env.DEBUG);
|
2020-12-21 02:16:42 +01:00
|
|
|
const Schema = mongoose.Schema;
|
|
|
|
|
|
2021-01-18 00:48:17 +01:00
|
|
|
const shared_consts = require('../tools/shared_nodejs');
|
|
|
|
|
|
2020-12-25 03:54:16 +01:00
|
|
|
const Order = require('../models/order');
|
|
|
|
|
|
2020-12-21 02:16:42 +01:00
|
|
|
const CartSchema = new Schema({
|
2020-12-25 03:54:16 +01:00
|
|
|
idapp: {
|
2022-03-03 20:32:04 +01:00
|
|
|
type: String,
|
2020-12-25 03:54:16 +01:00
|
|
|
},
|
2022-03-03 20:32:04 +01:00
|
|
|
userId: {type: Schema.Types.ObjectId, ref: 'User'},
|
|
|
|
|
totalQty: {type: Number, default: 0},
|
|
|
|
|
totalPrice: {type: Number, default: 0},
|
2021-02-03 01:33:30 +01:00
|
|
|
department: {
|
2022-03-03 20:32:04 +01:00
|
|
|
type: String, ref: 'Department',
|
2021-02-03 01:33:30 +01:00
|
|
|
},
|
2020-12-25 03:54:16 +01:00
|
|
|
items: [
|
|
|
|
|
{
|
|
|
|
|
order:
|
2022-03-03 20:32:04 +01:00
|
|
|
{type: Schema.Types.ObjectId, ref: 'Order'},
|
|
|
|
|
},
|
2020-12-25 03:54:16 +01:00
|
|
|
],
|
2021-01-18 00:48:17 +01:00
|
|
|
note: {
|
2022-03-03 20:32:04 +01:00
|
|
|
type: String,
|
2021-01-18 00:48:17 +01:00
|
|
|
},
|
2020-12-25 03:54:16 +01:00
|
|
|
modify_at: {
|
2022-03-03 20:32:04 +01:00
|
|
|
type: Date,
|
2020-12-25 03:54:16 +01:00
|
|
|
},
|
2020-12-21 02:16:42 +01:00
|
|
|
});
|
|
|
|
|
|
2020-12-25 03:54:16 +01:00
|
|
|
var Cart = module.exports = mongoose.model('Cart', CartSchema);
|
|
|
|
|
|
2022-03-03 20:32:04 +01:00
|
|
|
module.exports.findAllIdApp = async function(idapp, userId) {
|
|
|
|
|
const myfind = {idapp, userId};
|
2020-12-25 03:54:16 +01:00
|
|
|
|
2022-03-03 20:32:04 +01:00
|
|
|
return await Cart.findOne(myfind).lean();
|
2020-12-25 03:54:16 +01:00
|
|
|
};
|
|
|
|
|
|
2022-03-03 20:32:04 +01:00
|
|
|
module.exports.getCartByUserId = async function(uid, idapp) {
|
|
|
|
|
let query = {userId: uid, idapp};
|
|
|
|
|
const mycart = await Cart.findOne(query).lean();
|
2020-12-25 03:54:16 +01:00
|
|
|
|
|
|
|
|
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) {
|
2022-03-03 20:32:04 +01:00
|
|
|
mycart.items[idkey].order = myord[0];
|
2020-12-25 03:54:16 +01:00
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log('err', e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return mycart;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2020-12-21 02:16:42 +01:00
|
|
|
|
2022-03-03 20:32:04 +01:00
|
|
|
};
|
2020-12-21 02:16:42 +01:00
|
|
|
|
2022-03-03 20:32:04 +01:00
|
|
|
module.exports.updateCartByUserId = function(userId, newCart, callback) {
|
|
|
|
|
let query = {userId: userId};
|
|
|
|
|
Cart.find(query, function(err, c) {
|
|
|
|
|
if (err) throw err;
|
2020-12-21 02:16:42 +01:00
|
|
|
|
|
|
|
|
//exist cart in databse
|
|
|
|
|
if (c.length > 0) {
|
|
|
|
|
Cart.findOneAndUpdate(
|
2022-03-03 20:32:04 +01:00
|
|
|
{userId: userId},
|
|
|
|
|
{
|
|
|
|
|
$set: {
|
|
|
|
|
items: newCart.items,
|
|
|
|
|
totalQty: newCart.totalQty,
|
|
|
|
|
totalPrice: newCart.totalPrice,
|
|
|
|
|
userId: userId,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{new: true},
|
|
|
|
|
callback,
|
|
|
|
|
);
|
2020-12-21 02:16:42 +01:00
|
|
|
} else {
|
|
|
|
|
//no cart in database
|
2022-03-03 20:32:04 +01:00
|
|
|
newCart.save(callback);
|
2020-12-21 02:16:42 +01:00
|
|
|
}
|
2022-03-03 20:32:04 +01:00
|
|
|
});
|
|
|
|
|
};
|
2020-12-21 02:16:42 +01:00
|
|
|
|
2022-03-03 20:32:04 +01:00
|
|
|
module.exports.updateCartByCartId = async function(cartId, newCart) {
|
2020-12-25 03:54:16 +01:00
|
|
|
// delete newCart._doc._id;
|
2022-03-03 20:32:04 +01:00
|
|
|
const items = newCart.items;
|
2020-12-25 03:54:16 +01:00
|
|
|
const totalQty = newCart.totalQty;
|
|
|
|
|
const totalPrice = newCart.totalPrice;
|
|
|
|
|
|
2021-01-18 00:48:17 +01:00
|
|
|
const modify_at = new Date();
|
|
|
|
|
|
2022-09-11 11:45:33 +02:00
|
|
|
return await Cart.findOneAndUpdate({_id: cartId}, {
|
2021-01-18 00:48:17 +01:00
|
|
|
$set: {
|
|
|
|
|
items,
|
|
|
|
|
totalPrice,
|
|
|
|
|
totalQty,
|
2022-03-03 20:32:04 +01:00
|
|
|
modify_at,
|
|
|
|
|
},
|
|
|
|
|
}, {new: false}).lean().then((ris) => {
|
|
|
|
|
return ris;
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
console.log('err', err);
|
|
|
|
|
return null;
|
|
|
|
|
});
|
2020-12-21 02:16:42 +01:00
|
|
|
|
2022-03-03 20:32:04 +01:00
|
|
|
};
|
2021-01-18 00:48:17 +01:00
|
|
|
|
2022-03-03 20:32:04 +01:00
|
|
|
module.exports.deleteCartByCartId = async function(cartId) {
|
|
|
|
|
return await Cart.remove({_id: cartId});
|
|
|
|
|
};
|
2020-12-21 02:16:42 +01:00
|
|
|
|
2022-03-03 20:32:04 +01:00
|
|
|
module.exports.createCart = async function(newCart) {
|
|
|
|
|
return await newCart.save();
|
|
|
|
|
};
|
2020-12-21 02:16:42 +01:00
|
|
|
|