@@ -33,41 +33,68 @@ export async function getDashboardSummary(
3333 userId : number ,
3434) : Promise < { success : boolean ; data ?: SummaryData ; error ?: string } > {
3535 try {
36- // Get today's data
37- const today = new Date ( )
38- const startOfToday = new Date ( today )
39- startOfToday . setHours ( 0 , 0 , 0 , 0 )
40- const endOfToday = new Date ( today )
41- endOfToday . setHours ( 23 , 59 , 59 , 999 )
42-
43- // Get yesterday's data for comparison
44- const yesterday = new Date ( today )
45- yesterday . setDate ( yesterday . getDate ( ) - 1 )
46- const startOfYesterday = new Date ( yesterday )
47- startOfYesterday . setHours ( 0 , 0 , 0 , 0 )
48- const endOfYesterday = new Date ( yesterday )
49- endOfYesterday . setHours ( 23 , 59 , 59 , 999 )
50-
51- // Get start of week
52- const startOfWeek = new Date ( today )
53- startOfWeek . setDate ( today . getDate ( ) - today . getDay ( ) ) // Start from Sunday
54- startOfWeek . setHours ( 0 , 0 , 0 , 0 )
55-
56- // Get start of last week
57- const startOfLastWeek = new Date ( startOfWeek )
58- startOfLastWeek . setDate ( startOfLastWeek . getDate ( ) - 7 )
59- const endOfLastWeek = new Date ( startOfWeek )
60- endOfLastWeek . setMilliseconds ( - 1 )
61-
62- // Get start of month
63- const startOfMonth = new Date ( today . getFullYear ( ) , today . getMonth ( ) , 1 )
64- startOfMonth . setHours ( 0 , 0 , 0 , 0 )
65-
66- // Get start of last month
67- const startOfLastMonth = new Date ( today . getFullYear ( ) , today . getMonth ( ) - 1 , 1 )
68- startOfLastMonth . setHours ( 0 , 0 , 0 , 0 )
69- const endOfLastMonth = new Date ( startOfMonth )
70- endOfLastMonth . setMilliseconds ( - 1 )
36+ const tz = 'America/Sao_Paulo'
37+
38+ // Get year, month (0-11), day (1-31) in BRT timezone from a given Date
39+ const getBrParts = ( d : Date ) => {
40+ const parts = new Intl . DateTimeFormat ( 'en-US' , {
41+ timeZone : tz ,
42+ year : 'numeric' , month : 'numeric' , day : 'numeric'
43+ } ) . formatToParts ( d )
44+
45+ return {
46+ year : parseInt ( parts . find ( p => p . type === 'year' ) ! . value ) ,
47+ month : parseInt ( parts . find ( p => p . type === 'month' ) ! . value ) - 1 ,
48+ date : parseInt ( parts . find ( p => p . type === 'day' ) ! . value )
49+ }
50+ }
51+
52+ const brParts = getBrParts ( new Date ( ) )
53+
54+ const createBrDate = ( y : number , m : number , d : number , isEndOfDay : boolean ) => {
55+ const pad = ( n : number ) => String ( n ) . padStart ( 2 , '0' )
56+ const timeStr = isEndOfDay ? "23:59:59.999" : "00:00:00.000"
57+ return new Date ( `${ y } -${ pad ( m + 1 ) } -${ pad ( d ) } T${ timeStr } -03:00` )
58+ }
59+
60+ const getMathDate = ( y : number , m : number , d : number ) => new Date ( y , m , d )
61+
62+ // Today
63+ const { year, month, date } = brParts
64+ const todayMath = getMathDate ( year , month , date )
65+
66+ const startOfToday = createBrDate ( year , month , date , false )
67+ const endOfToday = createBrDate ( year , month , date , true )
68+
69+ // Yesterday
70+ const yesterdayMath = new Date ( todayMath )
71+ yesterdayMath . setDate ( yesterdayMath . getDate ( ) - 1 )
72+ const startOfYesterday = createBrDate ( yesterdayMath . getFullYear ( ) , yesterdayMath . getMonth ( ) , yesterdayMath . getDate ( ) , false )
73+ const endOfYesterday = createBrDate ( yesterdayMath . getFullYear ( ) , yesterdayMath . getMonth ( ) , yesterdayMath . getDate ( ) , true )
74+
75+ // This week (starting Sunday)
76+ const startOfWeekMath = new Date ( todayMath )
77+ startOfWeekMath . setDate ( startOfWeekMath . getDate ( ) - startOfWeekMath . getDay ( ) )
78+ const startOfWeek = createBrDate ( startOfWeekMath . getFullYear ( ) , startOfWeekMath . getMonth ( ) , startOfWeekMath . getDate ( ) , false )
79+
80+ // Last week
81+ const startOfLastWeekMath = new Date ( startOfWeekMath )
82+ startOfLastWeekMath . setDate ( startOfLastWeekMath . getDate ( ) - 7 )
83+ const startOfLastWeek = createBrDate ( startOfLastWeekMath . getFullYear ( ) , startOfLastWeekMath . getMonth ( ) , startOfLastWeekMath . getDate ( ) , false )
84+
85+ const endOfLastWeekMath = new Date ( startOfWeekMath )
86+ endOfLastWeekMath . setDate ( endOfLastWeekMath . getDate ( ) - 1 )
87+ const endOfLastWeek = createBrDate ( endOfLastWeekMath . getFullYear ( ) , endOfLastWeekMath . getMonth ( ) , endOfLastWeekMath . getDate ( ) , true )
88+
89+ // This month
90+ const startOfMonth = createBrDate ( year , month , 1 , false )
91+
92+ // Last month
93+ const startOfLastMonthMath = new Date ( year , month - 1 , 1 )
94+ const startOfLastMonth = createBrDate ( startOfLastMonthMath . getFullYear ( ) , startOfLastMonthMath . getMonth ( ) , 1 , false )
95+
96+ const endOfLastMonthMath = new Date ( year , month , 0 ) // Last day of last month
97+ const endOfLastMonth = createBrDate ( endOfLastMonthMath . getFullYear ( ) , endOfLastMonthMath . getMonth ( ) , endOfLastMonthMath . getDate ( ) , true )
7198
7299 // Today's data
73100 const todayResult = await sql `
0 commit comments