diff --git a/js/time-balance.js b/js/time-balance.js index 0abd25863..5fe560e2d 100644 --- a/js/time-balance.js +++ b/js/time-balance.js @@ -23,13 +23,23 @@ function getFirstInputInDb() { return inputs.length ? inputs[0] : ''; } -function _formatDateForWaivedWorkdayDb(year, month, day) { +/** +* @param {string} dbKey given key of the db +*/ +function _getDateFromStoreDb(dbKey) { + // Normal Store is formated with month described by 0-11 (jan - dec) + const [year, month, day] = dbKey.split('-'); + return new Date(year, month, day); +} + +/** +* @param {string} dbKey given key of the db +*/ +function _getDateFromWaivedWorkdayDb(dbKey) { // WaivedWorkday are formated with two digits for the month/day (01 instead of 1) // and has the month described by 1-12 (jan - dec) - function twoDigitNumber(num) { - return num < 10 ? `0${num}` : `${num}`; - } - return `${year}-${twoDigitNumber(month + 1)}-${twoDigitNumber(day)}`; + const [year, month, day] = dbKey.split('-'); + return new Date(year, month-1, day); } function _getHoursPerDay() { @@ -37,47 +47,79 @@ function _getHoursPerDay() { return savedPreferences['hours-per-day']; } -function _getBalanceForDay(date, hoursPerDay) { - const totalForDay = store.get(`${date.getFullYear()}-${date.getMonth()}-${date.getDate()}-day-total`); - const waivedDay = waivedWorkdays.get(_formatDateForWaivedWorkdayDb(date.getFullYear(), date.getMonth(), date.getDate())); - // add worked/waived count - var dayBalance = '00:00'; - if (waivedDay !== undefined) { - dayBalance = waivedDay['hours']; - } else if (totalForDay !== undefined) { - dayBalance = totalForDay; +/** +* Iterates over stores and returns total balance. +* Since waiver store precedes normal store, must not get from normal store if day is waived. +* @param {Date} firstDate +* @param {Date} limitDate +*/ +function _getDayTotalsFromStores(firstDate, limitDate) { + const preferences = getUserPreferences(); + + let totals = {}; + for (let value of waivedWorkdays) { + let date = _getDateFromWaivedWorkdayDb(value[0]); + const dateShown = showDay(date.getFullYear(), date.getMonth(), date.getDate(), preferences); + if (date >= firstDate && date <= limitDate && dateShown) { + totals[getDateStr(date)] = value[1]['hours']; + } + } + for (let value of store) { + if (value[0].endsWith('-day-total')) { + let date = _getDateFromStoreDb(value[0]); + if (!(getDateStr(date) in totals)) { + const dateShown = showDay(date.getFullYear(), date.getMonth(), date.getDate(), preferences); + if (date >= firstDate && date <= limitDate && dateShown) { + const totalForDay = value[1]; + totals[getDateStr(date)] = totalForDay; + } + } + } } - // From the worked/waived count, subtract the expected work time for the day - dayBalance = subtractTime(hoursPerDay, dayBalance); - return dayBalance; + return totals; } -async function computeAllTimeBalanceUntil(day) { - const preferences = getUserPreferences(); +/** +* Computation of all time balance, including limitDay. +* @param {Date} limitDate +*/ +async function computeAllTimeBalanceUntil(limitDate) { const firstInput = getFirstInputInDb(); if (firstInput === '') { return '00:00'; } var [firstYear, firstMonth, firstDay] = firstInput.split('-'); - var date = new Date(firstYear, firstMonth, firstDay); + var firstDate = new Date(firstYear, firstMonth, firstDay); + + let totals = _getDayTotalsFromStores(firstDate, limitDate); - var allTimeTotal = '00:00'; + const preferences = getUserPreferences(); const hoursPerDay = _getHoursPerDay(); - while (getDateStr(date) !== getDateStr(day) && day > date) { + let allTimeTotal = '00:00'; + let date = new Date(firstDate); + const limitDateStr = getDateStr(limitDate); + let dateStr = getDateStr(date); + while (dateStr !== limitDateStr && limitDate > date) { if (showDay(date.getFullYear(), date.getMonth(), date.getDate(), preferences)) { - const dayBalance = _getBalanceForDay(date, hoursPerDay); + const dayTotal = dateStr in totals ? totals[dateStr] : '00:00'; + const dayBalance = subtractTime(hoursPerDay, dayTotal); allTimeTotal = sumTime(dayBalance, allTimeTotal); } date.setDate(date.getDate() + 1); + dateStr = getDateStr(date); } return allTimeTotal; } -async function computeAllTimeBalanceUntilAsync(day) { +/** +* Computes all time balance using an async promise. +* @param {Date} limitDate +*/ +async function computeAllTimeBalanceUntilAsync(limitDate) { return new Promise(resolve => { setTimeout(() => { - resolve(computeAllTimeBalanceUntil(day)); + resolve(computeAllTimeBalanceUntil(limitDate)); }, 1); }); }