From 48a68eee69e81fd6941867059e24b12647560b59 Mon Sep 17 00:00:00 2001 From: Vinayak Date: Sun, 31 Jul 2022 22:59:00 +0530 Subject: [PATCH 1/4] new paginated functionality added in GET /tasks --- controllers/tasks.js | 4 +++- models/tasks.js | 22 ++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/controllers/tasks.js b/controllers/tasks.js index eb89c261f..f375f8193 100644 --- a/controllers/tasks.js +++ b/controllers/tasks.js @@ -35,8 +35,10 @@ const addNewTask = async (req, res) => { * @param res {Object} - Express response object */ const fetchTasks = async (req, res) => { + const { limit, offset } = req.query; + try { - const allTasks = await tasks.fetchTasks(); + const allTasks = await tasks.fetchTasks(limit, offset); return res.json({ message: "Tasks returned successfully!", tasks: allTasks.length > 0 ? allTasks : [], diff --git a/models/tasks.js b/models/tasks.js index ad2daf82c..df85ddc8e 100644 --- a/models/tasks.js +++ b/models/tasks.js @@ -44,9 +44,27 @@ const updateTask = async (taskData, taskId = null) => { * * @return {Promise} */ -const fetchTasks = async () => { +const fetchTasks = async (limit, offset) => { + let lastDoc; + + //if offset given + if (offset) { + lastDoc = await tasksModel.doc(offset).get(); + lastDoc = lastDoc.data(); + } + + // default case for limit + if (!limit) { + limit = 20; + } + limit = Number(limit); + try { - const tasksSnapshot = await tasksModel.get(); + const tasksSnapshot = await tasksModel + .orderBy("startedOn", "desc") + .startAfter(lastDoc ? lastDoc.startedOn : "") + .limit(limit) + .get(); const tasks = buildTasks(tasksSnapshot); const promises = tasks.map(async (task) => fromFirestoreData(task)); const updatedTasks = await Promise.all(promises); From 4d6a947e6f196bd27171f40a5eb37233fb21c260 Mon Sep 17 00:00:00 2001 From: Vinayak Date: Mon, 1 Aug 2022 15:58:28 +0530 Subject: [PATCH 2/4] tests are written and working perfectly --- models/tasks.js | 2 +- test/integration/tasks.test.js | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/models/tasks.js b/models/tasks.js index df85ddc8e..5c1ee7769 100644 --- a/models/tasks.js +++ b/models/tasks.js @@ -47,7 +47,7 @@ const updateTask = async (taskData, taskId = null) => { const fetchTasks = async (limit, offset) => { let lastDoc; - //if offset given + // if offset given if (offset) { lastDoc = await tasksModel.doc(offset).get(); lastDoc = lastDoc.data(); diff --git a/test/integration/tasks.test.js b/test/integration/tasks.test.js index 4be8c99af..83006e35d 100644 --- a/test/integration/tasks.test.js +++ b/test/integration/tasks.test.js @@ -138,6 +138,46 @@ describe("Tasks", function () { return done(); }); }); + it("Should get all the list of tasks by paginated", function (done) { + const limit = 1; + const chaiRequester = chai.request(app).keepOpen(); + chaiRequester + .get("/tasks") + .query({ limit }) + .end((err, res) => { + if (err) { + return done(err); + } + + expect(res).to.have.status(200); + expect(res.body).to.be.a("object"); + expect(res.body.tasks).to.be.a("array"); + expect(res.body.tasks.length).to.equal(limit); + const offset = { ...res.body.tasks[0] }; + + chaiRequester + .get("/tasks") + .query({ offset: offset.id }) + .end((err, res) => { + if (err) { + return done(err); + } + expect(res).to.have.status(200); + expect(res.body).to.be.a("object"); + expect(res.body.tasks).to.be.a("array"); + const taskWithParticipants = res.body.tasks; + taskWithParticipants.forEach((task) => { + if (task.type === "group") { + expect(task.participants).to.include(appOwner.username); + } else { + expect(task.assignee).to.equal(appOwner.username); + } + }); + return chaiRequester.close(); + }); + return done(); + }); + }); }); describe("GET /tasks/self", function () { From c26fc5a11430d0a9bd2f644d19a5b0d1eb4fd3bd Mon Sep 17 00:00:00 2001 From: Vinayak Date: Tue, 2 Aug 2022 13:19:59 +0530 Subject: [PATCH 3/4] edge cases handled better in modal, new test cases aded. --- models/tasks.js | 16 +++------------- test/integration/tasks.test.js | 29 ++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/models/tasks.js b/models/tasks.js index 5c1ee7769..f31754bca 100644 --- a/models/tasks.js +++ b/models/tasks.js @@ -46,25 +46,15 @@ const updateTask = async (taskData, taskId = null) => { */ const fetchTasks = async (limit, offset) => { let lastDoc; - - // if offset given if (offset) { lastDoc = await tasksModel.doc(offset).get(); lastDoc = lastDoc.data(); } - - // default case for limit - if (!limit) { - limit = 20; - } - limit = Number(limit); + let limitDocuments = Number(limit); try { - const tasksSnapshot = await tasksModel - .orderBy("startedOn", "desc") - .startAfter(lastDoc ? lastDoc.startedOn : "") - .limit(limit) - .get(); + const tasksSnapshotQuery = tasksModel.orderBy("startedOn", "desc").startAfter(lastDoc ? lastDoc.startedOn : ""); + const tasksSnapshot = limit ? await tasksSnapshotQuery.limit(limitDocuments).get() : await tasksSnapshotQuery.get(); const tasks = buildTasks(tasksSnapshot); const promises = tasks.map(async (task) => fromFirestoreData(task)); const updatedTasks = await Promise.all(promises); diff --git a/test/integration/tasks.test.js b/test/integration/tasks.test.js index 83006e35d..52988bd97 100644 --- a/test/integration/tasks.test.js +++ b/test/integration/tasks.test.js @@ -152,7 +152,6 @@ describe("Tasks", function () { expect(res).to.have.status(200); expect(res.body).to.be.a("object"); expect(res.body.tasks).to.be.a("array"); - expect(res.body.tasks.length).to.equal(limit); const offset = { ...res.body.tasks[0] }; chaiRequester @@ -163,21 +162,33 @@ describe("Tasks", function () { return done(err); } expect(res).to.have.status(200); - expect(res.body).to.be.a("object"); expect(res.body.tasks).to.be.a("array"); const taskWithParticipants = res.body.tasks; - taskWithParticipants.forEach((task) => { - if (task.type === "group") { - expect(task.participants).to.include(appOwner.username); - } else { - expect(task.assignee).to.equal(appOwner.username); - } - }); + expect(taskWithParticipants[0]).not.to.equal(offset); return chaiRequester.close(); }); return done(); }); }); + it("Should get all the list of tasks limited in number by the 'limit' passed in query", function (done) { + const limit = 1; + const chaiRequester = chai.request(app); + chaiRequester + .get("/tasks") + .query({ limit }) + .end((err, res) => { + if (err) { + return done(err); + } + + expect(res).to.have.status(200); + expect(res.body).to.be.a("object"); + expect(res.body.tasks).to.be.a("array"); + expect(res.body.tasks.length).to.equal(limit); + + return done(); + }); + }); }); describe("GET /tasks/self", function () { From 3c739b7fde1b34b953cfa86a03b8acee6447a789 Mon Sep 17 00:00:00 2001 From: Vinayak Date: Tue, 2 Aug 2022 13:20:24 +0530 Subject: [PATCH 4/4] minor lint fix --- models/tasks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/tasks.js b/models/tasks.js index f31754bca..e4f6d4146 100644 --- a/models/tasks.js +++ b/models/tasks.js @@ -50,7 +50,7 @@ const fetchTasks = async (limit, offset) => { lastDoc = await tasksModel.doc(offset).get(); lastDoc = lastDoc.data(); } - let limitDocuments = Number(limit); + const limitDocuments = Number(limit); try { const tasksSnapshotQuery = tasksModel.orderBy("startedOn", "desc").startAfter(lastDoc ? lastDoc.startedOn : "");