Skip to content

Commit 2f51843

Browse files
authored
Merge pull request #2375 from Real-Dev-Squad/develop
Dev to Main sync
2 parents 4336a62 + 41e2ce9 commit 2f51843

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

models/logs.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,28 +184,21 @@ const fetchAllLogs = async (query) => {
184184
}
185185

186186
if (isDev && (startDate || endDate)) {
187-
startDate = startDate ? parseInt(startDate) : null;
188-
endDate = endDate ? parseInt(endDate) : null;
187+
startDate = startDate ? parseInt(startDate, 10) * 1000 : null;
188+
endDate = endDate ? parseInt(endDate, 10) * 1000 : null;
189189

190190
if (startDate && endDate && startDate > endDate) {
191191
const error = new Error("Start date cannot be greater than end date.");
192192
error.statusCode = 400;
193193
throw error;
194194
}
195195

196-
const buildTimestamp = (milliseconds) => ({
197-
_seconds: Math.floor(milliseconds / 1000),
198-
_nanoseconds: (milliseconds % 1000) * 1000000,
199-
});
200-
201196
if (startDate) {
202-
const startTimestamp = buildTimestamp(startDate);
203-
requestQuery = requestQuery.where("timestamp._seconds", ">=", startTimestamp._seconds);
197+
requestQuery = requestQuery.where("timestamp", ">=", admin.firestore.Timestamp.fromMillis(startDate));
204198
}
205199

206200
if (endDate) {
207-
const endTimestamp = buildTimestamp(endDate);
208-
requestQuery = requestQuery.where("timestamp._seconds", "<=", endTimestamp._seconds);
201+
requestQuery = requestQuery.where("timestamp", "<=", admin.firestore.Timestamp.fromMillis(endDate));
209202
}
210203
}
211204

@@ -255,7 +248,6 @@ const fetchAllLogs = async (query) => {
255248
page: page ? page + 1 : null,
256249
};
257250
}
258-
259251
if (format === "feed") {
260252
const userList = await getUsersListFromLogs(allLogs);
261253
const taskIdList = await getTasksFromLogs(allLogs);

test/integration/logs.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ describe("/logs", function () {
204204

205205
it("should return logs filtered by username, startDate, and endDate when dev flag is enabled", function (done) {
206206
const username = "joygupta";
207-
const startDate = 1729841400000;
208-
const endDate = 1729841500000;
207+
const startDate = 1729841400;
208+
const endDate = 1729841500;
209209
chai
210210
.request(app)
211211
.get(`/logs?username=${username}&startDate=${startDate}&endDate=${endDate}&dev=true`)
@@ -249,8 +249,8 @@ describe("/logs", function () {
249249

250250
it("should return an empty array if no logs match username and date range", function (done) {
251251
const username = "nonexistentUser";
252-
const startDate = 1729841400000;
253-
const endDate = 1729841500000;
252+
const startDate = 1729841400;
253+
const endDate = 1729841500;
254254

255255
chai
256256
.request(app)

test/unit/models/logs.test.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ describe("Logs", function () {
201201
it("Should throw error when start date is greater than end date in dev mode", async function () {
202202
await cleanDb();
203203

204-
const startDate = Date.now();
205-
const endDate = startDate - 86400000;
204+
const startDate = Math.floor(Date.now() / 1000);
205+
const endDate = startDate - 86400;
206206

207207
try {
208208
await logsQuery.fetchAllLogs({
@@ -222,9 +222,8 @@ describe("Logs", function () {
222222
it("Should return logs within the specified date range in dev mode", async function () {
223223
await cleanDb();
224224

225-
const endDate = Date.now();
226-
const startDate = endDate - 86400000 * 7;
227-
225+
const endDate = Math.floor(Date.now() / 1000);
226+
const startDate = endDate - 86400 * 7;
228227
const result = await logsQuery.fetchAllLogs({
229228
dev: "true",
230229
startDate: startDate.toString(),
@@ -235,14 +234,16 @@ describe("Logs", function () {
235234
expect(result).to.have.property("allLogs");
236235
if (result.allLogs.length > 0) {
237236
result.allLogs.forEach((log) => {
238-
expect(log).to.have.property("timestamp");
237+
expect(log).to.have.property("timestamp").that.is.a("number");
238+
expect(log.timestamp).to.be.at.least(startDate);
239+
expect(log.timestamp).to.be.at.most(endDate);
239240
});
240241
}
241242
});
242243

243244
it("Should ignore date filters when not in dev mode", async function () {
244-
const endDate = Date.now();
245-
const startDate = endDate - 86400000 * 7;
245+
const endDate = Math.floor(Date.now() / 1000);
246+
const startDate = endDate - 86400 * 7;
246247

247248
const result = await logsQuery.fetchAllLogs({
248249
dev: "false",
@@ -258,7 +259,7 @@ describe("Logs", function () {
258259
});
259260

260261
it("Should handle only start date filter in dev mode", async function () {
261-
const startDate = Date.now() - 86400000 * 14;
262+
const startDate = Math.floor(Date.now() / 1000) - 86400 * 14;
262263

263264
const result = await logsQuery.fetchAllLogs({
264265
dev: "true",
@@ -269,10 +270,17 @@ describe("Logs", function () {
269270
expect(result).to.have.property("allLogs");
270271
expect(result).to.have.property("prev");
271272
expect(result).to.have.property("next");
273+
274+
if (result.allLogs.length > 0) {
275+
result.allLogs.forEach((log) => {
276+
expect(log).to.have.property("timestamp").that.is.a("number");
277+
expect(log.timestamp).to.be.at.least(startDate);
278+
});
279+
}
272280
});
273281

274282
it("Should handle only end date filter in dev mode", async function () {
275-
const endDate = Date.now();
283+
const endDate = Math.floor(Date.now() / 1000);
276284

277285
const result = await logsQuery.fetchAllLogs({
278286
dev: "true",
@@ -283,6 +291,13 @@ describe("Logs", function () {
283291
expect(result).to.have.property("allLogs");
284292
expect(result).to.have.property("prev");
285293
expect(result).to.have.property("next");
294+
295+
if (result.allLogs.length > 0) {
296+
result.allLogs.forEach((log) => {
297+
expect(log).to.have.property("timestamp").that.is.a("number");
298+
expect(log.timestamp).to.be.at.most(endDate);
299+
});
300+
}
286301
});
287302

288303
it("Should return null if no logs are presnet the logs for specific types", async function () {

0 commit comments

Comments
 (0)