All files / src/controllers/products-controllers didUserBuyTheProduct.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                            3x 3x                                           3x            
import OrdersModel from "../../models/Orders.js";
import toObjectId from "../../utilities/toObjectId.js";
 
/**
* Checks if the user has bought the product brfore
* This done by looking at all user's orders if the given `productId` exist at least in one of his orders.
* 
* @param { string } productId - Id of the product to check if the user has bought it brfore.
* @param userId - Id of the user to check if he has bought the product brfore.
*
* @return { Promise<boolean | null> } true if the user has bought the product brfore, false if he didn't,
* `null` if an unexpected error occurred.
*/
export default async function didUserBuyTheProduct(productId, userId) {
    try {
        const result = await OrdersModel.aggregate([
            { $match: { userId: toObjectId(userId), state: "Completed" } },
            {
                $match: {
                    $expr: {
                        $reduce: {
                            input: "$products",
                            initialValue: 0,
                            in: {
                                $cond: {
                                    if: { $eq: [{ $substrBytes: ["$$this", 0, 24] }, productId] },
                                    then: { $add: ["$$value", 1] },
                                    else: { $add: ["$$value", 0] }
                                }
                            }
                        }
                    }
                }
            },
            { $limit: 1 },
            { $project: { _id: 1 } }
        ])
        return !!result.length;
    } catch (err) {
        console.log(err)
        return null;
    }
}