Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

2.9 KB
Raw Blame
import { withMiddlewares } from "@/src/features/public-api/server/withMiddlewares";
import { prisma } from "@langfuse/shared/src/db";
import { type NextApiRequest, type NextApiResponse } from "next";
import { auditLog } from "@/src/features/audit-logs/auditLog";
import { InvalidRequestError, LangfuseNotFoundError } from "@langfuse/shared";
import type { BlobStorageIntegrationStatusResponseType } from "@/src/features/public-api/types/blob-storage-integrations";
import { deriveSyncStatus } from "@/src/features/blobstorage-integration/deriveSyncStatus";
import { authorizeBlobStorageRequest } from "@/src/features/blobstorage-integration/authorizeBlobStorageRequest";

export default withMiddlewares({
  GET: handleGetBlobStorageIntegrationStatus,
  DELETE: handleDeleteBlobStorageIntegration,
});

async function handleDeleteBlobStorageIntegration(
  req: NextApiRequest,
  res: NextApiResponse,
) {
  const scope = await authorizeBlobStorageRequest(req);

  const { id } = req.query;

  if (!id || typeof id !== "string") {
    throw new InvalidRequestError("Invalid integration ID");
  }

  // Check if the integration exists and belongs to a project in the organization
  const integration = await prisma.blobStorageIntegration.findUnique({
    where: { projectId: id },
    include: {
      project: {
        select: { orgId: true },
      },
    },
  });

  if (!integration || integration.project.orgId !== scope.orgId) {
    throw new LangfuseNotFoundError("Blob storage integration not found");
  }

  // Delete the integration
  await prisma.blobStorageIntegration.delete({
    where: { projectId: id },
  });

  await auditLog({
    action: "delete",
    resourceType: "blobStorageIntegration",
    resourceId: integration.projectId,
    projectId: integration.projectId,
    orgId: scope.orgId,
    apiKeyId: scope.apiKeyId,
  });

  return res.status(200).json({
    message: "Blob storage integration successfully deleted",
  });
}

async function handleGetBlobStorageIntegrationStatus(
  req: NextApiRequest,
  res: NextApiResponse,
) {
  const scope = await authorizeBlobStorageRequest(req);

  const { id } = req.query;
  if (!id || typeof id !== "string") {
    throw new InvalidRequestError("Invalid integration ID");
  }

  const integration = await prisma.blobStorageIntegration.findUnique({
    where: { projectId: id },
    include: {
      project: {
        select: { orgId: true },
      },
    },
  });

  if (!integration || integration.project.orgId !== scope.orgId) {
    throw new LangfuseNotFoundError("Blob storage integration not found");
  }

  const responseData: BlobStorageIntegrationStatusResponseType = {
    id: integration.projectId,
    projectId: integration.projectId,
    syncStatus: deriveSyncStatus(integration),
    enabled: integration.enabled,
    lastSyncAt: integration.lastSyncAt,
    nextSyncAt: integration.nextSyncAt,
    lastError: integration.lastError,
    lastErrorAt: integration.lastErrorAt,
  };

  return res.status(200).json(responseData);
}