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

Fix: Firestore query limit issue for departed users and orphaned tasks APIs #2279

Merged
Merged
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
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
Loading