Skip to content

Commit 5cf2d27

Browse files
committed
changed some bits of upcoming
1 parent 4b8c02b commit 5cf2d27

1 file changed

Lines changed: 38 additions & 14 deletions

File tree

pages/api/public/v1/workspace/[id]/sessions/upcoming.ts

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,35 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
1616
.status(400)
1717
.json({ success: false, error: "Missing workspace ID" });
1818

19-
const { endDate, category, startDate, status, limit, page, hideClaimed } =
20-
req.query;
21-
const start = startDate
22-
? new Date(startDate as string)
23-
: (() => {
24-
const now = new Date();
25-
now.setHours(0, 0, 0, 0);
26-
return now;
27-
})();
19+
const {
20+
endDate,
21+
category,
22+
startDate,
23+
status,
24+
limit,
25+
page,
26+
hideClaimed,
27+
showClaimed,
28+
hoursAhead,
29+
} = req.query;
30+
31+
const DEFAULT_HOURS_AHEAD = 24;
32+
const MAX_HOURS_AHEAD = 24 * 30;
33+
34+
const parsedHoursAhead = Number(hoursAhead);
35+
const windowHours =
36+
Number.isFinite(parsedHoursAhead) && parsedHoursAhead > 0
37+
? Math.min(parsedHoursAhead, MAX_HOURS_AHEAD)
38+
: DEFAULT_HOURS_AHEAD;
39+
40+
const start = startDate ? new Date(startDate as string) : new Date();
2841

2942
const end = endDate
3043
? new Date(endDate as string)
3144
: (() => {
32-
const tomorrow = new Date();
33-
tomorrow.setDate(tomorrow.getDate() + 1);
34-
tomorrow.setHours(23, 59, 59, 999);
35-
return tomorrow;
45+
const future = startDate ? new Date(start) : new Date();
46+
future.setTime(future.getTime() + windowHours * 60 * 60 * 1000);
47+
return future;
3648
})();
3749

3850
if (isNaN(start.getTime()) || isNaN(end.getTime()))
@@ -48,6 +60,12 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
4860
const take = Math.min(Number(limit) || 50, 10000);
4961
const skip = (Math.max(Number(page) || 1, 1) - 1) * take;
5062

63+
if (hideClaimed === "true" && showClaimed === "true")
64+
return res.status(400).json({
65+
success: false,
66+
error: "hideClaimed and showClaimed cannot both be true",
67+
});
68+
5169
try {
5270
const where: any = {
5371
sessionType: { workspaceGroupId: workspaceId },
@@ -58,6 +76,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
5876

5977
if (hideClaimed === "true") {
6078
where.users = { none: {} };
79+
} else if (showClaimed === "true") {
80+
where.users = { some: {} };
6181
}
6282

6383
if (status) {
@@ -177,7 +197,11 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
177197
success: true,
178198
sessions: formattedSessions,
179199
sessionsByDate,
180-
dateRange: { startDate: start.toISOString(), endDate: end.toISOString() },
200+
dateRange: {
201+
startDate: start.toISOString(),
202+
endDate: end.toISOString(),
203+
hoursAhead: !startDate && !endDate ? windowHours : undefined,
204+
},
181205
pagination: {
182206
total,
183207
page: Math.max(Number(page) || 1, 1),

0 commit comments

Comments
 (0)