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.

8.5 KB
Raw Blame
import {
  apiRouter,
  createRoute,
  errorResponse,
  jsonResponse,
} from "../openapi";
import { requireWorkspacePermission } from "../utils/require-workspace-permission";
import { workspaceAccess } from "../utils/workspace-access-middleware";
import assignLabelToTask from "./controllers/assign-label-to-task";
import createLabel from "./controllers/create-label";
import deleteLabel from "./controllers/delete-label";
import getLabel from "./controllers/get-label";
import getLabelsByTaskId from "./controllers/get-labels-by-task-id";
import getLabelsByWorkspaceId from "./controllers/get-labels-by-workspace-id";
import unassignLabelFromTask from "./controllers/unassign-label-from-task";
import updateLabel from "./controllers/update-label";
import {
  labelListSchema,
  labelSchema,
  pendingLabelDeletionSchema,
} from "./response";
import {
  attachLabelBody,
  createLabelBody,
  labelParam,
  taskIdParam,
  updateLabelBody,
  workspaceIdParam,
} from "./schema";

const getTaskLabelsRoute = createRoute({
  method: "get",
  operationId: "getTaskLabels",
  path: "/task/{taskId}",
  tags: ["Labels"],
  summary: "Get task labels",
  description: "Get all labels assigned to a specific task",
  middleware: [
    workspaceAccess.fromTaskId(),
    requireWorkspacePermission({ label: ["read"] }),
  ] as const,
  request: { params: taskIdParam },
  responses: {
    200: jsonResponse("List of labels for the task", labelListSchema),
    403: errorResponse("No access to the project, or missing read permission"),
    404: errorResponse("Task not found"),
  },
});

const getWorkspaceLabelsRoute = createRoute({
  method: "get",
  operationId: "getWorkspaceLabels",
  path: "/workspace/{workspaceId}",
  tags: ["Labels"],
  summary: "Get workspace labels",
  description: "Get all labels for a specific workspace",
  middleware: [
    workspaceAccess.fromParam(),
    requireWorkspacePermission({ label: ["read"] }),
  ] as const,
  request: { params: workspaceIdParam },
  responses: {
    200: jsonResponse("List of labels in the workspace", labelListSchema),
    403: errorResponse("Missing read permission"),
    404: errorResponse("Workspace not found"),
  },
});

const createLabelRoute = createRoute({
  method: "post",
  operationId: "createLabel",
  path: "/",
  tags: ["Labels"],
  summary: "Create label",
  description: "Create a new label in a workspace",
  middleware: [
    workspaceAccess.fromBody(),
    requireWorkspacePermission({ label: ["create"] }),
  ] as const,
  request: {
    body: {
      required: true,
      content: { "application/json": { schema: createLabelBody } },
    },
  },
  responses: {
    200: jsonResponse("Label created successfully", labelSchema),
    400: errorResponse("Invalid body, or workspace ID could not be determined"),
    403: errorResponse("Missing label:create permission"),
    404: errorResponse("Workspace or task not found"),
    409: errorResponse("The workspace label is being deleted"),
  },
});

const getLabelRoute = createRoute({
  method: "get",
  operationId: "getLabel",
  path: "/{id}",
  tags: ["Labels"],
  summary: "Get label",
  description: "Get a specific label by ID",
  middleware: [
    workspaceAccess.fromLabel(),
    requireWorkspacePermission({ label: ["read"] }),
  ] as const,
  request: { params: labelParam },
  responses: {
    200: jsonResponse("Label details", labelSchema),
    403: errorResponse("No access to the project, or missing read permission"),
    404: errorResponse("Label not found"),
  },
});

const attachLabelToTaskRoute = createRoute({
  method: "put",
  operationId: "attachLabelToTask",
  path: "/{id}/task",
  tags: ["Labels"],
  summary: "Attach label to task",
  description: "Attach an existing label to a task",
  middleware: [
    workspaceAccess.fromLabel(),
    requireWorkspacePermission({ label: ["update"] }),
  ] as const,
  request: {
    params: labelParam,
    body: {
      required: true,
      content: { "application/json": { schema: attachLabelBody } },
    },
  },
  responses: {
    200: jsonResponse("Label attached to task successfully", labelSchema),
    400: errorResponse("Label and task belong to different workspaces"),
    403: errorResponse(
      "No access to the project, or missing label:update permission",
    ),
    404: errorResponse("Label or task not found"),
    409: errorResponse("The workspace label is being deleted"),
  },
});

