Skip to content

Commit ae22c79

Browse files
Update paginated tasks queries and refactor model code (#1734)
* fix: update the queries and refactor * test: fix test
1 parent 5c2653b commit ae22c79

File tree

2 files changed

+51
-59
lines changed

2 files changed

+51
-59
lines changed

models/tasks.js

Lines changed: 47 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,29 @@ const fetchPaginatedTasks = async ({
139139
try {
140140
let initialQuery = tasksModel;
141141

142+
if (assignee) {
143+
const assignees = assignee.split(",");
144+
const users = [];
145+
for (const singleAssignee of assignees) {
146+
const user = await userUtils.getUserId(singleAssignee);
147+
if (user) {
148+
users.push(user);
149+
}
150+
}
151+
152+
if (users.length > 1) {
153+
initialQuery = initialQuery.where("assignee", "in", users);
154+
} else if (users.length === 1) {
155+
initialQuery = initialQuery.where("assignee", "==", users[0]);
156+
} else {
157+
return {
158+
allTasks: [],
159+
next: "",
160+
prev: "",
161+
};
162+
}
163+
}
164+
142165
if (status === TASK_STATUS.OVERDUE) {
143166
const currentTime = Math.floor(Date.now() / 1000);
144167
const OVERDUE_TASK_STATUSES = [
@@ -150,58 +173,24 @@ const fetchPaginatedTasks = async ({
150173
BLOCKED,
151174
SANITY_CHECK,
152175
];
153-
initialQuery = tasksModel.where("endsOn", "<", currentTime).where("status", "in", OVERDUE_TASK_STATUSES);
154-
155-
if (assignee) {
156-
const user = await userUtils.getUserId(assignee);
157-
if (user) {
158-
initialQuery = initialQuery.where("assignee", "==", user);
159-
}
160-
}
161-
} else {
162-
initialQuery = tasksModel.orderBy("updatedAt", "desc");
163-
if (status) {
164-
initialQuery = initialQuery.where("status", "==", status);
165-
}
166-
167-
if (assignee) {
168-
const assignees = assignee.split(",");
169-
if (assignees.length > 1) {
170-
const users = [];
171-
for (const singleAssignee of assignees) {
172-
const user = await userUtils.getUserId(singleAssignee);
173-
if (user) {
174-
users.push(user);
175-
}
176-
}
177-
if (users.length) {
178-
initialQuery = initialQuery.where("assignee", "in", users);
179-
} else {
180-
return {
181-
allTasks: [],
182-
next: "",
183-
prev: "",
184-
};
185-
}
186-
} else {
187-
const user = await userUtils.getUserId(assignees[0]);
188-
if (user) {
189-
initialQuery = initialQuery.where("assignee", "==", user);
190-
} else {
191-
return {
192-
allTasks: [],
193-
next: "",
194-
prev: "",
195-
};
196-
}
197-
}
198-
}
176+
initialQuery = initialQuery
177+
.where("endsOn", "<", currentTime)
178+
.where("status", "in", OVERDUE_TASK_STATUSES)
179+
.orderBy("endsOn")
180+
.orderBy("status");
181+
} else if (status) {
182+
initialQuery = initialQuery.where("status", "==", status);
183+
}
199184

200-
if (title) {
201-
initialQuery = initialQuery.where("title", ">=", title).where("title", "<=", title + "\uf8ff");
202-
}
185+
if (title) {
186+
initialQuery = initialQuery
187+
.where("title", ">=", title)
188+
.where("title", "<=", title + "\uf8ff")
189+
.orderBy("title", "asc");
203190
}
204191

192+
initialQuery = initialQuery.orderBy("updatedAt", "desc");
193+
205194
let queryDoc = initialQuery;
206195

207196
if (prev) {
@@ -222,19 +211,21 @@ const fetchPaginatedTasks = async ({
222211
}
223212

224213
const snapshot = await queryDoc.get();
214+
let nextDoc, prevDoc;
215+
if (snapshot.size) {
216+
const first = snapshot.docs[0];
217+
prevDoc = await initialQuery.endBefore(first).limitToLast(1).get();
225218

226-
const first = snapshot.docs[0];
227-
const prevDoc = await initialQuery.endBefore(first).limitToLast(1).get();
228-
229-
const last = snapshot.docs[snapshot.docs.length - 1];
230-
const nextDoc = await initialQuery.startAfter(last).limit(1).get();
219+
const last = snapshot.docs[snapshot.docs.length - 1];
220+
nextDoc = await initialQuery.startAfter(last).limit(1).get();
221+
}
231222

232223
const allTasks = await getBuiltTasks(snapshot);
233224

234225
return {
235226
allTasks,
236-
next: nextDoc.docs[0]?.id ?? "",
237-
prev: prevDoc.docs[0]?.id ?? "",
227+
next: nextDoc?.docs[0]?.id ?? "",
228+
prev: prevDoc?.docs[0]?.id ?? "",
238229
};
239230
} catch (err) {
240231
logger.error("Error retrieving user data", err);

test/integration/tasks.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,10 @@ describe("Tasks", function () {
184184
});
185185

186186
describe("GET /tasks", function () {
187+
let taskId2, taskId3;
187188
before(async function () {
188-
await tasks.updateTask({ ...taskData[0], createdAt: 1621717694, updatedAt: 1700680830 });
189-
await tasks.updateTask({ ...taskData[1], createdAt: 1621717694, updatedAt: 1700775753 });
189+
taskId2 = (await tasks.updateTask({ ...taskData[0], createdAt: 1621717694, updatedAt: 1700680830 })).taskId;
190+
taskId3 = (await tasks.updateTask({ ...taskData[1], createdAt: 1621717694, updatedAt: 1700775753 })).taskId;
190191
});
191192

192193
it("Should get all the list of tasks", function (done) {
@@ -315,7 +316,7 @@ describe("Tasks", function () {
315316
}
316317

317318
expect(res).to.have.status(200);
318-
expect(res.body.tasks[0].id).to.be.oneOf([taskId, taskId1]);
319+
expect(res.body.tasks[0].id).to.be.oneOf([taskId, taskId1, taskId2, taskId3]);
319320
return done();
320321
});
321322
});

0 commit comments

Comments
 (0)