From 49c68fdbf2ef8b2288abdf1ef57ae4344c60fc11 Mon Sep 17 00:00:00 2001 From: David Corbitt Date: Tue, 8 Aug 2023 12:07:18 -0700 Subject: [PATCH] Upsert personalOrg when listing orgs --- app/prisma/schema.prisma | 2 +- app/src/pages/project/settings/index.tsx | 46 +++++++++++-------- .../api/routers/organizations.router.ts | 28 ++--------- app/src/server/utils/userOrg.ts | 9 ++++ 4 files changed, 42 insertions(+), 43 deletions(-) diff --git a/app/prisma/schema.prisma b/app/prisma/schema.prisma index 06797f6..60b7d7d 100644 --- a/app/prisma/schema.prisma +++ b/app/prisma/schema.prisma @@ -206,7 +206,7 @@ model Organization { name String @default("Project 1") personalOrgUserId String? @unique @db.Uuid - PersonalOrgUser User? @relation(fields: [personalOrgUserId], references: [id], onDelete: Cascade) + personalOrgUser User? @relation(fields: [personalOrgUserId], references: [id], onDelete: Cascade) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt diff --git a/app/src/pages/project/settings/index.tsx b/app/src/pages/project/settings/index.tsx index cb1139e..18dfe17 100644 --- a/app/src/pages/project/settings/index.tsx +++ b/app/src/pages/project/settings/index.tsx @@ -113,24 +113,34 @@ export default function Settings() { - - Danger Zone - - Permanently delete your project and all of its data. This action cannot be undone. - - - - Delete {selectedOrg?.name} - - + {selectedOrg?.personalOrgUserId ? ( + + Personal Project + + This project is {selectedOrg?.personalOrgUser?.name}'s personal project. It cannot be + deleted. + + + ) : ( + + Danger Zone + + Permanently delete your project and all of its data. This action cannot be undone. + + + + Delete {selectedOrg?.name} + + + )} diff --git a/app/src/server/api/routers/organizations.router.ts b/app/src/server/api/routers/organizations.router.ts index 263c4aa..93cc864 100644 --- a/app/src/server/api/routers/organizations.router.ts +++ b/app/src/server/api/routers/organizations.router.ts @@ -5,6 +5,7 @@ import { z } from "zod"; import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc"; import { prisma } from "~/server/db"; import { generateApiKey } from "~/server/utils/generateApiKey"; +import userOrg from "~/server/utils/userOrg"; import { requireCanModifyOrganization, requireCanViewOrganization, @@ -33,30 +34,8 @@ export const organizationsRouter = createTRPCRouter({ }); if (!organizations.length) { - const newOrgId = uuidv4(); - const [newOrg] = await prisma.$transaction([ - prisma.organization.create({ - data: { - id: newOrgId, - personalOrgUserId: userId, - }, - }), - prisma.organizationUser.create({ - data: { - userId, - organizationId: newOrgId, - role: "ADMIN", - }, - }), - prisma.apiKey.create({ - data: { - name: "Default API Key", - organizationId: newOrgId, - apiKey: generateApiKey(), - }, - }), - ]); - organizations.push(newOrg); + // TODO: We should move this to a separate endpoint that is called on sign up + await userOrg(userId); } return organizations; @@ -70,6 +49,7 @@ export const organizationsRouter = createTRPCRouter({ }, include: { apiKeys: true, + personalOrgUser: true, }, }), prisma.organizationUser.findFirst({ diff --git a/app/src/server/utils/userOrg.ts b/app/src/server/utils/userOrg.ts index a88d8f7..cbbecb1 100644 --- a/app/src/server/utils/userOrg.ts +++ b/app/src/server/utils/userOrg.ts @@ -1,4 +1,5 @@ import { prisma } from "~/server/db"; +import { generateApiKey } from "./generateApiKey"; export default async function userOrg(userId: string) { return await prisma.organization.upsert({ @@ -14,6 +15,14 @@ export default async function userOrg(userId: string) { role: "ADMIN", }, }, + apiKeys: { + create: [ + { + name: "Default API Key", + apiKey: generateApiKey(), + }, + ], + }, }, }); }