Skip to content

Commit 38f78a6

Browse files
committed
feat: refactoring bunch of code
1 parent ba5eed0 commit 38f78a6

File tree

8 files changed

+196
-69
lines changed

8 files changed

+196
-69
lines changed

prisma/seeds/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { db } from "@/server/db";
2-
import inquirer from "inquirer";
32
import colors from "colors";
3+
import inquirer from "inquirer";
44
colors.enable();
55

6-
import seedTeam from "./team";
7-
import seedCompanies from "./companies";
86
import type { QuestionCollection } from "inquirer";
7+
import seedCompanies from "./companies";
8+
import seedTeam from "./team";
99

1010
if (process.env.NODE_ENV === "production") {
1111
console.log("❌ You cannot run this command on production".red);
@@ -44,6 +44,7 @@ const nuke = async () => {
4444
await db.shareClass.deleteMany();
4545
await db.equityPlan.deleteMany();
4646
await db.document.deleteMany();
47+
await db.bucket.deleteMany();
4748
await db.audit.deleteMany();
4849
await db.session.deleteMany();
4950
});
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use server";
22

33
import { getServerAuthSession } from "@/server/auth";
4-
import { db } from "@/server/db";
4+
import { api } from "@/trpc/server";
5+
import { notFound } from "next/navigation";
56
import DataRoomFiles from "../components/data-room-files";
67

