Repository navigation
fix(tasks): share mutation history and side effects #1876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tinsever
merged 5 commits into
fix/review-09-bulk-status
from
fix/review-10-mutation-parity
Sep 30, 2026
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
12ace3b
fix(tasks): share mutation history and side effects
tinsever 8f2edce
fix(tasks): preserve mutation scope and batch bulk effects
tinsever 43e0b37
fix(tasks): lock mutation snapshots with field writes
tinsever df36d9e
fix: keep queued writes and validation scoped to their owner
tinsever f03bf5f
Merge PR #1873 into dependent PR #1876
tinsever File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| 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: Partial<TaskBefore>, | ||
| after: Pick<TaskBefore, "id" | "projectId" | "title"> & Partial<TaskBefore>, | ||
| userId?: string, | ||
| options: { | ||
| skipSubtaskParentRefresh?: boolean; | ||
| skipRelationRefresh?: boolean; | ||
| fields?: Array<keyof Changes>; | ||
| assigneeName?: string | null; | ||
| } = {}, | ||
| ) { | ||
| const changed = (field: keyof Changes) => | ||
| (!options.fields || options.fields.includes(field)) && | ||
| before[field] !== after[field]; | ||
|
tinsever marked this conversation as resolved.
|
||
| const common = { | ||
| taskId: after.id, | ||
| projectId: after.projectId, | ||
| userId, | ||
| title: after.title, | ||
| }; | ||
| if (changed("status")) { | ||
| await publishEvent("task.status_changed", { | ||
| ...common, | ||
| oldStatus: before.status, | ||
| newStatus: after.status, | ||
| assigneeId: after.userId, | ||
| type: "status_changed", | ||
| ...(options.skipSubtaskParentRefresh | ||
| ? { skipSubtaskParentRefresh: true } | ||
| : {}), | ||
| }); | ||
| if (!options.skipRelationRefresh) | ||
| await publishEvent("task-relation.refresh", { | ||
| projectId: after.projectId, | ||
| userId, | ||
| }); | ||
| } | ||
| if (changed("title")) | ||
| await publishEvent("task.title_changed", { | ||
| ...common, | ||
| oldTitle: before.title, | ||
| newTitle: after.title, | ||
| type: "title_changed", | ||
| }); | ||
| if (changed("priority")) | ||
| await publishEvent("task.priority_changed", { | ||
| ...common, | ||
| oldPriority: before.priority, | ||
| newPriority: after.priority, | ||
| type: "priority_changed", | ||
| }); | ||
| if (changed("userId")) { | ||
| const assignee = | ||
| options.assigneeName !== undefined | ||
| ? { name: options.assigneeName ?? undefined } | ||
| : 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 ( | ||
| (!options.fields || options.fields.includes("dueDate")) && | ||
| before.dueDate?.getTime() !== after.dueDate?.getTime() | ||
| ) | ||
| await publishEvent("task.due_date_changed", { | ||
| ...common, | ||
| oldDueDate: before.dueDate, | ||
| newDueDate: after.dueDate, | ||
| type: "due_date_changed", | ||
| }); | ||
| if ( | ||
| (!options.fields || options.fields.includes("description")) && | ||
| 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", | ||
| }); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.