Your IP : 216.73.216.91


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

import requiredParam from '../helpers/required-param.js'
import { InvalidPropertyError } from '../helpers/errors.js'
import isValidEmail from '../helpers/is-valid-email.js'
import upperFirst from '../helpers/upper-first.js'

export default function makeFavourite(ratingInfo = requiredParam('ratingInfo')) {
    const validrating = validate(ratingInfo)
    const normalrating = normalize(validrating)
    return Object.freeze(normalrating)

    function validate({
        objectId = requiredParam('objectID'),
        type = requiredParam('type'),
        requestType = requiredParam("favouriteType"),
        ...otherInfo
    } = {}) {

        validateFavouriteType(type)
        validateRequestType(requestType)
       
        
        return { objectId, type,requestType , ...otherInfo }

        function validateFavouriteType(type) {
            //console.log("type", type , typeof type);
            if (type != "vendor" && type != "product") {
                
                throw new InvalidPropertyError(
                    `Favourite type must be product or vendor.`
                )
            }
        }

        function validateRequestType(type) {
            //console.log("type", type , typeof type);
            if (type != "favourite" && type != "unfavourite") {
                
                throw new InvalidPropertyError(
                    `Favourite type must be favourite or unfavourite.`
                )
            }
        }


      


      
    }


    function normalize({objectId , type , requestType,product=[] , userId , vendor=[] }) {
        //console.log("product" , product , requestType , objectId , typeof objectId , typeof product[0]);
        if(requestType == "favourite"){
            if (type == "vendor"){
                vendor = [...vendor , `${objectId}`]
            }else{
                product = [...product , `${objectId}`]
            }
        }else{
            if (type == "vendor"){
                vendor = vendor.filter(f => objectId.toString() != f.toString()  )
            }else{
                product = product.filter(f => objectId.toString() != f.toString()  )
            }
        }
        //console.log("2" , product);
        return{
            product,
            vendor,
            userId
            
        }
        
    }
}