diff --git a/api/src/routes/payday.ts b/api/src/routes/payday.ts index 681909a..6ab333d 100644 --- a/api/src/routes/payday.ts +++ b/api/src/routes/payday.ts @@ -105,21 +105,22 @@ const paydayRoutes: FastifyPluginAsync = async (app, opts) // Determine the currently due pay cycle: // - on payday: cycle starts today - // - after payday: cycle starts on the last expected payday - const lookbackDaysByFrequency = { - weekly: 14, - biweekly: 28, - monthly: 62, + // - after payday: cycle starts on the immediately prior expected payday + const oneCycleLookbackDaysByFrequency = { + weekly: 7, + biweekly: 14, + monthly: 32, } as const; - const lookbackAnchor = new Date( - now.getTime() - lookbackDaysByFrequency[user.incomeFrequency] * DAY_MS + const previousPaydayAnchor = new Date( + nextPayday.getTime() - oneCycleLookbackDaysByFrequency[user.incomeFrequency] * DAY_MS ); - const currentCycleStart = calculateNextPayday( + const previousExpectedPayday = calculateNextPayday( user.firstIncomeDate, user.incomeFrequency, - lookbackAnchor, + previousPaydayAnchor, userTimezone ); + const currentCycleStart = isPayday ? nextPayday : previousExpectedPayday; const isCycleDue = now.getTime() >= currentCycleStart.getTime(); const currentCycleEnd = currentCycleStart.getTime() === nextPayday.getTime()