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(api): keep completed tasks from starting a second series

olozano-df901b72db62
diff --git a/apps/api/src/task/controllers/create-task.ts b/apps/api/src/task/controllers/create-task.ts
index 5ffca8920..f2d51888a 100644
--- a/apps/api/src/task/controllers/create-task.ts
+++ b/apps/api/src/task/controllers/create-task.ts
@@ -133,6 +133,11 @@ async function createTask({
eq(columnTable.slug, resolvedStatus),
),
});
+ // A rule would never fire on a task that starts out completed.
+ if (recurrence && column?.isFinal)
+ throw new HTTPException(400, {
+ message: "Create the task in an open column to make it repeat",
+ });
const createdTask = await db.transaction(async (tx) => {
const taskNumber = await claimTaskNumber(projectId, tx);
diff --git a/apps/api/src/task/controllers/update-task-recurrence.ts b/apps/api/src/task/controllers/update-task-recurrence.ts
index 4b850629b..bb1267f85 100644
--- a/apps/api/src/task/controllers/update-task-recurrence.ts
+++ b/apps/api/src/task/controllers/update-task-recurrence.ts
@@ -1,9 +1,10 @@
-import { eq } from "drizzle-orm";
+import { and, eq, isNotNull, not, or } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
import { taskTable } from "../../database/schema";
import { publishEvent } from "../../events";
import type { TaskRecurrence } from "../recurrence/schema";
+import { taskIsCompleted } from "../task-is-completed";
async function updateTaskRecurrence({
id,
@@ -14,13 +15,31 @@ async function updateTaskRecurrence({
recurrence: TaskRecurrence | null;
currentUserId: string;
}) {
+ // A completed task can only edit the rule it has yet to hand over. Setting
+ // one after its next task took the rule would start a second series, so the
+ // check runs in the update and rechecks the row once a claim releases it.
const [task] = await db
.update(taskTable)
.set({ recurrence })
- .where(eq(taskTable.id, id))
+ .where(
+ and(
+ eq(taskTable.id, id),
+ recurrence
+ ? or(not(taskIsCompleted), isNotNull(taskTable.recurrence))
+ : undefined,
+ ),
+ )
.returning();
if (!task) {
+ const exists = await db.query.taskTable.findFirst({
+ columns: { id: true },
+ where: eq(taskTable.id, id),
+ });
+ if (exists)
+ throw new HTTPException(409, {
+ message: "Reopen the task to make it repeat",
+ });
throw new HTTPException(404, { message: "Task not found" });
}
diff --git a/apps/api/src/task/index.ts b/apps/api/src/task/index.ts
index 7878258e5..1c2d8a49a 100644
--- a/apps/api/src/task/index.ts
+++ b/apps/api/src/task/index.ts
@@ -301,7 +301,9 @@ const createTaskRoute = createRoute({
},
responses: {
200: jsonResponse("The created task", taskSchema),
- 400: errorResponse("Invalid body, or unknown project"),
+ 400: errorResponse(
+ "Invalid body, unknown project, or a repeating task in a final column",
+ ),
403: errorResponse(
"No workspace access, or missing task:create permission",
),
@@ -638,7 +640,7 @@ const updateTaskRecurrenceRoute = createRoute({
tags: ["Tasks"],
summary: "Update task recurrence",
description:
- "Make a task repeat, or stop it repeating. When a repeating task moves into a final column, the next task is created in the project's first open column with its dates moved forward one interval from the completed task's due date, and the rule moves to that new task.",
+ "Make a task repeat, or stop it repeating. When a repeating task moves into a final column, the next task is created in the project's first open column with its dates moved forward one interval from the completed task's due date, and the rule moves to that new task. A completed task cannot start repeating: reopen it, or set the rule on its next task.",
middleware: [
workspaceAccess.fromTask(),
requireWorkspacePermission({ task: ["update"] }),
@@ -658,6 +660,9 @@ const updateTaskRecurrenceRoute = createRoute({
"No workspace access, or missing task:update permission",
),
404: errorResponse("Task not found"),
+ 409: errorResponse(
+ "The task is completed and no longer holds a rule; reopen it first",
+ ),
},
});
diff --git a/apps/docs/openapi.json b/apps/docs/openapi.json
index 07095e0e7..e03149153 100644
--- a/apps/docs/openapi.json
+++ b/apps/docs/openapi.json
@@ -9811,7 +9811,7 @@
}
},
"400": {
- "description": "Invalid body, or unknown project",
+ "description": "Invalid body, unknown project, or a repeating task in a final column",
"content": {
"text/plain": {
"schema": {
@@ -10865,7 +10865,7 @@
"Tasks"
],
"summary": "Update task recurrence",
- "description": "Make a task repeat, or stop it repeating. When a repeating task moves into a final column, the next task is created in the project's first open column with its dates moved forward one interval from the completed task's due date, and the rule moves to that new task.",
+ "description": "Make a task repeat, or stop it repeating. When a repeating task moves into a final column, the next task is created in the project's first open column with its dates moved forward one interval from the completed task's due date, and the rule moves to that new task. A completed task cannot start repeating: reopen it, or set the rule on its next task.",
"parameters": [
{
"schema": {
@@ -10944,6 +10944,16 @@
}
}
}
+ },
+ "409": {
+ "description": "The task is completed and no longer holds a rule; reopen it first",
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
+ }
+ }
+ }
}
}
}
diff --git a/tests/api-integration/task-recurrence.test.ts b/tests/api-integration/task-recurrence.test.ts
index fb4cd7b11..5d87046d5 100644
--- a/tests/api-integration/task-recurrence.test.ts
+++ b/tests/api-integration/task-recurrence.test.ts
@@ -365,6 +365,73 @@ describe("API integration: recurring tasks", () => {
});
});
+ it("only lets a completed task edit the rule it still holds", async () => {
+ const { project, columns, app } = await seedProject();
+ const task = await seedTask(project.id, columns.done.id, {
+ status: "done",
+ });
+ const monthly = { ...weekly, frequency: "monthly" } as const;
+ const setRule = (recurrence: unknown) =>
+ request(app, "PUT", `/api/task/recurrence/${task.id}`, { recurrence });
+
+ // The rule has not moved yet, so the next task will take the edit.
+ expect((await setRule(monthly)).status).toBe(200);
+
+ // Another request claims the rule while this edit waits for the row.
+ let commitClaim = () => {};
+ const claimCommitted = new Promise<void>((resolve) => {
+ commitClaim = resolve;
+ });
+ let claimLocked = () => {};
+ const locked = new Promise<void>((resolve) => {
+ claimLocked = resolve;
+ });
+ const claim = db.transaction(async (tx) => {
+ await tx
+ .update(schema.taskTable)
+ .set({ recurrence: null })
+ .where(eq(schema.taskTable.id, task.id));
+ claimLocked();
+ await claimCommitted;
+ });
+ await locked;
+ const edit = setRule(weekly);
+ await vi.waitFor(async () => {
+ const { rows } = await db.execute(
+ sql`select 1 from pg_stat_activity where wait_event_type = 'Lock'`,
+ );
+ if (!rows.length) throw new Error("The edit is not waiting yet");
+ });
+ commitClaim();
+ await claim;
+
+ expect((await edit).status).toBe(409);
+ expect((await setRule(null)).status).toBe(200);
+ const stored = await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, task.id),
+ });
+ expect(stored?.recurrence).toBeNull();
+ });
+
+ it("does not create a task in a final column that repeats", async () => {
+ const { project, app } = await seedProject();
+
+ const created = await request(app, "POST", `/api/task/${project.id}`, {
+ title: "Water the plants",
+ description: "",
+ priority: "no-priority",
+ status: "done",
+ recurrence: weekly,
+ });
+
+ expect(created.status).toBe(400);
+ expect(
+ await db.query.taskTable.findMany({
+ where: eq(schema.taskTable.projectId, project.id),
+ }),
+ ).toHaveLength(0);
+ });
+
it("tells clients that the completed task no longer repeats", async () => {
const { project, columns } = await seedProject();
const task = await seedTask(project.id, columns.done.id, {