Start working on the database creation. throw the endpoint with the unique generatedId.

This commit is contained in:
charles-gauthereau
2024-11-27 19:44:16 +01:00
parent 7adaa44c98
commit 319662e39c
5 changed files with 127 additions and 37 deletions
+100 -33
View File
@@ -1,47 +1,114 @@
import {prisma} from "@/prisma";
import {NextResponse} from "next/server";
import {Dbms} from "@prisma/client";
export async function GET(
// 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 Body = {
name: string,
dbms: Dbms,
generatedId: string
}
export async function POST(
request: Request,
{params}: { params: Promise<{ agentId: string }> }
) {
const agentId = (await params).agentId
try {
const agent = await prisma.agent.findFirst({
where: {
id: agentId
const agentId = (await params).agentId
const body: Body = await request.json();
const lastContact = new Date()
if(!isUuidv4(body.generatedId)) {
return NextResponse.json(
{ error: 'generatedId is not a valid uuid' },
{ status: 500 }
);
}
})
if(!agent){
return NextResponse.json({error: "Agent not found"}, {status: 404})
const agent = await prisma.agent.findFirst({
where: {
id: agentId
}
})
if (!agent) {
return NextResponse.json({error: "Agent not found"}, {status: 404})
}
const database = await prisma.database.findFirst({
where: {
generatedId: body.name,
}
})
if(!database){
await prisma.database.create({
data : {
agentId: agent.id,
name: body.name,
dbms: body.dbms,
generatedId: body.generatedId,
lastContact: lastContact,
}
})
}else{
await prisma.database.update({
where: {
id: database.id,
},
data: {
lastContact: lastContact,
}
});
}
const response = {
agent: {
id: agentId,
lastContact: lastContact
},
database:{
generatedId: body.generatedId,
dbms: body.dbms,
},
backup: {
action: true,
cron: ""
},
restore: {
action: false,
file: ""
}
}
return Response.json({
message: response
})
} catch (error) {
console.error('Error in POST handler:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
await prisma.agent.update({
where: {
id: agent.id,
},
data: {
lastContact: new Date(),
}
});
const response = {
agent: {
id: agentId,
lastContact: agent.lastContact
},
backup: {
action: false,
cron : ""
},
restore: {
action: false,
file: ""
}
}
return Response.json({
message: response
})
}
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "databases" ADD COLUMN "last_contact" TIMESTAMP(3);
@@ -0,0 +1,8 @@
/*
Warnings:
- Added the required column `generatedId` to the `databases` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "databases" ADD COLUMN "generatedId" TEXT NOT NULL;
+4 -2
View File
@@ -100,12 +100,14 @@ model Database {
id String @id @default(cuid())
name String
dbms Dbms
generatedId String @unique
description String?
backupPolicy String? @map("backup_policy")
createdAt DateTime @default(now()) @map("created_at")
agentId String @map("agent_id")
agent Agent @relation(fields: [agentId], references: [id], onDelete: Cascade)
agentId String @map("agent_id")
agent Agent @relation(fields: [agentId], references: [id], onDelete: Cascade)
lastContact DateTime? @map("last_contact")
backups Backup[]
restaurations Restauration[]
+13 -2
View File
@@ -51,6 +51,7 @@ export const {handlers, auth: baseAuth, signIn, signOut} = NextAuth({
deleted: {not: true},
}
})
console.log(users.length)
const role = users.length > 0 ? "pending" : "admin"
@@ -96,16 +97,26 @@ export const {handlers, auth: baseAuth, signIn, signOut} = NextAuth({
if (!existingUser) {
// Create a new user with a pending role
const users = await prisma.user.findMany({
where: {
deleted: {not: true},
}
})
const role = users.length > 0 ? "pending" : "admin"
await prisma.user.create({
data: {
email: user.email,
name: user.name,
image: user.image,
role: "pending",
role: role,
authMethod: account.provider,
},
});
return false; // Prevent login if role is pending
return role !== "pending";
}
if (existingUser.role === "pending") {