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 paid schedules showing as upcoming in the account #4188

Merged
merged 2 commits into from
Jan 18, 2025
Merged
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
26 changes: 15 additions & 11 deletions packages/loot-core/src/client/data-hooks/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
extractScheduleConds,
getNextDate,
getUpcomingDays,
scheduleIsRecurring,
} from '../../shared/schedules';
import { ungroupTransactions } from '../../shared/transactions';
import {
Expand Down Expand Up @@ -169,24 +170,27 @@ export function usePreviewTransactions(): UsePreviewTransactionsResult {
const { date: dateConditions } = extractScheduleConds(
schedule._conditions,
);
let day = d.startOfDay(parseDate(schedule.next_date));

const status = statuses.get(schedule.id);
const isRecurring = scheduleIsRecurring(dateConditions);

const dates: string[] = [];
let day = d.startOfDay(parseDate(schedule.next_date));
if (isRecurring) {
while (day <= upcomingPeriodEnd) {
const nextDate = getNextDate(dateConditions, day);

const dates: Set<string> = new Set();
while (day <= upcomingPeriodEnd) {
const nextDate = getNextDate(dateConditions, day);
if (parseDate(nextDate) > upcomingPeriodEnd) break;

if (status === 'paid' && day.getTime() === today.getTime()) {
dates.push(nextDate);
day = parseDate(addDays(nextDate, 1));
continue;
}
} else {
dates.push(getNextDate(dateConditions, day));
}

if (dates.has(nextDate)) break;
if (parseDate(nextDate) > upcomingPeriodEnd) break;

dates.add(nextDate);
day = parseDate(addDays(nextDate, 1));
if (status === 'paid') {
dates.shift();
}

const schedules: {
Expand Down
7 changes: 7 additions & 0 deletions packages/loot-core/src/shared/schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,10 @@ export function getUpcomingDays(upcomingLength = '7') {
return parseInt(upcomingLength, 10);
}
}

export function scheduleIsRecurring(dateCond) {
const cond = new Condition(dateCond.op, 'date', dateCond.value, null);
const value = cond.getValue();

return value.type === 'recur';
}
Comment on lines +375 to +380
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

Add null safety checks to prevent runtime errors.

The function should handle cases where dateCond or its properties are undefined/null.

 export function scheduleIsRecurring(dateCond) {
+  if (!dateCond?.op || !dateCond?.value) {
+    return false;
+  }
   const cond = new Condition(dateCond.op, 'date', dateCond.value, null);
   const value = cond.getValue();

   return value.type === 'recur';
 }
📝 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
export function scheduleIsRecurring(dateCond) {
const cond = new Condition(dateCond.op, 'date', dateCond.value, null);
const value = cond.getValue();
return value.type === 'recur';
}
export function scheduleIsRecurring(dateCond) {
if (!dateCond?.op || !dateCond?.value) {
return false;
}
const cond = new Condition(dateCond.op, 'date', dateCond.value, null);
const value = cond.getValue();
return value.type === 'recur';
}

6 changes: 6 additions & 0 deletions upcoming-release-notes/4188.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [matt-fidd]
---

Fix paid schedules showing as upcoming in the account
Loading