1
+ import { DateTime } from "luxon"
2
+
3
+ import cCalendarEventService from "../../services/ccalendarevent"
4
+ const { getCurrentTimezone } = useFormatDate ( )
5
+
1
6
import { subscriptionVisibility , type } from "../../constants/entity/ccalendarevent"
7
+ import { useFormatDate } from "../formatDate"
2
8
3
9
export function useCalendarEvent ( ) {
4
10
return {
@@ -8,6 +14,7 @@ export function useCalendarEvent() {
8
14
canSubscribeToEvent,
9
15
allowSubscribeToEvent,
10
16
allowUnsubscribeToEvent,
17
+ getCalendarEvents,
11
18
}
12
19
}
13
20
@@ -72,3 +79,65 @@ function allowUnsubscribeToEvent(event, userId) {
72
79
73
80
return ! ! findUserLink ( event , userId )
74
81
}
82
+
83
+ /**
84
+ * @param {Object } params
85
+ * @returns {Promise<Object[]> }
86
+ */
87
+ async function requestCalendarEvents ( params ) {
88
+ const calendarEvents = await cCalendarEventService . findAll ( { params } ) . then ( ( response ) => response . json ( ) )
89
+
90
+ return calendarEvents [ "hydra:member" ] . map ( ( event ) => {
91
+ const timezone = getCurrentTimezone ( )
92
+ const start = DateTime . fromISO ( event . startDate , { zone : "utc" } ) . setZone ( timezone )
93
+ const end = DateTime . fromISO ( event . endDate , { zone : "utc" } ) . setZone ( timezone )
94
+
95
+ return {
96
+ ...event ,
97
+ start : start . toString ( ) ,
98
+ end : end . toString ( ) ,
99
+ color : event . color || "#007BFF" ,
100
+ }
101
+ } )
102
+ }
103
+
104
+ /**
105
+ * @param {Object } startDate
106
+ * @param {Object } endDate
107
+ * @param {Object } commonParams
108
+ * @returns {Promise<Object[]> }
109
+ */
110
+ async function getCalendarEvents ( startDate , endDate , commonParams ) {
111
+ const endingEventsPromise = requestCalendarEvents ( {
112
+ ...commonParams ,
113
+ "endDate[before]" : endDate . toISOString ( ) ,
114
+ "endDate[after]" : startDate . toISOString ( ) ,
115
+ } )
116
+
117
+ const currentEventsPromise = requestCalendarEvents ( {
118
+ ...commonParams ,
119
+ "endDate[before]" : endDate . toISOString ( ) ,
120
+ "startDate[after]" : startDate . toISOString ( ) ,
121
+ } )
122
+
123
+ const startingEventsPromise = requestCalendarEvents ( {
124
+ ...commonParams ,
125
+ "startDate[before]" : endDate . toISOString ( ) ,
126
+ "startDate[after]" : startDate . toISOString ( ) ,
127
+ } )
128
+
129
+ const [ endingEvents , currentEvents , startingEvents ] = await Promise . all ( [
130
+ endingEventsPromise ,
131
+ currentEventsPromise ,
132
+ startingEventsPromise ,
133
+ ] )
134
+
135
+ const uniqueEventsMap = new Map ( )
136
+
137
+ endingEvents
138
+ . concat ( currentEvents )
139
+ . concat ( startingEvents )
140
+ . forEach ( ( event ) => uniqueEventsMap . set ( event . id , event ) )
141
+
142
+ return Array . from ( uniqueEventsMap . values ( ) )
143
+ }
0 commit comments