Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parental-leave): Remove daysToUse from VMST from calculation #16774

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -836,9 +836,6 @@ export class ParentalLeaveService extends BaseTemplateApiService {
daysToUse?: string,
months?: number,
) {
if (daysToUse) {
return daysToUse
}
const start = parseISO(startDate)
const end = parseISO(endDate)
if (isNaN(start.getTime()) || isNaN(end.getTime())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1407,9 +1407,7 @@ export const calculateDaysUsedByPeriods = (periods: Period[]) =>
period.months,
)

const calculatedLength = period.daysToUse
? Number(period.daysToUse)
: Math.round(periodLength * percentage)
const calculatedLength = Math.round(periodLength * percentage)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Potential cumulative rounding errors in calculateDaysUsedByPeriods

By rounding each period's calculated length individually using Math.round(periodLength * percentage), there's a risk of cumulative rounding errors affecting the total days calculated. It might be more accurate to sum the unrounded values and apply rounding to the final total to ensure precision.

Apply this diff to adjust the rounding:

-export const calculateDaysUsedByPeriods = (periods: Period[]) =>
-  Math.round(
-    periods.reduce((total, period) => {
-      const start = parseISO(period.startDate)
-      const end = parseISO(period.endDate)
-      const percentage = Number(period.ratio) / 100
-      const periodLength = calculatePeriodLength(
-        start,
-        end,
-        undefined,
-        period.months,
-      )
-      const calculatedLength = Math.round(periodLength * percentage)
-
-      return total + calculatedLength
-    }, 0),
-  )
+export const calculateDaysUsedByPeriods = (periods: Period[]) => {
+  const totalDays = periods.reduce((total, period) => {
+    const start = parseISO(period.startDate)
+    const end = parseISO(period.endDate)
+    const percentage = Number(period.ratio) / 100
+    const periodLength = calculatePeriodLength(
+      start,
+      end,
+      undefined,
+      period.months,
+    )
+    const calculatedLength = periodLength * percentage
+
+    return total + calculatedLength
+  }, 0)
+  return Math.round(totalDays)
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const calculatedLength = Math.round(periodLength * percentage)
export const calculateDaysUsedByPeriods = (periods: Period[]) => {
const totalDays = periods.reduce((total, period) => {
const start = parseISO(period.startDate)
const end = parseISO(period.endDate)
const percentage = Number(period.ratio) / 100
const periodLength = calculatePeriodLength(
start,
end,
undefined,
period.months,
)
const calculatedLength = periodLength * percentage
return total + calculatedLength
}, 0)
return Math.round(totalDays)
}


return total + calculatedLength
}, 0),
Expand Down
Loading