1
1
import { generatePublicId } from "@/common/id" ;
2
2
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" ;
8
4
import { z } from "zod" ;
9
5
import { DataRoomSchema } from "./schema" ;
10
6
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
+
11
16
export const dataRoomRouter = createTRPCRouter ( {
12
17
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 ( ) } ) )
19
19
. query ( async ( { ctx, input } ) => {
20
20
const { db, session } = ctx ;
21
21
const user = session . user ;
22
22
const companyId = user . companyId ;
23
- const { dataRoomPublicId, getRecipients } = input ;
23
+ const { dataRoomPublicId } = input ;
24
24
25
25
const dataRoom = await db . dataRoom . findUniqueOrThrow ( {
26
26
where : {
@@ -30,68 +30,94 @@ export const dataRoomRouter = createTRPCRouter({
30
30
31
31
include : {
32
32
documents : true ,
33
-
34
- ...( getRecipients && { recipients : true } ) ,
33
+ recipients : true ,
35
34
} ,
36
35
} ) ;
37
36
38
37
const documentIds = dataRoom . documents . map ( ( doc ) => doc . id ) ;
39
38
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
+ } ,
44
44
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
+ } ,
50
51
} ,
51
52
} ,
52
- } ,
53
- } ) ;
53
+ } ) ;
54
54
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
+ } ) ) ;
59
64
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 ) ;
65
66
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
+ } ,
73
71
74
- stakeholder : {
72
+ include : {
73
+ member : {
74
+ select : {
75
+ id : true ,
76
+ user : {
75
77
select : {
76
- id : true ,
77
- name : true ,
78
78
email : true ,
79
+ name : true ,
79
80
} ,
80
81
} ,
81
82
} ,
82
- } ) ;
83
+ } ,
84
+ stakeholder : true ,
85
+ } ,
86
+ } ) ;
83
87
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
+ } ;
95
121
} ) ,
96
122
97
123
save : withAuth . input ( DataRoomSchema ) . mutation ( async ( { ctx, input } ) => {
0 commit comments