feat: options to clone the workflow

This commit is contained in:
BennyKok
2024-01-18 00:18:36 +08:00
parent 20635612bb
commit 56eb91d102
5 changed files with 144 additions and 27 deletions
+46
View File
@@ -0,0 +1,46 @@
import { db } from "@/db/db";
import type { WorkflowVersionType } from "@/db/schema";
import { workflowTable, workflowVersionTable } from "@/db/schema";
export async function createNewWorkflow({
workflow_name,
user_id,
org_id,
workflowData,
}: {
workflow_name: string;
user_id: string;
org_id?: string;
workflowData: Pick<
WorkflowVersionType,
"workflow" | "workflow_api" | "snapshot"
>;
}) {
// Create a new parent workflow
const workflow_parent = await db
.insert(workflowTable)
.values({
user_id,
name: workflow_name,
org_id: org_id,
})
.returning();
const workflow_id = workflow_parent[0].id;
// Create a new version
const data = await db
.insert(workflowVersionTable)
.values({
workflow_id: workflow_id,
version: 1,
...workflowData,
})
.returning();
const version = data[0].version;
return {
workflow_id,
version,
};
}
+44 -1
View File
@@ -3,10 +3,12 @@
import { db } from "@/db/db";
import type { DeploymentType } from "@/db/schema";
import { deploymentsTable, workflowTable } from "@/db/schema";
import { createNewWorkflow } from "@/server/createNewWorkflow";
import { withServerPromise } from "@/server/withServerPromise";
import { auth } from "@clerk/nextjs";
import { and, eq, isNull } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import "server-only";
export async function createDeployments(
@@ -18,6 +20,10 @@ export async function createDeployments(
const { userId } = auth();
if (!userId) throw new Error("No user id");
if (!machine_id) {
throw new Error("No machine id provided");
}
// Same environment and same workflow
const existingDeployment = await db.query.deploymentsTable.findFirst({
where: and(
@@ -108,7 +114,6 @@ export async function findSharedDeployment(workflow_id: string) {
export const removePublicShareDeployment = withServerPromise(
async (deployment_id: string) => {
// throw new Error("Not implemented");
await db
.delete(deploymentsTable)
.where(
@@ -119,3 +124,41 @@ export const removePublicShareDeployment = withServerPromise(
);
}
);
export const cloneWorkflow = withServerPromise(
async (deployment_id: string) => {
const deployment = await db.query.deploymentsTable.findFirst({
where: and(
eq(deploymentsTable.environment, "public-share"),
eq(deploymentsTable.id, deployment_id)
),
with: {
version: true,
workflow: true,
},
});
if (!deployment) throw new Error("No deployment found");
const { userId, orgId } = auth();
if (!userId) throw new Error("No user id");
await createNewWorkflow({
user_id: userId,
org_id: orgId,
workflow_name: `${deployment.workflow.name} (Cloned)`,
workflowData: {
workflow: deployment.version.workflow,
workflow_api: deployment?.version.workflow_api,
snapshot: deployment?.version.snapshot,
},
});
redirect(`/workflows/${deployment.workflow.id}`);
return {
message: "Successfully cloned workflow",
};
}
);