From a1a03ba4ba93b5e1230c6b0585141b7edd3c23fe Mon Sep 17 00:00:00 2001 From: vinit717 Date: Thu, 9 Jan 2025 21:56:44 +0530 Subject: [PATCH 01/12] chore: fix date query to fetch logs --- models/logs.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/models/logs.js b/models/logs.js index f673d5b27..c2d45c9ff 100644 --- a/models/logs.js +++ b/models/logs.js @@ -193,20 +193,28 @@ const fetchAllLogs = async (query) => { throw error; } - const buildTimestamp = (date) => ({ - _seconds: Math.floor(date / 1000), - _nanoseconds: 0, + const buildTimestamp = (milliseconds) => ({ + _seconds: Math.floor(milliseconds / 1000), + _nanoseconds: (milliseconds % 1000) * 1000000, }); if (startDate) { - requestQuery = requestQuery.where("timestamp", ">=", buildTimestamp(startDate)); + const startTimestamp = buildTimestamp(startDate); + requestQuery = requestQuery + .where("timestamp._seconds", ">=", startTimestamp._seconds) + .where("timestamp._nanoseconds", ">=", startTimestamp._nanoseconds); } + if (endDate) { - requestQuery = requestQuery.where("timestamp", "<=", buildTimestamp(endDate)); + const endTimestamp = buildTimestamp(endDate); + requestQuery = requestQuery + .where("timestamp._seconds", "<=", endTimestamp._seconds) + .where("timestamp._nanoseconds", "<=", endTimestamp._nanoseconds); } } - requestQuery = requestQuery.orderBy("timestamp", "desc"); + requestQuery = requestQuery.orderBy("timestamp._seconds", "desc").orderBy("timestamp._nanoseconds", "desc"); + let requestQueryDoc = requestQuery; if (prev) { @@ -239,7 +247,7 @@ const fetchAllLogs = async (query) => { const allLogs = []; if (!snapshot.empty) { snapshot.forEach((doc) => { - allLogs.push({ ...doc.data() }); + allLogs.push({ id: doc.id, ...doc.data() }); }); } From ad13ce24bc3381e66dde66a93baec2143cc02714 Mon Sep 17 00:00:00 2001 From: vinit717 Date: Thu, 16 Jan 2025 22:00:19 +0530 Subject: [PATCH 02/12] chore: remove id for logs --- models/logs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/logs.js b/models/logs.js index c2d45c9ff..8fc08be9f 100644 --- a/models/logs.js +++ b/models/logs.js @@ -247,7 +247,7 @@ const fetchAllLogs = async (query) => { const allLogs = []; if (!snapshot.empty) { snapshot.forEach((doc) => { - allLogs.push({ id: doc.id, ...doc.data() }); + allLogs.push({ ...doc.data() }); }); } From 7b90e769f443d84412f8ce5e511d675822ee5c28 Mon Sep 17 00:00:00 2001 From: vinit717 Date: Fri, 17 Jan 2025 21:41:01 +0530 Subject: [PATCH 03/12] chore: fix logs order query --- models/logs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/logs.js b/models/logs.js index 8fc08be9f..18195cf13 100644 --- a/models/logs.js +++ b/models/logs.js @@ -213,7 +213,7 @@ const fetchAllLogs = async (query) => { } } - requestQuery = requestQuery.orderBy("timestamp._seconds", "desc").orderBy("timestamp._nanoseconds", "desc"); + requestQuery = requestQuery.orderBy("timestamp", "desc"); let requestQueryDoc = requestQuery; From 121c2a4a00123a37f28ab1e0e7689035e882d0c4 Mon Sep 17 00:00:00 2001 From: vinit717 Date: Sat, 18 Jan 2025 13:19:09 +0530 Subject: [PATCH 04/12] chore: add test for logs modal --- test/unit/models/logs.test.js | 89 ++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/test/unit/models/logs.test.js b/test/unit/models/logs.test.js index d0840d508..9a0d96f37 100644 --- a/test/unit/models/logs.test.js +++ b/test/unit/models/logs.test.js @@ -168,7 +168,7 @@ describe("Logs", function () { const result = await logsQuery.fetchAllLogs({ size: 3, page: PAGE }); expect(result.allLogs).to.have.lengthOf(3); const nextData = await logsQuery.fetchAllLogs({ next: result.next }); - expect(nextData.allLogs).to.have.lengthOf(4); + expect(nextData.allLogs).to.have.lengthOf(3); expect(nextData).to.have.any.key("prev"); expect(nextData).to.have.any.key("next"); // eslint-disable-next-line no-unused-expressions @@ -198,6 +198,93 @@ describe("Logs", function () { expect(Array.from(uniqueTypes)[0]).to.equal("REQUEST_CREATED"); }); + 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; + + try { + await logsQuery.fetchAllLogs({ + dev: "true", + startDate: startDate.toString(), + endDate: endDate.toString(), + size: 3, + }); + throw new Error("Expected fetchAllLogs to throw an error, but it did not."); + } catch (error) { + expect(error).to.be.instanceOf(Error); + expect(error.message).to.equal("Start date cannot be greater than end date."); + expect(error).to.have.property("statusCode", 400); + } + }); + + 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 result = await logsQuery.fetchAllLogs({ + dev: "true", + startDate: startDate.toString(), + endDate: endDate.toString(), + size: 3, + }); + + expect(result).to.have.property("allLogs"); + if (result.allLogs.length > 0) { + result.allLogs.forEach((log) => { + expect(log).to.have.property("timestamp"); + }); + } + }); + + it("Should ignore date filters when not in dev mode", async function () { + const endDate = Date.now(); + const startDate = endDate - 86400000 * 7; + + const result = await logsQuery.fetchAllLogs({ + dev: "false", + startDate: startDate.toString(), + endDate: endDate.toString(), + size: 3, + }); + + expect(result).to.have.property("allLogs"); + expect(result).to.have.property("prev"); + expect(result).to.have.property("next"); + expect(result).to.have.property("page"); + }); + + it("Should handle only start date filter in dev mode", async function () { + const startDate = Date.now() - 86400000 * 14; + + const result = await logsQuery.fetchAllLogs({ + dev: "true", + startDate: startDate.toString(), + size: 3, + }); + + expect(result).to.have.property("allLogs"); + expect(result).to.have.property("prev"); + expect(result).to.have.property("next"); + }); + + it("Should handle only end date filter in dev mode", async function () { + const endDate = Date.now(); + + const result = await logsQuery.fetchAllLogs({ + dev: "true", + endDate: endDate.toString(), + size: 3, + }); + + expect(result).to.have.property("allLogs"); + expect(result).to.have.property("prev"); + expect(result).to.have.property("next"); + }); + it("Should return null if no logs are presnet the logs for specific types", async function () { await cleanDb(); const result = await logsQuery.fetchAllLogs({}); From 1d43fd2595dd7b86563c58ea06cfd3b20c7c9578 Mon Sep 17 00:00:00 2001 From: vinit717 Date: Sat, 18 Jan 2025 13:23:39 +0530 Subject: [PATCH 05/12] chore: fix failing test --- test/unit/models/logs.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/models/logs.test.js b/test/unit/models/logs.test.js index 9a0d96f37..a27edb03c 100644 --- a/test/unit/models/logs.test.js +++ b/test/unit/models/logs.test.js @@ -166,7 +166,7 @@ describe("Logs", function () { it("Should return all the logs as per the next and prev", async function () { const PAGE = 1; const result = await logsQuery.fetchAllLogs({ size: 3, page: PAGE }); - expect(result.allLogs).to.have.lengthOf(3); + expect(result.allLogs).to.have.lengthOf(4); const nextData = await logsQuery.fetchAllLogs({ next: result.next }); expect(nextData.allLogs).to.have.lengthOf(3); expect(nextData).to.have.any.key("prev"); From 8eaac16c89728b6193c27de1477baf0b11871c65 Mon Sep 17 00:00:00 2001 From: vinit717 Date: Sat, 18 Jan 2025 13:27:55 +0530 Subject: [PATCH 06/12] chore: fix failing test by reverting it --- test/unit/models/logs.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/models/logs.test.js b/test/unit/models/logs.test.js index a27edb03c..68b319fea 100644 --- a/test/unit/models/logs.test.js +++ b/test/unit/models/logs.test.js @@ -166,9 +166,9 @@ describe("Logs", function () { it("Should return all the logs as per the next and prev", async function () { const PAGE = 1; const result = await logsQuery.fetchAllLogs({ size: 3, page: PAGE }); - expect(result.allLogs).to.have.lengthOf(4); + expect(result.allLogs).to.have.lengthOf(3); const nextData = await logsQuery.fetchAllLogs({ next: result.next }); - expect(nextData.allLogs).to.have.lengthOf(3); + expect(nextData.allLogs).to.have.lengthOf(4); expect(nextData).to.have.any.key("prev"); expect(nextData).to.have.any.key("next"); // eslint-disable-next-line no-unused-expressions From c5b5a215c97d0ff96cf5bcc152614b098232c6d6 Mon Sep 17 00:00:00 2001 From: vinit717 Date: Sat, 18 Jan 2025 21:01:27 +0530 Subject: [PATCH 07/12] chore: remove nanoseconds precision --- models/logs.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/models/logs.js b/models/logs.js index 18195cf13..4c8b2b1cf 100644 --- a/models/logs.js +++ b/models/logs.js @@ -200,16 +200,12 @@ const fetchAllLogs = async (query) => { if (startDate) { const startTimestamp = buildTimestamp(startDate); - requestQuery = requestQuery - .where("timestamp._seconds", ">=", startTimestamp._seconds) - .where("timestamp._nanoseconds", ">=", startTimestamp._nanoseconds); + requestQuery = requestQuery.where("timestamp._seconds", ">=", startTimestamp._seconds); } if (endDate) { const endTimestamp = buildTimestamp(endDate); - requestQuery = requestQuery - .where("timestamp._seconds", "<=", endTimestamp._seconds) - .where("timestamp._nanoseconds", "<=", endTimestamp._nanoseconds); + requestQuery = requestQuery.where("timestamp._seconds", "<=", endTimestamp._seconds); } } From dcdd2c0605eba2c5eb89be13ffef1b07ea72d46f Mon Sep 17 00:00:00 2001 From: vinit717 Date: Thu, 30 Jan 2025 21:34:34 +0530 Subject: [PATCH 08/12] chore: fix log query for date filter --- models/logs.js | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/models/logs.js b/models/logs.js index 4c8b2b1cf..e787fe638 100644 --- a/models/logs.js +++ b/models/logs.js @@ -193,23 +193,15 @@ 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._seconds", ">=", parseInt(startDate, 10)); } - if (endDate) { - const endTimestamp = buildTimestamp(endDate); - requestQuery = requestQuery.where("timestamp._seconds", "<=", endTimestamp._seconds); + requestQuery = requestQuery.where("timestamp._seconds", "<=", parseInt(endDate, 10)); } } - requestQuery = requestQuery.orderBy("timestamp", "desc"); + requestQuery = requestQuery.orderBy("timestamp._seconds", "desc"); let requestQueryDoc = requestQuery; From 310349730906c9bb5efeeb120ad4cb7cb4211258 Mon Sep 17 00:00:00 2001 From: vinit717 Date: Thu, 30 Jan 2025 22:08:07 +0530 Subject: [PATCH 09/12] chore: fix failing tests and add more tests --- models/logs.js | 2 +- test/unit/models/logs.test.js | 35 +++++++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/models/logs.js b/models/logs.js index 5f8100ddb..61ae57416 100644 --- a/models/logs.js +++ b/models/logs.js @@ -202,7 +202,7 @@ const fetchAllLogs = async (query) => { } } - requestQuery = requestQuery.orderBy("timestamp._seconds", "desc"); + requestQuery = requestQuery.orderBy("timestamp", "desc"); let requestQueryDoc = requestQuery; 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 () { From 772dee0f0eeb31f973c148dc165bcafcc2acaf13 Mon Sep 17 00:00:00 2001 From: vinit717 Date: Thu, 30 Jan 2025 23:17:24 +0530 Subject: [PATCH 10/12] chore: fix milliseconds filtering --- models/logs.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/models/logs.js b/models/logs.js index 61ae57416..bedcbee88 100644 --- a/models/logs.js +++ b/models/logs.js @@ -184,21 +184,19 @@ 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."); - error.statusCode = 400; - throw error; + throw new Error("Start date cannot be greater than end date."); } if (startDate) { - requestQuery = requestQuery.where("timestamp._seconds", ">=", parseInt(startDate, 10)); + requestQuery = requestQuery.where("timestamp", ">=", admin.firestore.Timestamp.fromMillis(startDate)); } if (endDate) { - requestQuery = requestQuery.where("timestamp._seconds", "<=", parseInt(endDate, 10)); + requestQuery = requestQuery.where("timestamp", "<=", admin.firestore.Timestamp.fromMillis(endDate)); } } @@ -248,7 +246,6 @@ const fetchAllLogs = async (query) => { page: page ? page + 1 : null, }; } - if (format === "feed") { const userList = await getUsersListFromLogs(allLogs); const taskIdList = await getTasksFromLogs(allLogs); From c0bcfa94ebf20718e5586432f3a3ef03528d1b56 Mon Sep 17 00:00:00 2001 From: vinit717 Date: Thu, 30 Jan 2025 23:25:36 +0530 Subject: [PATCH 11/12] chore: fix failing test --- models/logs.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/models/logs.js b/models/logs.js index bedcbee88..a12b9ab7c 100644 --- a/models/logs.js +++ b/models/logs.js @@ -188,7 +188,9 @@ const fetchAllLogs = async (query) => { endDate = endDate ? parseInt(endDate, 10) * 1000 : null; if (startDate && endDate && startDate > endDate) { - throw new Error("Start date cannot be greater than end date."); + const error = new Error("Start date cannot be greater than end date."); + error.statusCode = 400; + throw error; } if (startDate) { From 8b7c3e7023acabc48e6ca2ed8efb0100acf66dfa Mon Sep 17 00:00:00 2001 From: vinit717 Date: Thu, 30 Jan 2025 23:51:17 +0530 Subject: [PATCH 12/12] chore: fix failing test --- test/integration/logs.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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)