Skip to content

Commit

Permalink
Fix: Handle firestore 'in' operator limitation by chunking queries; u…
Browse files Browse the repository at this point in the history
…pdate field name from 'assigneeId' to 'assignee' (#2279)
  • Loading branch information
VinuB-Dev authored Dec 4, 2024
1 parent 74f8324 commit 46bc908
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
20 changes: 16 additions & 4 deletions models/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const {
} = TASK_STATUS;
const { OLD_ACTIVE, OLD_BLOCKED, OLD_PENDING, OLD_COMPLETED } = TASK_STATUS_OLD;
const { INTERNAL_SERVER_ERROR } = require("../constants/errorMessages");
const { BATCH_SIZE_IN_CLAUSE } = require("../constants/firebase");

/**
* Update multiple tasks' status to DONE in one batch operation.
Expand Down Expand Up @@ -750,11 +751,22 @@ const fetchIncompleteTasksByUserIds = async (userIds) => {
return [];
}
try {
const incompleteTasksQuery = await tasksModel.where("assigneeId", "in", userIds).get();
const userIdChunks = [];

const incompleteTaskForUsers = incompleteTasksQuery.docs.filter(
(task) => !COMPLETED_STATUSES.includes(task.data().status)
);
for (let i = 0; i < userIds.length; i += BATCH_SIZE_IN_CLAUSE) {
userIdChunks.push(userIds.slice(i, i + BATCH_SIZE_IN_CLAUSE));
}

const promises = userIdChunks.map(async (userIdChunk) => {
const querySnapshot = await tasksModel.where("assignee", "in", userIdChunk).get();
return querySnapshot.docs.map((doc) => doc.data());
});

const snapshots = await Promise.all(promises);

const incompleteTasks = snapshots.flat();

const incompleteTaskForUsers = incompleteTasks.filter((task) => !COMPLETED_STATUSES.includes(task.status));

return incompleteTaskForUsers;
} catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions services/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ const fetchOrphanedTasks = async () => {

const userIds = userSnapshot.docs.map((doc) => doc.id);

const orphanedTasksQuerySnapshot = await tasksQuery.fetchIncompleteTasksByUserIds(userIds);
const orphanedTasksData = await tasksQuery.fetchIncompleteTasksByUserIds(userIds);

if (orphanedTasksQuerySnapshot.empty) {
if (orphanedTasksData.empty) {
return [];
}

const orphanedTasks = orphanedTasksQuerySnapshot.map((doc) => doc.data());
const orphanedTasks = orphanedTasksData;

return orphanedTasks;
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion services/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const getUsersWithIncompleteTasks = async (users) => {
return [];
}

const userIdsWithIncompleteTasks = new Set(abandonedTasksQuerySnapshot.map((doc) => doc.data().assigneeId));
const userIdsWithIncompleteTasks = new Set(abandonedTasksQuerySnapshot.map((doc) => doc.assignee));

const eligibleUsersWithTasks = users.filter((user) => userIdsWithIncompleteTasks.has(user.id));

Expand Down
12 changes: 4 additions & 8 deletions test/fixtures/abandoned-tasks/departed-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ const tasksData = [
updatedAt: 1727027999,
startedOn: 1727027777,
endsOn: 1731542400,
assignee: "archived_user1",
assigneeId: "user1_id",
assignee: "user1_id",
github: {
issue: {
html_url: "https://github.com/org/repo/issues/1",
Expand All @@ -97,8 +96,7 @@ const tasksData = [
updatedAt: 1727027999,
startedOn: 1727027777,
endsOn: 1731542400,
assignee: "archived_user2",
assigneeId: "user2_id",
assignee: "user2_id",
github: {
issue: {
html_url: "https://github.com/org/repo/issues/2",
Expand All @@ -118,8 +116,7 @@ const tasksData = [
updatedAt: 1727027999,
startedOn: 1727027777,
endsOn: 1731542400,
assignee: "archived_user1",
assigneeId: "user1_id",
assignee: "user1_id",
github: {
issue: {
html_url: "https://github.com/org/repo/issues/3",
Expand All @@ -139,8 +136,7 @@ const tasksData = [
updatedAt: 1727027999,
startedOn: 1727027777,
endsOn: 1731542400,
assignee: "active_user",
assigneeId: "user3_id",
assignee: "user3_id",
github: {
issue: {
html_url: "https://github.com/org/repo/issues/4",
Expand Down

0 comments on commit 46bc908

Please sign in to comment.