⚡ 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, notInArray } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import createActivities from "../../activity/controllers/create-activities";
import db from "../../database";
import {
assetTable,
columnTable,
externalLinkTable,
integrationTable,
projectTable,
taskTable,
} from "../../database/schema";
import { publishEvent } from "../../events";
import { assertProjectAccess } from "../../project-access/assert-project-access";
import { filterUsersWithProjectAccess } from "../../project-access/filter-users-with-project-access";
import { claimTaskNumber } from "./claim-task-numbers";
import { nextTaskPosition } from "./next-task-position";
function isSameProjectMove(
sourceProjectId: string,
destinationProjectId: string,
) {
return sourceProjectId === destinationProjectId;
}
async function resolveDestinationStatus(
destinationProjectId: string,
currentStatus: string,
requestedStatus?: string,
) {
const destinationColumns = await db
.select({
id: columnTable.id,
slug: columnTable.slug,
position: columnTable.position,
})
.from(columnTable)
.where(eq(columnTable.projectId, destinationProjectId))
.orderBy(asc(columnTable.position));
const [firstColumn] = destinationColumns;
if (!firstColumn) {
throw new HTTPException(400, {
message: "Destination project does not have a workflow",
});
}
const requestedColumn = requestedStatus
? destinationColumns.find((column) => column.slug === requestedStatus)
: null;
if (requestedStatus && !requestedColumn) {
throw new HTTPException(400, {
message: "Selected status is not valid for the destination project",
});
}
const matchingCurrentColumn = destinationColumns.find(
(column) => column.slug === currentStatus,
);
return requestedColumn ?? matchingCurrentColumn ?? firstColumn;
}
async function moveTask({
taskId,
destinationProjectId,
destinationStatus,
currentUserId,
}: {
taskId: string;
destinationProjectId: string;
destinationStatus?: string;
currentUserId: string;
}) {
const existingTask = await db.query.taskTable.findFirst({
where: eq(taskTable.id, taskId),
});
if (!existingTask) {
throw new HTTPException(404, {
message: "Task not found",
});
}
if (isSameProjectMove(existingTask.projectId, destinationProjectId)) {
throw new HTTPException(400, {
message: "Task is already in that project",
});
}
const sourceProject = await db.query.projectTable.findFirst({
where: eq(projectTable.id, existingTask.projectId),
});
if (!sourceProject) {
throw new HTTPException(404, {
message: "Project not found",
});
}
const destinationProject = await db.query.projectTable.findFirst({
where: and(
eq(projectTable.id, destinationProjectId),
eq(projectTable.workspaceId, sourceProject.workspaceId),
),
});
if (!destinationProject) {
throw new HTTPException(404, {
message: "Project not found",
});
}
await assertProjectAccess(currentUserId, destinationProjectId);
const resolvedColumn = await resolveDestinationStatus(
destinationProjectId,
existingTask.status,
destinationStatus,
);
const movedTask = await db.transaction(async (tx) => {
for (const projectId of [sourceProject.id, destinationProjectId].sort()) {
const [project] = await tx
.select({ id: projectTable.id })
.from(projectTable)
.where(
and(
eq(projectTable.id, projectId),
eq(projectTable.workspaceId, sourceProject.workspaceId),
),
)
.for("key share");
if (!project)
throw new HTTPException(409, {
message: "Project was moved to another workspace, please try again",
});
}
const nextTaskNumber = await claimTaskNumber(destinationProjectId, tx);
const nextPosition = await nextTaskPosition(
tx,
destinationProjectId,
resolvedColumn.slug,
resolvedColumn.id,
);
const [updatedTask] = await tx
.update(taskTable)
.set({
projectId: destinationProjectId,
status: resolvedColumn.slug,
columnId: resolvedColumn.id,
number: nextTaskNumber,
position: nextPosition,
})
.where(
and(
eq(taskTable.id, taskId),
eq(taskTable.projectId, sourceProject.id),
),
)
.returning();
if (!updatedTask) {
throw new HTTPException(409, {
message: "Task was moved concurrently, please try again",
});
}
let movedTask = updatedTask;
const assigneeId = updatedTask.userId;
if (
assigneeId &&
!(
await filterUsersWithProjectAccess(
[assigneeId],
destinationProjectId,
tx,
)
).has(assigneeId)
) {
const [unassignedTask] = await tx
.update(taskTable)
.set({ userId: null })
.where(eq(taskTable.id, taskId))
.returning();
movedTask = unassignedTask ?? { ...updatedTask, userId: null };
await createActivities(
[
{
taskId,
type: "unassigned",
userId: currentUserId,
content: null,
eventData: {},
},
],
tx,
);
}
await tx
.delete(externalLinkTable)
.where(
and(
eq(externalLinkTable.taskId, taskId),
isNotNull(externalLinkTable.integrationId),
notInArray(
externalLinkTable.integrationId,
tx
.select({ id: integrationTable.id })
.from(integrationTable)
.where(eq(integrationTable.projectId, destinationProjectId)),
),
),
);
await tx
.update(assetTable)
.set({ projectId: destinationProjectId })
.where(eq(assetTable.taskId, taskId));
return movedTask;
});
await publishEvent("task.moved", {
taskId,
type: "moved",
userId: currentUserId,
fromProjectId: sourceProject.id,
fromProjectName: sourceProject.name,
toProjectId: destinationProject.id,
toProjectName: destinationProject.name,
oldStatus: existingTask.status,
newStatus: resolvedColumn.slug,
});
return {
task: movedTask,
sourceProjectId: sourceProject.id,
destinationProjectId: destinationProject.id,
};
}
export default moveTask;