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): share mutation history and side effects

Tin12ace3b452e3
diff --git a/apps/api/src/task/controllers/bulk-update-tasks.ts b/apps/api/src/task/controllers/bulk-update-tasks.ts
index 571313608..45bb38688 100644
--- a/apps/api/src/task/controllers/bulk-update-tasks.ts
+++ b/apps/api/src/task/controllers/bulk-update-tasks.ts
@@ -6,7 +6,6 @@ import {
labelTable,
projectTable,
taskTable,
- userTable,
workspaceUserTable,
} from "../../database/schema";
import { publishEvent } from "../../events";
@@ -14,6 +13,10 @@ import { removeLabelFromGitea } from "../../plugins/gitea/utils/sync-label-to-gi
import { removeLabelFromGitHub } from "../../plugins/github/utils/sync-label-to-github";
import { removeLabelFromGitlab } from "../../plugins/gitlab/utils/sync-label-to-gitlab";
import { assertAssignableUser } from "../../utils/assert-assignable-user";
+import {
+ publishTaskMutation,
+ recordTaskMutation,
+} from "./task-mutation-effects";
import { getSubtaskParentProjects } from "../get-subtask-parent-projects";
import {
assertValidPriority,
@@ -153,21 +156,10 @@ async function bulkUpdateTasks({
});
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,
- });
+ await publishTaskMutation(before, updatedTask, userId, {
+ skipSubtaskParentRefresh: true,
+ });
}
- for (const projectId of projectIds)
- await publishEvent("task-relation.refresh", { projectId, userId });
break;
}
@@ -184,17 +176,16 @@ async function bulkUpdateTasks({
updatedCount = result.rowCount ?? foundIds.length;
- for (const task of tasks) {
- await publishEvent("task.priority_changed", {
- taskId: task.id,
- projectId: task.projectId,
+ for (const task of tasks)
+ await publishTaskMutation(
+ task,
+ {
+ ...task,
+ description: null,
+ priority: value,
+ },
userId,
- oldPriority: task.priority,
- newPriority: value,
- title: task.title,
- type: "priority_changed",
- });
- }
+ );
break;
}
@@ -205,38 +196,21 @@ async function bulkUpdateTasks({
await assertAssignableUser(assigneeId, workspaceId);
}
- const newAssigneeName = assigneeId
- ? (
- await db
- .select({ name: userTable.name })
- .from(userTable)
- .where(eq(userTable.id, assigneeId))
- .limit(1)
- )[0]?.name
- : undefined;
-
const result = await db
.update(taskTable)
.set({ userId: assigneeId })
.where(inArray(taskTable.id, foundIds));
-
updatedCount = result.rowCount ?? foundIds.length;
-
- for (const task of tasks) {
- const eventType = assigneeId
- ? "task.assignee_changed"
- : "task.unassigned";
- await publishEvent(eventType, {
- taskId: task.id,
- projectId: task.projectId,
+ for (const task of tasks)
+ await publishTaskMutation(
+ task,
+ {
+ ...task,
+ description: null,
+ userId: assigneeId,
+ },
userId,
- oldAssignee: task.userId,
- newAssignee: newAssigneeName,
- newAssigneeId: assigneeId,
- title: task.title,
- type: assigneeId ? "assignee_changed" : "unassigned",
- });
- }
+ );
break;
}
@@ -386,24 +360,26 @@ async function bulkUpdateTasks({
}
}
- const result = await db
- .update(taskTable)
- .set({ dueDate: parsedDate })
- .where(inArray(taskTable.id, foundIds));
-
- updatedCount = result.rowCount ?? foundIds.length;
-
- for (const task of tasks) {
- await publishEvent("task.due_date_changed", {
- taskId: task.id,
- projectId: task.projectId,
+ const updatedTasks = await db.transaction(async (tx) => {
+ const changed = await tx
+ .update(taskTable)
+ .set({ dueDate: parsedDate })
+ .where(inArray(taskTable.id, foundIds))
+ .returning({
+ ...getTableColumns(taskTable),
+ description: sql<null>`null`,
+ });
+ for (const task of tasks)
+ await recordTaskMutation(tx, task, { dueDate: parsedDate }, userId);
+ return changed;
+ });
+ updatedCount = updatedTasks.length;
+ for (const updatedTask of updatedTasks)
+ await publishTaskMutation(
+ tasks.find((task) => task.id === updatedTask.id)!,
+ updatedTask,
userId,
- oldDueDate: task.dueDate,
- newDueDate: parsedDate,
- title: task.title,
- type: "due_date_changed",
- });
- }
+ );
break;
}
diff --git a/apps/api/src/task/controllers/task-mutation-effects.ts b/apps/api/src/task/controllers/task-mutation-effects.ts
new file mode 100644
index 000000000..e8da5d963
--- /dev/null
+++ b/apps/api/src/task/controllers/task-mutation-effects.ts
@@ -0,0 +1,160 @@
+import { eq } from "drizzle-orm";
+import db from "../../database";
+import {
+ activityTable,
+ taskReminderSentTable,
+ taskTable,
+ userTable,
+} from "../../database/schema";
+import { publishEvent } from "../../events";
+import createNotification from "../../notification/controllers/create-notification";
+import { deleteOrphanedAssets } from "../../storage/cleanup-assets";
+import { parseMentionIds } from "../../utils/parse-mentions";
+
+type Task = typeof taskTable.$inferSelect;
+type Transaction = Parameters<Parameters<typeof db.transaction>[0]>[0];
+type Changes = Partial<
+ Pick<
+ Task,
+ "title" | "status" | "priority" | "description" | "userId" | "dueDate"
+ >
+>;
+export type TaskBefore = Pick<
+ Task,
+ "id" | "projectId" | "title" | "status" | "priority" | "userId" | "dueDate"
+> & { description?: string | null };
+
+export async function recordTaskMutation(
+ tx: Transaction,
+ before: TaskBefore,
+ changes: Changes,
+ userId?: string,
+) {
+ if (changes.title !== undefined && before.title !== changes.title) {
+ await tx.insert(activityTable).values({
+ taskId: before.id,
+ type: "title_changed",
+ userId: userId ?? null,
+ content: null,
+ eventData: { oldTitle: before.title, newTitle: changes.title },
+ });
+ }
+ if (
+ changes.dueDate !== undefined &&
+ before.dueDate?.getTime() !== changes.dueDate?.getTime()
+ ) {
+ await tx
+ .delete(taskReminderSentTable)
+ .where(eq(taskReminderSentTable.taskId, before.id));
+ }
+}
+
+export async function publishTaskMutation(
+ before: TaskBefore,
+ after: TaskBefore & { description: string | null },
+ userId?: string,
+ options?: { skipSubtaskParentRefresh?: boolean },
+) {
+ const common = {
+ taskId: after.id,
+ projectId: after.projectId,
+ userId,
+ title: after.title,
+ };
+ if (before.status !== after.status) {
+ await publishEvent("task.status_changed", {
+ ...common,
+ oldStatus: before.status,
+ newStatus: after.status,
+ assigneeId: after.userId,
+ type: "status_changed",
+ ...options,
+ });
+ await publishEvent("task-relation.refresh", {
+ projectId: after.projectId,
+ userId,
+ });
+ }
+ if (before.title !== after.title)
+ await publishEvent("task.title_changed", {
+ ...common,
+ oldTitle: before.title,
+ newTitle: after.title,
+ type: "title_changed",
+ });
+ if (before.priority !== after.priority)
+ await publishEvent("task.priority_changed", {
+ ...common,
+ oldPriority: before.priority,
+ newPriority: after.priority,
+ type: "priority_changed",
+ });
+ if (before.userId !== after.userId) {
+ const assignee = after.userId
+ ? (
+ await db
+ .select({ name: userTable.name })
+ .from(userTable)
+ .where(eq(userTable.id, after.userId))
+ .limit(1)
+ )[0]
+ : undefined;
+ await publishEvent(
+ after.userId ? "task.assignee_changed" : "task.unassigned",
+ {
+ ...common,
+ oldAssignee: before.userId,
+ newAssignee: assignee?.name,
+ newAssigneeId: after.userId,
+ type: after.userId ? "assignee_changed" : "unassigned",
+ },
+ );
+ }
+ if (before.dueDate?.getTime() !== after.dueDate?.getTime())
+ await publishEvent("task.due_date_changed", {
+ ...common,
+ oldDueDate: before.dueDate,
+ newDueDate: after.dueDate,
+ type: "due_date_changed",
+ });
+ if (
+ before.description !== undefined &&
+ before.description !== after.description
+ ) {
+ await publishEvent("task.description_changed", {
+ ...common,
+ oldDescription: before.description,
+ newDescription: after.description,
+ type: "description_changed",
+ });
+ deleteOrphanedAssets(before.description, after.description, {
+ taskId: after.id,
+ }).catch(() => {});
+ const oldMentions = new Set(parseMentionIds(before.description));
+ const newlyMentioned = parseMentionIds(after.description).filter(
+ (id) => id !== userId && !oldMentions.has(id),
+ );
+ if (newlyMentioned.length) {
+ const editor = userId
+ ? (
+ await db
+ .select({ name: userTable.name })
+ .from(userTable)
+ .where(eq(userTable.id, userId))
+ .limit(1)
+ )[0]
+ : undefined;
+ for (const mentionedId of newlyMentioned)
+ await createNotification({
+ userId: mentionedId,
+ type: "task_mention",
+ eventData: {
+ taskTitle: after.title,
+ mentionerName: editor?.name ?? null,
+ },
+ resourceId: after.id,
+ resourceType: "task",
+ });
+ }
+ }
+}
diff --git a/apps/api/src/task/controllers/update-task-assignee.ts b/apps/api/src/task/controllers/update-task-assignee.ts
index 11de30d59..d16a0b773 100644
--- a/apps/api/src/task/controllers/update-task-assignee.ts
+++ b/apps/api/src/task/controllers/update-task-assignee.ts
@@ -1,8 +1,8 @@
import { eq } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
-import { taskTable, userTable } from "../../database/schema";
-import { publishEvent } from "../../events";
+import { taskTable } from "../../database/schema";
+import { publishTaskMutation } from "./task-mutation-effects";
import {
assertAssignableUser,
getProjectWorkspaceId,
@@ -51,38 +51,7 @@ async function updateTaskAssignee({
});
}
- const newAssigneeName = nextAssigneeId
- ? (
- await db
- .select({ name: userTable.name })
- .from(userTable)
- .where(eq(userTable.id, nextAssigneeId))
- .limit(1)
- )[0]?.name
- : undefined;
-
- if (!nextAssigneeId) {
- await publishEvent("task.unassigned", {
- taskId: updatedTask.id,
- projectId: updatedTask.projectId,
- userId: currentUserId,
- title: updatedTask.title,
- type: "unassigned",
- });
-
- return updatedTask;
- }
-
- await publishEvent("task.assignee_changed", {
- taskId: updatedTask.id,
- projectId: updatedTask.projectId,
- userId: currentUserId,
- oldAssignee: existingTask.userId,
- newAssignee: newAssigneeName,
- newAssigneeId: nextAssigneeId,
- title: updatedTask.title,
- type: "assignee_changed",
- });
+ await publishTaskMutation(existingTask, updatedTask, currentUserId);
return updatedTask;
}
diff --git a/apps/api/src/task/controllers/update-task-description.ts b/apps/api/src/task/controllers/update-task-description.ts
index 6a232a9de..be62c6f11 100644
--- a/apps/api/src/task/controllers/update-task-description.ts
+++ b/apps/api/src/task/controllers/update-task-description.ts
@@ -1,11 +1,8 @@
import { eq } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
-import { taskTable, userTable } from "../../database/schema";
-import { publishEvent } from "../../events";
-import createNotification from "../../notification/controllers/create-notification";
-import { deleteOrphanedAssets } from "../../storage/cleanup-assets";
-import { parseMentionIds } from "../../utils/parse-mentions";
+import { taskTable } from "../../database/schema";
+import { publishTaskMutation } from "./task-mutation-effects";
async function updateTaskDescription({
id,
@@ -38,46 +35,7 @@ async function updateTaskDescription({
});
}
- await publishEvent("task.description_changed", {
- taskId: updatedTask.id,
- projectId: updatedTask.projectId,
- userId: currentUserId,
- oldDescription: existingTask.description,
- newDescription: description,
- type: "description_changed",
- });
-
- deleteOrphanedAssets(existingTask.description, description, {
- taskId: id,
- }).catch(() => {});
-
- // Notify members newly @mentioned by this edit (skip ones already mentioned
- // in the previous description, and the editor themselves).
- const alreadyMentioned = new Set(parseMentionIds(existingTask.description));
- const newlyMentioned = parseMentionIds(description).filter(
- (mentionedId) =>
- mentionedId !== currentUserId && !alreadyMentioned.has(mentionedId),
- );
-
- if (newlyMentioned.length > 0) {
- const [editor] = await db
- .select({ name: userTable.name })
- .from(userTable)
- .where(eq(userTable.id, currentUserId));
-
- for (const mentionedId of newlyMentioned) {
- await createNotification({
- userId: mentionedId,
- type: "task_mention",
- eventData: {
- taskTitle: updatedTask.title,
- mentionerName: editor?.name ?? null,
- },
- resourceId: updatedTask.id,
- resourceType: "task",
- });
- }
- }
+ await publishTaskMutation(existingTask, updatedTask, currentUserId);
return updatedTask;
}
diff --git a/apps/api/src/task/controllers/update-task-due-date.ts b/apps/api/src/task/controllers/update-task-due-date.ts
index de50436ac..9cbec0907 100644
--- a/apps/api/src/task/controllers/update-task-due-date.ts
+++ b/apps/api/src/task/controllers/update-task-due-date.ts
@@ -1,8 +1,11 @@
import { eq } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
-import { taskReminderSentTable, taskTable } from "../../database/schema";
-import { publishEvent } from "../../events";
+import { taskTable } from "../../database/schema";
+import {
+ publishTaskMutation,
+ recordTaskMutation,
+} from "./task-mutation-effects";
async function updateTaskDueDate({
id,
@@ -13,7 +16,7 @@ async function updateTaskDueDate({
dueDate: Date | null;
currentUserId: string;
}) {
- const existingTask = await db.query.taskTable.findFirst({
+ let existingTask = await db.query.taskTable.findFirst({
where: eq(taskTable.id, id),
});
@@ -23,16 +26,24 @@ async function updateTaskDueDate({
});
}
- // Clear sent reminders so new due date triggers fresh notifications
- await db
- .delete(taskReminderSentTable)
- .where(eq(taskReminderSentTable.taskId, id));
-
- const [updatedTask] = await db
- .update(taskTable)
- .set({ dueDate: dueDate || null })
- .where(eq(taskTable.id, id))
- .returning();
+ const updatedTask = await db.transaction(async (tx) => {
+ const [locked] = await tx
+ .select()
+ .from(taskTable)
+ .where(eq(taskTable.id, id))
+ .for("update");
+ if (!locked) throw new HTTPException(404, { message: "Task not found" });
+ existingTask = locked;
+
+ const [task] = await tx
+ .update(taskTable)
+ .set({ dueDate: dueDate || null })
+ .where(eq(taskTable.id, id))
+ .returning();
+ if (task)
+ await recordTaskMutation(tx, existingTask, { dueDate }, currentUserId);
+ return task;
+ });
if (!updatedTask) {
throw new HTTPException(500, {
@@ -40,15 +51,7 @@ async function updateTaskDueDate({
});
}
- await publishEvent("task.due_date_changed", {
- taskId: updatedTask.id,
- projectId: updatedTask.projectId,
- userId: currentUserId,
- oldDueDate: existingTask.dueDate,
- newDueDate: dueDate,
- title: updatedTask.title,
- type: "due_date_changed",
- });
+ await publishTaskMutation(existingTask, updatedTask, currentUserId);
return updatedTask;
}
diff --git a/apps/api/src/task/controllers/update-task-priority.ts b/apps/api/src/task/controllers/update-task-priority.ts
index ce63fd728..2250465e8 100644
--- a/apps/api/src/task/controllers/update-task-priority.ts
+++ b/apps/api/src/task/controllers/update-task-priority.ts
@@ -2,7 +2,7 @@ import { eq } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
import { taskTable } from "../../database/schema";
-import { publishEvent } from "../../events";
+import { publishTaskMutation } from "./task-mutation-effects";
async function updateTaskPriority({
id,
@@ -35,15 +35,7 @@ async function updateTaskPriority({
});
}
- await publishEvent("task.priority_changed", {
- taskId: updatedTask.id,
- projectId: updatedTask.projectId,
- userId: currentUserId,
- oldPriority: existingTask.priority,
- newPriority: priority,
- title: updatedTask.title,
- type: "priority_changed",
- });
+ await publishTaskMutation(existingTask, updatedTask, currentUserId);
return updatedTask;
}
diff --git a/apps/api/src/task/controllers/update-task-status.ts b/apps/api/src/task/controllers/update-task-status.ts
index 328e1866e..a2cf07e4c 100644
--- a/apps/api/src/task/controllers/update-task-status.ts
+++ b/apps/api/src/task/controllers/update-task-status.ts
@@ -2,7 +2,7 @@ import { and, eq } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
import { columnTable, taskTable } from "../../database/schema";
-import { publishEvent } from "../../events";
+import { publishTaskMutation } from "./task-mutation-effects";
import { assertValidTaskStatus } from "../validate-task-fields";
async function updateTaskStatus({
@@ -45,21 +45,7 @@ async function updateTaskStatus({
});
}
- await publishEvent("task.status_changed", {
- taskId: updatedTask.id,
- projectId: updatedTask.projectId,
- userId: currentUserId,
- oldStatus: existingTask.status,
- newStatus: status,
- title: updatedTask.title,
- assigneeId: updatedTask.userId,
- type: "status_changed",
- });
-
- await publishEvent("task-relation.refresh", {
- projectId: updatedTask.projectId,
- userId: currentUserId,
- });
+ await publishTaskMutation(existingTask, updatedTask, currentUserId);
return updatedTask;
}
diff --git a/apps/api/src/task/controllers/update-task-title.ts b/apps/api/src/task/controllers/update-task-title.ts
index e2e935633..f42835300 100644
--- a/apps/api/src/task/controllers/update-task-title.ts
+++ b/apps/api/src/task/controllers/update-task-title.ts
@@ -1,8 +1,11 @@
import { eq } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
-import { activityTable, taskTable } from "../../database/schema";
-import { publishEvent } from "../../events";
+import { taskTable } from "../../database/schema";
+import {
+ publishTaskMutation,
+ recordTaskMutation,
+} from "./task-mutation-effects";
async function updateTaskTitle({
id,
@@ -13,7 +16,7 @@ async function updateTaskTitle({
title: string;
currentUserId: string;
}) {
- const existingTask = await db.query.taskTable.findFirst({
+ let existingTask = await db.query.taskTable.findFirst({
where: eq(taskTable.id, id),
});
@@ -29,6 +32,14 @@ async function updateTaskTitle({
// history row atomically; event subscribers remain notifications/integrations
// only and cannot make the audit trail disappear.
const updatedTask = await db.transaction(async (tx) => {
+ const [locked] = await tx
+ .select()
+ .from(taskTable)
+ .where(eq(taskTable.id, id))
+ .for("update");
+ if (!locked) throw new HTTPException(404, { message: "Task not found" });
+ existingTask = locked;
+
const [task] = await tx
.update(taskTable)
.set({ title })
@@ -41,25 +52,12 @@ async function updateTaskTitle({
});
}
- await tx.insert(activityTable).values({
- taskId: task.id,
- type: "title_changed",
- userId: currentUserId,
- content: null,
- eventData: { oldTitle: existingTask.title, newTitle: title },
- });
+ await recordTaskMutation(tx, existingTask, { title }, currentUserId);
return task;
});
- await publishEvent("task.title_changed", {
- taskId: updatedTask.id,
- projectId: updatedTask.projectId,
- userId: currentUserId,
- oldTitle: existingTask.title,
- newTitle: title,
- type: "title_changed",
- });
+ await publishTaskMutation(existingTask, updatedTask, currentUserId);
return updatedTask;
}
diff --git a/apps/api/src/task/controllers/update-task.ts b/apps/api/src/task/controllers/update-task.ts
index a3faec9c3..ccc7e7708 100644
--- a/apps/api/src/task/controllers/update-task.ts
+++ b/apps/api/src/task/controllers/update-task.ts
@@ -3,7 +3,10 @@ import { HTTPException } from "hono/http-exception";
import db from "../../database";
import { columnTable, taskTable } from "../../database/schema";
import { publishEvent } from "../../events";
-import { deleteOrphanedAssets } from "../../storage/cleanup-assets";
+import {
+ publishTaskMutation,
+ recordTaskMutation,
+} from "./task-mutation-effects";
import {
assertAssignableUser,
getProjectWorkspaceId,
@@ -27,11 +30,13 @@ async function updateTask(
) {
assertTaskPosition(position);
- const [existingTask] = await db
+ let [existingTask] = await db
.select({
id: taskTable.id,
title: taskTable.title,
priority: taskTable.priority,
+ userId: taskTable.userId,
+ dueDate: taskTable.dueDate,
description:
description === undefined ? sql<null>`null` : taskTable.description,
status: taskTable.status,
@@ -71,26 +76,57 @@ async function updateTask(
),
});
- const [updatedTask] = await db
- .update(taskTable)
- .set({
- title,
- status,
- columnId: column?.id ?? null,
- startDate: startDate || null,
- dueDate: dueDate || null,
- projectId,
- description,
- priority,
- position,
- userId: normalizedUserId ?? null,
- })
- .where(eq(taskTable.id, id))
- .returning({
- ...getTableColumns(taskTable),
- description: boardDescription,
- descriptionDeferred,
- });
+ const updatedTask = await db.transaction(async (tx) => {
+ const [locked] = await tx
+ .select({
+ id: taskTable.id,
+ title: taskTable.title,
+ priority: taskTable.priority,
+ userId: taskTable.userId,
+ dueDate: taskTable.dueDate,
+ description:
+ description === undefined ? sql<null>`null` : taskTable.description,
+ status: taskTable.status,
+ projectId: taskTable.projectId,
+ })
+ .from(taskTable)
+ .where(and(eq(taskTable.id, id), eq(taskTable.projectId, projectId)))
+ .for("update");
+ if (!locked)
+ throw new HTTPException(409, {
+ message: "Task changed projects; retry the update",
+ });
+ existingTask = locked;
+
+ const [task] = await tx
+ .update(taskTable)
+ .set({
+ title,
+ status,
+ columnId: column?.id ?? null,
+ startDate: startDate || null,
+ dueDate: dueDate || null,
+ projectId,
+ description,
+ priority,
+ position,
+ userId: normalizedUserId ?? null,
+ })
+ .where(and(eq(taskTable.id, id), eq(taskTable.projectId, projectId)))
+ .returning({
+ ...getTableColumns(taskTable),
+ description: boardDescription,
+ descriptionDeferred,
+ });
+ if (task)
+ await recordTaskMutation(
+ tx,
+ existingTask,
+ { title, dueDate: dueDate ?? null },
+ currentUserId,
+ );
+ return task;
+ });
if (!updatedTask) {
throw new HTTPException(500, {
@@ -98,57 +134,15 @@ async function updateTask(
});
}
- if (existingTask.status !== status) {
- await publishEvent("task.status_changed", {
- taskId: updatedTask.id,
- projectId: updatedTask.projectId,
- userId: currentUserId,
- oldStatus: existingTask.status,
- newStatus: status,
- title: updatedTask.title,
- assigneeId: updatedTask.userId,
- type: "status_changed",
- });
-
- await publishEvent("task-relation.refresh", {
- projectId: updatedTask.projectId,
- userId: currentUserId,
- });
- }
-
- if (existingTask.title !== title) {
- await publishEvent("task.title_changed", {
- taskId: updatedTask.id,
- projectId: updatedTask.projectId,
- userId: currentUserId,
- oldTitle: existingTask.title,
- newTitle: title,
- type: "title_changed",
- });
- }
-
- if (description !== undefined && existingTask.description !== description) {
- await publishEvent("task.description_changed", {
- taskId: updatedTask.id,
- projectId: updatedTask.projectId,
- userId: currentUserId,
- oldDescription: existingTask.description,
- newDescription: description,
- type: "description_changed",
- });
- }
-
- if (existingTask.priority !== priority) {
- await publishEvent("task.priority_changed", {
- taskId: updatedTask.id,
- projectId: updatedTask.projectId,
- userId: currentUserId,
- oldPriority: existingTask.priority,
- newPriority: priority,
- title: updatedTask.title,
- type: "priority_changed",
- });
- }
+ await publishTaskMutation(
+ {
+ ...existingTask,
+ description:
+ description === undefined ? undefined : existingTask.description,
+ },
+ { ...updatedTask, description: description ?? updatedTask.description },
+ currentUserId,
+ );
await publishEvent("task.updated", {
taskId: updatedTask.id,
@@ -158,12 +152,6 @@ async function updateTask(
userId: currentUserId,
});
- if (description !== undefined && existingTask.description !== description) {
- deleteOrphanedAssets(existingTask.description, description, {
- taskId: id,
- }).catch(() => {});
- }
-
return updatedTask;
}
diff --git a/tests/api-integration/task-mutation-parity.test.ts b/tests/api-integration/task-mutation-parity.test.ts
new file mode 100644
index 000000000..b226517e0
--- /dev/null
+++ b/tests/api-integration/task-mutation-parity.test.ts
@@ -0,0 +1,96 @@
+import { eq } from "drizzle-orm";
+import { beforeEach, expect, it, vi } from "vite-plus/test";
+import db, { schema } from "../../apps/api/src/database";
+import updateTask from "../../apps/api/src/task/controllers/update-task";
+import bulkUpdateTasks from "../../apps/api/src/task/controllers/bulk-update-tasks";
+import { resetTestDatabase } from "./helpers/database";
+import {
+ createProjectFixture,
+ createWorkspaceMember,
+} from "./helpers/fixtures";
+const publish = vi.hoisted(() => vi.fn(async () => undefined));
+vi.mock("../../apps/api/src/events", () => ({ publishEvent: publish }));
+beforeEach(async () => {
+ await resetTestDatabase();
+ vi.clearAllMocks();
+});
+it("full updates store title history, assignment events and reset reminders together", async () => {
+ const { user, workspace } = await createWorkspaceMember();
+ const { project } = await createProjectFixture({ workspaceId: workspace.id });
+ const due = new Date("2030-01-01");
+ const [task] = await db
+ .insert(schema.taskTable)
+ .values({
+ projectId: project.id,
+ title: "old",
+ status: "to-do",
+ priority: "low",
+ dueDate: due,
+ position: 0,
+ })
+ .returning();
+ await db
+ .insert(schema.taskReminderSentTable)
+ .values({ taskId: task.id, reminderType: "due_today" });
+ await updateTask(
+ task.id,
+ "new",
+ "to-do",
+ undefined,
+ new Date("2030-02-01"),
+ project.id,
+ undefined,
+ "low",
+ 0,
+ user.id,
+ user.id,
+ );
+ expect(await db.query.activityTable.findMany()).toEqual([
+ expect.objectContaining({
+ type: "title_changed",
+ eventData: { oldTitle: "old", newTitle: "new" },
+ }),
+ ]);
+ expect(await db.query.taskReminderSentTable.findMany()).toHaveLength(0);
+ expect(publish).toHaveBeenCalledWith(
+ "task.assignee_changed",
+ expect.objectContaining({ newAssigneeId: user.id }),
+ );
+});
+it("bulk due date updates clear claims and preserve claims for unchanged dates", 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: "task",
+ status: "to-do",
+ dueDate: new Date("2030-01-01"),
+ })
+ .returning();
+ await db
+ .insert(schema.taskReminderSentTable)
+ .values({ taskId: task.id, reminderType: "due_today" });
+ await bulkUpdateTasks({
+ taskIds: [task.id],
+ operation: "updateDueDate",
+ value: "2030-01-01",
+ userId: user.id,
+ });
+ expect(await db.query.taskReminderSentTable.findMany()).toHaveLength(1);
+ await bulkUpdateTasks({
+ taskIds: [task.id],
+ operation: "updateDueDate",
+ value: null,
+ userId: user.id,
+ });
+ expect(await db.query.taskReminderSentTable.findMany()).toHaveLength(0);
+ expect(
+ (
+ await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, task.id),
+ })
+ )?.dueDate,
+ ).toBeNull();
+});