Skip to content

Commit

Permalink
fix: issue with grand plan recalculation
Browse files Browse the repository at this point in the history
Grand plan recalculation was using the start date of the grand plan instead of today's date, which was leading to incorrect results.

- Update recalculateGrandPlanPacerPlanProperties to use a dateProvider.
  • Loading branch information
kkartch0 committed Feb 7, 2025
1 parent 69ba154 commit 94456e0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/commands/recalculateActiveGrandPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const recalculateActiveGrandPlan = async (app: App, settings: PacerPlanPl
return;
}

recalculateGrandPlanPacerPlanProperties(grandPlan);
recalculateGrandPlanPacerPlanProperties(grandPlan, dateProvider);

grandPlan.pacerPlans.forEach((pacerPlan: PacerPlan) => {
pacerPlan.tasks = generateTasksForPacerPlan(pacerPlan, dateProvider);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/recalculateAllPacerPlans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const recalculateAllPacerPlans = async (app: App, settings: PacerPlanPlug
return;
}

recalculateGrandPlanPacerPlanProperties(grandPlan);
recalculateGrandPlanPacerPlanProperties(grandPlan, dateProvider);

} catch (error) {
console.error("Error recalculating grand plan: " + fileTitle);
Expand Down
10 changes: 6 additions & 4 deletions src/grandPlans/GrandPlan.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { calculateAvailableActionDates } from "../dateHelpers";
import { GrandPlan } from "../grandPlans/GrandPlan";
import { PacerPlan } from "../PacerPlan";
import { getQuantitiesRemaining } from "../getQuantitiesRemaining";
import { IDateProvider } from "../../src/dateProvider";

export function recalculateGrandPlanPacerPlanProperties(grandPlan: GrandPlan) {
export function recalculateGrandPlanPacerPlanProperties(grandPlan: GrandPlan, dateProvider: IDateProvider) {
if (grandPlan.pacerPlans.length === 0) return;

const totalRemainingQuantity = grandPlan.pacerPlans.reduce((acc: number, plan: PacerPlan) => acc + getQuantitiesRemaining(plan).length, 0);

const availableActionDates = calculateAvailableActionDates(grandPlan.startDate, grandPlan.endDate, grandPlan.actionDays);
const today = dateProvider.today();
const availableActionDates = calculateAvailableActionDates(today, grandPlan.endDate, grandPlan.actionDays);

for (let i = 0; i < grandPlan.pacerPlans.length; i++) {
grandPlan.pacerPlans[i].actionDays = grandPlan.actionDays;
Expand All @@ -21,14 +23,14 @@ export function recalculateGrandPlanPacerPlanProperties(grandPlan: GrandPlan) {
grandPlan.pacerPlans[i].startDate = grandPlan.endDate;
grandPlan.pacerPlans[i].endDate = grandPlan.endDate;
}
grandPlan.pacerPlans[0].startDate = grandPlan.startDate;
grandPlan.pacerPlans[0].startDate = today;
return;
}

const quantityPerDay = Math.ceil(totalRemainingQuantity / availableActionDates.length);

let currentPlan = grandPlan.pacerPlans[0];
currentPlan.startDate = grandPlan.startDate;
currentPlan.startDate = today;
currentPlan.endDate = calculatePacerPlanEndDate(grandPlan.pacerPlans[0], quantityPerDay);


Expand Down
12 changes: 6 additions & 6 deletions test/GrandPlan.helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("adjustGrandPlanPacerPlanDates", () => {
pacerPlans: []
};

recalculateGrandPlanPacerPlanProperties(grandPlan);
recalculateGrandPlanPacerPlanProperties(grandPlan, { today: () => new Date() });

expect(grandPlan.pacerPlans.length).toBe(0);
});
Expand All @@ -31,7 +31,7 @@ describe("adjustGrandPlanPacerPlanDates", () => {
]
};

recalculateGrandPlanPacerPlanProperties(grandPlan);
recalculateGrandPlanPacerPlanProperties(grandPlan, { today: () => new Date("2023-01-01") });

expect(grandPlan.pacerPlans[0].startDate).toEqual(new Date("2023-01-01"));
expect(grandPlan.pacerPlans[0].endDate).toEqual(new Date("2023-01-08"));
Expand All @@ -56,7 +56,7 @@ describe("adjustGrandPlanPacerPlanDates", () => {
]
};

recalculateGrandPlanPacerPlanProperties(grandPlan);
recalculateGrandPlanPacerPlanProperties(grandPlan, { today: () => new Date("2025-01-18") });

expect(grandPlan.pacerPlans[0].startDate).toEqual(new Date("2025-01-18"));
expect(grandPlan.pacerPlans[0].endDate).toEqual(new Date("2025-01-18"));
Expand All @@ -67,7 +67,7 @@ describe("adjustGrandPlanPacerPlanDates", () => {
it("plans should adjust properly when there are less quantities remaining than days left", () => {
const grandPlan: GrandPlan = {
title: "Grand Plan",
startDate: new Date("2025-01-18"),
startDate: new Date("2025-01-17"),
endDate: new Date("2025-01-26"),
actionDays: Days.Everyday,
pacerPlans: [
Expand All @@ -76,7 +76,7 @@ describe("adjustGrandPlanPacerPlanDates", () => {
]
};

recalculateGrandPlanPacerPlanProperties(grandPlan);
recalculateGrandPlanPacerPlanProperties(grandPlan, { today: () => new Date("2025-01-18") });

expect(grandPlan.pacerPlans[0].startDate).toEqual(new Date("2025-01-18"));
expect(grandPlan.pacerPlans[0].endDate).toEqual(new Date("2025-01-18"));
Expand All @@ -97,7 +97,7 @@ describe("adjustGrandPlanPacerPlanDates", () => {
]
};

recalculateGrandPlanPacerPlanProperties(grandPlan);
recalculateGrandPlanPacerPlanProperties(grandPlan, { today: () => new Date("2025-01-18") });

expect(grandPlan.pacerPlans[0].startDate).toEqual(new Date("2025-01-18"));
expect(grandPlan.pacerPlans[0].endDate).toEqual(new Date("2025-01-19"));
Expand Down

0 comments on commit 94456e0

Please sign in to comment.