All files / src/controllers/products-controllers search.js

61.53% Statements 8/13
80% Branches 4/5
100% Functions 1/1
61.53% Lines 8/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 30 31 32      12x   12x 12x 12x 12x   12x           12x               12x              
import ProductsModel from "../../models/Products.js";
 
export default async function search({ queries, projection }) {
    try {
        const
            { category, title, series, limit = 8, page } = queries,
            matchAll = new RegExp(/.*/, "i"),
            titleReg = new RegExp(title, "i"),
            seriesReg = new RegExp(series, "i");
 
        const filter = {
            category: category ?? matchAll,
            title: titleReg,
            series: seriesReg
        }
 
        Iif (!isNaN(+page)) {
            const skip = (+page - 1) * +limit;
            const products = await ProductsModel.find(filter, projection, { limit: +limit + 1, skip });
            return {
                products: products.slice(0, +limit),
                thereIsMore: !!products[limit]
            }
        } else {
            return await ProductsModel.find(filter, projection, { limit });
        }
    } catch (error) {
        console.log(error)
        return null;
    }
}