All files / src/models YearlyStatistics.js

100% Statements 7/7
100% Branches 2/2
100% Functions 2/2
100% Lines 7/7

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        77x                 77x                                         924x                         77x     77x 77x 6x            
import { model, Schema } from "mongoose";
import { MONTHES } from "../CONSTANT/MONTHES.js";
import { ANumber, RequiredNumber, RequiredString } from "../utilities/schemaTypesOptions.js";
 
const MonthStatistics = new Schema({
    month: RequiredString(),
    totalEarnings: ANumber(),
    productsSold: ANumber(),
    totalOrders: ANumber(),
    earningsTarget: ANumber(),
    _id: false
})
 
const YearlyStatisticsSchema = new Schema(
    {
        year: RequiredNumber({ unique: true, min: 2023 }),
        categories: {
            type: [
                {
                    category: String,
                    monthlyStatistics: [{
                        month: RequiredString(),
                        totalEarnings: ANumber(),
                        productsSold: ANumber(),
                        _id: false
                    }],
                    _id: false
                }
            ],
            default: []
        },
        monthes: {
            type: [MonthStatistics],
            default: MONTHES.map((month) => {
                return {
                    month,
                    totalEarnings: 0,
                    productsSold: 0,
                    totalOrders: 0,
                    earningsTarget: 0
                }
            })
        }
    },
    { versionKey: false }
)
 
const YearlyStatisticsModel = model("yearly-statistics", YearlyStatisticsSchema);
 
export async function initializeYearlyStatisticsCollection() {
    try {
        if (!(await YearlyStatisticsModel.count())) {
            await new YearlyStatisticsModel({ year: new Date().getFullYear() }).save()
        }
    } catch { }
}
 
export default YearlyStatisticsModel;