⚡ 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
import { and, asc, eq, isNotNull } from "drizzle-orm";
import db from "../../database";
import { columnTable, taskTable } from "../../database/schema";
import { publishEvent } from "../../events";
import duplicateTask from "../controllers/duplicate-task";
import { taskIsCompleted } from "../task-is-completed";
import { OccurrenceProjectChangedError } from "./occurrence-project-changed-error";
// 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
// moved forward one step from the completed task's due date.
async function createInCurrentProject(
taskId: string,
currentUserId: string | null | undefined,
completedAt: Date,
) {
const [task] = await db
.select({ projectId: taskTable.projectId })
.from(taskTable)
.where(
and(
eq(taskTable.id, taskId),
isNotNull(taskTable.recurrence),
taskIsCompleted,
),
)
.limit(1);
if (!task) return null;
const [openColumn] = await db
.select({ slug: columnTable.slug })
.from(columnTable)
.where(
and(
eq(columnTable.projectId, task.projectId),
eq(columnTable.isFinal, false),
),
)
.orderBy(asc(columnTable.position))
.limit(1);
if (!openColumn) return null;
const next = await duplicateTask({
taskId,
currentUserId: currentUserId ?? "",
canUpdateTasks: true,
occurrence: {
projectId: task.projectId,
status: openColumn.slug,
completedAt,
},
});
// Clients refetched the completed task on its status change, before its rule
// moved, so they still show it as repeating.
if (next)
await publishEvent("task.updated", {
taskId,
projectId: task.projectId,
userId: currentUserId,
});
return next;
}
export async function createNextOccurrence(
taskId: string,
currentUserId: string | null | undefined,
) {
const completedAt = new Date();
for (let attempt = 0; attempt < 3; attempt++) {
try {
return await createInCurrentProject(taskId, currentUserId, completedAt);
} catch (error) {
if (!(error instanceof OccurrenceProjectChangedError) || attempt === 2) {
throw error;
}
}
}
return null;
}