Files
freeplanet_serverside/src/server/models/cart.js

125 lines
2.8 KiB
JavaScript
Raw Normal View History

const mongoose = require('mongoose').set('debug', false);
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: {
type: String,
2020-12-25 03:54:16 +01:00
},
userId: {type: Schema.Types.ObjectId, ref: 'User'},
totalQty: {type: Number, default: 0},
totalPrice: {type: Number, default: 0},
department: {
type: String, ref: 'Department',
},
2020-12-25 03:54:16 +01:00
items: [
{
order:
{type: Schema.Types.ObjectId, ref: 'Order'},
},
2020-12-25 03:54:16 +01:00
],
2021-01-18 00:48:17 +01:00
note: {
type: String,
2021-01-18 00:48:17 +01:00
},
2020-12-25 03:54:16 +01:00
modify_at: {
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);
module.exports.findAllIdApp = async function(idapp, userId) {
const myfind = {idapp, userId};
2020-12-25 03:54:16 +01:00
return await Cart.findOne(myfind).lean();
2020-12-25 03:54:16 +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) {
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
};
2020-12-21 02:16:42 +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(
{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
newCart.save(callback);
2020-12-21 02:16:42 +01:00
}
});
};
2020-12-21 02:16:42 +01:00
module.exports.updateCartByCartId = async function(cartId, newCart) {
2020-12-25 03:54:16 +01:00
// delete newCart._doc._id;
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();
return Cart.findOneAndUpdate({_id: cartId}, {
2021-01-18 00:48:17 +01:00
$set: {
items,
totalPrice,
totalQty,
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
};
2021-01-18 00:48:17 +01:00
module.exports.deleteCartByCartId = async function(cartId) {
return await Cart.remove({_id: cartId});
};
2020-12-21 02:16:42 +01:00
module.exports.createCart = async function(newCart) {
return await newCart.save();
};
2020-12-21 02:16:42 +01:00