@@ -15,7 +15,8 @@ import {
1515 Machinery ,
1616 ScheduleSlot ,
1717 notGuest ,
18- isSameDay
18+ isSameDay ,
19+ EventInstance
1920} from 'shared' ;
2021import { getCalendarQueryArgs } from '../prisma-query-args/calendar.query-args.js' ;
2122import { 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