Upsert personalOrg when listing orgs

This commit is contained in:
David Corbitt
2023-08-08 12:07:18 -07:00
parent 6188f55569
commit 49c68fdbf2
4 changed files with 42 additions and 43 deletions

View File

@@ -206,7 +206,7 @@ model Organization {
name String @default("Project 1") name String @default("Project 1")
personalOrgUserId String? @unique @db.Uuid 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()) createdAt DateTime @default(now())
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt

View File

@@ -113,24 +113,34 @@ export default function Settings() {
</VStack> </VStack>
<CopiableCode code={`OPENPIPE_API_KEY=${apiKey}`} /> <CopiableCode code={`OPENPIPE_API_KEY=${apiKey}`} />
<Divider /> <Divider />
<VStack alignItems="flex-start"> {selectedOrg?.personalOrgUserId ? (
<Subtitle color="red.600">Danger Zone</Subtitle> <VStack alignItems="flex-start">
<Text fontSize="sm"> <Subtitle>Personal Project</Subtitle>
Permanently delete your project and all of its data. This action cannot be undone. <Text fontSize="sm">
</Text> This project is {selectedOrg?.personalOrgUser?.name}'s personal project. It cannot be
<HStack deleted.
as={Button} </Text>
isDisabled={selectedOrg?.role !== "ADMIN"} </VStack>
colorScheme="red" ) : (
variant="outline" <VStack alignItems="flex-start">
borderRadius={4} <Subtitle color="red.600">Danger Zone</Subtitle>
mt={2} <Text fontSize="sm">
onClick={deleteProjectOpen.onOpen} Permanently delete your project and all of its data. This action cannot be undone.
> </Text>
<Icon as={BsTrash} /> <HStack
<Text>Delete {selectedOrg?.name}</Text> as={Button}
</HStack> isDisabled={selectedOrg?.role !== "ADMIN"}
</VStack> colorScheme="red"
variant="outline"
borderRadius={4}
mt={2}
onClick={deleteProjectOpen.onOpen}
>
<Icon as={BsTrash} />
<Text>Delete {selectedOrg?.name}</Text>
</HStack>
</VStack>
)}
</VStack> </VStack>
</VStack> </VStack>
</AppShell> </AppShell>

View File

@@ -5,6 +5,7 @@ import { z } from "zod";
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc"; import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
import { prisma } from "~/server/db"; import { prisma } from "~/server/db";
import { generateApiKey } from "~/server/utils/generateApiKey"; import { generateApiKey } from "~/server/utils/generateApiKey";
import userOrg from "~/server/utils/userOrg";
import { import {
requireCanModifyOrganization, requireCanModifyOrganization,
requireCanViewOrganization, requireCanViewOrganization,
@@ -33,30 +34,8 @@ export const organizationsRouter = createTRPCRouter({
}); });
if (!organizations.length) { if (!organizations.length) {
const newOrgId = uuidv4(); // TODO: We should move this to a separate endpoint that is called on sign up
const [newOrg] = await prisma.$transaction([ await userOrg(userId);
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);
} }
return organizations; return organizations;
@@ -70,6 +49,7 @@ export const organizationsRouter = createTRPCRouter({
}, },
include: { include: {
apiKeys: true, apiKeys: true,
personalOrgUser: true,
}, },
}), }),
prisma.organizationUser.findFirst({ prisma.organizationUser.findFirst({

View File

@@ -1,4 +1,5 @@
import { prisma } from "~/server/db"; import { prisma } from "~/server/db";
import { generateApiKey } from "./generateApiKey";
export default async function userOrg(userId: string) { export default async function userOrg(userId: string) {
return await prisma.organization.upsert({ return await prisma.organization.upsert({
@@ -14,6 +15,14 @@ export default async function userOrg(userId: string) {
role: "ADMIN", role: "ADMIN",
}, },
}, },
apiKeys: {
create: [
{
name: "Default API Key",
apiKey: generateApiKey(),
},
],
},
}, },
}); });
} }