@@ -145,12 +145,18 @@ export class EmailService {
145145// Reminder types
146146export type ReminderType = "due_soon" | "overdue" | "return_reminder" ;
147147
148- // Get books that are due soon (within 2 days)
148+ // Get books that are due soon (within 2 days, including today )
149149export async function getBooksDueSoon ( ) {
150150 const twoDaysFromNow = new Date ( ) ;
151151 twoDaysFromNow . setDate ( twoDaysFromNow . getDate ( ) + 2 ) ;
152152
153153 const now = new Date ( ) ;
154+ // Set to start of today to include books due today
155+ const startOfToday = new Date (
156+ now . getFullYear ( ) ,
157+ now . getMonth ( ) ,
158+ now . getDate ( )
159+ ) ;
154160
155161 const dueSoonBooks = await db
156162 . select ( {
@@ -161,7 +167,7 @@ export async function getBooksDueSoon() {
161167 userEmail : users . email ,
162168 borrowDate : borrowRecords . borrowDate ,
163169 dueDate : borrowRecords . dueDate ,
164- daysUntilDue : sql < number > `(${ borrowRecords . dueDate } ::date - ${ now } ::date)` ,
170+ daysUntilDue : sql < number > `(${ borrowRecords . dueDate } ::date - ${ startOfToday } ::date)` ,
165171 } )
166172 . from ( borrowRecords )
167173 . innerJoin ( books , eq ( borrowRecords . bookId , books . id ) )
@@ -170,7 +176,7 @@ export async function getBooksDueSoon() {
170176 and (
171177 eq ( borrowRecords . status , "BORROWED" ) ,
172178 sql `${ borrowRecords . dueDate } IS NOT NULL` ,
173- sql `${ borrowRecords . dueDate } > ${ now } ` ,
179+ sql `${ borrowRecords . dueDate } >= ${ startOfToday } ` ,
174180 sql `${ borrowRecords . dueDate } <= ${ twoDaysFromNow } `
175181 )
176182 ) ;
@@ -181,6 +187,12 @@ export async function getBooksDueSoon() {
181187// Get overdue books
182188export async function getOverdueBooks ( ) {
183189 const now = new Date ( ) ;
190+ // Set to start of today to exclude books due today from overdue
191+ const startOfToday = new Date (
192+ now . getFullYear ( ) ,
193+ now . getMonth ( ) ,
194+ now . getDate ( )
195+ ) ;
184196
185197 const overdueBooks = await db
186198 . select ( {
@@ -191,7 +203,7 @@ export async function getOverdueBooks() {
191203 userEmail : users . email ,
192204 borrowDate : borrowRecords . borrowDate ,
193205 dueDate : borrowRecords . dueDate ,
194- daysOverdue : sql < number > `(${ now } ::date - ${ borrowRecords . dueDate } ::date)` ,
206+ daysOverdue : sql < number > `(${ startOfToday } ::date - ${ borrowRecords . dueDate } ::date)` ,
195207 fineAmount : borrowRecords . fineAmount ,
196208 } )
197209 . from ( borrowRecords )
@@ -201,7 +213,7 @@ export async function getOverdueBooks() {
201213 and (
202214 eq ( borrowRecords . status , "BORROWED" ) ,
203215 sql `${ borrowRecords . dueDate } IS NOT NULL` ,
204- sql `${ borrowRecords . dueDate } < ${ now } `
216+ sql `${ borrowRecords . dueDate } < ${ startOfToday } `
205217 )
206218 ) ;
207219
@@ -254,6 +266,8 @@ This is an automated reminder. For assistance, please contact us at support@book
254266 subject ,
255267 body
256268 ) ;
269+ // Update the lastReminderSent timestamp after successful email
270+ await updateLastReminderSent ( book . recordId ) ;
257271 results . push ( {
258272 recordId : book . recordId ,
259273 userEmail : book . userEmail ,
@@ -325,6 +339,8 @@ This is an automated notice. For assistance, please contact us at support@bookwi
325339 subject ,
326340 body
327341 ) ;
342+ // Update the lastReminderSent timestamp after successful email
343+ await updateLastReminderSent ( book . recordId ) ;
328344 results . push ( {
329345 recordId : book . recordId ,
330346 userEmail : book . userEmail ,
@@ -363,6 +379,13 @@ export async function getReminderStats() {
363379 const twoDaysFromNow = new Date ( ) ;
364380 twoDaysFromNow . setDate ( twoDaysFromNow . getDate ( ) + 2 ) ;
365381
382+ // Set to start of today to include books due today
383+ const startOfToday = new Date (
384+ now . getFullYear ( ) ,
385+ now . getMonth ( ) ,
386+ now . getDate ( )
387+ ) ;
388+
366389 const [ dueSoonCount , overdueCount , remindersSentToday ] = await Promise . all ( [
367390 db
368391 . select ( { count : sql < number > `count(*)` } )
@@ -371,7 +394,7 @@ export async function getReminderStats() {
371394 and (
372395 eq ( borrowRecords . status , "BORROWED" ) ,
373396 sql `${ borrowRecords . dueDate } IS NOT NULL` ,
374- sql `${ borrowRecords . dueDate } > ${ now } ` ,
397+ sql `${ borrowRecords . dueDate } >= ${ startOfToday } ` ,
375398 sql `${ borrowRecords . dueDate } <= ${ twoDaysFromNow } `
376399 )
377400 ) ,
@@ -382,7 +405,7 @@ export async function getReminderStats() {
382405 and (
383406 eq ( borrowRecords . status , "BORROWED" ) ,
384407 sql `${ borrowRecords . dueDate } IS NOT NULL` ,
385- sql `${ borrowRecords . dueDate } < ${ now } `
408+ sql `${ borrowRecords . dueDate } < ${ startOfToday } `
386409 )
387410 ) ,
388411 db
0 commit comments