78
const DataRoomSettinsPage = async ({
@@ -10,24 +11,24 @@ const DataRoomSettinsPage = async ({
1011
params: { publicId: string; dataRoomPublicId: string };
1112
}) => {
1213
const session = await getServerAuthSession();
13-
const companyId = session?.user.companyId;
14+
const { dataRoom, documents, recipients } =
15+
await api.dataRoom.getDataRoom.query({
16+
dataRoomPublicId,
17+
getRecipients: true,
18+
});
1419

15-
const dataRoom = await db.dataRoom.findFirstOrThrow({
16-
where: {
17-
publicId: dataRoomPublicId,
18-
companyId,
19-
},
20+
if (!dataRoom) {
21+
return notFound();
22+
}
2023

21-
include: {
22-
documents: {
23-
orderBy: {
24-
createdAt: "desc",
25-
},
26-
},
27-
},
28-
});
29-
30-
return <DataRoomFiles dataRoom={dataRoom} companyPublicId={publicId} />;
24+
return (
25+
<DataRoomFiles
26+
dataRoom={dataRoom}
27+
documents={documents}
28+
recipients={recipients}
29+
companyPublicId={publicId}
30+
/>
31+
);
3132
};
3233

3334
export default DataRoomSettinsPage;
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use server";
22

33
import { getServerAuthSession } from "@/server/auth";
4-
import { db } from "@/server/db";
4+
import { api } from "@/trpc/server";
5+
import { notFound } from "next/navigation";
56
import DataRoomFiles from "../components/data-room-files";
67

78
const DataRoomSettinsPage = async ({
@@ -10,24 +11,24 @@ const DataRoomSettinsPage = async ({
1011
params: { publicId: string; dataRoomPublicId: string };
1112
}) => {
1213
const session = await getServerAuthSession();
13-
const companyId = session?.user.companyId;
14+
const { dataRoom, documents, recipients } =
15+
await api.dataRoom.getDataRoom.query({
16+
dataRoomPublicId,
17+
getRecipients: true,
18+
});
1419

15-
const dataRoom = await db.dataRoom.findFirstOrThrow({
16-
where: {
17-
publicId: dataRoomPublicId,
18-
companyId,
19-
},
20+
if (!dataRoom) {
21+
return notFound();
22+
}
2023

21-
include: {
22-
documents: {
23-
orderBy: {
24-
createdAt: "desc",
25-
},
26-
},
27-
},
28-
});
29-
30-
return <DataRoomFiles dataRoom={dataRoom} companyPublicId={publicId} />;
24+
return (
25+
<DataRoomFiles
26+
dataRoom={dataRoom}
27+
documents={documents}
28+
recipients={recipients}
29+
companyPublicId={publicId}
30+
/>
31+
);
3132
};
3233

3334
export default DataRoomSettinsPage;

src/app/(authenticated)/(dashboard)/[publicId]/documents/data-rooms/components/data-room-files.tsx

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22

33
import EmptyState from "@/components/common/empty-state";
44
import Loading from "@/components/common/loading";
5+
import DataRoomFileExplorer from "@/components/documents/data-room/explorer";
56
import { Button } from "@/components/ui/button";
67
import { DropdownButton } from "@/components/ui/dropdown-button";
7-
import DataRoomUploader from "./data-room-uploader";
8-
// import { useToast } from "@/components/ui/use-toast";
9-
import type { DataRoom } from "@prisma/client";
8+
import { api } from "@/trpc/react";
9+
import type {
10+
DataRoom,
11+
DataRoomDocument,
12+
DataRoomRecipient,
13+
} from "@prisma/client";
1014
import {
1115
RiFolder3Fill as FolderIcon,
1216
RiAddFill,
1317
RiArrowDownSLine,
1418
RiUploadCloudLine,
1519
} from "@remixicon/react";
1620
import Link from "next/link";
17-
// import { useRouter } from "next/navigation";
1821
import { Fragment, useState } from "react";
22+
import DataRoomUploader from "./data-room-uploader";
1923

2024
interface DataRoomType extends DataRoom {
2125
documents: {
@@ -29,12 +33,17 @@ interface DataRoomType extends DataRoom {
2933
type DataRoomFilesProps = {
3034
dataRoom: DataRoomType;
3135
companyPublicId: string;
36+
documents: DataRoomDocument[];
37+
recipients: DataRoomRecipient[];
3238
};
3339

34-
const DataRoomFiles = ({ dataRoom, companyPublicId }: DataRoomFilesProps) => {
35-
// const router = useRouter();
36-
// const { toast } = useToast();
37-
40+
const DataRoomFiles = ({
41+
dataRoom,
42+
documents,
43+
recipients,
44+
companyPublicId,
45+
}: DataRoomFilesProps) => {
46+
const { mutateAsync } = api.dataRoom.save.useMutation();
3847
const [loading, setLoading] = useState<boolean>(false);
3948

4049
return (
@@ -60,14 +69,20 @@ const DataRoomFiles = ({ dataRoom, companyPublicId }: DataRoomFilesProps) => {
6069
className="h4 min-w-[300px] bg-transparent px-2 text-gray-800 outline-none focus:ring-0 focus:ring-offset-0"
6170
placeholder={`Data room's folder name`}
6271
defaultValue={dataRoom.name}
63-
onChange={(e) => {
64-
console.log("TODO: save with debounce", e.target.value);
72+
onChange={async (e) => {
73+
// TODO - debounce this
74+
75+
const name = e.target.value;
76+
await mutateAsync({
77+
name,
78+
publicId: dataRoom.publicId,
79+
});
6580
}}
6681
/>
6782
</div>
6883
</div>
6984

70-
{dataRoom?.documents.length > 0 && (
85+
{documents.length > 0 && (
7186
<div>
7287
<DropdownButton
7388
buttonSlot={
@@ -109,22 +124,26 @@ const DataRoomFiles = ({ dataRoom, companyPublicId }: DataRoomFilesProps) => {
109124
</form>
110125

111126
<div>
112-
<EmptyState
113-
icon={<RiUploadCloudLine />}
114-
title="Data room is empty!"
115-
subtitle="Upload one or many documents to get started."
116-
>
117-
<DataRoomUploader
118-
dataRoom={dataRoom}
119-
companyPublicId={companyPublicId}
120-
trigger={
121-
<Button size="lg">
122-
<RiAddFill className="mr-2 h-5 w-5" />
123-
Upload documents
124-
</Button>
125-
}
126-
/>
127-
</EmptyState>
127+
{documents.length > 0 ? (
128+
<DataRoomFileExplorer documents={documents} />
129+
) : (
130+
<EmptyState
131+
icon={<RiUploadCloudLine />}
132+
title="Data room is empty!"
133+
subtitle="Upload one or many documents to get started."
134+
>
135+
<DataRoomUploader
136+
dataRoom={dataRoom}
137+
companyPublicId={companyPublicId}
138+
trigger={
139+
<Button size="lg">
140+
<RiAddFill className="mr-2 h-5 w-5" />
141+
Upload documents
142+
</Button>
143+
}
144+
/>
145+
</EmptyState>
146+
)}
128147
</div>
129148

130149
{loading && <Loading />}

src/app/(authenticated)/(dashboard)/[publicId]/documents/data-rooms/components/data-room-uploader.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const DataRoomUploader = ({
2121
const router = useRouter();
2222
const [open, setOpen] = useState(false);
2323

24-
const { mutateAsync } = api.dataRoom.save.useMutation();
24+
const dataRoomMutation = api.dataRoom.save.useMutation();
25+
const documentMutation = api.document.create.useMutation();
2526

2627
return (
2728
<Modal
@@ -39,13 +40,18 @@ const DataRoomUploader = ({
3940
multiple={true}
4041
identifier={companyPublicId}
4142
keyPrefix={`data-room/${dataRoom.publicId}/file`}
42-
onSuccess={async (document: UploadReturn) => {
43-
await mutateAsync({
43+
onSuccess={async (upload: UploadReturn) => {
44+
const document = await documentMutation.mutateAsync({
45+
name: upload.name,
46+
bucketId: upload.id,
47+
});
48+
49+
await dataRoomMutation.mutateAsync({
4450
name: dataRoom.name,
4551
publicId: dataRoom.publicId,
4652
documents: [
4753
{
48-
documentId: document.key,
54+
documentId: document.id,
4955
},
5056
],
5157
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Document } from "@prisma/client";
2+
3+
const DataRoomFileExplorer = (documents: Document[]) => {
4+
<div className="flex flex-col gap-y-8">
5+
Documents JSON.stringify(documents, null, 2)
6+
</div>;
7+
};
8+
9+
export default DataRoomFileExplorer;

src/components/documents/data-room/preview.tsx

Whitespace-only changes.

src/trpc/routers/data-room-router/router.ts

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,99 @@
11
import { generatePublicId } from "@/common/id";
22
import { createTRPCRouter, withAuth } from "@/trpc/api/trpc";
3-
import type { DataRoom } from "@prisma/client";
3+
import type {
4+
DataRoom,
5+
DataRoomDocument,
6+
DataRoomRecipient,
7+
} from "@prisma/client";
8+
import { z } from "zod";
49
import { DataRoomSchema } from "./schema";
510

611
export const dataRoomRouter = createTRPCRouter({
12+
getDataRoom: withAuth
13+
.input(
14+
z.object({
15+
dataRoomPublicId: z.string(),
16+
getRecipients: z.boolean().optional().default(false),
17+
}),
18+
)
19+
.query(async ({ ctx, input }) => {
20+
const { db, session } = ctx;
21+
const user = session.user;
22+
const companyId = user.companyId;
23+
const { dataRoomPublicId, getRecipients } = input;
24+
25+
const dataRoom = await db.dataRoom.findUniqueOrThrow({
26+
where: {
27+
publicId: dataRoomPublicId,
28+
companyId,
29+
},
30+
31+
include: {
32+
documents: true,
33+
34+
...(getRecipients && { recipients: true }),
35+
},
36+
});
37+
38+
const documentIds = dataRoom.documents.map((doc) => doc.id);
39+
40+
const documents: DataRoomDocument[] = await db.dataRoomDocument.findMany({
41+
where: {
42+
id: { in: documentIds },
43+
},
44+
45+
include: {
46+
document: {
47+
select: {
48+
id: true,
49+
bucket: true,
50+
},
51+
},
52+
},
53+
});
54+
55+
if (getRecipients) {
56+
const recipientIds = dataRoom.recipients.map(
57+
(recipient) => recipient.id,
58+
);
59+
60+
const recipients: DataRoomRecipient[] =
61+
await db.dataRoomRecipient.findMany({
62+
where: {
63+
id: { in: recipientIds },
64+
},
65+
66+
include: {
67+
member: {
68+
select: {
69+
id: true,
70+
workEmail: true,
71+
},
72+
},
73+
74+
stakeholder: {
75+
select: {
76+
id: true,
77+
name: true,
78+
email: true,
79+
},
80+
},
81+
},
82+
});
83+
84+
return {
85+
dataRoom,
86+
documents,
87+
recipients,
88+
};
89+
} else {
90+
return {
91+
dataRoom,
92+
documents,
93+
};
94+
}
95+
}),
96+
797
save: withAuth.input(DataRoomSchema).mutation(async ({ ctx, input }) => {
898
try {
999
let room = {} as DataRoom;

0 commit comments

Comments
 (0)