361 lines
11 KiB
TypeScript
361 lines
11 KiB
TypeScript
import { Types } from "mongoose";
|
|
|
|
import { ICart } from "./models/Abstraction/ICart";
|
|
import { ICartShipmentItem } from "./models/Abstraction/ICartShipmentItem";
|
|
import { CartModel } from "./models/cart.model";
|
|
import { CartShipmentItemModel } from "./models/cartShipmentItem.model";
|
|
import { BaseRepository } from "../../common/base/repository";
|
|
|
|
class CartRepository extends BaseRepository<ICart> {
|
|
constructor() {
|
|
super(CartModel);
|
|
}
|
|
|
|
async getCartWithPopulate(userId: string) {
|
|
return this.model
|
|
.findOne({ user: userId })
|
|
.populate("user")
|
|
.populate({
|
|
path: "items.product",
|
|
})
|
|
.populate({
|
|
path: "items.variant",
|
|
populate: [{ path: "warranty" }, { path: "shop" }, { path: "themeValue" }],
|
|
});
|
|
}
|
|
|
|
// async getCartWithAggregate(userId: string) {
|
|
// return this.model.aggregate([
|
|
// {
|
|
// $match: {
|
|
// user: new Types.ObjectId(userId),
|
|
// },
|
|
// },
|
|
// {
|
|
// $lookup: {
|
|
// from: "users",
|
|
// localField: "user",
|
|
// foreignField: "_id",
|
|
// as: "user",
|
|
// },
|
|
// },
|
|
// {
|
|
// $unwind: "$user",
|
|
// },
|
|
// {
|
|
// $unwind: "$items",
|
|
// },
|
|
// {
|
|
// $lookup: {
|
|
// from: "products",
|
|
// localField: "items.product",
|
|
// foreignField: "_id",
|
|
// as: "productDetails",
|
|
// },
|
|
// },
|
|
// {
|
|
// $unwind: "$productDetails",
|
|
// },
|
|
// {
|
|
// $lookup: {
|
|
// from: "productvariants",
|
|
// localField: "items.variant",
|
|
// foreignField: "_id",
|
|
// as: "variantDetails",
|
|
// },
|
|
// },
|
|
// {
|
|
// $unwind: "$variantDetails",
|
|
// },
|
|
// {
|
|
// $lookup: {
|
|
// from: "warranties",
|
|
// localField: "variantDetails.warranty",
|
|
// foreignField: "_id",
|
|
// as: "variantDetails.warranty",
|
|
// },
|
|
// },
|
|
// {
|
|
// $lookup: {
|
|
// from: "colors",
|
|
// localField: "variantDetails.color",
|
|
// foreignField: "_id",
|
|
// as: "variantDetails.color",
|
|
// },
|
|
// },
|
|
// {
|
|
// $lookup: {
|
|
// from: "sizes",
|
|
// localField: "variantDetails.size",
|
|
// foreignField: "_id",
|
|
// as: "variantDetails.size",
|
|
// },
|
|
// },
|
|
// {
|
|
// $lookup: {
|
|
// from: "meterages",
|
|
// localField: "variantDetails.meterage",
|
|
// foreignField: "_id",
|
|
// as: "variantDetails.meterage",
|
|
// },
|
|
// },
|
|
// {
|
|
// $unwind: {
|
|
// path: "$variantDetails.warranty",
|
|
// preserveNullAndEmptyArrays: true,
|
|
// },
|
|
// },
|
|
// {
|
|
// $unwind: {
|
|
// path: "$variantDetails.color",
|
|
// preserveNullAndEmptyArrays: true,
|
|
// },
|
|
// },
|
|
// {
|
|
// $unwind: {
|
|
// path: "$variantDetails.size",
|
|
// preserveNullAndEmptyArrays: true,
|
|
// },
|
|
// },
|
|
// {
|
|
// $unwind: {
|
|
// path: "$variantDetails.meterage",
|
|
// preserveNullAndEmptyArrays: true,
|
|
// },
|
|
// },
|
|
// {
|
|
// $lookup: {
|
|
// from: "shops",
|
|
// localField: "variantDetails.shop",
|
|
// foreignField: "_id",
|
|
// as: "variantDetails.shop",
|
|
// },
|
|
// },
|
|
// {
|
|
// $unwind: "$variantDetails.shop",
|
|
// },
|
|
// {
|
|
// $lookup: {
|
|
// from: "shipments",
|
|
// localField: "variantDetails.shop.shipmentMethod",
|
|
// foreignField: "_id",
|
|
// as: "variantDetails.shipmentMethod",
|
|
// },
|
|
// },
|
|
// {
|
|
// $project: {
|
|
// productDetails: {
|
|
// adminComments: 0,
|
|
// brand: 0,
|
|
// comments: 0,
|
|
// createdAt: 0,
|
|
// updatedAt: 0,
|
|
// step: 0,
|
|
// questions: 0,
|
|
// status: 0,
|
|
// variants: 0,
|
|
// __v: 0,
|
|
// },
|
|
// variantDetails: {
|
|
// createdAt: 0,
|
|
// updatedAt: 0,
|
|
// // warranty: 0,
|
|
// __v: 0,
|
|
// },
|
|
// user: {
|
|
// createdAt: 0,
|
|
// updatedAt: 0,
|
|
// __v: 0,
|
|
// },
|
|
// },
|
|
// },
|
|
// {
|
|
// $group: {
|
|
// _id: "$_id",
|
|
// user: { $first: "$user" },
|
|
// items_count: { $sum: "$items.quantity" }, // Sum all quantities in the items array
|
|
// payable_price: {
|
|
// $sum: {
|
|
// $multiply: ["$items.quantity", "$variantDetails.price.selling_price"],
|
|
// },
|
|
// },
|
|
// retail_price: {
|
|
// $sum: {
|
|
// $multiply: ["$items.quantity", "$variantDetails.price.retailPrice"],
|
|
// },
|
|
// },
|
|
// total_discount: {
|
|
// $sum: {
|
|
// $subtract: [
|
|
// { $multiply: ["$items.quantity", "$variantDetails.price.retailPrice"] },
|
|
// { $multiply: ["$items.quantity", "$variantDetails.price.selling_price"] },
|
|
// ],
|
|
// },
|
|
// },
|
|
// coupon_discount: { $first: "$coupon_discount" },
|
|
// items: {
|
|
// $push: {
|
|
// product: "$productDetails",
|
|
// variant: "$variantDetails",
|
|
// quantity: "$items.quantity",
|
|
// },
|
|
// },
|
|
// },
|
|
// },
|
|
|
|
// {
|
|
// $project: {
|
|
// _id: 1,
|
|
// user: 1,
|
|
// items_count: 1,
|
|
// payable_price: {
|
|
// $subtract: ["$payable_price", { $ifNull: ["$coupon_discount", 0] }],
|
|
// },
|
|
// retail_price: 1,
|
|
// total_discount: 1,
|
|
// coupon_discount: 1,
|
|
// items: 1,
|
|
// },
|
|
// },
|
|
// ]);
|
|
// }
|
|
|
|
async getCartWithAggregate(userId: string) {
|
|
const data = await this.model.aggregate([
|
|
{ $match: { user: new Types.ObjectId(userId) } },
|
|
{ $lookup: { from: "users", localField: "user", foreignField: "_id", as: "user" } },
|
|
{ $unwind: "$user" },
|
|
{ $unwind: "$items" },
|
|
{ $lookup: { from: "products", localField: "items.product", foreignField: "_id", as: "productDetails" } },
|
|
{ $unwind: "$productDetails" },
|
|
{ $lookup: { from: "productvariants", localField: "items.variant", foreignField: "_id", as: "variantDetails" } },
|
|
{ $unwind: "$variantDetails" },
|
|
{ $lookup: { from: "warranties", localField: "variantDetails.warranty", foreignField: "_id", as: "variantDetails.warranty" } },
|
|
{ $lookup: { from: "themeValues", localField: "variantDetails.themeValue", foreignField: "_id", as: "variantDetails.themeValue" } },
|
|
{ $unwind: { path: "$variantDetails.warranty", preserveNullAndEmptyArrays: true } },
|
|
{ $unwind: { path: "$variantDetails.themeValue", preserveNullAndEmptyArrays: true } },
|
|
{ $lookup: { from: "shops", localField: "variantDetails.shop", foreignField: "_id", as: "variantDetails.shop" } },
|
|
{ $unwind: "$variantDetails.shop" },
|
|
{
|
|
$lookup: {
|
|
from: "shipments",
|
|
localField: "variantDetails.shop.shipmentMethod",
|
|
foreignField: "_id",
|
|
as: "variantDetails.shipmentMethod",
|
|
},
|
|
},
|
|
{
|
|
$project: {
|
|
productDetails: {
|
|
adminComments: 0,
|
|
brand: 0,
|
|
comments: 0,
|
|
createdAt: 0,
|
|
updatedAt: 0,
|
|
step: 0,
|
|
questions: 0,
|
|
status: 0,
|
|
variants: 0,
|
|
__v: 0,
|
|
},
|
|
variantDetails: { createdAt: 0, updatedAt: 0, __v: 0 },
|
|
user: { createdAt: 0, updatedAt: 0, __v: 0 },
|
|
},
|
|
},
|
|
{
|
|
$addFields: {
|
|
effectiveRetailPrice: {
|
|
$cond: {
|
|
if: "$variantDetails.isWholeSale",
|
|
then: {
|
|
$let: {
|
|
vars: {
|
|
wholesaleMatch: {
|
|
$arrayElemAt: [
|
|
{
|
|
$filter: {
|
|
input: "$variantDetails.saleFormat.wholeSale",
|
|
as: "ws",
|
|
cond: {
|
|
$and: [{ $lte: ["$$ws.from", "$items.quantity"] }, { $gte: ["$$ws.to", "$items.quantity"] }],
|
|
},
|
|
},
|
|
},
|
|
0,
|
|
],
|
|
},
|
|
},
|
|
in: { $ifNull: ["$$wholesaleMatch.price", "$variantDetails.price.retailPrice"] },
|
|
},
|
|
},
|
|
else: "$variantDetails.price.retailPrice",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
$group: {
|
|
_id: "$_id",
|
|
user: { $first: "$user" },
|
|
items_count: { $sum: "$items.quantity" },
|
|
payable_price: {
|
|
$sum: { $multiply: ["$items.quantity", "$variantDetails.price.selling_price"] },
|
|
},
|
|
retail_price: {
|
|
$sum: { $multiply: ["$items.quantity", "$effectiveRetailPrice"] },
|
|
},
|
|
total_discount: {
|
|
$sum: {
|
|
$multiply: ["$items.quantity", { $subtract: ["$variantDetails.price.retailPrice", "$variantDetails.price.selling_price"] }],
|
|
},
|
|
},
|
|
coupon_discount: { $first: "$coupon_discount" },
|
|
isWholeSale: { $first: "$variantDetails.isWholeSale" },
|
|
items: {
|
|
$push: {
|
|
product: "$productDetails",
|
|
variant: "$variantDetails",
|
|
quantity: "$items.quantity",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
$project: {
|
|
_id: 1,
|
|
user: 1,
|
|
items_count: 1,
|
|
payable_price: { $subtract: ["$payable_price", { $ifNull: ["$coupon_discount", 0] }] },
|
|
retail_price: 1,
|
|
total_discount: {
|
|
$add: ["$total_discount", { $ifNull: ["$coupon_discount", 0] }],
|
|
},
|
|
coupon_discount: 1,
|
|
items: 1,
|
|
isWholeSale: 1,
|
|
},
|
|
},
|
|
]);
|
|
return data;
|
|
}
|
|
}
|
|
|
|
function createCartRepo(): CartRepository {
|
|
return new CartRepository();
|
|
}
|
|
//=========================================>
|
|
class CartShipItemRepo extends BaseRepository<ICartShipmentItem> {
|
|
constructor() {
|
|
super(CartShipmentItemModel);
|
|
}
|
|
async getCartShipmentItems(cartId: string) {
|
|
return this.model.find({ cart: cartId });
|
|
}
|
|
}
|
|
|
|
function createCartShipItemRepo(): CartShipItemRepo {
|
|
return new CartShipItemRepo();
|
|
}
|
|
|
|
export { createCartRepo, CartRepository, CartShipItemRepo, createCartShipItemRepo };
|