@@ -4,7 +4,7 @@ const logsModel = firestore.collection("logs");
4
4
const admin = require ( "firebase-admin" ) ;
5
5
const { logType, ERROR_WHILE_FETCHING_LOGS } = require ( "../constants/logs" ) ;
6
6
const { INTERNAL_SERVER_ERROR } = require ( "../constants/errorMessages" ) ;
7
- const { getFullName } = require ( "../utils/users" ) ;
7
+ const { getFullName, getUserId } = require ( "../utils/users" ) ;
8
8
const {
9
9
getUsersListFromLogs,
10
10
formatLogsForFeed,
@@ -164,18 +164,48 @@ const fetchLastAddedCacheLog = async (id) => {
164
164
} ;
165
165
166
166
const fetchAllLogs = async ( query ) => {
167
- let { type, prev, next, page, size = SIZE , format } = query ;
167
+ let { type, prev, next, page, size = SIZE , format, userId, username, startDate, endDate, dev } = query ;
168
+
168
169
size = parseInt ( size ) ;
169
170
page = parseInt ( page ) ;
170
171
171
172
try {
172
173
let requestQuery = logsModel ;
174
+ const isDev = dev === "true" ;
175
+
176
+ if ( isDev && username ) {
177
+ userId = await getUserId ( username ) ;
178
+ requestQuery = requestQuery . where ( "meta.userId" , "==" , userId ) ;
179
+ }
173
180
174
181
if ( type ) {
175
182
const logType = type . split ( "," ) ;
176
183
if ( logType . length >= 1 ) requestQuery = requestQuery . where ( "type" , "in" , logType ) ;
177
184
}
178
185
186
+ if ( isDev && ( startDate || endDate ) ) {
187
+ startDate = startDate ? parseInt ( startDate ) : null ;
188
+ endDate = endDate ? parseInt ( endDate ) : null ;
189
+
190
+ if ( startDate && endDate && startDate > endDate ) {
191
+ const error = new Error ( "Start date cannot be greater than end date." ) ;
192
+ error . statusCode = 400 ;
193
+ throw error ;
194
+ }
195
+
196
+ const buildTimestamp = ( date ) => ( {
197
+ _seconds : Math . floor ( date / 1000 ) ,
198
+ _nanoseconds : 0 ,
199
+ } ) ;
200
+
201
+ if ( startDate ) {
202
+ requestQuery = requestQuery . where ( "timestamp" , ">=" , buildTimestamp ( startDate ) ) ;
203
+ }
204
+ if ( endDate ) {
205
+ requestQuery = requestQuery . where ( "timestamp" , "<=" , buildTimestamp ( endDate ) ) ;
206
+ }
207
+ }
208
+
179
209
requestQuery = requestQuery . orderBy ( "timestamp" , "desc" ) ;
180
210
let requestQueryDoc = requestQuery ;
181
211
@@ -205,12 +235,14 @@ const fetchAllLogs = async (query) => {
205
235
const last = snapshot . docs [ snapshot . docs . length - 1 ] ;
206
236
nextDoc = await requestQuery . startAfter ( last ) . limit ( 1 ) . get ( ) ;
207
237
}
238
+
208
239
const allLogs = [ ] ;
209
240
if ( ! snapshot . empty ) {
210
241
snapshot . forEach ( ( doc ) => {
211
242
allLogs . push ( { ...doc . data ( ) } ) ;
212
243
} ) ;
213
244
}
245
+
214
246
if ( allLogs . length === 0 ) {
215
247
return {
216
248
allLogs : [ ] ,
@@ -219,17 +251,23 @@ const fetchAllLogs = async (query) => {
219
251
page : page ? page + 1 : null ,
220
252
} ;
221
253
}
254
+
222
255
if ( format === "feed" ) {
223
- let logsData = [ ] ;
224
256
const userList = await getUsersListFromLogs ( allLogs ) ;
225
257
const taskIdList = await getTasksFromLogs ( allLogs ) ;
226
258
const usersMap = mapify ( userList , "id" ) ;
227
259
const tasksMap = mapify ( taskIdList , "id" ) ;
228
- logsData = allLogs . map ( ( data ) => {
260
+
261
+ const logsData = allLogs . map ( ( data ) => {
229
262
const formattedLogs = formatLogsForFeed ( data , usersMap , tasksMap ) ;
230
263
if ( ! Object . keys ( formattedLogs ) . length ) return null ;
231
- return { ...formattedLogs , type : data . type , timestamp : convertTimestamp ( data . timestamp ) } ;
264
+ return {
265
+ ...formattedLogs ,
266
+ type : data . type ,
267
+ timestamp : convertTimestamp ( data . timestamp ) ,
268
+ } ;
232
269
} ) ;
270
+
233
271
return {
234
272
allLogs : logsData . filter ( ( log ) => log ) ,
235
273
prev : prevDoc . empty ? null : prevDoc . docs [ 0 ] . id ,
0 commit comments