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

71.42% Statements 10/14
50% Branches 1/2
100% Functions 1/1
69.23% Lines 9/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            2x 2x   2x 2x 2x   2x 2x           2x     2x          
import ProductsModel from "../../models/Products.js";
import idParser from "../../utilities/idParser.js";
import registerCategoriesStatistics from "./registerCategoriesStatistics.js";
 
 
export default async function registerProductsStatistics(products, currentYearStatistics, session) {
    try {
        let categories = {};
 
        for (let i = 0; i < products.length; i++) {
            const { count, category, price, id: _id } = idParser(products[i]);
            const earnings = price * count;
 
            if (!categories[category]) {
                categories[category] = { earnings, productsCount: count };
            } else E{
                categories[category].earnings += earnings;
                categories[category].productsCount += count;
            }
 
            await ProductsModel.updateOne({ _id }, { $inc: { amount: -count, sold: count, earnings } }, { session });
        }
 
        return registerCategoriesStatistics(categories, currentYearStatistics);
    } catch (error) {
        console.log(error)
        return false
    }
}