3
\$\begingroup\$

Currently my solution for calculating wages based on average weekly hours and any other wages field is very long and elaborate. I think it can be concised and make more readable but when I try to do it it creates some nasty bugs as it is based on field changed. Any elegant refactoring suggestion would be much appreciated!

/**
 * Executed on change of Hourly Wage.
 * Calculates monthly and annual wage based on average weekly hours and hourly wage.
 * 
 * @param {object} executionContext : provided by dynamics
 * @returns {boolean} : returns true if calculation is done
 */
function onChangeHourlyWage(executionContext) {
    const formContext = executionContext.getFormContext();

    const {
        AVERAGE_WEEKLY_HOURS,
        HOURLY_WAGE,
        MONTHLY_WAGE,
        ANNUAL_WAGE
    } = EMPLOYMENT_HISTORY_FIELDS;

    const averageWeeklyHours = formContext
        .getAttribute(AVERAGE_WEEKLY_HOURS)
        .getValue();

    const hourlyWage = formContext
        .getAttribute(HOURLY_WAGE)
        .getValue();

    if (!averageWeeklyHours || !hourlyWage) return false;

    annualWage = hourlyWage * averageWeeklyHours * WEEKS_IN_A_YEAR;
    monthlyWage = annualWage / 12;

    formContext.getAttribute(ANNUAL_WAGE).setValue(annualWage);
    formContext.getAttribute(MONTHLY_WAGE).setValue(monthlyWage);

    return true;
}

/**
 * Executed on change of Monthly Wage.
 * Calculates hourly and annual wage based on average weekly hours and monthly wage.
 * 
 * @param {object} executionContext : provided by dynamics
 * @returns {boolean} : returns true if calculation is done
 */
function onChangeMonthlyWage(executionContext) {
    const formContext = executionContext.getFormContext();

    const {
        AVERAGE_WEEKLY_HOURS,
        HOURLY_WAGE,
        MONTHLY_WAGE,
        ANNUAL_WAGE
    } = EMPLOYMENT_HISTORY_FIELDS;

    const averageWeeklyHours = formContext
        .getAttribute(AVERAGE_WEEKLY_HOURS)
        .getValue();

    const monthlyWage = formContext
        .getAttribute(MONTHLY_WAGE)
        .getValue();

    if (!averageWeeklyHours || !monthlyWage) return false;

    annualWage = monthlyWage * 12;
    hourlyWage = annualWage / (WEEKS_IN_A_YEAR * averageWeeklyHours);

    formContext.getAttribute(ANNUAL_WAGE).setValue(annualWage);
    formContext.getAttribute(HOURLY_WAGE).setValue(hourlyWage);

    return true;
}

/**
 * Executed on change of Annual Wage.
 * Calculates hourly and monthly wage based on average weekly hours and annual wage.
 * 
 * @param {object} executionContext : provided by dynamics
 * @returns {boolean} : returns true if calculation is done
 */
function onChangeAnnualWage(executionContext) {
    const formContext = executionContext.getFormContext();

    const {
        AVERAGE_WEEKLY_HOURS,
        HOURLY_WAGE,
        MONTHLY_WAGE,
        ANNUAL_WAGE
    } = EMPLOYMENT_HISTORY_FIELDS;

    const averageWeeklyHours = formContext
        .getAttribute(AVERAGE_WEEKLY_HOURS)
        .getValue();

    const annualWage = formContext
        .getAttribute(ANNUAL_WAGE)
        .getValue();

    if (!averageWeeklyHours || !annualWage) return false;

    hourlyWage = annualWage / (WEEKS_IN_A_YEAR * averageWeeklyHours);
    monthlyWage = annualWage / 12;

    formContext.getAttribute(HOURLY_WAGE).setValue(hourlyWage);
    formContext.getAttribute(MONTHLY_WAGE).setValue(monthlyWage);

    return true;
}

/**
 * Executed on change of AverageWeeklyHours.
 * Calculates hourly, monthly and annual wage if any three of a mentioned 
 * field and average weekly hours is filled.
 * 
 * @param {object} executionContext : dynamics 365 execution context
 */
function onChangeAverageWeeklyHours(executionContext) {
    if (onChangeHourlyWage(executionContext)) return;
    if (onChangeMonthlyWage(executionContext)) return;
    onChangeAnnualWage(executionContext);
}
\$\endgroup\$
  • \$\begingroup\$ Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Please check that I haven't misrepresented your code, and correct it if I have. \$\endgroup\$ – Toby Speight Jan 24 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.