All files / src/controllers/orders-controllers getOrders.js

85.71% Statements 18/21
76.19% Branches 16/21
100% Functions 1/1
90% Lines 18/20

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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127                4x               4x   4x 4x 4x 4x   4x 4x 4x   4x         4x 2x           4x                                                                                                                                                 4x 4x 4x 2x         2x            
import { productDataTypes } from "../../CONSTANT/projections.js";
import createProjection from "../../utilities/createProjection.js";
import OrdersModel from "../../models/Orders.js";
import productRatingPreparingStages from "../../utilities/productRatingPreparingStages.js";
import toObjectId from "../../utilities/toObjectId.js";
 
export default async function getOrders({ orderId, userId }, query = {}) {
 
    Iif (!userId && !orderId) return null
 
    const {
        productsReturnType,
        ordersReturnType,
        state,
        sliceNumber,
        sliceSize
    } = query;
 
    const thereIsPaginationOptions = !!(sliceSize && sliceNumber);
    const productsProjection = productDataTypes[productsReturnType] || productDataTypes.basic
    const ordersProjection = ordersReturnType ? createProjection(ordersReturnType) : { __v: 0 };
    const filter = {};
 
    orderId && Object.assign(filter, { _id: toObjectId(orderId) });
    userId && Object.assign(filter, { userId: toObjectId(userId) });
    state && Object.assign(filter, { state });
 
    const pipeline = [
        { $match: filter },
        { $sort: { createdAt: -1 } }
    ];
 
    if (thereIsPaginationOptions) {
        pipeline.push(
            { $skip: ((+sliceNumber) - 1) * +sliceSize },
            { $limit: +sliceSize + 1 }
        )
    }
 
    pipeline.push(
        {
            $lookup: {
                from: "products",
                as: "products",
                let: { products: "$products" },
                pipeline: [
                    {
                        $match: {
                            $expr: {
                                $gt: [
                                    {
                                        $reduce: {
                                            input: "$$products",
                                            initialValue: 0,
                                            in: {
                                                $sum: [
                                                    {
                                                        $cond: {
                                                            if: { $eq: [{ $substrBytes: ["$$this", 0, 24] }, { $toString: "$_id" }] },
                                                            then: 1,
                                                            else: 0
                                                        }
                                                    },
                                                    "$$value"
                                                ]
                                            }
                                        }
                                    },
                                    0
                                ]
                            }
                        }
                    },
                    { $project: productsProjection },
                    {
                        $addFields: {
                            count: {
                                $reduce: {
                                    input: "$$products",
                                    initialValue: 0,
                                    in: {
                                        $cond: {
                                            if: {
                                                $eq: [
                                                    { $substr: ["$$this", 0, 24] },
                                                    { $toString: "$_id" }
                                                ]
                                            },
                                            then: {
                                                $sum: [
                                                    "$$value",
                                                    {
                                                        $toDouble: {
                                                            $arrayElemAt: [{ $split: ["$$this", "-"] }, 1]
                                                        }
                                                    }
                                                ]
                                            },
                                            else: { $sum: ["$$value", 0] }
                                        }
                                    }
                                }
                            }
                        }
                    },
                    ...productRatingPreparingStages()
                ]
            }
        },
        { $project: ordersProjection }
    )
 
    try {
        const orders = await OrdersModel.aggregate(pipeline)
        if (thereIsPaginationOptions) {
            return {
                thereIsMore: !!orders[sliceSize],
                orders: orders.slice(0, sliceSize)
            }
        }
        else return orders
    } catch (err) {
        console.log(err)
        return null;
    }
}