Skip to content

A local-first personal finance app

Fix type mismatch in getStatus (#4199)

Samuel Barnes6cbf3e33e6c4
diff --git a/packages/loot-core/src/client/data-hooks/schedules.tsx b/packages/loot-core/src/client/data-hooks/schedules.tsx
index 5824f6bb30..d6d54e7919 100644
--- a/packages/loot-core/src/client/data-hooks/schedules.tsx
+++ b/packages/loot-core/src/client/data-hooks/schedules.tsx
@@ -1,25 +1,21 @@
// @ts-strict-ignore
import React, {
createContext,
+ type PropsWithChildren,
useContext,
useEffect,
- useState,
useRef,
- type PropsWithChildren,
+ useState,
} from 'react';
import { useSyncedPref } from '@actual-app/web/src/hooks/useSyncedPref';
import { q, type Query } from '../../shared/query';
+import { getHasTransactionsQuery, getStatus } from '../../shared/schedules';
import {
- getUpcomingDays,
- getHasTransactionsQuery,
- getStatus,
-} from '../../shared/schedules';
-import {
- type TransactionEntity,
- type ScheduleEntity,
type AccountEntity,
+ type ScheduleEntity,
+ type TransactionEntity,
} from '../../types/models';
import { accountFilter } from '../queries';
import { type LiveQuery, liveQuery } from '../query-helpers';
@@ -31,7 +27,7 @@ function loadStatuses(
schedules: readonly ScheduleEntity[],
onData: (data: ScheduleStatuses) => void,
onError: (error: Error) => void,
- upcomingLength: number,
+ upcomingLength: string,
) {
return liveQuery<TransactionEntity>(getHasTransactionsQuery(schedules), {
onData: data => {
@@ -77,7 +73,6 @@ export function useSchedules({
statuses: new Map(),
});
const [upcomingLength] = useSyncedPref('upcomingScheduledTransactionLength');
- const upcomingDays = getUpcomingDays(upcomingLength);
const scheduleQueryRef = useRef<LiveQuery<ScheduleEntity> | null>(null);
const statusQueryRef = useRef<LiveQuery<TransactionEntity> | null>(null);
@@ -116,7 +111,7 @@ export function useSchedules({
}
},
onError,
- upcomingDays,
+ upcomingLength,
);
},
onError,
@@ -127,7 +122,7 @@ export function useSchedules({
scheduleQueryRef.current?.unsubscribe();
statusQueryRef.current?.unsubscribe();
};
- }, [query, upcomingDays]);
+ }, [query, upcomingLength]);
return {
isLoading,
diff --git a/packages/loot-core/src/server/schedules/app.ts b/packages/loot-core/src/server/schedules/app.ts
index 365f15604c..a61f762cbc 100644
--- a/packages/loot-core/src/server/schedules/app.ts
+++ b/packages/loot-core/src/server/schedules/app.ts
@@ -9,13 +9,12 @@ import { currentDay, dayFromDate, parseDate } from '../../shared/months';
import { q } from '../../shared/query';
import {
extractScheduleConds,
+ getDateWithSkippedWeekend,
getHasTransactionsQuery,
+ getNextDate,
getScheduledAmount,
getStatus,
- getUpcomingDays,
recurConfigToRSchedule,
- getNextDate,
- getDateWithSkippedWeekend,
} from '../../shared/schedules';
import { Rule } from '../accounts/rules';
import { addTransactions } from '../accounts/sync';
@@ -455,14 +454,12 @@ async function advanceSchedulesService(syncSuccess) {
.select('value'),
);
- const upcomingLengthValue = getUpcomingDays(upcomingLength[0]?.value ?? '7');
-
for (const schedule of schedules) {
const status = getStatus(
schedule.next_date,
schedule.completed,
hasTrans.has(schedule.id),
- upcomingLengthValue,
+ upcomingLength[0]?.value ?? '7',
);
if (status === 'paid') {
diff --git a/packages/loot-core/src/shared/schedules.test.ts b/packages/loot-core/src/shared/schedules.test.ts
index ccabf7f5ec..67f82e6ce8 100644
--- a/packages/loot-core/src/shared/schedules.test.ts
+++ b/packages/loot-core/src/shared/schedules.test.ts
@@ -17,15 +17,15 @@ describe('schedules', () => {
describe('getStatus', () => {
it('returns completed if completed', () => {
- expect(getStatus(todayString, true, false, 7)).toBe('completed');
+ expect(getStatus(todayString, true, false, '7')).toBe('completed');
});
it('returns paid if has transactions', () => {
- expect(getStatus(todayString, false, true, 7)).toBe('paid');
+ expect(getStatus(todayString, false, true, '7')).toBe('paid');
});
it('returns due if today', () => {
- expect(getStatus(todayString, false, false, 7)).toBe('due');
+ expect(getStatus(todayString, false, false, '7')).toBe('due');
});
it.each([1, 7, 14, 30])(
@@ -35,26 +35,26 @@ describe('schedules', () => {
const tomorrow = monthUtils.addDays(today, 1);
const upcomingDate = monthUtils.addDays(today, daysOut);
const scheduledDate = monthUtils.addDays(today, daysOut + 1);
- expect(getStatus(tomorrow, false, false, upcomingLength)).toBe(
- 'upcoming',
- );
- expect(getStatus(upcomingDate, false, false, upcomingLength)).toBe(
- 'upcoming',
- );
- expect(getStatus(scheduledDate, false, false, upcomingLength)).toBe(
- 'scheduled',
- );
+ expect(
+ getStatus(tomorrow, false, false, upcomingLength.toString()),
+ ).toBe('upcoming');
+ expect(
+ getStatus(upcomingDate, false, false, upcomingLength.toString()),
+ ).toBe('upcoming');
+ expect(
+ getStatus(scheduledDate, false, false, upcomingLength.toString()),
+ ).toBe('scheduled');
},
);
it('returns missed if past', () => {
- expect(getStatus(monthUtils.addDays(today, -1), false, false, 7)).toBe(
+ expect(getStatus(monthUtils.addDays(today, -1), false, false, '7')).toBe(
'missed',
);
});
it('returns scheduled if not due, upcoming, or missed', () => {
- expect(getStatus(monthUtils.addDays(today, 8), false, false, 7)).toBe(
+ expect(getStatus(monthUtils.addDays(today, 8), false, false, '7')).toBe(
'scheduled',
);
});
diff --git a/packages/loot-core/src/shared/schedules.ts b/packages/loot-core/src/shared/schedules.ts
index 127ee21477..43e91e09d4 100644
--- a/packages/loot-core/src/shared/schedules.ts
+++ b/packages/loot-core/src/shared/schedules.ts
@@ -11,8 +11,9 @@ export function getStatus(
nextDate: string,
completed: boolean,
hasTrans: boolean,
- upcomingLength: number,
+ upcomingLength: string,
) {
+ const upcomingDays = getUpcomingDays(upcomingLength);
const today = monthUtils.currentDay();
if (completed) {
return 'completed';
@@ -22,7 +23,7 @@ export function getStatus(
return 'due';
} else if (
nextDate > today &&
- nextDate <= monthUtils.addDays(today, upcomingLength)
+ nextDate <= monthUtils.addDays(today, upcomingDays)
) {
return 'upcoming';
} else if (nextDate < today) {
@@ -349,7 +350,7 @@ export function describeSchedule(schedule, payee) {
}
}
-export function getUpcomingDays(upcomingLength = '7') {
+export function getUpcomingDays(upcomingLength = '7'): number {
const today = monthUtils.currentDay();
switch (upcomingLength) {
diff --git a/upcoming-release-notes/4199.md b/upcoming-release-notes/4199.md
new file mode 100644
index 0000000000..2572dbe7e7
--- /dev/null
+++ b/upcoming-release-notes/4199.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [ SamBobBarnes ]
+---
+
+Fix upcomingLength type mismatch in getStatus