All files / src/controllers/users-controllers getShoppingCart.js

60% Statements 3/5
100% Branches 0/0
100% Functions 1/1
60% Lines 3/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67              2x 2x                                                                                                         2x          
import UsersModel from "../../models/Users.js";
import { productDataTypes } from "../../CONSTANT/projections.js";
import productRatingPreparingStages from "../../utilities/productRatingPreparingStages.js";
import toObjectId from "../../utilities/toObjectId.js";
 
 
export default async function getShoppingCart(userId) {
    try {
        const [{ products }] = await UsersModel.aggregate([
            { $match: { _id: toObjectId(userId) } },
            {
                $lookup: {
                    from: "products",
                    as: "products",
                    let: {
                        productsInCart: {
                            $map: {
                                input: "$userShoppingCart",
                                as: "id",
                                in: {
                                    productId: { $substrBytes: ["$$id", 0, 24] },
                                    count: { $toDouble: { $substrBytes: ["$$id", 25, -1] } }
                                }
                            }
                        }
                    },
                    pipeline: [
                        {
                            $match: {
                                $expr: { $setIsSubset: [[{ $toString: "$_id" }], "$$productsInCart.productId"] }
                            }
                        },
                        {
                            $set: {
                                count: {
                                    $reduce: {
                                        input: "$$productsInCart",
                                        initialValue: 0,
                                        in: {
                                            $sum: [
                                                "$$value",
                                                {
                                                    $cond: {
                                                        if: { $eq: [{ $toString: "$_id" }, "$$this.productId"] },
                                                        then: "$$this.count",
                                                        else: 0,
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            }
                        },
                        { $project: productDataTypes.basic },
                        ...productRatingPreparingStages()
                    ]
                }
            },
            { $project: { products: 1 } }
        ])
        return products;
    } catch (error) {
        console.log(error);
        return null;
    }
}