mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Working on API side.
This commit is contained in:
@@ -0,0 +1,80 @@
|
|||||||
|
import {Agent, Database} from "@prisma/client";
|
||||||
|
import {prisma} from "@/prisma";
|
||||||
|
import {NextResponse} from "next/server";
|
||||||
|
import {Body} from "./route";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Regular expression for UUIDv4
|
||||||
|
const uuidv4Regex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||||
|
|
||||||
|
// Type guard to check if a string is a valid UUIDv4
|
||||||
|
function isUuidv4(value: string): value is string {
|
||||||
|
console.log(value)
|
||||||
|
console.log(uuidv4Regex.test(value))
|
||||||
|
return uuidv4Regex.test(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export async function handleDatabases(body: Body, agent: Agent, lastContact: Date) {
|
||||||
|
const databasesResponse = [];
|
||||||
|
|
||||||
|
const formatDatabase = (database: Database) => ({
|
||||||
|
generatedId: database.generatedId,
|
||||||
|
dbms: database.dbms,
|
||||||
|
data: {
|
||||||
|
backup: {
|
||||||
|
action: true,
|
||||||
|
cron: "",
|
||||||
|
},
|
||||||
|
restore: {
|
||||||
|
action: false,
|
||||||
|
file: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const db of body.databases) {
|
||||||
|
const existingDatabase = await prisma.database.findFirst({
|
||||||
|
where: {
|
||||||
|
generatedId: db.generatedId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!existingDatabase) {
|
||||||
|
if (!isUuidv4(db.generatedId)) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "generatedId is not a valid uuid" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const databaseCreated = await prisma.database.create({
|
||||||
|
data: {
|
||||||
|
agentId: agent.id,
|
||||||
|
name: db.name,
|
||||||
|
dbms: db.dbms,
|
||||||
|
generatedId: db.generatedId,
|
||||||
|
lastContact: lastContact,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (databaseCreated) {
|
||||||
|
databasesResponse.push(formatDatabase(databaseCreated));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const databaseUpdated = await prisma.database.update({
|
||||||
|
where: {
|
||||||
|
id: existingDatabase.id,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
lastContact: lastContact,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
databasesResponse.push(formatDatabase(databaseUpdated));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return databasesResponse;
|
||||||
|
}
|
||||||
@@ -2,17 +2,7 @@ import {prisma} from "@/prisma";
|
|||||||
import {NextRequest, NextResponse} from "next/server";
|
import {NextRequest, NextResponse} from "next/server";
|
||||||
import {Agent, Database, Dbms} from "@prisma/client";
|
import {Agent, Database, Dbms} from "@prisma/client";
|
||||||
import {getFileUrlPresignedLocal} from "@/features/upload/private/upload.action";
|
import {getFileUrlPresignedLocal} from "@/features/upload/private/upload.action";
|
||||||
import {tree} from "next/dist/build/templates/app-page";
|
import {handleDatabases} from "./helpers";
|
||||||
|
|
||||||
// Regular expression for UUIDv4
|
|
||||||
const uuidv4Regex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
||||||
|
|
||||||
// Type guard to check if a string is a valid UUIDv4
|
|
||||||
function isUuidv4(value: string): value is string {
|
|
||||||
console.log(value)
|
|
||||||
console.log(uuidv4Regex.test(value))
|
|
||||||
return uuidv4Regex.test(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export type databaseAgent = {
|
export type databaseAgent = {
|
||||||
@@ -33,14 +23,11 @@ export async function GET(request: Request) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export async function POST(
|
export async function POST(
|
||||||
request: Request,
|
request: Request,
|
||||||
{params}: { params: Promise<{ agentId: string }> }
|
{params}: { params: Promise<{ agentId: string }> }
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const agentId = (await params).agentId
|
const agentId = (await params).agentId
|
||||||
const body: Body = await request.json();
|
const body: Body = await request.json();
|
||||||
const lastContact = new Date()
|
const lastContact = new Date()
|
||||||
@@ -50,14 +37,11 @@ export async function POST(
|
|||||||
id: agentId
|
id: agentId
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!agent) {
|
if (!agent) {
|
||||||
return NextResponse.json({error: "Agent not found"}, {status: 404})
|
return NextResponse.json({error: "Agent not found"}, {status: 404})
|
||||||
}
|
}
|
||||||
|
|
||||||
const databasesResponse = await handleDatabases(body, agent, lastContact)
|
const databasesResponse = await handleDatabases(body, agent, lastContact)
|
||||||
|
|
||||||
|
|
||||||
const response = {
|
const response = {
|
||||||
agent: {
|
agent: {
|
||||||
id: agentId,
|
id: agentId,
|
||||||
@@ -66,10 +50,7 @@ export async function POST(
|
|||||||
databases: databasesResponse
|
databases: databasesResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Response.json(response)
|
||||||
return Response.json({
|
|
||||||
message: response
|
|
||||||
})
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in POST handler:', error);
|
console.error('Error in POST handler:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
@@ -79,69 +60,3 @@ export async function POST(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function handleDatabases(body: Body, agent: Agent, lastContact: Date) {
|
|
||||||
const databasesResponse = [];
|
|
||||||
|
|
||||||
// Helper function to format the database response
|
|
||||||
const formatDatabase = (database) => ({
|
|
||||||
generatedId: database.generatedId,
|
|
||||||
dbms: database.dbms,
|
|
||||||
data: {
|
|
||||||
backup: {
|
|
||||||
action: true,
|
|
||||||
cron: "",
|
|
||||||
},
|
|
||||||
restore: {
|
|
||||||
action: false,
|
|
||||||
file: "",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const db of body.databases) {
|
|
||||||
const existingDatabase = await prisma.database.findFirst({
|
|
||||||
where: {
|
|
||||||
generatedId: db.generatedId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!existingDatabase) {
|
|
||||||
if (!isUuidv4(db.generatedId)) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "generatedId is not a valid uuid" },
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a new database
|
|
||||||
const databaseCreated = await prisma.database.create({
|
|
||||||
data: {
|
|
||||||
agentId: agent.id,
|
|
||||||
name: db.name,
|
|
||||||
dbms: db.dbms,
|
|
||||||
generatedId: db.generatedId,
|
|
||||||
lastContact: lastContact,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (databaseCreated) {
|
|
||||||
databasesResponse.push(formatDatabase(databaseCreated));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Update the existing database
|
|
||||||
const databaseUpdated = await prisma.database.update({
|
|
||||||
where: {
|
|
||||||
id: existingDatabase.id,
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
lastContact: lastContact,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
databasesResponse.push(formatDatabase(databaseUpdated));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return databasesResponse;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
/*
|
||||||
|
Warnings:
|
||||||
|
|
||||||
|
- A unique constraint covering the columns `[generatedId]` on the table `databases` will be added. If there are existing duplicate values, this will fail.
|
||||||
|
|
||||||
|
*/
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "databases_generatedId_key" ON "databases"("generatedId");
|
||||||
+1
-1
@@ -62,7 +62,7 @@ function consoleAscii(){
|
|||||||
console.log("\n" +
|
console.log("\n" +
|
||||||
" ____ __ __ _____ \n" +
|
" ____ __ __ _____ \n" +
|
||||||
" / __ \\ ____ _____ / /_ ____ _ / /_ ____ _ _____ ___ / ___/ ___ _____ _ __ ___ _____\n" +
|
" / __ \\ ____ _____ / /_ ____ _ / /_ ____ _ _____ ___ / ___/ ___ _____ _ __ ___ _____\n" +
|
||||||
" / /_/ // __ \\ / ___// __// __ `// __ \\ / __ `// ___// _ \\ \\__ \\ / _ \\ / ___/| | / // _ \\ / ___/\n" +
|
" / /_/ // __ \\ / ___// __// __ // __ \\ / __ // ___// _ \\ \\__ \\ / _ \\ / ___/| | / // _ \\ / ___/\n" +
|
||||||
" / ____// /_/ // / / /_ / /_/ // /_/ // /_/ /(__ )/ __/ ___/ // __// / | |/ // __// / \n" +
|
" / ____// /_/ // / / /_ / /_/ // /_/ // /_/ /(__ )/ __/ ___/ // __// / | |/ // __// / \n" +
|
||||||
"/_/ \\____//_/ \\__/ \\__,_//_.___/ \\__,_//____/ \\___/ /____/ \\___//_/ |___/ \\___//_/ \n" +
|
"/_/ \\____//_/ \\__/ \\__,_//_.___/ \\__,_//____/ \\___/ /____/ \\___//_/ |___/ \\___//_/ \n" +
|
||||||
" \n")
|
" \n")
|
||||||
|
|||||||
Reference in New Issue
Block a user