Skip to content

Commit 77365dc

Browse files
Fixing eatery days
1 parent 4c8b52c commit 77365dc

2 files changed

Lines changed: 38 additions & 10 deletions

File tree

src/eateries/eateries.schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import { z } from 'zod';
22

33
export const getAllEateriesSchema = z.object({
44
query: z.object({
5-
days: z.coerce.number().int().min(0).default(0),
5+
days: z.coerce.number().int().min(0).optional(),
66
}),
77
});

src/eateries/eateryService.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,48 @@ async function getCachedEateries(): Promise<EateryWithEvents[]> {
1414
}
1515
}
1616

17-
export const getAllEateries = async (days: number = 0) => {
17+
export const getAllEateries = async (days?: number) => {
1818
const cachedEateries = await getCachedEateries();
1919

20-
// Calculate date range for filtering events
21-
// days=0 means today, days=1 means tomorrow, etc.
20+
// If no days parameter provided, return all eateries with all events
21+
if (days === undefined) {
22+
return cachedEateries;
23+
}
24+
25+
// Calculate date range for filtering events using UTC to match event timestamps
26+
// days=0 means today, days=1 means tomorrow, days=2 means day after tomorrow, etc.
2227
const now = new Date();
23-
const targetDay = new Date(now);
24-
targetDay.setDate(now.getDate() + days);
2528

26-
const startOfDay = new Date(targetDay);
27-
startOfDay.setHours(0, 0, 0, 0);
29+
// Add days by adding milliseconds (avoids timezone issues)
30+
const msPerDay = 24 * 60 * 60 * 1000;
31+
const targetTimestamp = now.getTime() + days * msPerDay;
32+
const targetDate = new Date(targetTimestamp);
33+
34+
// Set to start of day in UTC
35+
const startOfDay = new Date(
36+
Date.UTC(
37+
targetDate.getUTCFullYear(),
38+
targetDate.getUTCMonth(),
39+
targetDate.getUTCDate(),
40+
0,
41+
0,
42+
0,
43+
0,
44+
),
45+
);
2846

29-
const endOfDay = new Date(targetDay);
30-
endOfDay.setHours(23, 59, 59, 999);
47+
// Set to end of day in UTC
48+
const endOfDay = new Date(
49+
Date.UTC(
50+
targetDate.getUTCFullYear(),
51+
targetDate.getUTCMonth(),
52+
targetDate.getUTCDate(),
53+
23,
54+
59,
55+
59,
56+
999,
57+
),
58+
);
3159

3260
// Filter events to only include those on the specified day
3361
const filteredEateries = cachedEateries.map((eatery) => ({

0 commit comments

Comments
 (0)