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.

14.6 KB
Raw Blame
import { createId } from "@paralleldrive/cuid2";
import { and, eq, isNotNull } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
import {
  assetTable,
  columnTable,
  customFieldDefinitionTable,
  customFieldValueTable,
  labelTable,
  projectTable,
  taskRelationTable,
  taskTable,
  userTable,
} from "../../database/schema";
import { publishEvent } from "../../events";
import { projectAccessCondition } from "../../project-access/project-access-condition";
import { contentReferencesAsset } from "../../storage/cleanup-assets";
import { copyTaskAssetObject, deleteS3Object } from "../../storage/s3";
import { filterUsersWithProjectAccess } from "../../project-access/filter-users-with-project-access";
import {
  filterAssignableUsers,
  NOT_ASSIGNABLE,
} from "../../utils/assert-assignable-user";
import { nextOccurrenceDates } from "../recurrence/next-occurrence-date";
import { OccurrenceProjectChangedError } from "../recurrence/occurrence-project-changed-error";
import { taskIsCompleted } from "../task-is-completed";
import {
  assertRequiredCustomFields,
  assertValidTaskStatus,
} from "../validate-task-fields";
import { claimTaskNumber } from "./claim-task-numbers";
import { nextTaskPosition } from "./next-task-position";

async function discardCopiedObjects(objectKeys: string[]) {
  await Promise.all(
    objectKeys.map((objectKey) =>
      deleteS3Object(objectKey).catch((error) => {
        console.error("Failed to discard a duplicated task asset:", error);
      }),
    ),
  );
}

// Assets are addressed by `/api/asset/<id>` inside the description, so the copy
// needs its own asset rows and stored objects. Sharing the source rows is not an
// option: `asset.objectKey` is unique and deleting the source task cascades to them.
async function duplicateDescriptionAssets({
  sourceTask,
  duplicatedTaskId,
  workspaceId,
}: {
  sourceTask: typeof taskTable.$inferSelect;
  duplicatedTaskId: string;
  workspaceId: string;
}) {
  const description = sourceTask.description;

  if (!description) {
    return { assets: [], description };
  }

  const sourceAssets = await db
    .select()
    .from(assetTable)
    .where(
      and(
        eq(assetTable.taskId, sourceTask.id),
        eq(assetTable.projectId, sourceTask.projectId),
        eq(assetTable.workspaceId, workspaceId),
      ),
    );

  const referencedAssets = sourceAssets.filter((asset) =>
    contentReferencesAsset(description, asset.id),
  );

  const assets: (typeof assetTable.$inferInsert)[] = [];
  let duplicatedDescription = description;

  try {
    for (const asset of referencedAssets) {
      const duplicatedAssetId = createId();
      const objectKey = await copyTaskAssetObject({
        sourceKey: asset.objectKey,
        destination: {
          workspaceId,
          projectId: sourceTask.projectId,
          taskId: duplicatedTaskId,
          surface: asset.surface === "comment" ? "comment" : "description",
          filename: asset.filename,
          contentType: asset.mimeType,
        },
      });

      assets.push({
        id: duplicatedAssetId,
        workspaceId,
        projectId: sourceTask.projectId,
        taskId: duplicatedTaskId,
        objectKey,
        filename: asset.filename,
        mimeType: asset.mimeType,
        size: asset.size,
        kind: asset.kind,
        surface: asset.surface,
        createdBy: asset.createdBy,
      });

      duplicatedDescription = duplicatedDescription.replaceAll(
        `/api/asset/${asset.id}`,
        `/api/asset/${duplicatedAssetId}`,
      );
    }
  } catch (error) {
    await discardCopiedObjects(assets.map((asset) => asset.objectKey));

    console.error("Failed to copy task attachments:", error);
    throw new HTTPException(503, {
      message: "Failed to copy the task attachments",
    });
  }

  return { assets, description: duplicatedDescription };
}

type DuplicateTaskOptions = {
  taskId: string;
  currentUserId: string;
  canUpdateTasks: boolean;
  title?: string;
};

// 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 = {
  projectId: string;
  status: string;
  completedAt: Date;
};

// Rolls back an occurrence whose rule another transaction already moved.
class RuleAlreadyMovedError extends Error {}

type DuplicatedTask = typeof taskTable.$inferSelect & {
  assigneeName: string | undefined;
};

