Skip to content

Commit

Permalink
Fix type mismatch in getStatus (#4199)
Browse files Browse the repository at this point in the history
* fixed type mismatch

* release notes

* updated references to submit string

* fixed type in test

* updated test to work with add days method
  • Loading branch information
SamBobBarnes authored Jan 20, 2025
1 parent cdbf3e0 commit 6cbf3e3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 36 deletions.
21 changes: 8 additions & 13 deletions packages/loot-core/src/client/data-hooks/schedules.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -116,7 +111,7 @@ export function useSchedules({
}
},
onError,
upcomingDays,
upcomingLength,
);
},
onError,
Expand All @@ -127,7 +122,7 @@ export function useSchedules({
scheduleQueryRef.current?.unsubscribe();
statusQueryRef.current?.unsubscribe();
};
}, [query, upcomingDays]);
}, [query, upcomingLength]);

return {
isLoading,
Expand Down
9 changes: 3 additions & 6 deletions packages/loot-core/src/server/schedules/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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') {
Expand Down
28 changes: 14 additions & 14 deletions packages/loot-core/src/shared/schedules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])(
Expand All @@ -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',
);
});
Expand Down
7 changes: 4 additions & 3 deletions packages/loot-core/src/shared/schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/4199.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [ SamBobBarnes ]
---

Fix upcomingLength type mismatch in getStatus

0 comments on commit 6cbf3e3

Please sign in to comment.