8
8
PROGRESSES_RESPONSE_MESSAGES : { PROGRESS_DOCUMENT_NOT_FOUND } ,
9
9
MILLISECONDS_IN_DAY ,
10
10
PROGRESS_VALID_SORT_FIELDS ,
11
+ PROGRESSES_PAGE_SIZE ,
12
+ PROGRESSES_SIZE ,
11
13
} = require ( "../constants/progresses" ) ;
12
14
const { convertTimestampToUTCStartOrEndOfDay } = require ( "./time" ) ;
13
15
const progressesCollection = fireStore . collection ( "progresses" ) ;
@@ -120,6 +122,42 @@ const buildQueryToFetchDocs = (queryParams) => {
120
122
}
121
123
} ;
122
124
125
+ /**
126
+ * Builds a Firestore query to retrieve a paginated list of progress documents within a date range,
127
+ * optionally filtered by user ID, task ID, type, and sorted by a specific field.
128
+ * @param {Object } queryParams - Query parameters including userId, taskId, type, orderBy, size, and page.
129
+ * @returns {Query } A Firestore query object for filtered and paginated progress documents.
130
+ */
131
+
132
+ const buildQueryToFetchPaginatedDocs = async ( queryParams ) => {
133
+ const { type, userId, taskId, orderBy, size = PROGRESSES_SIZE , page = PROGRESSES_PAGE_SIZE } = queryParams ;
134
+ const orderByField = PROGRESS_VALID_SORT_FIELDS [ 0 ] ;
135
+ const isAscOrDsc = orderBy && PROGRESS_VALID_SORT_FIELDS [ 0 ] === orderBy ? "asc" : "desc" ;
136
+ const limit = parseInt ( size , 10 ) ;
137
+ const offset = parseInt ( page , 10 ) * limit ;
138
+
139
+ let baseQuery ;
140
+ if ( type ) {
141
+ baseQuery = progressesCollection . where ( "type" , "==" , type ) . orderBy ( orderByField , isAscOrDsc ) ;
142
+ } else if ( userId ) {
143
+ baseQuery = progressesCollection
144
+ . where ( "type" , "==" , "user" )
145
+ . where ( "userId" , "==" , userId )
146
+ . orderBy ( orderByField , isAscOrDsc ) ;
147
+ } else {
148
+ baseQuery = progressesCollection
149
+ . where ( "type" , "==" , "task" )
150
+ . where ( "taskId" , "==" , taskId )
151
+ . orderBy ( orderByField , isAscOrDsc ) ;
152
+ }
153
+
154
+ const totalProgress = await baseQuery . get ( ) ;
155
+ const totalProgressCount = totalProgress . size ;
156
+
157
+ baseQuery = baseQuery . limit ( limit ) . offset ( offset ) ;
158
+ return { baseQuery, totalProgressCount } ;
159
+ } ;
160
+
123
161
/**
124
162
* Retrieves progress documents from Firestore based on the given query.
125
163
* @param {Query } query - A Firestore query object for fetching progress documents.
@@ -137,6 +175,31 @@ const getProgressDocs = async (query) => {
137
175
} ) ;
138
176
return docsData ;
139
177
} ;
178
+ /**
179
+ * Retrieves progress documents from Firestore based on the given query and page number.
180
+ *
181
+ * @param {Query } query - A Firestore query object for fetching progress documents.
182
+ * @param {number } [pageNumber] - The current page number (optional). If not provided, it will check for documents without pagination.
183
+ * @returns {Array.<Object> } An array of objects representing the retrieved progress documents.
184
+ * Each object contains the document ID (`id`) and its associated data.
185
+ *
186
+ * @throws {NotFound } If no progress documents are found and no page number is specified.
187
+ */
188
+ const getPaginatedProgressDocs = async ( query , page ) => {
189
+ const progressesDocs = await query . get ( ) ;
190
+ if ( ! page && ! progressesDocs . size ) {
191
+ throw new NotFound ( PROGRESS_DOCUMENT_NOT_FOUND ) ;
192
+ }
193
+ if ( ! progressesDocs . size ) {
194
+ return [ ] ;
195
+ }
196
+ const docsData = [ ] ;
197
+ progressesDocs . forEach ( ( doc ) => {
198
+ docsData . push ( { id : doc . id , ...doc . data ( ) } ) ;
199
+ } ) ;
200
+ return docsData ;
201
+ } ;
202
+
140
203
/**
141
204
* Builds a Firestore query for retrieving progress documents within a date range and optionally filtered by user ID or task ID.
142
205
* @param {Object } queryParams - An object containing the query parameters.
@@ -231,8 +294,10 @@ module.exports = {
231
294
assertUserOrTaskExists,
232
295
buildQueryToFetchDocs,
233
296
getProgressDocs,
297
+ getPaginatedProgressDocs,
234
298
buildRangeProgressQuery,
235
299
getProgressRecords,
236
300
buildQueryToSearchProgressByDay,
237
301
buildProgressQueryForMissedUpdates,
302
+ buildQueryToFetchPaginatedDocs,
238
303
} ;
0 commit comments