From a0b0b61e2fa7324170e1c0ae9f211fda79c4c5f1 Mon Sep 17 00:00:00 2001 From: Sarim Malik Date: Mon, 29 Jan 2024 00:48:53 -0500 Subject: [PATCH] Fix bug for missing project for new user --- lib/prisma.ts | 4 ++++ lib/utils/github.ts | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/prisma.ts b/lib/prisma.ts index a077c740..02aee9f7 100644 --- a/lib/prisma.ts +++ b/lib/prisma.ts @@ -1,6 +1,10 @@ import {PrismaClient} from '@prisma/client' import env from './env.mjs' +declare global { + var prisma: PrismaClient | undefined +} + const prisma = global.prisma || new PrismaClient() if (env.NODE_ENV === 'development') global.prisma = prisma diff --git a/lib/utils/github.ts b/lib/utils/github.ts index e58f2fd5..a0d1cf60 100644 --- a/lib/utils/github.ts +++ b/lib/utils/github.ts @@ -255,7 +255,8 @@ export async function handleInstall({ // Get user & request to add project const user = await prisma.user.findUnique({ where: {userName: userName}, - include: { + select: { + id: true, addProject: { take: 1, orderBy: {createdAt: 'desc'} @@ -263,14 +264,20 @@ export async function handleInstall({ } }) + if (!user?.id) + return new Response(`Could not find user ${userName}`, { + status: 500 + }) + // Create projects const projects = await prisma.project.createMany({ data: repositories.map((repo: Repository) => ({ name: repo.name, slug: repo.name, - teamId: user.addProject[0].teamId ?? '', - createdBy: user.id - })) + createdBy: user.id, + teamId: user.addProject[0].teamId ?? '' + })), + skipDuplicates: true }) // Clone, vectorize, and save public code to database @@ -352,8 +359,9 @@ export async function handleAddOrDeleteProjects({ } } }) + if (!user?.id) - return new Response(`Could not find or create customer ${userName}`, { + return new Response(`Could not find user ${userName}`, { status: 500 })