Your IP : 216.73.216.91


Current Path : /var/node/inatote/qa_inatote/rating/
Upload File :
Current File : /var/node/inatote/qa_inatote/rating/ratings-endpoints.js

import {
    UniqueConstraintError,
    InvalidPropertyError,
    RequiredParameterError,
    ApiTokenError,
    AuthorizationTokenError
} from '../helpers/errors.js'
import makeHttpError from '../helpers/http-error.js'
import makeRating from './ratings.js';
import makeorderList from '../orders/order-list.js';
import makeDb from '../db/index.js'


import multer from 'multer'
import validateHeaders from '../helpers/validate-headers.js'
import config from '../config.js'


export default function makeproductsEndpointHandler({ ratingList }) {
    return {
        postrating : postrating,
        getratings : getratings,
        // updaterating : updaterating,
        // deleterating : deleterating
    }

    // todo
    async function updateproduct(req , res) {
        const { categoryId, id } = req.params || {}
        //console.log(categoryId)
        //console.log(id)
        let ratingInfo = req.body

        //console.log(productInfo)

        const result = await productList.update({ productId: id, categoryId: categoryId, product: productInfo })

        res.status(200).json({
            headers: config.app.headers,
            statusCode: 200,
            data: result
        })
    }

    // todo
    async function replaceproduct(httpRequest) { }

    // todo
    async function postrating(req , res) {
        let ratingInfo = req.body
        //console.log("ratingInfo" ,ratingInfo , req.body);
        let database = await makeDb()
        let orderList = await makeorderList({database})
        if (!ratingInfo) {
            return  res.status(400).json({
                headers: config.app.headers,
                statusCode: 400,
                errorMessage: 'Bad request. POST body must be valid JSON.'
            })
        }
        if (typeof req.body === 'string') {
            try {
                ratingInfo = JSON.parse(ratingInfo)
            } catch {
                return  res.status(400).json({
                    headers: config.app.headers,
                    statusCode: 400,
                    errorMessage: 'Bad request. POST body must be valid JSON.'
                })
            }
        }
        
        try {
            // let check_ratings = await ratingList.getItems({userId : ratingInfo.userId , objectId : ratingInfo.objectId}) ;

            // //console.log("check_ratings", check_ratings);
            // if(check_ratings && check_ratings.length > 0){
            //     throw `You have already rated this ${ratingInfo.type}`
            // }
        
            const rating =  makeRating(ratingInfo)
            const result = await ratingList.add(rating)
            let order = await orderList.singleOrder(ratingInfo.orderId);
            let products = order.products;
            let updateEntities = await ratingList.update({riderId : order.riderId , vendorId : order.vendorId , order : order  , products : products.map(m => m.id), ratingInfo})
            // let cal_ratings = await ratingList.getItems({ objectId : ratingInfo.objectId}) ;
            // let rating_sum = 0;
            // if(cal_ratings && cal_ratings.length > 0){
            //     cal_ratings.forEach(element => {
            //         rating_sum = parseInt(element.stars) + rating_sum;
            //     });
            // }
            // let final_rating = rating_sum/cal_ratings.length;
            // //console.log("rating_sum" , rating_sum ,cal_ratings.length );
            // //console.log("final" ,final_rating );
            
            // let update_ratings = await ratingList.updateVendorOrProduct({id : ratingInfo.objectId , ratingType : ratingInfo.type ,items : {ratingAverage : final_rating , ratingCount :cal_ratings.length }  }) ;
            
            res.json({
                headers: config.app.headers,
                statusCode: 201,
                data: result
            })
            //console.log("rating" , rating);
        } catch (e) {
            //console.log("e" , e);
            return  res.status(400).json({
                headers: config.app.headers,
                statusCode: 400,
                errorMessage: e ? e : e.message
            })
        }
    }

    // todo
    async function getratings(req , res) {
       //console.log("req" , req.params);
        const { id } =req.params || {}
        const { max, page, objectId  } = req.query || {}
        const result = id
            ? await ratingList.findById(id)
            : await ratingList.getItems({ max, page , objectId })

        //console.log("asanjnjn")
        res.status(200).json({
            headers: config.app.headers,
            statusCode: 200,
            data: result
        })
    }

    // todo
    async function deleteproduct(req , res) {
        const { id } = req.params || {}

        const result = await productList.remove({ productId: id })

        res.status(200).json({
            headers: config.app.headers,
            statusCode: 200,
            data: result
        })
    }

}