mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Working on cron api for portabase agent.
This commit is contained in:
@@ -4,126 +4,145 @@ import {prisma} from "@/prisma";
|
||||
import {isUuidv4} from "@/utils/verify-uuid";
|
||||
import {uploadLocalPrivate} from "@/features/upload/private/upload.action";
|
||||
import {v4 as uuidv4} from "uuid";
|
||||
|
||||
import {Backup, Database} from "@prisma/client";
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{params}: { params: Promise<{ agentId: string }> }
|
||||
{ params }: { params: Promise<{ agentId: string }> }
|
||||
) {
|
||||
|
||||
try {
|
||||
|
||||
const contentType = request.headers.get("Content-Type");
|
||||
|
||||
// Ensure `contentType` is not null before checking
|
||||
if (!contentType || (!contentType.includes("multipart/form-data"))) {
|
||||
if (!contentType || !contentType.includes("multipart/form-data")) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Unsupported or missing Content-Type' },
|
||||
{ error: "Unsupported or missing Content-Type" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const agentId = (await params).agentId
|
||||
const agentId = (await params).agentId;
|
||||
const formData = await request.formData();
|
||||
const generatedId = formData.get("generatedId") as string;
|
||||
console.log(formData)
|
||||
const generatedId = formData.get("generatedId") as string | null;
|
||||
const method = formData.get("method") as string | null;
|
||||
|
||||
|
||||
if (!isUuidv4(generatedId)) {
|
||||
if (!generatedId || !isUuidv4(generatedId)) {
|
||||
return NextResponse.json(
|
||||
{error: "generatedId is not a valid uuid"},
|
||||
{status: 500}
|
||||
{ error: "generatedId is not a valid UUID" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
const agent = await prisma.agent.findFirst({
|
||||
where: {
|
||||
id: agentId
|
||||
}
|
||||
})
|
||||
where: { id: agentId },
|
||||
});
|
||||
|
||||
if (!agent) {
|
||||
return NextResponse.json({error: "Agent not found"}, {status: 404})
|
||||
return NextResponse.json(
|
||||
{ error: "Agent not found" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const database = await prisma.database.findFirst({
|
||||
where: {
|
||||
generatedId: generatedId
|
||||
}
|
||||
})
|
||||
where: { generatedId },
|
||||
});
|
||||
|
||||
if (!database) {
|
||||
return NextResponse.json({error: "Database associated with generatedId provided not found"}, {status: 404})
|
||||
return NextResponse.json(
|
||||
{ error: "Database associated with generatedId not found" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const backup = await prisma.backup.findFirst({
|
||||
where: {
|
||||
status : "ongoing",
|
||||
databaseId: database.id
|
||||
let backup: Backup | null = null;
|
||||
|
||||
if (method === "automatic") {
|
||||
backup = await prisma.backup.create({
|
||||
data: {
|
||||
status: "ongoing",
|
||||
databaseId: database.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!backup) {
|
||||
return NextResponse.json(
|
||||
{ error: "Unable to create an automatic backup" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
} else {
|
||||
backup = await prisma.backup.findFirst({
|
||||
where: {
|
||||
status: "ongoing",
|
||||
databaseId: database.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!backup) {
|
||||
return NextResponse.json(
|
||||
{ error: "Unable to find the corresponding backup" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
})
|
||||
if (!backup) {
|
||||
return NextResponse.json({error: "Unable to fin the corresponding backup"}, {status: 404})
|
||||
}
|
||||
|
||||
const status = formData.get("status") as string;
|
||||
const status = formData.get("status") as string | null;
|
||||
|
||||
if(status === "success"){
|
||||
if (status === "success") {
|
||||
const file = formData.get("file") as File | null;
|
||||
|
||||
const file = formData.get("file") as File
|
||||
const uuid = uuidv4()
|
||||
const fileName = `${uuid}.dump`
|
||||
if (!file) {
|
||||
return NextResponse.json(
|
||||
{ error: "File is required for successful backup" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const uuid = uuidv4();
|
||||
const fileName = `${uuid}.dump`;
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
|
||||
console.log(buffer)
|
||||
const { success, message, filePath } = await uploadLocalPrivate(fileName, buffer);
|
||||
|
||||
const {success, message, filePath} = await uploadLocalPrivate(fileName, buffer)
|
||||
console.log(success, message, filePath)
|
||||
if (!success){
|
||||
if (!success) {
|
||||
return NextResponse.json(
|
||||
{error: message},
|
||||
{status: 500}
|
||||
{ error: message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
await prisma.backup.update({
|
||||
where: {
|
||||
id : backup.id,
|
||||
},
|
||||
where: { id: backup.id },
|
||||
data: {
|
||||
file: fileName,
|
||||
status: "success"
|
||||
}
|
||||
})
|
||||
|
||||
const response = {
|
||||
message: true,
|
||||
details: "Backup successfully uploaded"
|
||||
}
|
||||
return Response.json(response, {status: 200})
|
||||
|
||||
}else{
|
||||
await prisma.backup.update({
|
||||
where: {
|
||||
id : backup.id,
|
||||
status: "success",
|
||||
},
|
||||
data: {
|
||||
status: "failed"
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
const response = {
|
||||
message: true,
|
||||
details: "Backup successfully updated with status failed"
|
||||
}
|
||||
return Response.json(response , {status: 200})
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: "Backup successfully uploaded",
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
} else {
|
||||
await prisma.backup.update({
|
||||
where: { id: backup.id },
|
||||
data: { status: "failed" },
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: "Backup successfully updated with status failed",
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error in POST handler:', error);
|
||||
console.error("Error in POST handler:", error);
|
||||
return NextResponse.json(
|
||||
{error: 'Internal server error'},
|
||||
{status: 500}
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ export async function handleDatabases(body: Body, agent: Agent, lastContact: Dat
|
||||
data: {
|
||||
backup: {
|
||||
action: backupAction,
|
||||
cron: "",
|
||||
cron: database.backupPolicy,
|
||||
},
|
||||
restore: {
|
||||
action: restoreAction,
|
||||
@@ -59,7 +59,7 @@ export async function handleDatabases(body: Body, agent: Agent, lastContact: Dat
|
||||
});
|
||||
|
||||
if (databaseCreated) {
|
||||
databasesResponse.push(formatDatabase(databaseCreated, backupAction,restoreAction , UrlBackup));
|
||||
databasesResponse.push(formatDatabase(databaseCreated, backupAction,restoreAction, UrlBackup));
|
||||
}
|
||||
} else {
|
||||
const databaseUpdated = await prisma.database.update({
|
||||
@@ -122,5 +122,6 @@ export async function handleDatabases(body: Body, agent: Agent, lastContact: Dat
|
||||
databasesResponse.push(formatDatabase(databaseUpdated, backupAction, restoreAction, UrlBackup));
|
||||
}
|
||||
}
|
||||
|
||||
return databasesResponse;
|
||||
}
|
||||
@@ -45,6 +45,7 @@
|
||||
"@t3-oss/env-nextjs": "^0.11.1",
|
||||
"@tanstack/react-query": "^5.59.18",
|
||||
"@tanstack/react-table": "^8.20.5",
|
||||
"@types/ws": "^8.5.13",
|
||||
"argon2": "^0.41.1",
|
||||
"bcrypt": "^5.1.1",
|
||||
"class-variance-authority": "^0.7.0",
|
||||
@@ -70,11 +71,14 @@
|
||||
"react-resizable-panels": "^2.1.6",
|
||||
"react-twc": "^1.4.2",
|
||||
"recharts": "^2.13.3",
|
||||
"socket.io": "^4.8.1",
|
||||
"socket.io-client": "^4.8.1",
|
||||
"sonner": "^1.6.1",
|
||||
"tailwind-merge": "^2.5.4",
|
||||
"tailwindcss-animate": "^1.0.7",
|
||||
"uuid": "^11.0.3",
|
||||
"vaul": "^1.1.1",
|
||||
"ws": "^8.18.0",
|
||||
"zod": "^3.23.8",
|
||||
"zustand": "^5.0.2"
|
||||
},
|
||||
|
||||
@@ -21,7 +21,10 @@ import {
|
||||
import {toast} from "sonner";
|
||||
import {useRouter} from "next/navigation";
|
||||
import {Database} from "@prisma/client";
|
||||
import {updateBackupPolicyAction} from "@/components/wrappers/Database/CronButton/cron.action";
|
||||
import {
|
||||
updateBackupPolicyAction,
|
||||
updateDatabaseBackupPolicyAction
|
||||
} from "@/components/wrappers/Database/CronButton/cron.action";
|
||||
|
||||
|
||||
export type CronButtonProps = {
|
||||
@@ -32,8 +35,8 @@ export const CronButton = (props: CronButtonProps) => {
|
||||
const router = useRouter();
|
||||
const [isSwitched, setIsSwitched] = useState(props.database.backupPolicy !== null);
|
||||
|
||||
const updateBackupPolicy = useMutation({
|
||||
mutationFn: (value: string) => updateBackupPolicyAction({databaseId: props.database.id, backupPolicy:value}),
|
||||
const updateDatabaseBackupPolicy = useMutation({
|
||||
mutationFn: (value: string) => updateDatabaseBackupPolicyAction({databaseId: props.database.id, backupPolicy:value}),
|
||||
onSuccess: () => {
|
||||
toast.success(`Method updated successfully.`);
|
||||
router.refresh()
|
||||
@@ -47,7 +50,7 @@ export const CronButton = (props: CronButtonProps) => {
|
||||
const handleTypeChange = async (state: boolean) => {
|
||||
setIsSwitched(state);
|
||||
if(state == false) {
|
||||
await updateBackupPolicy.mutateAsync(null)
|
||||
await updateDatabaseBackupPolicy.mutateAsync("")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { AdvancedCronSelect } from "./AdvancedCronSelect";
|
||||
import {updateBackupPolicyAction} from "@/components/wrappers/Database/CronButton/cron.action";
|
||||
import {
|
||||
updateBackupPolicyAction,
|
||||
updateDatabaseBackupPolicyAction
|
||||
} from "@/components/wrappers/Database/CronButton/cron.action";
|
||||
import {useMutation} from "@tanstack/react-query";
|
||||
import {useState} from "react";
|
||||
import {useRouter} from "next/navigation";
|
||||
@@ -18,7 +21,7 @@ export const CronInput = ({ database }: CronInputProps) => {
|
||||
const router = useRouter();
|
||||
|
||||
const updateBackupPolicy = useMutation({
|
||||
mutationFn: (value: string) => updateBackupPolicyAction({ databaseId: database.id, backupPolicy: value }),
|
||||
mutationFn: (value: string) => updateDatabaseBackupPolicyAction({ databaseId: database.id, backupPolicy: value }),
|
||||
onSuccess: () => {
|
||||
toast.success(`Cron updated successfully.`);
|
||||
router.refresh();
|
||||
|
||||
@@ -4,7 +4,7 @@ import {z} from "zod";
|
||||
import {prisma} from "@/prisma";
|
||||
|
||||
|
||||
export const updateBackupPolicyAction = userAction
|
||||
export const updateDatabaseBackupPolicyAction = userAction
|
||||
.schema(
|
||||
z.object({
|
||||
databaseId: z.string(),
|
||||
@@ -13,12 +13,14 @@ export const updateBackupPolicyAction = userAction
|
||||
)
|
||||
)
|
||||
.action(async ({parsedInput, ctx}) => {
|
||||
const cronPolicy = parsedInput.backupPolicy == "" ? null : parsedInput.backupPolicy
|
||||
|
||||
const updatedDatabase = await prisma.database.update({
|
||||
where: {
|
||||
id: parsedInput.databaseId,
|
||||
},
|
||||
data: {
|
||||
backupPolicy: parsedInput.backupPolicy,
|
||||
backupPolicy: cronPolicy,
|
||||
}
|
||||
})
|
||||
return {
|
||||
|
||||
@@ -1726,6 +1726,13 @@
|
||||
dependencies:
|
||||
csstype "^3.0.2"
|
||||
|
||||
"@types/ws@^8.5.13":
|
||||
version "8.5.13"
|
||||
resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.13.tgz#6414c280875e2691d0d1e080b05addbf5cb91e20"
|
||||
integrity sha512-osM/gWBTPKgHV8XkTunnegTRIsvF6owmf5w+JtAfOw472dptdm0dlGv4xCt6GwQRcC2XVOvvRE/0bAoQcL2QkA==
|
||||
dependencies:
|
||||
"@types/node" "*"
|
||||
|
||||
"@typescript-eslint/eslint-plugin@^5.4.2 || ^6.0.0 || ^7.0.0 || ^8.0.0":
|
||||
version "8.13.0"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-8.13.0.tgz#650c50b8c795b5d092189f139f6d00535b5b0f3d"
|
||||
@@ -2741,6 +2748,17 @@ end-of-stream@^1.1.0, end-of-stream@^1.4.1:
|
||||
dependencies:
|
||||
once "^1.4.0"
|
||||
|
||||
engine.io-client@~6.6.1:
|
||||
version "6.6.2"
|
||||
resolved "https://registry.yarnpkg.com/engine.io-client/-/engine.io-client-6.6.2.tgz#e0a09e1c90effe5d6264da1c56d7281998f1e50b"
|
||||
integrity sha512-TAr+NKeoVTjEVW8P3iHguO1LO6RlUz9O5Y8o7EY0fU+gY1NYqas7NN3slpFtbXEsLMHk0h90fJMfKjRkQ0qUIw==
|
||||
dependencies:
|
||||
"@socket.io/component-emitter" "~3.1.0"
|
||||
debug "~4.3.1"
|
||||
engine.io-parser "~5.2.1"
|
||||
ws "~8.17.1"
|
||||
xmlhttprequest-ssl "~2.1.1"
|
||||
|
||||
engine.io-parser@~5.2.1:
|
||||
version "5.2.3"
|
||||
resolved "https://registry.yarnpkg.com/engine.io-parser/-/engine.io-parser-5.2.3.tgz#00dc5b97b1f233a23c9398d0209504cf5f94d92f"
|
||||
@@ -5100,6 +5118,16 @@ socket.io-adapter@~2.5.2:
|
||||
debug "~4.3.4"
|
||||
ws "~8.17.1"
|
||||
|
||||
socket.io-client@^4.8.1:
|
||||
version "4.8.1"
|
||||
resolved "https://registry.yarnpkg.com/socket.io-client/-/socket.io-client-4.8.1.tgz#1941eca135a5490b94281d0323fe2a35f6f291cb"
|
||||
integrity sha512-hJVXfu3E28NmzGk8o1sHhN3om52tRvwYeidbj7xKy2eIIse5IoKX3USlS6Tqt3BHAtflLIkCQBkzVrEEfWUyYQ==
|
||||
dependencies:
|
||||
"@socket.io/component-emitter" "~3.1.0"
|
||||
debug "~4.3.2"
|
||||
engine.io-client "~6.6.1"
|
||||
socket.io-parser "~4.2.4"
|
||||
|
||||
socket.io-parser@~4.2.4:
|
||||
version "4.2.4"
|
||||
resolved "https://registry.yarnpkg.com/socket.io-parser/-/socket.io-parser-4.2.4.tgz#c806966cf7270601e47469ddeec30fbdfda44c83"
|
||||
@@ -5121,6 +5149,19 @@ socket.io@4.8.0:
|
||||
socket.io-adapter "~2.5.2"
|
||||
socket.io-parser "~4.2.4"
|
||||
|
||||
socket.io@^4.8.1:
|
||||
version "4.8.1"
|
||||
resolved "https://registry.yarnpkg.com/socket.io/-/socket.io-4.8.1.tgz#fa0eaff965cc97fdf4245e8d4794618459f7558a"
|
||||
integrity sha512-oZ7iUCxph8WYRHHcjBEc9unw3adt5CmSNlppj/5Q4k2RIrhl8Z5yY2Xr4j9zj0+wzVZ0bxmYoGSzKJnRl6A4yg==
|
||||
dependencies:
|
||||
accepts "~1.3.4"
|
||||
base64id "~2.0.0"
|
||||
cors "~2.8.5"
|
||||
debug "~4.3.2"
|
||||
engine.io "~6.6.0"
|
||||
socket.io-adapter "~2.5.2"
|
||||
socket.io-parser "~4.2.4"
|
||||
|
||||
sonner@^1.6.1:
|
||||
version "1.7.0"
|
||||
resolved "https://registry.yarnpkg.com/sonner/-/sonner-1.7.0.tgz#f59a2a70e049a179b6fbd1bb1bf2619d5ced07c0"
|
||||
@@ -5788,6 +5829,11 @@ wrappy@1:
|
||||
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
|
||||
integrity sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==
|
||||
|
||||
ws@^8.18.0:
|
||||
version "8.18.0"
|
||||
resolved "https://registry.yarnpkg.com/ws/-/ws-8.18.0.tgz#0d7505a6eafe2b0e712d232b42279f53bc289bbc"
|
||||
integrity sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==
|
||||
|
||||
ws@~8.17.1:
|
||||
version "8.17.1"
|
||||
resolved "https://registry.yarnpkg.com/ws/-/ws-8.17.1.tgz#9293da530bb548febc95371d90f9c878727d919b"
|
||||
@@ -5806,6 +5852,11 @@ xmlbuilder@~11.0.0:
|
||||
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-11.0.1.tgz#be9bae1c8a046e76b31127726347d0ad7002beb3"
|
||||
integrity sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==
|
||||
|
||||
xmlhttprequest-ssl@~2.1.1:
|
||||
version "2.1.2"
|
||||
resolved "https://registry.yarnpkg.com/xmlhttprequest-ssl/-/xmlhttprequest-ssl-2.1.2.tgz#e9e8023b3f29ef34b97a859f584c5e6c61418e23"
|
||||
integrity sha512-TEU+nJVUUnA4CYJFLvK5X9AOeH4KvDvhIfm0vV1GaQRtchnG0hgK5p8hw/xjv8cunWYCsiPCSDzObPyhEwq3KQ==
|
||||
|
||||
yallist@^3.0.2:
|
||||
version "3.1.1"
|
||||
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.1.1.tgz#dbb7daf9bfd8bac9ab45ebf602b8cbad0d5d08fd"
|
||||
|
||||
Reference in New Issue
Block a user