diff --git a/models/logs.js b/models/logs.js index 4c8b2b1cf..a12b9ab7c 100644 --- a/models/logs.js +++ b/models/logs.js @@ -184,8 +184,8 @@ const fetchAllLogs = async (query) => { } if (isDev && (startDate || endDate)) { - startDate = startDate ? parseInt(startDate) : null; - endDate = endDate ? parseInt(endDate) : null; + startDate = startDate ? parseInt(startDate, 10) * 1000 : null; + endDate = endDate ? parseInt(endDate, 10) * 1000 : null; if (startDate && endDate && startDate > endDate) { const error = new Error("Start date cannot be greater than end date."); @@ -193,19 +193,12 @@ const fetchAllLogs = async (query) => { throw error; } - const buildTimestamp = (milliseconds) => ({ - _seconds: Math.floor(milliseconds / 1000), - _nanoseconds: (milliseconds % 1000) * 1000000, - }); - if (startDate) { - const startTimestamp = buildTimestamp(startDate); - requestQuery = requestQuery.where("timestamp._seconds", ">=", startTimestamp._seconds); + requestQuery = requestQuery.where("timestamp", ">=", admin.firestore.Timestamp.fromMillis(startDate)); } if (endDate) { - const endTimestamp = buildTimestamp(endDate); - requestQuery = requestQuery.where("timestamp._seconds", "<=", endTimestamp._seconds); + requestQuery = requestQuery.where("timestamp", "<=", admin.firestore.Timestamp.fromMillis(endDate)); } } @@ -255,7 +248,6 @@ const fetchAllLogs = async (query) => { page: page ? page + 1 : null, }; } - if (format === "feed") { const userList = await getUsersListFromLogs(allLogs); const taskIdList = await getTasksFromLogs(allLogs); diff --git a/test/integration/logs.test.js b/test/integration/logs.test.js index ef9dfc88a..fe2b22b77 100644 --- a/test/integration/logs.test.js +++ b/test/integration/logs.test.js @@ -204,8 +204,8 @@ describe("/logs", function () { it("should return logs filtered by username, startDate, and endDate when dev flag is enabled", function (done) { const username = "joygupta"; - const startDate = 1729841400000; - const endDate = 1729841500000; + const startDate = 1729841400; + const endDate = 1729841500; chai .request(app) .get(`/logs?username=${username}&startDate=${startDate}&endDate=${endDate}&dev=true`) @@ -249,8 +249,8 @@ describe("/logs", function () { it("should return an empty array if no logs match username and date range", function (done) { const username = "nonexistentUser"; - const startDate = 1729841400000; - const endDate = 1729841500000; + const startDate = 1729841400; + const endDate = 1729841500; chai .request(app) diff --git a/test/unit/models/logs.test.js b/test/unit/models/logs.test.js index 68b319fea..bde51a63e 100644 --- a/test/unit/models/logs.test.js +++ b/test/unit/models/logs.test.js @@ -201,8 +201,8 @@ describe("Logs", function () { it("Should throw error when start date is greater than end date in dev mode", async function () { await cleanDb(); - const startDate = Date.now(); - const endDate = startDate - 86400000; + const startDate = Math.floor(Date.now() / 1000); + const endDate = startDate - 86400; try { await logsQuery.fetchAllLogs({ @@ -222,9 +222,8 @@ describe("Logs", function () { it("Should return logs within the specified date range in dev mode", async function () { await cleanDb(); - const endDate = Date.now(); - const startDate = endDate - 86400000 * 7; - + const endDate = Math.floor(Date.now() / 1000); + const startDate = endDate - 86400 * 7; const result = await logsQuery.fetchAllLogs({ dev: "true", startDate: startDate.toString(), @@ -235,14 +234,16 @@ describe("Logs", function () { expect(result).to.have.property("allLogs"); if (result.allLogs.length > 0) { result.allLogs.forEach((log) => { - expect(log).to.have.property("timestamp"); + expect(log).to.have.property("timestamp").that.is.a("number"); + expect(log.timestamp).to.be.at.least(startDate); + expect(log.timestamp).to.be.at.most(endDate); }); } }); it("Should ignore date filters when not in dev mode", async function () { - const endDate = Date.now(); - const startDate = endDate - 86400000 * 7; + const endDate = Math.floor(Date.now() / 1000); + const startDate = endDate - 86400 * 7; const result = await logsQuery.fetchAllLogs({ dev: "false", @@ -258,7 +259,7 @@ describe("Logs", function () { }); it("Should handle only start date filter in dev mode", async function () { - const startDate = Date.now() - 86400000 * 14; + const startDate = Math.floor(Date.now() / 1000) - 86400 * 14; const result = await logsQuery.fetchAllLogs({ dev: "true", @@ -269,10 +270,17 @@ describe("Logs", function () { expect(result).to.have.property("allLogs"); expect(result).to.have.property("prev"); expect(result).to.have.property("next"); + + if (result.allLogs.length > 0) { + result.allLogs.forEach((log) => { + expect(log).to.have.property("timestamp").that.is.a("number"); + expect(log.timestamp).to.be.at.least(startDate); + }); + } }); it("Should handle only end date filter in dev mode", async function () { - const endDate = Date.now(); + const endDate = Math.floor(Date.now() / 1000); const result = await logsQuery.fetchAllLogs({ dev: "true", @@ -283,6 +291,13 @@ describe("Logs", function () { expect(result).to.have.property("allLogs"); expect(result).to.have.property("prev"); expect(result).to.have.property("next"); + + if (result.allLogs.length > 0) { + result.allLogs.forEach((log) => { + expect(log).to.have.property("timestamp").that.is.a("number"); + expect(log.timestamp).to.be.at.most(endDate); + }); + } }); it("Should return null if no logs are presnet the logs for specific types", async function () {