Skip to content

Commit

Permalink
Merge pull request #73 from diggerhq/fix/create-project-query
Browse files Browse the repository at this point in the history
Move create project query to Prisma
  • Loading branch information
ZIJ authored Oct 31, 2024
2 parents b9f8f4b + 73a15ab commit 6a6f1ef
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 66 deletions.
6 changes: 3 additions & 3 deletions src/components/CreateProjectForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -43,7 +43,7 @@ const createProjectFormSchema = z.object({
type CreateProjectFormData = z.infer<typeof createProjectFormSchema>;

type Repository = {
id: number;
id: bigint;
repo_full_name: string | null;
};

Expand All @@ -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",
Expand Down
95 changes: 46 additions & 49 deletions src/data/user/projects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -104,7 +104,7 @@ export const createProjectAction = async ({
organizationId: string;
name: string;
slug: string;
repoId: number;
repoId: bigint;
branch: string;
terraformWorkingDir: string;
workspace?: string;
Expand All @@ -117,60 +117,57 @@ export const createProjectAction = async ({
teamId: number | null;
is_drift_detection_enabled: boolean;
drift_crontab?: string;
}): Promise<SAPayload<Tables<"projects">>> => {
}): Promise<SAPayload<projects>> => {
"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,
}
});
// why are we revalidating paths in a getter query?
// a getter query is not supposed to have side effects. bad code.
// leaving as is because idk what breaks if I remove it
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 (
Expand Down
36 changes: 22 additions & 14 deletions src/data/user/repos.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
}

0 comments on commit 6a6f1ef

Please sign in to comment.