fix: endpoints

This commit is contained in:
charles-gauthereau
2026-05-27 22:02:22 +02:00
parent 56517226b6
commit c1b71e7772
18 changed files with 941 additions and 457 deletions
@@ -2,49 +2,60 @@ import { NextResponse } from "next/server";
import { withApiKey } from "@/lib/api-v1/middleware";
import { db } from "@/db";
import * as drizzleDb from "@/db";
import { eq, and, isNull } from "drizzle-orm";
import { and, eq, isNull } from "drizzle-orm";
import { logger } from "@/lib/logger";
import {getAccessibleDatabaseIds} from "@/lib/api-v1/services/databases";
import {ApiKeyContext} from "@/lib/api-v1/types";
import { ApiKeyContext } from "@/lib/api-v1/types";
import {requireDatabaseAccess} from "@/lib/api-v1/services/databases";
const log = logger.child({ module: "api/v1/databases/[id]/backup/[backupId]" });
const log = logger.child({
module: "api/v1/databases/[id]/backup/[backupId]",
});
export const GET = withApiKey(
async (_req: Request, ctx: ApiKeyContext, params?: Record<string, string>) => {
try {
const { id, backupId } = params ?? {};
if (!id || !backupId) return NextResponse.json({ error: "Not found" }, { status: 404 });
async (_req: Request, ctx: ApiKeyContext, params?: Record<string, string>) => {
try {
const guard = await requireDatabaseAccess(params, ctx.user);
const accessibleIds = await getAccessibleDatabaseIds(ctx.user);
if (!accessibleIds.includes(id)) {
const exists = await db.query.database.findFirst({
where: eq(drizzleDb.schemas.database.id, id),
columns: { id: true },
if (!guard.ok) {
return guard.response;
}
const { id } = guard.data;
const backupId = params?.backupId;
if (!backupId) {
return NextResponse.json({ error: "Not found" }, { status: 404 });
}
const backup = await db.query.backup.findFirst({
where: and(
eq(drizzleDb.schemas.backup.id, backupId),
eq(drizzleDb.schemas.backup.databaseId, id),
isNull(drizzleDb.schemas.backup.deletedAt)
),
with: {
storages: {
where: (backupStorage, { isNull }) =>
isNull(backupStorage.deletedAt),
},
},
});
if (!backup) {
return NextResponse.json({ error: "Not found" }, { status: 404 });
}
return NextResponse.json({ data: backup });
} catch (error) {
log.error(
{ error },
"Error in GET /api/v1/databases/[id]/backup/[backupId]"
);
return NextResponse.json(
{ error: exists ? "Forbidden" : "Not found" },
{ status: exists ? 403 : 404 }
{ error: "Internal server error" },
{ status: 500 }
);
}
const backup = await db.query.backup.findFirst({
where: and(
eq(drizzleDb.schemas.backup.id, backupId),
eq(drizzleDb.schemas.backup.databaseId, id),
isNull(drizzleDb.schemas.backup.deletedAt)
),
with: {
storages: {
where: (bs, { isNull }) => isNull(bs.deletedAt),
},
},
});
if (!backup) return NextResponse.json({ error: "Not found" }, { status: 404 });
return NextResponse.json({ data: backup });
} catch (error) {
log.error({ error }, "Error in GET /api/v1/databases/[id]/backup/[backupId]");
return NextResponse.json({ error: "Internal server error" }, { status: 500 });
}
}
);
);
+74 -52
View File
@@ -2,73 +2,95 @@ import { NextResponse } from "next/server";
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 {and, desc, eq, inArray, isNull} from "drizzle-orm";
import { logger } from "@/lib/logger";
import {getAccessibleDatabaseIds} from "@/lib/api-v1/services/databases";
import {ApiKeyContext, ApiKeyContextUser} from "@/lib/api-v1/types";
import { ApiKeyContext } from "@/lib/api-v1/types";
import {requireDatabaseAccess} from "@/lib/api-v1/services/databases";
const log = logger.child({ module: "api/v1/databases/[id]/backup" });
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),
columns: { id: true },
});
return exists ? "forbidden" : "not_found";
}
export const GET = withApiKey(
async (_req: Request, ctx: ApiKeyContext, params?: Record<string, string>) => {
try {
const id = params?.id;
async (_req: Request, ctx: ApiKeyContext, params?: Record<string, string>) => {
try {
const guard = await requireDatabaseAccess(params, ctx.user);
if (!id) return NextResponse.json({ error: "Not found" }, { status: 404 });
if (!guard.ok) {
return guard.response;
}
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 });
const { id } = guard.data;
const backups = await db.query.backup.findMany({
where: and(
eq(drizzleDb.schemas.backup.databaseId, id),
isNull(drizzleDb.schemas.backup.deletedAt)
),
orderBy: [desc(drizzleDb.schemas.backup.createdAt)],
});
const backups = await db.query.backup.findMany({
where: and(
eq(drizzleDb.schemas.backup.databaseId, id),
isNull(drizzleDb.schemas.backup.deletedAt)
),
orderBy: [desc(drizzleDb.schemas.backup.createdAt)],
});
return NextResponse.json({ data: backups });
} catch (error) {
log.error({ error }, "Error in GET /api/v1/databases/[id]/backup");
return NextResponse.json({ error: "Internal server error" }, { status: 500 });
return NextResponse.json({ data: backups });
} catch (error) {
log.error({ error }, "Error in GET /api/v1/databases/[id]/backup");
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 }
);
}
}
}
);
export const POST = 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 });
async (_req: Request, ctx: ApiKeyContext, params?: Record<string, string>) => {
try {
const guard = await requireDatabaseAccess(params, ctx.user);
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 });
if (!guard.ok) {
return guard.response;
}
const [createdBackup] = await db
.insert(drizzleDb.schemas.backup)
.values({ databaseId: id, status: "waiting" })
.returning();
const { id } = guard.data;
if (!createdBackup) {
return NextResponse.json({ error: "Failed to create backup" }, { status: 500 });
const existingBackup = await db.query.backup.findFirst({
where: and(
eq(drizzleDb.schemas.backup.databaseId, id),
inArray(drizzleDb.schemas.backup.status, ["waiting", "ongoing"])
),
});
if (existingBackup) {
return NextResponse.json(
{
error:
"A backup is already waiting or ongoing for this database",
},
{ status: 409 }
);
}
const [createdBackup] = await db
.insert(drizzleDb.schemas.backup)
.values({
databaseId: id,
status: "waiting",
})
.returning();
if (!createdBackup) {
return NextResponse.json(
{ error: "Failed to create backup" },
{ status: 500 }
);
}
return NextResponse.json({ data: createdBackup }, { status: 201 });
} catch (error) {
log.error({ error }, "Error in POST /api/v1/databases/[id]/backup");
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 }
);
}
return NextResponse.json({ data: createdBackup }, { status: 201 });
} catch (error) {
log.error({ error }, "Error in POST /api/v1/databases/[id]/backup");
return NextResponse.json({ error: "Internal server error" }, { status: 500 });
}
}
);
);