function duplicateTask(options: DuplicateTaskOptions): Promise<DuplicatedTask>;
// Resolves to null when the source no longer holds a rule to hand over: it was
// reopened, or a concurrent completion already created the next occurrence.
function duplicateTask(
  options: DuplicateTaskOptions & { occurrence: Occurrence },
): Promise<DuplicatedTask | null>;
async function duplicateTask({
  taskId,
  currentUserId,
  canUpdateTasks,
  title,
  occurrence,
}: DuplicateTaskOptions & {
  occurrence?: Occurrence;
}): Promise<DuplicatedTask | null> {
  const sourceTask = await db.query.taskTable.findFirst({
    where: eq(taskTable.id, taskId),
  });

  if (!sourceTask) {
    throw new HTTPException(404, {
      message: "Task not found",
    });
  }

  if (occurrence && sourceTask.projectId !== occurrence.projectId) {
    throw new OccurrenceProjectChangedError();
  }

  const [project] = await db
    .select({ workspaceId: projectTable.workspaceId })
    .from(projectTable)
    .where(eq(projectTable.id, sourceTask.projectId))
    .limit(1);

  if (!project) {
    throw new HTTPException(404, {
      message: "Project not found",
    });
  }

  const status = occurrence?.status ?? sourceTask.status;
  await assertValidTaskStatus(status, sourceTask.projectId);
  // A manual copy reports a departed assignee; an occurrence goes unassigned
  // so the series keeps going.
  let userId = sourceTask.userId;
  if (userId) {
    const members = await filterAssignableUsers([userId], project.workspaceId);
    if (!members.has(userId)) {
      if (!occurrence) {
        throw new HTTPException(403, { message: NOT_ASSIGNABLE });
      }
      userId = null;
    }
  }
  if (userId) {
    const permitted = await filterUsersWithProjectAccess(
      [userId],
      sourceTask.projectId,
    );
    if (!permitted.has(userId)) userId = null;
  }
  const column = await db.query.columnTable.findFirst({
    where: and(
      eq(columnTable.projectId, sourceTask.projectId),
      eq(columnTable.slug, status),
    ),
  });

  const fieldDefinitions = await db
    .select()
    .from(customFieldDefinitionTable)
    .where(eq(customFieldDefinitionTable.projectId, sourceTask.projectId));
  const sourceCustomFields = await db
    .select({
      fieldId: customFieldValueTable.fieldId,
      value: customFieldValueTable.value,
    })
    .from(customFieldValueTable)
    .innerJoin(
      customFieldDefinitionTable,
      eq(customFieldValueTable.fieldId, customFieldDefinitionTable.id),
    )
    .where(
      and(
        eq(customFieldValueTable.taskId, sourceTask.id),
        eq(customFieldDefinitionTable.projectId, sourceTask.projectId),
      ),
    );
  const customFields = sourceCustomFields.map(({ fieldId, value }) => ({
    fieldId,
    value: value ?? "",
  }));
  // Older tasks may predate a required field. Apply its current default, then
  // fail validation as creation does before copying anything in storage. An
  // occurrence leaves the field empty instead, so the series keeps going.
  for (const definition of fieldDefinitions) {
    if (!definition.required || !definition.defaultValue?.trim()) continue;
    const existing = customFields.find(
      (field) => field.fieldId === definition.id,
    );
    if (!existing)
      customFields.push({
        fieldId: definition.id,
        value: definition.defaultValue.trim(),
      });
    else if (!existing.value.trim())
      existing.value = definition.defaultValue.trim();
  }
  if (!occurrence)
    await assertRequiredCustomFields(sourceTask.projectId, customFields);

  const sourceLabels = await db
    .select({
      name: labelTable.name,
      color: labelTable.color,
      workspaceId: labelTable.workspaceId,
    })
    .from(labelTable)
    .where(
      and(
        eq(labelTable.taskId, sourceTask.id),
        eq(labelTable.workspaceId, project.workspaceId),
      ),
    );

  // A duplicated subtask stays a subtask of the same parents. The source's own
  // subtasks are not duplicated: a copy is one task, not a tree.
  const parentRelations = await db
    .select({
      parentTaskId: taskRelationTable.sourceTaskId,
      parentProjectId: taskTable.projectId,
      relationType: taskRelationTable.relationType,
    })
    .from(taskRelationTable)
    .innerJoin(taskTable, eq(taskRelationTable.sourceTaskId, taskTable.id))
    .innerJoin(projectTable, eq(taskTable.projectId, projectTable.id))
    .where(
      and(
        eq(taskRelationTable.targetTaskId, sourceTask.id),
        eq(projectTable.workspaceId, project.workspaceId),
        eq(taskRelationTable.relationType, "subtask"),
        // An occurrence continues its series with the links it already had,
        // whoever completed it; only a manual copy is limited to what the
        // user can see.
        occurrence
          ? undefined
          : projectAccessCondition(currentUserId, taskTable.projectId),
      ),
    );

  if (parentRelations.length > 0 && !canUpdateTasks) {
    throw new HTTPException(403, {
      message: "Duplicating parent links requires task:update permission",
    });
  }

  // The destination object keys embed the new task id, so it is generated up front
  // instead of being left to the insert.
  const duplicatedTaskId = createId();

  const { assets, description } = await duplicateDescriptionAssets({
    sourceTask,
    duplicatedTaskId,
    workspaceId: project.workspaceId,
  });

  let duplicated: {
    task: typeof taskTable.$inferSelect;
    relations: (typeof taskRelationTable.$inferSelect)[];
  };

  try {
    duplicated = await db.transaction(async (tx) => {
      // Locks the project row before the task row, the same order task updates
      // use.
      const taskNumber = await claimTaskNumber(sourceTask.projectId, tx);

      // 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. 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({
            projectId: taskTable.projectId,
            startDate: taskTable.startDate,
            dueDate: taskTable.dueDate,
            recurrence: taskTable.recurrence,
          })
          .from(taskTable)
          .where(
            and(
              eq(taskTable.id, sourceTask.id),
              isNotNull(taskTable.recurrence),
              taskIsCompleted,
            ),
          )
          .for("update");
        if (!claimed?.recurrence) throw new RuleAlreadyMovedError();
        // Attachment copies and workflow selection happened before this lock.
        // A project move invalidates all of that work, so roll it back and
        // reload the task in its current project before claiming its rule.
        if (claimed.projectId !== occurrence.projectId) {
          throw new OccurrenceProjectChangedError();
        }
        recurrence = claimed.recurrence;
        dates = nextOccurrenceDates(
          claimed,
          claimed.recurrence,
          occurrence.completedAt,
        );
        await tx
          .update(taskTable)
          .set({ recurrence: null })
          .where(eq(taskTable.id, sourceTask.id));
      }

      const nextPosition = await nextTaskPosition(
        tx,
        sourceTask.projectId,
        status,
        column?.id ?? null,
      );

      const [task] = await tx
        .insert(taskTable)
        .values({
          id: duplicatedTaskId,
          projectId: sourceTask.projectId,
          userId,
          title: title?.trim() || sourceTask.title,
          status,
          columnId: column?.id ?? null,
          ...dates,
          recurrence,
          description,
          priority: sourceTask.priority,
          number: taskNumber,
          position: nextPosition,
        })
        .returning();

      if (!task) {
        throw new HTTPException(500, {
          message: "Failed to duplicate task",
        });
      }

      if (customFields.length > 0) {
        await tx
          .insert(customFieldValueTable)
          .values(customFields.map((field) => ({ ...field, taskId: task.id })));
      }

      if (sourceLabels.length > 0) {
        await tx.insert(labelTable).values(
          sourceLabels.map((label) => ({
            name: label.name,
            color: label.color,
            taskId: task.id,
            workspaceId: label.workspaceId,
          })),
        );
      }

      if (assets.length > 0) {
        await tx.insert(assetTable).values(assets);
      }

      const relations =
        parentRelations.length > 0
          ? await tx
              .insert(taskRelationTable)
              .values(
                parentRelations.map((relation) => ({
                  sourceTaskId: relation.parentTaskId,
                  targetTaskId: task.id,
                  relationType: relation.relationType,
                })),
              )
              .returning()
          : [];

      return { task, relations };
    });
  } catch (error) {
    await discardCopiedObjects(assets.map((asset) => asset.objectKey));
    if (error instanceof RuleAlreadyMovedError) return null;
    throw error;
  }

  const duplicatedTask = duplicated.task;

  const [assignee] = duplicatedTask.userId
    ? await db
        .select({ name: userTable.name })
        .from(userTable)
        .where(eq(userTable.id, duplicatedTask.userId))
    : [];

  await publishEvent("task.created", {
    ...duplicatedTask,
    taskId: duplicatedTask.id,
    userId: duplicatedTask.userId ?? "",
    currentUserId,
    type: "created",
    content: null,
  });

  for (const relation of duplicated.relations) {
    const parentProjectId = parentRelations.find(
      (parentRelation) => parentRelation.parentTaskId === relation.sourceTaskId,
    )?.parentProjectId;

    await publishEvent("task-relation.created", {
      ...relation,
      taskId: relation.sourceTaskId,
      projectId: parentProjectId ?? sourceTask.projectId,
      userId: currentUserId,
    });
  }

  return {
    ...duplicatedTask,
    assigneeName: assignee?.name,
  };
}

export default duplicateTask;