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

87.5% Statements 14/16
100% Branches 4/4
100% Functions 3/3
86.66% Lines 13/15

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        2x 2x 2x 2x 2x 2x 1x     12x 1x           11x                 1x         1x           2x          
import { MONTHES } from "../../CONSTANT/MONTHES.js";
import { getCurrentDate } from "../../utilities/dateMaker.js";
 
export default function registerCategoriesStatistics(categories, currentYearStatistics) {
    try {
        const { monthIndex } = getCurrentDate();
        for (const cat in categories) {
            const { productsCount, earnings } = categories[cat]
            const catIndex = currentYearStatistics.categories.findIndex(({ category }) => category === cat)
            if (catIndex === -1) {
                currentYearStatistics.categories.push({
                    category: cat,
                    monthlyStatistics: MONTHES.map((month, index) => {
                        if (index === monthIndex) {
                            return {
                                month,
                                totalEarnings: productsCount,
                                productsSold: earnings
                            }
                        } else {
                            return {
                                month,
                                totalEarnings: 0,
                                productsSold: 0
                            }
                        }
                    })
                })
            } else {
                currentYearStatistics
                    .categories[catIndex]
                    .monthlyStatistics[monthIndex]
                    .productsSold += productsCount
 
                currentYearStatistics
                    .categories[catIndex]
                    .monthlyStatistics[monthIndex]
                    .totalEarnings += earnings
            }
        }
        return true
    } catch (error) {
        console.log(error)
        return false
    }
}