Skip to content

Commit 70cfd78

Browse files
authored
Merge pull request #2269 from Real-Dev-Squad/fetch-logs
Added queries to search the logs
2 parents 5dc221c + 978c9e9 commit 70cfd78

File tree

3 files changed

+107
-5
lines changed

3 files changed

+107
-5
lines changed

controllers/logs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ const fetchAllLogs = async (req, res) => {
6767
prev: prevUrl,
6868
});
6969
} catch (err) {
70+
if (err.statusCode) {
71+
return res.status(err.statusCode).json({ error: err.message });
72+
}
7073
logger.error(ERROR_WHILE_FETCHING_LOGS, err);
7174
return res.boom.badImplementation(ERROR_WHILE_FETCHING_LOGS);
7275
}

models/logs.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const logsModel = firestore.collection("logs");
44
const admin = require("firebase-admin");
55
const { logType, ERROR_WHILE_FETCHING_LOGS } = require("../constants/logs");
66
const { INTERNAL_SERVER_ERROR } = require("../constants/errorMessages");
7-
const { getFullName } = require("../utils/users");
7+
const { getFullName, getUserId } = require("../utils/users");
88
const {
99
getUsersListFromLogs,
1010
formatLogsForFeed,
@@ -164,18 +164,48 @@ const fetchLastAddedCacheLog = async (id) => {
164164
};
165165

166166
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+
168169
size = parseInt(size);
169170
page = parseInt(page);
170171

171172
try {
172173
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+
}
173180

174181
if (type) {
175182
const logType = type.split(",");
176183
if (logType.length >= 1) requestQuery = requestQuery.where("type", "in", logType);
177184
}
178185

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+
179209
requestQuery = requestQuery.orderBy("timestamp", "desc");
180210
let requestQueryDoc = requestQuery;
181211

@@ -205,12 +235,14 @@ const fetchAllLogs = async (query) => {
205235
const last = snapshot.docs[snapshot.docs.length - 1];
206236
nextDoc = await requestQuery.startAfter(last).limit(1).get();
207237
}
238+
208239
const allLogs = [];
209240
if (!snapshot.empty) {
210241
snapshot.forEach((doc) => {
211242
allLogs.push({ ...doc.data() });
212243
});
213244
}
245+
214246
if (allLogs.length === 0) {
215247
return {
216248
allLogs: [],
@@ -219,17 +251,23 @@ const fetchAllLogs = async (query) => {
219251
page: page ? page + 1 : null,
220252
};
221253
}
254+
222255
if (format === "feed") {
223-
let logsData = [];
224256
const userList = await getUsersListFromLogs(allLogs);
225257
const taskIdList = await getTasksFromLogs(allLogs);
226258
const usersMap = mapify(userList, "id");
227259
const tasksMap = mapify(taskIdList, "id");
228-
logsData = allLogs.map((data) => {
260+
261+
const logsData = allLogs.map((data) => {
229262
const formattedLogs = formatLogsForFeed(data, usersMap, tasksMap);
230263
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+
};
232269
});
270+
233271
return {
234272
allLogs: logsData.filter((log) => log),
235273
prev: prevDoc.empty ? null : prevDoc.docs[0].id,

test/integration/logs.test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,67 @@ describe("/logs", function () {
201201
return done();
202202
});
203203
});
204+
205+
it("should return logs filtered by username, startDate, and endDate when dev flag is enabled", function (done) {
206+
const username = "joygupta";
207+
const startDate = 1729841400000;
208+
const endDate = 1729841500000;
209+
chai
210+
.request(app)
211+
.get(`/logs?username=${username}&startDate=${startDate}&endDate=${endDate}&dev=true`)
212+
.set("cookie", `${cookieName}=${superUserToken}`)
213+
.end(function (err, res) {
214+
if (err) {
215+
return done(err);
216+
}
217+
expect(res).to.have.status(200);
218+
expect(res.body.message).to.equal("All Logs fetched successfully");
219+
220+
expect(res.body.data).to.be.an("array");
221+
res.body.data.forEach((log) => {
222+
expect(log).to.have.property("meta");
223+
expect(log).to.have.property("body");
224+
expect(log.meta).to.have.property("userId");
225+
const timestamp = log.timestamp._seconds * 1000;
226+
expect(timestamp).to.be.at.least(startDate);
227+
expect(timestamp).to.be.at.most(endDate);
228+
});
229+
230+
return done();
231+
});
232+
});
233+
234+
it("should return an error if startDate is greater than endDate", function (done) {
235+
const username = "joygupta";
236+
const startDate = 1729841500000;
237+
const endDate = 1729841400000;
238+
239+
chai
240+
.request(app)
241+
.get(`/logs?username=${username}&startDate=${startDate}&endDate=${endDate}&dev=true`)
242+
.set("cookie", `${cookieName}=${superUserToken}`)
243+
.end(function (_err, res) {
244+
expect(res).to.have.status(400);
245+
expect(res.body.error).to.equal("Start date cannot be greater than end date.");
246+
return done();
247+
});
248+
});
249+
250+
it("should return an empty array if no logs match username and date range", function (done) {
251+
const username = "nonexistentUser";
252+
const startDate = 1729841400000;
253+
const endDate = 1729841500000;
254+
255+
chai
256+
.request(app)
257+
.get(`/logs?username=${username}&startDate=${startDate}&endDate=${endDate}&dev=true`)
258+
.set("cookie", `${cookieName}=${superUserToken}`)
259+
.end(function (_err, res) {
260+
expect(res).to.have.status(200);
261+
expect(res.body.message).to.equal("All Logs fetched successfully");
262+
return done();
263+
});
264+
});
204265
});
205266

206267
describe("Update logs", function () {

0 commit comments

Comments
 (0)