All files / src/controllers/statistics-controllers topProducts.js

66.66% Statements 4/6
0% Branches 0/1
100% Functions 1/1
66.66% Lines 4/6

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        2x 2x 2x                           2x          
import { productDataTypes } from "../../CONSTANT/projections.js";
import ProductsModel from "../../models/Products.js";
 
export default async function topProducts(limit = 5) {
    const projection = productDataTypes.summary;
    try {
        const [products] = await ProductsModel.aggregate([{
            $facet: {
                topSales: [
                    { $sort: { sold: -1 } },
                    { $limit: +limit },
                    { $project: { ...projection, "sold": 1 } }
                ],
                topEarnings: [
                    { $sort: { earnings: -1 } },
                    { $limit: +limit },
                    { $project: { ...projection, "earnings": 1 } }
                ]
            }
        }])
        return products
    } catch (error) {
        console.log(error)
        return null
    }
}