Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(tasks): lock bulk status snapshots and preserve refresh events

Tin5a05383473fd
diff --git a/apps/api/src/task/controllers/bulk-update-tasks.ts b/apps/api/src/task/controllers/bulk-update-tasks.ts
index 571313608..3f827b30c 100644
--- a/apps/api/src/task/controllers/bulk-update-tasks.ts
+++ b/apps/api/src/task/controllers/bulk-update-tasks.ts
@@ -1,4 +1,4 @@
-import { and, eq, getTableColumns, inArray, sql } from "drizzle-orm";
+import { and, asc, eq, inArray } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
import {
@@ -116,8 +116,21 @@ async function bulkUpdateTasks({
});
destinations.set(projectId, column?.id ?? null);
}
- const updatedTasks = await db.transaction(async (tx) => {
- const result: (typeof taskTable.$inferSelect)[] = [];
+ const { updatedTasks, beforeById } = await db.transaction(async (tx) => {
+ const before = await tx
+ .select({ id: taskTable.id, status: taskTable.status })
+ .from(taskTable)
+ .where(inArray(taskTable.id, foundIds))
+ .orderBy(asc(taskTable.id))
+ .for("update");
+ const beforeById = new Map(before.map((task) => [task.id, task]));
+ const result: Array<{
+ id: string;
+ projectId: string;
+ status: string;
+ title: string;
+ userId: string | null;
+ }> = [];
for (const projectId of projectIds) {
const projectTaskIds = tasks
.filter((task) => task.projectId === projectId)
@@ -135,8 +148,11 @@ async function bulkUpdateTasks({
),
)
.returning({
- ...getTableColumns(taskTable),
- description: sql<null>`null`,
+ id: taskTable.id,
+ projectId: taskTable.projectId,
+ status: taskTable.status,
+ title: taskTable.title,
+ userId: taskTable.userId,
});
if (changed.length !== projectTaskIds.length)
throw new HTTPException(409, {
@@ -144,7 +160,7 @@ async function bulkUpdateTasks({
});
result.push(...changed);
}
- return result;
+ return { updatedTasks: result, beforeById };
});
updatedCount = updatedTasks.length;
const parentProjects = await getSubtaskParentProjects(foundIds);
@@ -152,19 +168,18 @@ async function bulkUpdateTasks({
projects: parentProjects,
});
for (const updatedTask of updatedTasks) {
- const before = tasks.find((task) => task.id === updatedTask.id)!;
- if (before.status !== updatedTask.status)
- await publishEvent("task.status_changed", {
- taskId: updatedTask.id,
- projectId: updatedTask.projectId,
- userId,
- oldStatus: before.status,
- newStatus: updatedTask.status,
- title: updatedTask.title,
- assigneeId: updatedTask.userId,
- type: "status_changed",
- skipSubtaskParentRefresh: true,
- });
+ const before = beforeById.get(updatedTask.id)!;
+ await publishEvent("task.status_changed", {
+ taskId: updatedTask.id,
+ projectId: updatedTask.projectId,
+ userId,
+ oldStatus: before.status,
+ newStatus: updatedTask.status,
+ title: updatedTask.title,
+ assigneeId: updatedTask.userId,
+ type: "status_changed",
+ skipSubtaskParentRefresh: true,
+ });
}
for (const projectId of projectIds)
await publishEvent("task-relation.refresh", { projectId, userId });
diff --git a/apps/api/src/task/index.ts b/apps/api/src/task/index.ts
index 53c2e04e9..c96540e0e 100644
--- a/apps/api/src/task/index.ts
+++ b/apps/api/src/task/index.ts
@@ -144,6 +144,7 @@ const bulkUpdateTasksRoute = createRoute({
"No workspace access, or missing the permission the operation needs",
),
404: errorResponse("No tasks found"),
+ 409: errorResponse("Tasks changed projects; retry the operation"),
},
});
diff --git a/apps/docs/openapi.json b/apps/docs/openapi.json
index b263bb917..c6b61c790 100644
--- a/apps/docs/openapi.json
+++ b/apps/docs/openapi.json
@@ -8546,6 +8546,16 @@
}
}
}
+ },
+ "409": {
+ "description": "Tasks changed projects; retry the operation",
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
+ }
+ }
+ }
}
}
}
diff --git a/tests/api-integration/bulk-task-atomic-status.test.ts b/tests/api-integration/bulk-task-atomic-status.test.ts
index 62f606e84..eb8f47008 100644
--- a/tests/api-integration/bulk-task-atomic-status.test.ts
+++ b/tests/api-integration/bulk-task-atomic-status.test.ts
@@ -1,6 +1,6 @@
import { eq } from "drizzle-orm";
import { beforeEach, expect, it, vi } from "vite-plus/test";
-import db, { schema } from "../../apps/api/src/database";
+import db, { getDatabase, schema } from "../../apps/api/src/database";
import bulkUpdateTasks from "../../apps/api/src/task/controllers/bulk-update-tasks";
import { resetTestDatabase } from "./helpers/database";
import {
@@ -12,6 +12,7 @@ vi.mock("../../apps/api/src/events", () => ({ publishEvent: publish }));
beforeEach(async () => {
await resetTestDatabase();
vi.clearAllMocks();
+ publish.mockResolvedValue(undefined);
});
it("validates every destination before committing any project", async () => {
const { user, workspace } = await createWorkspaceMember();
@@ -40,3 +41,75 @@ it("validates every destination before committing any project", async () => {
).toEqual(["to-do", "to-do"]);
expect(publish).not.toHaveBeenCalled();
});
+
+it("commits every project before publishing successful status changes", async () => {
+ const { user, workspace } = await createWorkspaceMember();
+ const a = await createProjectFixture({ workspaceId: workspace.id });
+ const b = await createProjectFixture({ workspaceId: workspace.id });
+ const tasks = await db
+ .insert(schema.taskTable)
+ .values([
+ { projectId: a.project.id, title: "a", status: "to-do" },
+ { projectId: b.project.id, title: "b", status: "to-do" },
+ ])
+ .returning();
+ publish.mockImplementation(async () => {
+ expect(
+ (
+ await db
+ .select({ status: schema.taskTable.status })
+ .from(schema.taskTable)
+ ).map((task) => task.status),
+ ).toEqual(["in-progress", "in-progress"]);
+ });
+ expect(
+ await bulkUpdateTasks({
+ taskIds: tasks.map((task) => task.id),
+ operation: "updateStatus",
+ value: "in-progress",
+ userId: user.id,
+ }),
+ ).toMatchObject({ updatedCount: 2 });
+ for (const task of tasks)
+ expect(publish).toHaveBeenCalledWith(
+ "task.status_changed",
+ expect.objectContaining({
+ taskId: task.id,
+ oldStatus: "to-do",
+ newStatus: "in-progress",
+ }),
+ );
+});
+
+it("uses the locked prior status when a concurrent edit rewrites a no-op snapshot", async () => {
+ const { user, workspace } = await createWorkspaceMember();
+ const { project } = await createProjectFixture({ workspaceId: workspace.id });
+ const [task] = await db
+ .insert(schema.taskTable)
+ .values({ projectId: project.id, title: "concurrent", status: "to-do" })
+ .returning();
+ const transaction = db.transaction.bind(db);
+ vi.spyOn(getDatabase(), "transaction").mockImplementationOnce(
+ async (apply, config) => {
+ await db
+ .update(schema.taskTable)
+ .set({ status: "in-progress" })
+ .where(eq(schema.taskTable.id, task.id));
+ return transaction(apply, config);
+ },
+ );
+ await bulkUpdateTasks({
+ taskIds: [task.id],
+ operation: "updateStatus",
+ value: "to-do",
+ userId: user.id,
+ });
+ expect(publish).toHaveBeenCalledWith(
+ "task.status_changed",
+ expect.objectContaining({
+ taskId: task.id,
+ oldStatus: "in-progress",
+ newStatus: "to-do",
+ }),
+ );
+});