const detachLabelFromTaskRoute = createRoute({
  method: "delete",
  operationId: "detachLabelFromTask",
  path: "/{id}/task",
  tags: ["Labels"],
  summary: "Detach label from task",
  description: "Detach a label from its current task",
  middleware: [
    workspaceAccess.fromLabel(),
    requireWorkspacePermission({ label: ["update"] }),
  ] as const,
  request: { params: labelParam },
  responses: {
    200: jsonResponse("Label detached from task successfully", labelSchema),
    400: errorResponse("Label is not assigned to a task"),
    403: errorResponse(
      "No access to the project, or missing label:update permission",
    ),
    404: errorResponse("Label or task not found"),
  },
});

const updateLabelRoute = createRoute({
  method: "put",
  operationId: "updateLabel",
  path: "/{id}",
  tags: ["Labels"],
  summary: "Update label",
  description: "Update an existing label",
  middleware: [
    workspaceAccess.fromLabel(),
    requireWorkspacePermission({ label: ["update"] }),
  ] as const,
  request: {
    params: labelParam,
    body: {
      required: true,
      content: { "application/json": { schema: updateLabelBody } },
    },
  },
  responses: {
    200: jsonResponse("Label updated successfully", labelSchema),
    409: errorResponse("The workspace label is being deleted"),
    400: errorResponse("Invalid body"),
    403: errorResponse(
      "No access to the project, or missing label:update permission",
    ),
    404: errorResponse("Label not found"),
  },
});

const deleteLabelRoute = createRoute({
  method: "delete",
  operationId: "deleteLabel",
  path: "/{id}",
  tags: ["Labels"],
  summary: "Delete label",
  description:
    "Delete a label by ID. Large workspace cascades remove at most 25 task copies per request. Repeat DELETE with the same ID after HTTP 202 until HTTP 200 completes the operation. The persisted start boundary allows safe resumption after a disconnect. HTTP 429 asks the caller to retry later.",
  middleware: [
    workspaceAccess.fromLabel(),
    requireWorkspacePermission({ label: ["delete"] }),
  ] as const,
  request: { params: labelParam },
  responses: {
    200: jsonResponse("Label deleted successfully", labelSchema),
    202: jsonResponse(
      "Deletion has more batches; repeat the same request",
      pendingLabelDeletionSchema,
    ),
    429: errorResponse("Deletion capacity is busy; retry after Retry-After"),
    403: errorResponse(
      "No access to the project, or missing label:delete permission",
    ),
    404: errorResponse("Label not found, or the label's task no longer exists"),
  },
});

const label = apiRouter()
  .openapi(getTaskLabelsRoute, async (c) => {
    const { taskId } = c.req.valid("param");
    return c.json(await getLabelsByTaskId(taskId), 200);
  })
  .openapi(getWorkspaceLabelsRoute, async (c) => {
    const { workspaceId } = c.req.valid("param");
    return c.json(
      await getLabelsByWorkspaceId(workspaceId, c.get("userId")),
      200,
    );
  })
  .openapi(createLabelRoute, async (c) => {
    const { name, color, workspaceId, taskId } = c.req.valid("json");
    const userId = c.get("userId");
    return c.json(
      await createLabel(name, color, taskId, workspaceId, userId),
      200,
    );
  })
  .openapi(getLabelRoute, async (c) => {
    const { id } = c.req.valid("param");
    return c.json(await getLabel(id), 200);
  })
  .openapi(attachLabelToTaskRoute, async (c) => {
    const { id } = c.req.valid("param");
    const { taskId } = c.req.valid("json");
    const userId = c.get("userId");
    return c.json(await assignLabelToTask(id, taskId, userId), 200);
  })
  .openapi(detachLabelFromTaskRoute, async (c) => {
    const { id } = c.req.valid("param");
    const userId = c.get("userId");
    return c.json(await unassignLabelFromTask(id, userId), 200);
  })
  .openapi(updateLabelRoute, async (c) => {
    const { id } = c.req.valid("param");
    const { name, color } = c.req.valid("json");
    return c.json(await updateLabel(id, name, color), 200);
  })
  .openapi(deleteLabelRoute, async (c) => {
    const { id } = c.req.valid("param");
    const userId = c.get("userId");
    const result = await deleteLabel(id, userId);
    if (result.pendingDeletion)
      return c.json({ ...result, pendingDeletion: true as const }, 202);
    return c.json(result, 200);
  });

export default label;