mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
fix: endpoints
This commit is contained in:
+70
-67
@@ -1,18 +1,20 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { withApiKey, ApiKeyContext } from "@/lib/api-v1/middleware";
|
||||
import { getAccessibleAgentIds } from "@/lib/api-v1/acl";
|
||||
import { withApiKey } from "@/lib/api-v1/middleware";
|
||||
import { db } from "@/db";
|
||||
import * as drizzleDb from "@/db";
|
||||
import { inArray, eq, count, and, or, isNull } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { slugify } from "@/utils/slugify";
|
||||
import { logger } from "@/lib/logger";
|
||||
import {createAgentService} from "@/features/agents/agents.action";
|
||||
import { ActionError } from "@/lib/safe-actions/actions";
|
||||
import {getAccessibleAgentIds} from "@/lib/api-v1/services/agents";
|
||||
import {ApiKeyContext} from "@/lib/api-v1/types";
|
||||
|
||||
const log = logger.child({ module: "api/v1/agents" });
|
||||
|
||||
export const GET = withApiKey(async (_req: Request, ctx: ApiKeyContext) => {
|
||||
try {
|
||||
const agentIds = await getAccessibleAgentIds(ctx.userId);
|
||||
const agentIds = await getAccessibleAgentIds(ctx.user);
|
||||
|
||||
if (agentIds.length === 0) {
|
||||
return NextResponse.json({ data: [] });
|
||||
@@ -37,76 +39,77 @@ export const GET = withApiKey(async (_req: Request, ctx: ApiKeyContext) => {
|
||||
|
||||
const CreateAgentSchema = z.object({
|
||||
name: z.string().min(1, "name is required"),
|
||||
description: z.string().min(1, "description is required"),
|
||||
organizationId: z.string().uuid("organizationId must be a valid UUID"),
|
||||
organizationId: z.string().uuid("organizationId must be a valid UUID").optional(),
|
||||
});
|
||||
|
||||
export const POST = withApiKey(async (req: Request, ctx: ApiKeyContext) => {
|
||||
try {
|
||||
let body: unknown;
|
||||
try {
|
||||
body = await req.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Invalid JSON body" }, { status: 422 });
|
||||
}
|
||||
export const POST = withApiKey(
|
||||
async (req: Request, ctx: ApiKeyContext) => {
|
||||
try {
|
||||
const body = await req.json().catch(() => null);
|
||||
|
||||
const parsed = CreateAgentSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ error: parsed.error.issues[0].message },
|
||||
{ status: 422 }
|
||||
);
|
||||
}
|
||||
if (!body) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid JSON body" },
|
||||
{ status: 422 }
|
||||
);
|
||||
}
|
||||
|
||||
const { name, description, organizationId } = parsed.data;
|
||||
const parsed = CreateAgentSchema.safeParse(body);
|
||||
|
||||
if (!ctx.orgIds.includes(organizationId)) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: parsed.error.issues[0]?.message ??
|
||||
"Invalid payload",
|
||||
},
|
||||
{ status: 422 }
|
||||
);
|
||||
}
|
||||
|
||||
const createdAgent = await db.transaction(async (tx) => {
|
||||
const slug = slugify(name);
|
||||
const { name, organizationId } = parsed.data;
|
||||
|
||||
const [existing] = await tx
|
||||
.select({ count: count() })
|
||||
.from(drizzleDb.schemas.agent)
|
||||
.where(eq(drizzleDb.schemas.agent.slug, slug));
|
||||
const org = ctx.organizations.find(
|
||||
(org) => org.id === organizationId
|
||||
);
|
||||
|
||||
if (existing.count > 0) {
|
||||
return null; // signal slug conflict
|
||||
if (
|
||||
organizationId &&
|
||||
(!org || !org.permissions.canManageAgents)
|
||||
) {
|
||||
return NextResponse.json(
|
||||
{ error: "Forbidden" },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
const createdAgent = await createAgentService({
|
||||
organizationId,
|
||||
data: {
|
||||
name,
|
||||
description: "",
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{ data: createdAgent },
|
||||
{ status: 201 }
|
||||
);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof ActionError) {
|
||||
return NextResponse.json(
|
||||
{ error: error.message },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
log.error({error},
|
||||
"Error in POST /api/v1/agents"
|
||||
);
|
||||
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
const [agent] = await tx
|
||||
.insert(drizzleDb.schemas.agent)
|
||||
.values({ name, description, slug, organizationId })
|
||||
.returning();
|
||||
|
||||
if (!agent) throw new Error("Failed to create agent");
|
||||
|
||||
await tx.insert(drizzleDb.schemas.organizationAgent).values({
|
||||
organizationId,
|
||||
agentId: agent.id,
|
||||
});
|
||||
|
||||
return agent;
|
||||
});
|
||||
|
||||
if (createdAgent === null) {
|
||||
return NextResponse.json(
|
||||
{ error: "An agent with this name already exists" },
|
||||
{ status: 422 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({ data: createdAgent }, { status: 201 });
|
||||
} catch (error: any) {
|
||||
if (error?.code === "23505") {
|
||||
return NextResponse.json(
|
||||
{ error: "An agent with this name already exists" },
|
||||
{ status: 422 }
|
||||
);
|
||||
}
|
||||
log.error({ error }, "Error in POST /api/v1/agents");
|
||||
return NextResponse.json({ error: "Internal server error" }, { status: 500 });
|
||||
}
|
||||
});
|
||||
);
|
||||
@@ -1,10 +1,11 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { withApiKey, ApiKeyContext } from "@/lib/api-v1/middleware";
|
||||
import { getAccessibleDatabaseIds } from "@/lib/api-v1/acl";
|
||||
import { withApiKey } from "@/lib/api-v1/middleware";
|
||||
import { db } from "@/db";
|
||||
import * as drizzleDb from "@/db";
|
||||
import { eq, and, isNull } from "drizzle-orm";
|
||||
import { logger } from "@/lib/logger";
|
||||
import {getAccessibleDatabaseIds} from "@/lib/api-v1/services/databases";
|
||||
import {ApiKeyContext} from "@/lib/api-v1/types";
|
||||
|
||||
const log = logger.child({ module: "api/v1/databases/[id]/backup/[backupId]" });
|
||||
|
||||
@@ -14,7 +15,7 @@ export const GET = withApiKey(
|
||||
const { id, backupId } = params ?? {};
|
||||
if (!id || !backupId) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
|
||||
const accessibleIds = await getAccessibleDatabaseIds(ctx.userId);
|
||||
const accessibleIds = await getAccessibleDatabaseIds(ctx.user);
|
||||
if (!accessibleIds.includes(id)) {
|
||||
const exists = await db.query.database.findFirst({
|
||||
where: eq(drizzleDb.schemas.database.id, id),
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { withApiKey, ApiKeyContext } from "@/lib/api-v1/middleware";
|
||||
import { getAccessibleDatabaseIds } from "@/lib/api-v1/acl";
|
||||
import { withApiKey } from "@/lib/api-v1/middleware";
|
||||
import { db } from "@/db";
|
||||
import * as drizzleDb from "@/db";
|
||||
import { eq, desc, isNull, and } from "drizzle-orm";
|
||||
import { logger } from "@/lib/logger";
|
||||
import {getAccessibleDatabaseIds} from "@/lib/api-v1/services/databases";
|
||||
import {ApiKeyContext, ApiKeyContextUser} from "@/lib/api-v1/types";
|
||||
|
||||
const log = logger.child({ module: "api/v1/databases/[id]/backup" });
|
||||
|
||||
async function resolveDatabaseAccess(id: string, userId: string) {
|
||||
const accessibleIds = await getAccessibleDatabaseIds(userId);
|
||||
async function resolveDatabaseAccess(id: string, user: ApiKeyContextUser) {
|
||||
const accessibleIds = await getAccessibleDatabaseIds(user);
|
||||
if (accessibleIds.includes(id)) return "ok";
|
||||
const exists = await db.query.database.findFirst({
|
||||
where: eq(drizzleDb.schemas.database.id, id),
|
||||
@@ -22,9 +23,10 @@ export const GET = withApiKey(
|
||||
async (_req: Request, ctx: ApiKeyContext, params?: Record<string, string>) => {
|
||||
try {
|
||||
const id = params?.id;
|
||||
|
||||
if (!id) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
|
||||
const access = await resolveDatabaseAccess(id, ctx.userId);
|
||||
const access = await resolveDatabaseAccess(id, ctx.user);
|
||||
if (access === "forbidden") return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
if (access === "not_found") return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
|
||||
@@ -50,7 +52,7 @@ export const POST = withApiKey(
|
||||
const id = params?.id;
|
||||
if (!id) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
|
||||
const access = await resolveDatabaseAccess(id, ctx.userId);
|
||||
const access = await resolveDatabaseAccess(id, ctx.user);
|
||||
if (access === "forbidden") return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
if (access === "not_found") return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { withApiKey, ApiKeyContext } from "@/lib/api-v1/middleware";
|
||||
import { getAccessibleDatabaseIds } from "@/lib/api-v1/acl";
|
||||
import { withApiKey } from "@/lib/api-v1/middleware";
|
||||
import { db } from "@/db";
|
||||
import * as drizzleDb from "@/db";
|
||||
import { eq, and, isNull } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { logger } from "@/lib/logger";
|
||||
import {ApiKeyContext} from "@/lib/api-v1/types";
|
||||
import {getAccessibleDatabaseIds} from "@/lib/api-v1/services/databases";
|
||||
|
||||
const log = logger.child({ module: "api/v1/databases/[id]/restore" });
|
||||
|
||||
@@ -20,7 +21,7 @@ export const POST = withApiKey(
|
||||
const id = params?.id;
|
||||
if (!id) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
|
||||
const accessibleIds = await getAccessibleDatabaseIds(ctx.userId);
|
||||
const accessibleIds = await getAccessibleDatabaseIds(ctx.user);
|
||||
if (!accessibleIds.includes(id)) {
|
||||
const exists = await db.query.database.findFirst({
|
||||
where: eq(drizzleDb.schemas.database.id, id),
|
||||
@@ -63,7 +64,6 @@ export const POST = withApiKey(
|
||||
return NextResponse.json({ error: "Backup not found for this database" }, { status: 404 });
|
||||
}
|
||||
|
||||
// Validate backupStorage belongs to backup and is successful
|
||||
const backupStorage = await db.query.backupStorage.findFirst({
|
||||
where: and(
|
||||
eq(drizzleDb.schemas.backupStorage.id, backupStorageId),
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { withApiKey, ApiKeyContext } from "@/lib/api-v1/middleware";
|
||||
import { getAccessibleDatabaseIds } from "@/lib/api-v1/acl";
|
||||
import { withApiKey } from "@/lib/api-v1/middleware";
|
||||
import { db } from "@/db";
|
||||
import * as drizzleDb from "@/db";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { logger } from "@/lib/logger";
|
||||
import {getAccessibleDatabaseIds} from "@/lib/api-v1/services/databases";
|
||||
import {ApiKeyContext} from "@/lib/api-v1/types";
|
||||
|
||||
const log = logger.child({ module: "api/v1/databases/[id]" });
|
||||
|
||||
@@ -14,7 +15,7 @@ export const GET = withApiKey(
|
||||
const id = params?.id;
|
||||
if (!id) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
|
||||
const accessibleIds = await getAccessibleDatabaseIds(ctx.userId);
|
||||
const accessibleIds = await getAccessibleDatabaseIds(ctx.user);
|
||||
if (!accessibleIds.includes(id)) {
|
||||
const exists = await db.query.database.findFirst({
|
||||
where: eq(drizzleDb.schemas.database.id, id),
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { withApiKey, ApiKeyContext } from "@/lib/api-v1/middleware";
|
||||
import { getAccessibleDatabaseIds } from "@/lib/api-v1/acl";
|
||||
import { withApiKey } from "@/lib/api-v1/middleware";
|
||||
import { db } from "@/db";
|
||||
import * as drizzleDb from "@/db";
|
||||
import { eq, desc, and, isNull } from "drizzle-orm";
|
||||
import { logger } from "@/lib/logger";
|
||||
import {ApiKeyContext} from "@/lib/api-v1/types";
|
||||
import {getAccessibleDatabaseIds} from "@/lib/api-v1/services/databases";
|
||||
|
||||
const log = logger.child({ module: "api/v1/databases/[id]/status" });
|
||||
|
||||
@@ -14,7 +15,7 @@ export const GET = withApiKey(
|
||||
const id = params?.id;
|
||||
if (!id) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
|
||||
const accessibleIds = await getAccessibleDatabaseIds(ctx.userId);
|
||||
const accessibleIds = await getAccessibleDatabaseIds(ctx.user);
|
||||
if (!accessibleIds.includes(id)) {
|
||||
const exists = await db.query.database.findFirst({
|
||||
where: eq(drizzleDb.schemas.database.id, id),
|
||||
@@ -26,7 +27,13 @@ export const GET = withApiKey(
|
||||
);
|
||||
}
|
||||
|
||||
const [latestBackup, latestRestoration] = await Promise.all([
|
||||
const [database, latestBackup, latestRestoration] = await Promise.all([
|
||||
db.query.database.findFirst({
|
||||
where: and(
|
||||
eq(drizzleDb.schemas.database.id, id),
|
||||
isNull(drizzleDb.schemas.database.deletedAt)
|
||||
)
|
||||
}),
|
||||
db.query.backup.findFirst({
|
||||
where: and(
|
||||
eq(drizzleDb.schemas.backup.databaseId, id),
|
||||
@@ -43,8 +50,14 @@ export const GET = withApiKey(
|
||||
}),
|
||||
]);
|
||||
|
||||
if (!database){
|
||||
return NextResponse.json({ error: "Database not found" });
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
data: {
|
||||
isWaitingForBackup: database.isWaitingForBackup,
|
||||
lastContact: database.lastContact,
|
||||
latestBackup: latestBackup ?? null,
|
||||
latestRestoration: latestRestoration ?? null,
|
||||
},
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { withApiKey, ApiKeyContext } from "@/lib/api-v1/middleware";
|
||||
import { getAccessibleAgentIds } from "@/lib/api-v1/acl";
|
||||
import { withApiKey } from "@/lib/api-v1/middleware";
|
||||
import { db } from "@/db";
|
||||
import * as drizzleDb from "@/db";
|
||||
import { inArray, and, isNull } from "drizzle-orm";
|
||||
import { logger } from "@/lib/logger";
|
||||
import {getAccessibleAgentIds} from "@/lib/api-v1/services/agents";
|
||||
import {ApiKeyContext} from "@/lib/api-v1/types";
|
||||
|
||||
const log = logger.child({ module: "api/v1/databases" });
|
||||
|
||||
export const GET = withApiKey(async (_req: Request, ctx: ApiKeyContext) => {
|
||||
try {
|
||||
const agentIds = await getAccessibleAgentIds(ctx.userId);
|
||||
const agentIds = await getAccessibleAgentIds(ctx.user);
|
||||
|
||||
if (agentIds.length === 0) {
|
||||
return NextResponse.json({ data: [] });
|
||||
|
||||
Reference in New Issue
Block a user