All files / src/controllers/system-controller loggedUser.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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92            2x 2x                                                                                                                                                         2x              
import { productDataTypes } from "../../CONSTANT/projections.js";
import UsersModel from "../../models/Users.js";
import productRatingPreparingStages from "../../utilities/productRatingPreparingStages.js";
import toObjectId from "../../utilities/toObjectId.js";
 
export default async function loggedUser(userId) {
    try {
        const [initialUserData] = await UsersModel.aggregate([
            { $match: { _id: toObjectId(userId) } },
            {
                $project: {
                    userData: {
                        _id: "$_id",
                        userName: "$userName",
                        userEmail: "$userEmail",
                        avatar: "$avatar",
                        hisEmailVerified: "$hisEmailVerified"
                    },
                    favorites: "$userFavorites",
                    userShoppingCart: 1
                }
            },
            {
                $lookup: {
                    from: "products",
                    let: { userShoppingCart: "$userShoppingCart" },
                    as: "shoppingCart",
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $gt: [
                                        {
                                            $reduce: {
                                                input: "$$userShoppingCart",
                                                initialValue: 0,
                                                in: {
                                                    $sum: [
                                                        {
                                                            $cond: {
                                                                if: { $eq: [{ $substrBytes: ["$$this", 0, 24] }, { $toString: "$_id" }] },
                                                                then: 1,
                                                                else: 0
                                                            }
                                                        },
                                                        "$$value"
                                                    ]
                                                }
                                            }
                                        },
                                        0
                                    ]
                                }
                            }
                        },
                        { $project: productDataTypes.basic },
                        {
                            $set: {
                                count: {
                                    $reduce: {
                                        input: "$$userShoppingCart",
                                        initialValue: 0,
                                        in: {
                                            $sum: [
                                                {
                                                    $cond: {
                                                        if: { $eq: [{ $substrBytes: ["$$this", 0, 24] }, { $toString: "$_id" }] },
                                                        then: { $toDouble: { $substrBytes: ["$$this", 25, -1] } },
                                                        else: 0
                                                    }
                                                },
                                                "$$value"
                                            ]
                                        }
                                    }
                                },
                            }
                        },
                        ...productRatingPreparingStages()
                    ]
                }
            },
            { $project: { userShoppingCart: 0, _id: 0 } }
        ]);
        return initialUserData;
    } catch (error) {
        console.log(error)
        return null;
    }
}