Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new paginated functionality added in GET /tasks #647

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion controllers/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 : [],
Expand Down
12 changes: 10 additions & 2 deletions models/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,17 @@ const updateTask = async (taskData, taskId = null) => {
*
* @return {Promise<tasks|Array>}
*/
const fetchTasks = async () => {
const fetchTasks = async (limit, offset) => {
let lastDoc;
if (offset) {
lastDoc = await tasksModel.doc(offset).get();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Offset should be a number, but here, it seems to be used as a document_id.

lastDoc = lastDoc.data();
}
const limitDocuments = Number(limit);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we request any number of documents from Firestore? Should there be an upper limit here?


try {
const tasksSnapshot = await tasksModel.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);
Expand Down
51 changes: 51 additions & 0 deletions test/integration/tasks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,57 @@ describe("Tasks", function () {
expect(taskWithParticipants.assignee).to.equal(appOwner.username);
}

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");
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.tasks).to.be.a("array");
const taskWithParticipants = res.body.tasks;
expect(taskWithParticipants[0]).not.to.equal(offset);
return chaiRequester.close();
});
return done();
});
ivinayakg marked this conversation as resolved.
Show resolved Hide resolved
});
ivinayakg marked this conversation as resolved.
Show resolved Hide resolved
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();
});
});
Expand Down