Skip to content

Commit 84af033

Browse files
committed
feat: reusable trpc endpoint to fetch documents
1 parent 38f78a6 commit 84af033

File tree

7 files changed

+111
-76
lines changed

7 files changed

+111
-76
lines changed

.eslintrc.cjs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const config = {
3535
destructuredArrayIgnorePattern: "^_",
3636
},
3737
],
38+
"@typescript-eslint/prefer-optional-chain": "off",
3839
"@typescript-eslint/require-await": "off",
3940
"@typescript-eslint/no-unsafe-assignment": "warn",
4041
"@typescript-eslint/no-unsafe-member-access": "warn",

src/app/(authenticated)/(dashboard)/[publicId]/documents/data-rooms/[dataRoomPublicId]/page.tsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ const DataRoomSettinsPage = async ({
1212
}) => {
1313
const session = await getServerAuthSession();
1414
const { dataRoom, documents, recipients } =
15-
await api.dataRoom.getDataRoom.query({
16-
dataRoomPublicId,
17-
getRecipients: true,
18-
});
15+
await api.dataRoom.getDataRoom.query({ dataRoomPublicId });
1916

2017
if (!dataRoom) {
2118
return notFound();

src/app/(authenticated)/(dashboard)/[publicId]/documents/data-rooms/[dataroomPublicId]/page.tsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ const DataRoomSettinsPage = async ({
1212
}) => {
1313
const session = await getServerAuthSession();
1414
const { dataRoom, documents, recipients } =
15-
await api.dataRoom.getDataRoom.query({
16-
dataRoomPublicId,
17-
getRecipients: true,
18-
});
15+
await api.dataRoom.getDataRoom.query({ dataRoomPublicId });
1916

2017
if (!dataRoom) {
2118
return notFound();

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
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";
65
import { Button } from "@/components/ui/button";
76
import { DropdownButton } from "@/components/ui/dropdown-button";
87
import { api } from "@/trpc/react";
9-
import type {
10-
DataRoom,
11-
DataRoomDocument,
12-
DataRoomRecipient,
13-
} from "@prisma/client";
8+
import type { Bucket, DataRoom } from "@prisma/client";
9+
10+
import { type DataRoomRecipientType } from "@/types/documents/data-room";
11+
1412
import {
1513
RiFolder3Fill as FolderIcon,
1614
RiAddFill,
@@ -31,10 +29,10 @@ interface DataRoomType extends DataRoom {
3129
}
3230

3331
type DataRoomFilesProps = {
34-
dataRoom: DataRoomType;
32+
dataRoom: DataRoom;
33+
documents: Bucket[];
3534
companyPublicId: string;
36-
documents: DataRoomDocument[];
37-
recipients: DataRoomRecipient[];
35+
recipients: DataRoomRecipientType[];
3836
};
3937

4038
const DataRoomFiles = ({
@@ -125,7 +123,8 @@ const DataRoomFiles = ({
125123

126124
<div>
127125
{documents.length > 0 ? (
128-
<DataRoomFileExplorer documents={documents} />
126+
// <DataRoomFileExplorer documents={documents} />
127+
<pre>{JSON.stringify(documents, null, 2)}</pre>
129128
) : (
130129
<EmptyState
131130
icon={<RiUploadCloudLine />}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { Document } from "@prisma/client";
1+
import type { DataRoomDocument } from "@prisma/client";
22

3-
const DataRoomFileExplorer = (documents: Document[]) => {
3+
const DataRoomFileExplorer = (documents: DataRoomDocument[]) => {
44
<div className="flex flex-col gap-y-8">
55
Documents JSON.stringify(documents, null, 2)
66
</div>;

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

+82-56
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import { generatePublicId } from "@/common/id";
22
import { createTRPCRouter, withAuth } from "@/trpc/api/trpc";
3-
import type {
4-
DataRoom,
5-
DataRoomDocument,
6-
DataRoomRecipient,
7-
} from "@prisma/client";
3+
import type { Bucket, DataRoom, DataRoomDocument } from "@prisma/client";
84
import { z } from "zod";
95
import { DataRoomSchema } from "./schema";
106

7+
import { type DataRoomRecipientType } from "@/types/documents/data-room";
8+
9+
interface DataRoomDocumentType extends DataRoomDocument {
10+
document: {
11+
id: string;
12+
bucket: Bucket;
13+
};
14+
}
15+
1116
export const dataRoomRouter = createTRPCRouter({
1217
getDataRoom: withAuth
13-
.input(
14-
z.object({
15-
dataRoomPublicId: z.string(),
16-
getRecipients: z.boolean().optional().default(false),
17-
}),
18-
)
18+
.input(z.object({ dataRoomPublicId: z.string() }))
1919
.query(async ({ ctx, input }) => {
2020
const { db, session } = ctx;
2121
const user = session.user;
2222
const companyId = user.companyId;
23-
const { dataRoomPublicId, getRecipients } = input;
23+
const { dataRoomPublicId } = input;
2424

2525
const dataRoom = await db.dataRoom.findUniqueOrThrow({
2626
where: {
@@ -30,68 +30,94 @@ export const dataRoomRouter = createTRPCRouter({
3030

3131
include: {
3232
documents: true,
33-
34-
...(getRecipients && { recipients: true }),
33+
recipients: true,
3534
},
3635
});
3736

3837
const documentIds = dataRoom.documents.map((doc) => doc.id);
3938

40-
const documents: DataRoomDocument[] = await db.dataRoomDocument.findMany({
41-
where: {
42-
id: { in: documentIds },
43-
},
39+
const dataRoomDocument: DataRoomDocumentType[] =
40+
await db.dataRoomDocument.findMany({
41+
where: {
42+
id: { in: documentIds },
43+
},
4444

45-
include: {
46-
document: {
47-
select: {
48-
id: true,
49-
bucket: true,
45+
include: {
46+
document: {
47+
select: {
48+
id: true,
49+
bucket: true,
50+
},
5051
},
5152
},
52-
},
53-
});
53+
});
5454

55-
if (getRecipients) {
56-
const recipientIds = dataRoom.recipients.map(
57-
(recipient) => recipient.id,
58-
);
55+
const documents: Bucket[] = dataRoomDocument.map((doc) => ({
56+
id: doc.document.bucket.id,
57+
name: doc.document.bucket.name,
58+
key: doc.document.bucket.key,
59+
mimeType: doc.document.bucket.mimeType,
60+
size: doc.document.bucket.size,
61+
createdAt: doc.document.bucket.createdAt,
62+
updatedAt: doc.document.bucket.updatedAt,
63+
}));
5964

60-
const recipients: DataRoomRecipient[] =
61-
await db.dataRoomRecipient.findMany({
62-
where: {
63-
id: { in: recipientIds },
64-
},
65+
const recipientIds = dataRoom.recipients.map((recipient) => recipient.id);
6566

66-
include: {
67-
member: {
68-
select: {
69-
id: true,
70-
workEmail: true,
71-
},
72-
},
67+
const dataRoomRecipient = await db.dataRoomRecipient.findMany({
68+
where: {
69+
id: { in: recipientIds },
70+
},
7371

74-
stakeholder: {
72+
include: {
73+
member: {
74+
select: {
75+
id: true,
76+
user: {
7577
select: {
76-
id: true,
77-
name: true,
7878
email: true,
79+
name: true,
7980
},
8081
},
8182
},
82-
});
83+
},
84+
stakeholder: true,
85+
},
86+
});
8387

84-
return {
85-
dataRoom,
86-
documents,
87-
recipients,
88-
};
89-
} else {
90-
return {
91-
dataRoom,
92-
documents,
93-
};
94-
}
88+
const recipients: DataRoomRecipientType[] = dataRoomRecipient.map(
89+
(recipient) => {
90+
const r = {
91+
id: recipient.id,
92+
email: recipient.email,
93+
} as DataRoomRecipientType;
94+
95+
if (recipient.member && recipient.member.user) {
96+
r.member = {
97+
id: recipient.member.id,
98+
email: recipient.member.user.email ?? "",
99+
name: recipient.member.user.name ?? "",
100+
};
101+
}
102+
103+
if (recipient.stakeholder) {
104+
r.stakeholder = {
105+
id: recipient.stakeholder.id,
106+
email: recipient.stakeholder.email,
107+
name: recipient.stakeholder.name,
108+
institutionName: recipient.stakeholder.institutionName ?? "",
109+
};
110+
}
111+
112+
return r;
113+
},
114+
);
115+
116+
return {
117+
dataRoom,
118+
documents,
119+
recipients,
120+
};
95121
}),
96122

97123
save: withAuth.input(DataRoomSchema).mutation(async ({ ctx, input }) => {

src/types/documents/data-room.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export type DataRoomRecipientType = {
2+
id: string;
3+
email?: string | null;
4+
member?: {
5+
id: string;
6+
name: string;
7+
email: string;
8+
};
9+
stakeholder?: {
10+
id: string;
11+
name: string;
12+
email: string;
13+
institutionName: string;
14+
};
15+
};

0 commit comments

Comments
 (0)