Your IP : 216.73.216.91


Current Path : /var/node/inatote/Inatote-Backend/vouchers/
Upload File :
Current File : /var/node/inatote/Inatote-Backend/vouchers/vouchers.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 makeVoucher(productInfo = requiredParam('productInfo')) {
    const validproduct = validate(productInfo)
    const normalproduct = normalize(validproduct)
    return Object.freeze(normalproduct)

    function validate({
        code = requiredParam('code'),
        amount = requiredParam('amount'),
        status = requiredParam('status'),
        price = requiredParam('price'),
        userId = requiredParam("userId"),
        expiry = requiredParam("expiry"),
        ...otherInfo
    } = {}) {

        validateCode(code)
        validateAmount(amount)
        validateQuantity(price)

        return { code, amount, status, price,userId , expiry, ...otherInfo }

        function validateCode(name) {
            if (name.length < 4) {
                throw new InvalidPropertyError(
                    `Code must be at least 4 characters long.`
                )
            }
        }


        function validateImageUrl(url) {
            if (url.length < 1) {
                throw new InvalidPropertyError("Atleast one image is required")
            }
        }


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

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


    function normalize({  ...otherInfo }) {
        return {
            ...otherInfo,
            history: [],
        }
    }
}