Your IP : 216.73.216.91


Current Path : /var/node/inatote/Inatote-Backend/search/
Upload File :
Current File : /var/node/inatote/Inatote-Backend/search/search-list.js

// import makeproduct from './product.js'
import { UniqueConstraintError } from '../helpers/errors.js'

export default function makeSearchList({ database }) {
    return Object.freeze({
        // add,
        // findByEmail,
        // findById,
        getItems,
        // remove,
        // replace,
        // update
    })

    async function getItems( name ) {
        try{
            const db = await database
            let result = [];
            let vendor_ids = [] 
            
        let vendors = await db.collection("vendors").find({"vendorName" : {$regex : name ,  $options: 'i'}}).toArray();
        let category = await db.collection("categories").find({"products.productName" : {$regex : name ,  $options: 'i'}}).toArray();
        category.forEach(element => {
            vendor_ids=[...vendor_ids , element.vendorID]
        });
        let vendors_byids = await db.collection("vendors").find({"_id" : {$in : vendor_ids }}).toArray();
        result = [...vendors ,...vendors_byids ]
        return result
        }catch(err){
            //console.log("err", err);
        }
        
    }

    // async function add({ productId, ...product }) {
    //     const db = await database

    //     product.productId = db.makeId(productId)
    //     product.createdDate = new Date()
    //     product.lastUpdatedDate = new Date()

    //     const { result, ops } = await db.collection('categories').updateOne(
    //         {
    //             _id: db.makeId(product.categoryId)
    //         },
    //         {
    //             $push: {
    //                 products: product
    //             }
    //         }
    //     );
    //     //console.log(result)
    //     //console.log(ops)

    //     // const { result, ops } = await db
    //     //     .collection('products')
    //     //     .insertOne(product)
    //     //     .catch(mongoError => {
    //     //         const [errorCode] = mongoError.message.split(' ')
    //     //         if (errorCode === 'E11000') {
    //     //             const [_, mongoIndex] = mongoError.message.split(':')[2].split(' ')
    //     //             throw new UniqueConstraintError(
    //     //                 mongoIndex === 'productEmailIndex' ? 'emailAddress' : 'productId'
    //     //             )
    //     //         }
    //     //         throw mongoError
    //     //     })
    //     return {
    //         success: result.ok === 1,
    //         created: product
    //     }
    // }

    // async function findById({ productId }) {
    //     const db = await database
    //     //console.log(productId)
    //     const result = await db
    //         .collection('categories')
    //         .aggregate([{ $match: { "products.productId": db.makeId(productId) } }, { $project: { products: { $filter: { input: "$products", as: "products", cond: { $eq: ["$$products.productId", db.makeId(productId)] } } } } }])

    //     if (result) {
    //         var res = await result.toArray();
    //         if (res.length < 1) {
    //             return
    //         }
    //         var product = {
    //             categoryId: res[0]._id,
    //             ...res[0].products[0]
    //         }
    //         return documentToproduct(product)
    //     }
    //     return
    // }

    // async function findByEmail({ emailAddress }) {
    //     return {
    //         emailAddress: emailAddress
    //     }
    // }

    // async function remove({ productId, ...product }) {
    //     return {
    //         productId: productId
    //     }
    // }

    // // todo:
    // async function replace(product) {

    // }

    // // todo:
    // async function update({productId, categoryId, product }) {
    //     const db = await database
    //     var q = {};
    //     product.lastUpdatedDate = new Date();

    //     for (var item in product) {
    //             //console.log(item)
    //         q[`products.$.${item}`] = product[item]
    //     }

    //     const query = {
    //         $set: q
    //     }

    //     const {result} = await db.collection('categories').updateOne({
    //         _id: db.makeId(categoryId),
    //         "products.productId": db.makeId(productId)
    //     }, query, {strict: false})
        
    //     //console.log(result)

    //     if (result.nModified != 1) {
    //         return {
    //             success: false,
    //             error: "Something went wrong when performing this operation."
    //         }
    //     }

    //     return {
    //         success: true,
    //         updated: product
    //     };
    // }

    // function documentToproduct({ _id: productId, ...doc }) {
    //     return makeproduct({ productId, ...doc })
    // }
}