From 9fdbafc5e08ddd63fa0612e05d33af57e48099a0 Mon Sep 17 00:00:00 2001 From: Igor Zalutski Date: Thu, 31 Oct 2024 16:19:41 -0700 Subject: [PATCH] Move create project query to Prisma --- src/components/CreateProjectForm.tsx | 6 +- src/data/user/projects.tsx | 92 +++++++++++++--------------- src/data/user/repos.ts | 36 ++++++----- 3 files changed, 68 insertions(+), 66 deletions(-) diff --git a/src/components/CreateProjectForm.tsx b/src/components/CreateProjectForm.tsx index fd1aa0b7..19abbb8d 100644 --- a/src/components/CreateProjectForm.tsx +++ b/src/components/CreateProjectForm.tsx @@ -25,7 +25,7 @@ const MotionCard = motion(Card); const createProjectFormSchema = z.object({ name: z.string().min(1, "Project name is required"), - repository: z.number().int().positive("Please select a repository"), + repository: z.bigint().positive("Please select a repository"), terraformDir: z.string().min(1, "Terraform working directory is required"), iac_type: z.enum(["terraform", "terragrunt", "opentofu"]).default("terraform"), workspace: z.string().default("default").optional(), @@ -43,7 +43,7 @@ const createProjectFormSchema = z.object({ type CreateProjectFormData = z.infer; type Repository = { - id: number; + id: bigint; repo_full_name: string | null; }; @@ -68,7 +68,7 @@ export default function CreateProjectForm({ organizationId, repositories, teams, resolver: zodResolver(createProjectFormSchema), defaultValues: { name: "", - repository: repositories[0]?.id || 0, + repository: repositories[0]?.id || BigInt(0), terraformDir: "", iac_type: "terraform", workflow_file: "digger_workflow.yml", diff --git a/src/data/user/projects.tsx b/src/data/user/projects.tsx index f532c86c..11a92696 100644 --- a/src/data/user/projects.tsx +++ b/src/data/user/projects.tsx @@ -8,7 +8,7 @@ import { createSupabaseUserServerComponentClient } from "@/supabase-clients/user import type { CommentWithUser, Enum, SAPayload } from "@/types"; import { normalizeComment } from "@/utils/comments"; import { serverGetLoggedInUser } from "@/utils/server/serverGetLoggedInUser"; -import { PrismaClient } from '@prisma/client'; +import { PrismaClient, projects } from '@prisma/client'; import { randomUUID } from "crypto"; import { revalidatePath } from "next/cache"; import { Suspense } from "react"; @@ -104,7 +104,7 @@ export const createProjectAction = async ({ organizationId: string; name: string; slug: string; - repoId: number; + repoId: bigint; branch: string; terraformWorkingDir: string; workspace?: string; @@ -117,60 +117,54 @@ export const createProjectAction = async ({ teamId: number | null; is_drift_detection_enabled: boolean; drift_crontab?: string; -}): Promise>> => { +}): Promise> => { "use server"; - const supabaseClient = createSupabaseUserServerActionClient(); - const { data: project, error } = await supabaseClient - .from("projects") - .insert({ - id: randomUUID(), - organization_id: organizationId, - name, - slug, - team_id: teamId, - repo_id: repoId, - branch, - terraform_working_dir: terraformWorkingDir, - workspace, - workflow_file, - iac_type, - include_patterns, - exclude_patterns, - is_managing_state: managedState, - is_in_main_branch: true, - is_generated: true, - project_status: "draft", - latest_action_on: new Date().toISOString(), - labels, - is_drift_detection_enabled, - drift_crontab, - }) - .select("*") - .single(); - - - if (error) { + const prisma = new PrismaClient(); + try { + const project = await prisma.projects.create({ + data: { + id: randomUUID(), + organization_id: organizationId, + name, + slug, + team_id: teamId, + repo_id: repoId, + branch, + terraform_working_dir: terraformWorkingDir, + workspace, + workflow_file, + iac_type, + include_patterns, + exclude_patterns, + is_managing_state: managedState, + is_in_main_branch: true, + is_generated: true, + project_status: "draft", + latest_action_on: String(new Date()), + labels, + is_drift_detection_enabled, + drift_crontab, + } + }); + if (teamId) { + revalidatePath(`/org/[organizationId]/team/[teamId]`, "layout"); + } else { + revalidatePath(`/org/[organizationId]`, "layout"); + revalidatePath(`/org/[organizationId]/projects/`, "layout"); + } + return { + status: 'success', + data: project, + }; + } catch (error) { console.log(`could not create project ${name} for org ID: ${organizationId}`, error); return { status: 'error', message: error.message, }; + } finally { + await prisma.$disconnect() } - - if (teamId) { - revalidatePath(`/org/[organizationId]/team/[teamId]`, "layout"); - } else { - revalidatePath(`/org/[organizationId]`, "layout"); - revalidatePath(`/org/[organizationId]/projects/`, "layout"); - } - - - - - return { - status: 'success', - data: project, - }; }; export const getProjectComments = async ( diff --git a/src/data/user/repos.ts b/src/data/user/repos.ts index 5bade67e..ad3f3702 100644 --- a/src/data/user/repos.ts +++ b/src/data/user/repos.ts @@ -1,6 +1,7 @@ 'use server'; import { createSupabaseUserServerComponentClient } from '@/supabase-clients/user/createSupabaseUserServerComponentClient'; +import { PrismaClient } from '@prisma/client'; import { revalidatePath } from 'next/cache'; export async function getRepoDetails(repoId: number) { @@ -19,19 +20,26 @@ export async function getRepoDetails(repoId: number) { } export async function getOrganizationRepos(organizationId: string) { - const supabaseClient = createSupabaseUserServerComponentClient(); - const { data, error } = await supabaseClient - .from('repos') - .select('id, repo_full_name') - .eq('organization_id', organizationId) - .is('deleted_at', null); - - if (error) { - throw error; + const prisma = new PrismaClient(); + try { + const data = await prisma.repos.findMany({ + where: { + organization_id: organizationId, + deleted_at: null, + }, + select: { + id: true, + repo_full_name: true, + }, + }); + + console.log(`get org repos: ${data} org: ${organizationId}`); + + revalidatePath(`/org/${organizationId}/projects`, 'page'); + revalidatePath(`/org/${organizationId}/projects/create`, 'page'); + + return data; + } finally { + await prisma.$disconnect(); } - - revalidatePath(`/org/${organizationId}/projects`, 'page'); - revalidatePath(`/org/${organizationId}/projects/create`, 'page'); - - return data; }