calculations/finance.js

import { requiredKeys } from '../keys/required';

/**
 * Finance related calculations
 * @module
 * @since 1.2.14
 */

/**
 * total finance cost for an entire term
 * @param {struct} skv_config price, initial_payment (aka deposit months), months
 * @returns {struct} total
 */
const totalForTerm = (
    skv_config,
) => {
    const rst_keys = requiredKeys(skv_config, ['price', 'initial_payment', 'months']);

    if (!rst_keys.success) {
        throw new Error(rst_keys.msg);
    }

    const total_months = skv_config.months + skv_config.initial_payment - 1;

    return { amount: skv_config.price * total_months };
};

const averageFinanceCost = (
    skv_config,
) => {
    const rst_keys = requiredKeys(skv_config, ['price', 'initial_payment', 'months']);

    if (!rst_keys.success) {
        return rst_keys;
    }

    const result = totalForTerm(skv_config);
    const { amount } = result;

    return {
        total: amount,
        per_annum: amount / (skv_config.months / 12),
        per_month: amount / skv_config.months,
    };
};

export {
    totalForTerm,
    averageFinanceCost,
};