Skip to content

Commit fb80e83

Browse files
fix: Make sure a month has elapsed before adding free monthly credits
1 parent 0724c25 commit fb80e83

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/utils/credits.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ function shouldRefreshCredits(session: KVSession, currentTime: Date): boolean {
1919
return true;
2020
}
2121

22-
// Check if the month or year has changed since last refresh
23-
return (
24-
session.user.lastCreditRefreshAt.getMonth() !== currentTime.getMonth() ||
25-
session.user.lastCreditRefreshAt.getFullYear() !== currentTime.getFullYear()
26-
);
22+
// Calculate the date exactly one month after the last refresh
23+
const oneMonthAfterLastRefresh = new Date(session.user.lastCreditRefreshAt);
24+
oneMonthAfterLastRefresh.setMonth(oneMonthAfterLastRefresh.getMonth() + 1);
25+
26+
// Only refresh if we've passed the one month mark
27+
return currentTime >= oneMonthAfterLastRefresh;
2728
}
2829

2930
async function processExpiredCredits(userId: string, currentTime: Date) {

0 commit comments

Comments
 (0)