(queryParams)
| 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 | |
| 161 | /** |
| 162 | * Retrieves progress documents from Firestore based on the given query. |
no test coverage detected