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): reject legacy updates superseded by atomic reorders

Tinb42096b0f16e
diff --git a/apps/api/src/task/controllers/update-task.ts b/apps/api/src/task/controllers/update-task.ts
index ccc7e7708..48278edb3 100644
--- a/apps/api/src/task/controllers/update-task.ts
+++ b/apps/api/src/task/controllers/update-task.ts
@@ -1,7 +1,7 @@
import { and, eq, getTableColumns, sql } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
-import { columnTable, taskTable } from "../../database/schema";
+import { columnTable, projectTable, taskTable } from "../../database/schema";
import { publishEvent } from "../../events";
import {
publishTaskMutation,
@@ -40,6 +40,7 @@ async function updateTask(
description:
description === undefined ? sql<null>`null` : taskTable.description,
status: taskTable.status,
+ position: taskTable.position,
projectId: taskTable.projectId,
})
.from(taskTable)
@@ -76,7 +77,16 @@ async function updateTask(
),
});
+ const initialPosition = existingTask.position;
+ const initialStatus = existingTask.status;
const updatedTask = await db.transaction(async (tx) => {
+ const [project] = await tx
+ .select({ id: projectTable.id })
+ .from(projectTable)
+ .where(eq(projectTable.id, projectId))
+ .for("update");
+ if (!project)
+ throw new HTTPException(404, { message: "Project not found" });
const [locked] = await tx
.select({
id: taskTable.id,
@@ -87,6 +97,7 @@ async function updateTask(
description:
description === undefined ? sql<null>`null` : taskTable.description,
status: taskTable.status,
+ position: taskTable.position,
projectId: taskTable.projectId,
})
.from(taskTable)
@@ -96,6 +107,10 @@ async function updateTask(
throw new HTTPException(409, {
message: "Task changed projects; retry the update",
});
+ if (locked.position !== initialPosition || locked.status !== initialStatus)
+ throw new HTTPException(409, {
+ message: "Task order changed; refresh before updating",
+ });
existingTask = locked;
const [task] = await tx
diff --git a/tests/api-integration/task-reorder.test.ts b/tests/api-integration/task-reorder.test.ts
index 0bed1a798..bc73f1519 100644
--- a/tests/api-integration/task-reorder.test.ts
+++ b/tests/api-integration/task-reorder.test.ts
@@ -1,6 +1,6 @@
import { eq } from "drizzle-orm";
import { beforeEach, describe, 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 reorderTasks from "../../apps/api/src/task/controllers/reorder-tasks";
import { resetTestDatabase } from "./helpers/database";
import {
@@ -157,3 +157,53 @@ it("rejects a second drag from a stale column snapshot without duplicating posit
expect(new Set(current.map((task) => task.position)).size).toBe(4);
expect(publish).not.toHaveBeenCalled();
});
+
+it("rejects a legacy full update superseded by an atomic reorder", async () => {
+ const { default: updateTask } =
+ await import("../../apps/api/src/task/controllers/update-task");
+ const { user, workspace } = await createWorkspaceMember();
+ const { project } = await createProjectFixture({ workspaceId: workspace.id });
+ const [task] = await db
+ .insert(schema.taskTable)
+ .values({
+ projectId: project.id,
+ title: "card",
+ status: "to-do",
+ position: 0,
+ priority: "low",
+ })
+ .returning();
+ const transaction = getDatabase().transaction.bind(getDatabase());
+ const intercepted = vi
+ .spyOn(getDatabase(), "transaction")
+ .mockImplementationOnce(async (apply, config) => {
+ await reorderTasks(project.id, [{ id: task.id, position: 2 }], user.id);
+ return transaction(apply, config);
+ });
+ try {
+ await expect(
+ updateTask(
+ task.id,
+ task.title,
+ task.status,
+ undefined,
+ undefined,
+ project.id,
+ undefined,
+ task.priority,
+ 1,
+ undefined,
+ user.id,
+ ),
+ ).rejects.toMatchObject({ status: 409 });
+ expect(
+ (
+ await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, task.id),
+ })
+ )?.position,
+ ).toBe(2);
+ } finally {
+ intercepted.mockRestore();
+ }
+});