Your IP : 216.73.216.91


Current Path : /var/node/inatote/Inatote-Backend/orders/
Upload File :
Current File : /var/node/inatote/Inatote-Backend/orders/order.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'
import makeorderList from './order-list.js'

export default function makeOrder(orderInfo = requiredParam('orderInfo')) {
    const validorder = validate(orderInfo)
    const normalorder = normalize(validorder)
    return Object.freeze(normalorder)

    function validate({
        userId = requiredParam('userId'),
        paymentId = requiredParam('paymentId'),
        products = requiredParam('products'),
        vendorId = requiredParam("vendorId"),
        address = requiredParam('address'),
        subTotal = requiredParam('subTotal'),
        deliveryService = requiredParam("deliveryService"),
        serviceFee = requiredParam("serviceFee"),
        totalPrice = requiredParam('totalPrice'),
        orderNo = requiredParam('orderNo'),
     
        ...otherInfo
    } = {}) {
        validateproducts(products)
        validateAddress(address)
        validateNumber(totalPrice)          
        validateNumber(serviceFee)
        // validateNumber(tax)
        validateNumber(subTotal)
         
        

        return { userId , products,vendorId,orderNo , address , totalPrice,deliveryService,serviceFee  ,subTotal , paymentId , ...otherInfo }

        function validateproducts(products) {
            if (products.length < 1) {
                throw new InvalidPropertyError('No product found in this order.')
            }

            for (var i = 0; i < products.length; i++) {
                if (!products[i]['id'])
                    throw new InvalidPropertyError('ID field required for product.')

                if (!products[i]['quantity'])
                    throw new InvalidPropertyError('Quantity required for product.')

                  
    
            }

        }

        function validateAddress(locations) {

            if (!locations)
                throw new InvalidPropertyError('Address field required.')

        }

        function validateNumber(number) {
            if (parseFloat(number) == undefined || !parseFloat(number)) {
                throw new InvalidPropertyError(
                    `Invalid price format.`
                )
            }
        }
    }


    function normalize( otherInfo ) {
        return {
            ...otherInfo,
            orderStatus : "pending",
            show : true,
            orderHistory : [{
                action: "pending",
                date: new Date()
            }]
        }
    }
}