Skip to content

Commit d6dec7e

Browse files
committed
#4029 events paginated endpoint
1 parent 5cc0b31 commit d6dec7e

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

src/backend/src/controllers/calendar.controllers.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,4 +596,18 @@ export default class CalendarController {
596596
next(error);
597597
}
598598
}
599+
600+
static async getAllEventsPaginated(req: Request, res: Response, next: NextFunction) {
601+
try {
602+
const { cursor, pageSize } = req.body;
603+
const paginatedEvents = await CalendarService.getAllEventsPaginated(
604+
req.organization,
605+
cursor ? new Date(cursor) : undefined,
606+
pageSize ? parseInt(pageSize) : undefined
607+
);
608+
res.status(200).json(paginatedEvents);
609+
} catch (error: unknown) {
610+
next(error);
611+
}
612+
}
599613
}

src/backend/src/routes/calendar.routes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,5 +297,6 @@ calendarRouter.post(
297297
);
298298

299299
calendarRouter.get('/calendars', CalendarController.getAllCalendars);
300+
calendarRouter.get('/events-paginated', CalendarController.getAllEventsPaginated);
300301

301302
export default calendarRouter;

src/backend/src/services/calendar.services.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
Machinery,
1616
ScheduleSlot,
1717
notGuest,
18-
isSameDay
18+
isSameDay,
19+
EventInstance
1920
} from 'shared';
2021
import { getCalendarQueryArgs } from '../prisma-query-args/calendar.query-args.js';
2122
import { getEventTypeQueryArgs } from '../prisma-query-args/event-type.query-args.js';
@@ -2749,4 +2750,55 @@ export default class CalendarService {
27492750
});
27502751
return eventTypes.map(eventTypeTransformer);
27512752
}
2753+
2754+
/**
2755+
* Gets all the events paginated, ordered by start time and grouped by date
2756+
* @param organization the org the user is currently in
2757+
* @param cursor the start time of the last event on the prev page
2758+
* @param pageSize the number of events to return per page
2759+
* @returns
2760+
*/
2761+
static async getAllEventsPaginated(
2762+
organization: Organization,
2763+
cursor?: Date,
2764+
pageSize: number = 25
2765+
): Promise<{ instances: EventInstance[]; nextCursor: Date | null }> {
2766+
const now = new Date();
2767+
2768+
const slots = await prisma.schedule_Slot.findMany({
2769+
where: {
2770+
startTime: {
2771+
lt: cursor ?? now
2772+
},
2773+
event: {
2774+
dateDeleted: null,
2775+
eventType: {
2776+
organizationId: organization.organizationId
2777+
}
2778+
}
2779+
},
2780+
include: {
2781+
event: getEventQueryArgs(organization.organizationId)
2782+
},
2783+
orderBy: { startTime: 'desc' },
2784+
take: pageSize
2785+
});
2786+
2787+
const nextCursor = slots.length === pageSize ? slots[slots.length - 1].startTime : null;
2788+
2789+
const instances: EventInstance[] = slots.map((slot) => {
2790+
const { scheduledTimes, ...eventWithoutSlots } = eventTransformer(slot.event);
2791+
return {
2792+
...eventWithoutSlots,
2793+
scheduleSlotId: slot.scheduleSlotId,
2794+
startTime: slot.startTime,
2795+
endTime: slot.endTime,
2796+
allDay: slot.allDay,
2797+
recurring: slot.event.scheduledTimes.length > 1,
2798+
totalScheduledSlots: slot.event.scheduledTimes.length
2799+
};
2800+
});
2801+
2802+
return { instances, nextCursor };
2803+
}
27522804
}

0 commit comments

Comments
 (0)