All files / src/controllers/products-controllers getProductRating.js

84.61% Statements 11/13
83.33% Branches 5/6
100% Functions 3/3
84.61% Lines 11/13

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        4x 4x 4x   4x                                                             4x 3x   1x                                 77x 24x                     77x 20x                            
import ProductsModel from "../../models/Products.js";
import toObjectId from "../../utilities/toObjectId.js";
 
export default async function getProductRating(productId, userId) {
    try {
        productId = toObjectId(productId);
        userId = userId?.length === 24 ? toObjectId(userId) : userId;
 
        const [result] = await ProductsModel.aggregate([
            { $match: { _id: productId } },
            { $unwind: "$ratings" },
            { $replaceRoot: { newRoot: "$ratings" } },
            {
                $group: {
                    _id: "product ratings",
                    reviews: { $count: {} },
                    usersRatings: { $sum: "$rating" },
                    oneStar: mongoQueryCondition("$rating", 1),
                    twoStars: mongoQueryCondition("$rating", 2),
                    threeStars: mongoQueryCondition("$rating", 3),
                    fuorStars: mongoQueryCondition("$rating", 4),
                    fiveStars: mongoQueryCondition("$rating", 5),
                    currentUserRating: mongoQueryCondition("$raterId", userId, "$rating")
                }
            },
            {
                $project: {
                    ratingAverage: { $trunc: [{ $divide: ["$usersRatings", "$reviews"] }, 1] },
                    oneStar: mongoQueryPercentage("$oneStar"),
                    twoStars: mongoQueryPercentage("$twoStars"),
                    threeStars: mongoQueryPercentage("$threeStars"),
                    fuorStars: mongoQueryPercentage("$fuorStars"),
                    fiveStars: mongoQueryPercentage("$fiveStars"),
                    currentUserRating: true,
                    reviews: true,
                    _id: false
                }
            }
        ])
        if (result) {
            return result;
        } else {
            return {
                reviews: 0,
                oneStar: { count: 0, percentage: 0 },
                twoStars: { count: 0, percentage: 0 },
                threeStars: { count: 0, percentage: 0 },
                fuorStars: { count: 0, percentage: 0 },
                fiveStars: { count: 0, percentage: 0 },
                currentUserRating: 0,
                ratingAverage: 0
            }
        }
    } catch (error) {
        console.log(error)
        return null
    }
}
 
const mongoQueryCondition = (field, value, customSum) => {
    return {
        $sum: {
            $cond: {
                if: { $eq: [field, value] },
                then: customSum || 1,
                else: 0,
            }
        }
    }
}
 
const mongoQueryPercentage = (value) => {
    return {
        count: value,
        percentage: {
            $trunc: [
                {
                    $multiply: [
                        { $divide: [value, "$reviews"] },
                        100
                    ]
                },
                1
            ]
        }
    }
}