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

81.25% Statements 13/16
75% Branches 3/4
100% Functions 1/1
81.25% Lines 13/16

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        2x 2x   2x 2x   2x 2x   2x 2x   2x                                                             2x 2x 2x 2x                                        
import { MONTHES } from "../../CONSTANT/MONTHES.js";
import YearlyStatisticsModel from "../../models/YearlyStatistics.js";
 
export default async function salesGrowth() {
    try {
        const currentMonthIndex = new Date().getMonth();
 
        const lastMonth = new Date(new Date().setMonth(currentMonthIndex - 1));
        const beforeLastMonth = new Date(new Date().setMonth(currentMonthIndex - 2));
 
        const lastMonthYear = lastMonth.getFullYear();
        const beforeLastMonthYear = beforeLastMonth.getFullYear();
 
        const lastMonthIndex = lastMonth.getMonth();
        const beforeLastMonthIndex = beforeLastMonth.getMonth();
 
        const [salesGrowthData] = await YearlyStatisticsModel.aggregate([
            { $match: { $or: [{ year: lastMonthYear }, { year: beforeLastMonthYear }] } },
            { $project: { monthes: 1, year: 1 } },
            {
                $project: {
                    lastMonthEarnings: {
                        $cond: {
                            if: { $eq: ["$year", lastMonthYear] },
                            then: { $arrayElemAt: ["$monthes", lastMonthIndex] },
                            else: {}
                        }
                    },
                    beforeLastMonthEarnings: {
                        $cond: {
                            if: { $eq: ["$year", beforeLastMonthYear] },
                            then: { $arrayElemAt: ["$monthes", beforeLastMonthIndex] },
                            else: {}
                        }
                    },
                    _id: false
                }
            },
            {
                $group: {
                    _id: "monthes earnings",
                    lastMonthEarnings: { $sum: "$lastMonthEarnings.totalEarnings" },
                    beforeLastMonthEarnings: { $sum: "$beforeLastMonthEarnings.totalEarnings" }
                }
            }
        ]);
 
        if (salesGrowthData) {
            const { lastMonthEarnings, beforeLastMonthEarnings } = salesGrowthData
            const growthRate = (lastMonthEarnings - beforeLastMonthEarnings) / beforeLastMonthEarnings * 100
            return {
                lastMonth: {
                    year: lastMonthYear,
                    month: MONTHES[lastMonthIndex],
                    earnings: lastMonthEarnings
                },
                beforeLastMonth: {
                    year: beforeLastMonthYear,
                    month: MONTHES[beforeLastMonthIndex],
                    earnings: beforeLastMonthEarnings
                },
                growthRate: +(growthRate.toFixed(2)) || 0
            }
        } else E{
            return null
        }
    } catch (error) {
        console.log(error)
        return;
    }
}