migration

This commit is contained in:
Théo LAGACHE
2025-05-16 15:54:17 +02:00
parent 01019fe1a3
commit 6f2523e524
285 changed files with 15492 additions and 46090 deletions
@@ -1,28 +1,24 @@
"use client";
import {Card, CardContent} from "@/components/ui/card";
import {
FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, useZodForm
} from "@/components/ui/form";
import {Input} from "@/components/ui/input";
import {Form} from "@/components/ui/form"
import {Button} from "@/components/ui/button";
import {useRouter} from "next/navigation";
import {useMutation} from "@tanstack/react-query";
import {TooltipProvider} from "@/components/ui/tooltip";
import {AgentSchema, AgentType} from "@/components/wrappers/dashboard/agent/AgentForm/agent-form.schema";
import {toast} from "sonner";
import {createAgentAction, updateAgentAction} from "@/components/wrappers/dashboard/agent/AgentForm/agent-form.action";
import { Card, CardContent } from "@/components/ui/card";
import { FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, useZodForm } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Form } from "@/components/ui/form";
import { Button } from "@/components/ui/button";
import { useRouter } from "next/navigation";
import { useMutation } from "@tanstack/react-query";
import { TooltipProvider } from "@/components/ui/tooltip";
import { AgentSchema, AgentType } from "@/components/wrappers/dashboard/agent/AgentForm/agent-form.schema";
import { toast } from "sonner";
import { createAgentAction, updateAgentAction } from "@/components/wrappers/dashboard/agent/AgentForm/agent-form.action";
export type agentFormProps = {
defaultValues?: AgentType;
agentId?: string;
}
};
export const AgentForm = (props: agentFormProps) => {
const isCreate = !Boolean(props.defaultValues)
// const defaultValues = isCreate ? {slug: ""} : props.defaultValues
const isCreate = !Boolean(props.defaultValues);
const form = useZodForm({
schema: AgentSchema,
@@ -33,14 +29,16 @@ export const AgentForm = (props: agentFormProps) => {
const mutation = useMutation({
mutationFn: async (values: AgentType) => {
console.log("values", values)
console.log("values", values);
const createAgent = isCreate ? await createAgentAction(values) : await updateAgentAction({
id: props.agentId ?? "-",
data: values
});
const createAgent = isCreate
? await createAgentAction(values)
: await updateAgentAction({
id: props.agentId ?? "-",
data: values,
});
const data = createAgent?.data?.data
const data = createAgent?.data?.data;
if (createAgent?.serverError || !data) {
console.log(createAgent?.serverError);
toast.error(createAgent?.serverError);
@@ -48,83 +46,78 @@ export const AgentForm = (props: agentFormProps) => {
}
toast.success(`Success`);
router.push(`/dashboard/agents/${data.id}`);
router.refresh()
}
})
router.refresh();
},
});
return (
<TooltipProvider>
<Card>
<CardContent>
<Form form={form}
className="flex flex-col gap-4 mt-3"
onSubmit={async (values) => {
await mutation.mutateAsync(values);
}}
<Form
form={form}
className="flex flex-col gap-4 mt-3"
onSubmit={async (values) => {
await mutation.mutateAsync(values);
}}
>
<FormField
control={form.control}
name="name"
defaultValue=""
render={({field}) => (
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
<Input
placeholder="Agent 1" {...field} />
<Input placeholder="Agent 1" {...field} />
</FormControl>
<FormDescription>Your agent project name</FormDescription>
<FormMessage/>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
defaultValue=""
name="slug"
render={({field}) => (
render={({ field }) => (
<FormItem>
<FormLabel>Slug</FormLabel>
<FormControl>
<Input
value={field.value ?? ""}
placeholder="agent-1" {...field}
//value={field.value ?? ""}
placeholder="agent-1"
{...field}
onChange={(e) => {
const value = e.target.value.replaceAll(" ", "-").toLowerCase()
field.onChange(value)
const value = e.target.value.replaceAll(" ", "-").toLowerCase();
field.onChange(value);
}}
/>
</FormControl>
<FormDescription>The slug is used in the url of the agent</FormDescription>
<FormMessage/>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
defaultValue=""
name="description"
render={({field}) => (
render={({ field }) => (
<FormItem>
<FormLabel>Description</FormLabel>
<FormControl>
<Input
placeholder='This agent is for the client exemple.com' {...field}
value={field.value ?? ""}/>
<Input placeholder="This agent is for the client exemple.com" {...field} value={field.value ?? ""} />
</FormControl>
<FormDescription>Enter your project agent description</FormDescription>
<FormMessage/>
<FormMessage />
</FormItem>
)}
/>
<Button>
{isCreate ? `Create agent` : `Save agent`}
</Button>
<Button>{isCreate ? `Create agent` : `Save agent`}</Button>
</Form>
</CardContent>
</Card>
</TooltipProvider>
)
}
);
};
@@ -1,68 +1,44 @@
"use server"
import {ActionError, userAction} from "@/safe-actions";
import {prisma} from "@/prisma";
import {AgentSchema} from "@/components/wrappers/dashboard/agent/AgentForm/agent-form.schema";
import {z} from "zod";
"use server";
import { ActionError, userAction } from "@/safe-actions";
import { AgentSchema } from "@/components/wrappers/dashboard/agent/AgentForm/agent-form.schema";
import { z } from "zod";
import { eq, and, ne, count } from "drizzle-orm";
import { db } from "@/db";
import { agent } from "@/db/schema";
const verifySlugUniqueness = async (slug: string, agentId?: string) => {
const slugExists = await prisma.agent.count({
where: {
slug: slug,
id: agentId ? {
not: agentId
} : undefined,
},
})
const conditions = agentId ? and(eq(agent.slug, slug), ne(agent.id, agentId)) : eq(agent.slug, slug);
console.log(slugExists)
if (slugExists) {
const [countResult] = await db.select({ count: count() }).from(agent).where(conditions);
if (countResult.count > 0) {
throw new ActionError("Slug already exists");
}
}
};
export const createAgentAction = userAction.schema(AgentSchema).action(async ({ parsedInput }) => {
await verifySlugUniqueness(parsedInput.slug);
export const createAgentAction = userAction
.schema(AgentSchema)
.action(async ({parsedInput, ctx}) => {
// Verify if slug already exist
await verifySlugUniqueness(parsedInput.slug);
const agent = await prisma.agent.create({
data: {
...parsedInput
}
})
// await sendEmailIfUserCreatedFirstForm(ctx.user)
return {
data: agent,
}
});
const [createdAgent] = await db.insert(agent).values(parsedInput).returning();
return {
data: createdAgent,
};
});
export const updateAgentAction = userAction
.schema(
z.object({
id: z.string(),
data: AgentSchema,
}
)
id: z.string(),
data: AgentSchema,
})
)
.action(async ({parsedInput, ctx}) => {
.action(async ({ parsedInput }) => {
await verifySlugUniqueness(parsedInput.data.slug, parsedInput.id);
console.log("parsedInput", parsedInput.data)
const updatedAgent = await prisma.agent.update({
where: {
id: parsedInput.id,
},
data: parsedInput.data,
})
const [updatedAgent] = await db.update(agent).set(parsedInput.data).where(eq(agent.id, parsedInput.id)).returning();
return {
data: updatedAgent,
}
})
};
});
@@ -1,9 +1,13 @@
import {z} from "zod";
import { z } from "zod";
export const AgentSchema = z.object({
name: z.string(),
slug: z.string().regex(/^[a-zA-Z0-9_-]*$/).min(5).max(25),
description: z.string().optional().nullable(),
slug: z
.string()
.regex(/^[a-zA-Z0-9_-]*$/)
.min(5)
.max(25),
description: z.string(),
});
export type AgentType = z.infer<typeof AgentSchema>;