⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix(api): date the next occurrence from the rule it claims
olozano-739e8e05ded6
diff --git a/apps/api/src/task/controllers/duplicate-task.ts b/apps/api/src/task/controllers/duplicate-task.tsindex 83401dc72..645c74a26 100644--- a/apps/api/src/task/controllers/duplicate-task.ts+++ b/apps/api/src/task/controllers/duplicate-task.ts@@ -20,6 +20,7 @@ import {assertAssignableUser,filterAssignableUsers,} from "../../utils/assert-assignable-user";+import { nextOccurrenceDates } from "../recurrence/next-occurrence-date";import { taskIsCompleted } from "../task-is-completed";import {assertRequiredCustomFields,@@ -127,12 +128,11 @@ type DuplicateTaskOptions = {title?: string;};-// The next occurrence of a recurring task: a copy in a new status and dates that-// takes over the completed task's recurrence rule.+// The next occurrence of a recurring task: a copy in a new status that takes+// over the completed task's recurrence rule, with its dates moved one step on.type Occurrence = {status: string;- startDate: Date | null;- dueDate: Date | null;+ completedAt: Date;};// Rolls back an occurrence whose rule another transaction already moved.@@ -303,11 +303,20 @@ async function duplicateTask({// Moving the rule off the completed task is the claim. The row lock makes// a concurrent claim wait and then see no rule, so each completion- // creates one next task.+ // creates one next task. Dates come from the same locked row, so an edit+ // to the rule or due date cannot pair one with the other's old value.let recurrence: (typeof taskTable.$inferSelect)["recurrence"] = null;+ let dates = {+ startDate: sourceTask.startDate,+ dueDate: sourceTask.dueDate,+ };if (occurrence) {const [claimed] = await tx- .select({ recurrence: taskTable.recurrence })+ .select({+ startDate: taskTable.startDate,+ dueDate: taskTable.dueDate,+ recurrence: taskTable.recurrence,+ }).from(taskTable).where(and(@@ -317,8 +326,13 @@ async function duplicateTask({),).for("update");- if (!claimed) throw new RuleAlreadyMovedError();+ if (!claimed?.recurrence) throw new RuleAlreadyMovedError();recurrence = claimed.recurrence;+ dates = nextOccurrenceDates(+ claimed,+ claimed.recurrence,+ occurrence.completedAt,+ );await tx.update(taskTable).set({ recurrence: null })@@ -341,8 +355,7 @@ async function duplicateTask({title: title?.trim() || sourceTask.title,status,columnId: column?.id ?? null,- startDate: occurrence ? occurrence.startDate : sourceTask.startDate,- dueDate: occurrence ? occurrence.dueDate : sourceTask.dueDate,+ ...dates,recurrence,description,priority: sourceTask.priority,diff --git a/apps/api/src/task/recurrence/create-next-occurrence.ts b/apps/api/src/task/recurrence/create-next-occurrence.tsindex b70e2bd89..cfd9cdf47 100644--- a/apps/api/src/task/recurrence/create-next-occurrence.ts+++ b/apps/api/src/task/recurrence/create-next-occurrence.ts@@ -4,7 +4,6 @@ import { columnTable, taskTable } from "../../database/schema";import { publishEvent } from "../../events";import duplicateTask from "../controllers/duplicate-task";import { taskIsCompleted } from "../task-is-completed";-import { nextOccurrenceDates } from "./next-occurrence-date";// Creates the next task of a recurring series once the task sits in a final// column. The copy lands in the project's first open column, with its dates@@ -14,12 +13,7 @@ export async function createNextOccurrence(currentUserId: string | null | undefined,) {const [task] = await db- .select({- projectId: taskTable.projectId,- startDate: taskTable.startDate,- dueDate: taskTable.dueDate,- recurrence: taskTable.recurrence,- })+ .select({ projectId: taskTable.projectId }).from(taskTable).where(and(@@ -29,7 +23,7 @@ export async function createNextOccurrence(),).limit(1);- if (!task?.recurrence) return null;+ if (!task) return null;const [openColumn] = await db.select({ slug: columnTable.slug })@@ -48,10 +42,7 @@ export async function createNextOccurrence(taskId,currentUserId: currentUserId ?? "",canUpdateTasks: true,- occurrence: {- status: openColumn.slug,- ...nextOccurrenceDates(task, task.recurrence, new Date()),- },+ occurrence: { status: openColumn.slug, completedAt: new Date() },});// Clients refetched the completed task on its status change, before its rule// moved, so they still show it as repeating.diff --git a/tests/api-integration/task-recurrence.test.ts b/tests/api-integration/task-recurrence.test.tsindex 983cc781c..fb4cd7b11 100644--- a/tests/api-integration/task-recurrence.test.ts+++ b/tests/api-integration/task-recurrence.test.ts@@ -1,4 +1,4 @@-import { and, eq, ne } from "drizzle-orm";+import { and, eq, ne, sql } from "drizzle-orm";import { beforeEach, describe, expect, it, vi } from "vite-plus/test";import db, { schema } from "../../apps/api/src/database";import { subscribeToEvent } from "../../apps/api/src/events";@@ -322,6 +322,49 @@ describe("API integration: recurring tasks", () => {expect(await otherTasks(project.id, task.id)).toHaveLength(1);});+ it("dates the next occurrence from the rule it claims", async () => {+ const { project, columns } = await seedProject();+ const task = await seedTask(project.id, columns.done.id, {+ status: "done",+ });+ const daily = { ...weekly, frequency: "daily" } as const;++ // An edit holds the row while the occurrence reads the old rule and waits+ // for the lock.+ let commitEdit = () => {};+ const editCommitted = new Promise<void>((resolve) => {+ commitEdit = resolve;+ });+ let editLocked = () => {};+ const locked = new Promise<void>((resolve) => {+ editLocked = resolve;+ });+ const edit = db.transaction(async (tx) => {+ await tx+ .update(schema.taskTable)+ .set({ recurrence: daily })+ .where(eq(schema.taskTable.id, task.id));+ editLocked();+ await editCommitted;+ });+ await locked;+ const occurrence = createNextOccurrence(task.id, null);+ 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 claim is not waiting yet");+ });+ commitEdit();+ await edit;++ await expect(occurrence).resolves.toMatchObject({+ startDate: new Date("2026-03-10T00:00:00.000Z"),+ dueDate: new Date("2026-03-11T00:00:00.000Z"),+ recurrence: daily,+ });+ });+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, {