@@ -60,20 +60,46 @@ export const getAllDocumentsOfProject = async (
6060) : Promise < void > => {
6161 try {
6262 const { projectId } = req . params ;
63+ const userId = ( req as any ) . user ?. id ;
6364 const query = req . query . query as string ;
6465 const page = parseInt ( req . query . page as string ) || 1 ;
6566 const limit = parseInt ( req . query . limit as string ) || 10 ;
6667 const skip = ( page - 1 ) * limit ;
6768 const search = query ? query . trim ( ) . toLowerCase ( ) : undefined ;
6869
69- const whereClause : any = { projectId } ;
70+ const whereClause : any = {
71+ categoryId : {
72+ in : await prisma . documentCategory
73+ . findMany ( {
74+ where : { projectId } ,
75+ select : { id : true } ,
76+ } )
77+ . then ( ( categories ) => categories . map ( ( cat ) => cat . id ) ) ,
78+ } ,
79+ } ;
80+
81+ whereClause . OR = [
82+ { visibility : "public" } ,
83+ {
84+ permissions : {
85+ some : {
86+ userId,
87+ } ,
88+ } ,
89+ } ,
90+ { createdById : userId } ,
91+ ] ;
7092
7193 if ( search ) {
72- whereClause . OR = [
73- { title : { contains : search , mode : "insensitive" } } ,
74- { description : { contains : search , mode : "insensitive" } } ,
75- { tags : { contains : search , mode : "insensitive" } } ,
76- { link : { contains : search , mode : "insensitive" } } ,
94+ whereClause . AND = [
95+ {
96+ OR : [
97+ { title : { contains : search , mode : "insensitive" } } ,
98+ { description : { contains : search , mode : "insensitive" } } ,
99+ { tags : { contains : search , mode : "insensitive" } } ,
100+ { link : { contains : search , mode : "insensitive" } } ,
101+ ] ,
102+ } ,
77103 ] ;
78104 }
79105
@@ -84,12 +110,25 @@ export const getAllDocumentsOfProject = async (
84110 orderBy : { createdAt : "desc" } ,
85111 skip,
86112 take : limit ,
113+ include : {
114+ category : {
115+ select : {
116+ name : true ,
117+ } ,
118+ } ,
119+ createdBy : {
120+ select : {
121+ id : true ,
122+ fullName : true ,
123+ } ,
124+ } ,
125+ } ,
87126 } ) ;
88127
89128 if ( documents . length === 0 && page === 1 ) {
90129 return response . errorResponse (
91130 res ,
92- "No documents found for this project."
131+ "No accessible documents found for this project."
93132 ) ;
94133 }
95134
@@ -115,6 +154,7 @@ export const getDocumentsByCategory = async (
115154) : Promise < void > => {
116155 try {
117156 const { categoryId } = req . params ;
157+ const userId = ( req as any ) . user ?. id ;
118158 const query = req . query . query as string ;
119159 const page = parseInt ( req . query . page as string ) || 1 ;
120160 const limit = parseInt ( req . query . limit as string ) || 10 ;
@@ -123,12 +163,28 @@ export const getDocumentsByCategory = async (
123163
124164 const whereClause : any = { categoryId } ;
125165
166+ whereClause . OR = [
167+ { visibility : "public" } ,
168+ {
169+ permissions : {
170+ some : {
171+ userId,
172+ } ,
173+ } ,
174+ } ,
175+ { createdById : userId } ,
176+ ] ;
177+
126178 if ( search ) {
127- whereClause . OR = [
128- { title : { contains : search , mode : "insensitive" } } ,
129- { description : { contains : search , mode : "insensitive" } } ,
130- { tags : { contains : search , mode : "insensitive" } } ,
131- { link : { contains : search , mode : "insensitive" } } ,
179+ whereClause . AND = [
180+ {
181+ OR : [
182+ { title : { contains : search , mode : "insensitive" } } ,
183+ { description : { contains : search , mode : "insensitive" } } ,
184+ { tags : { contains : search , mode : "insensitive" } } ,
185+ { link : { contains : search , mode : "insensitive" } } ,
186+ ] ,
187+ } ,
132188 ] ;
133189 }
134190
@@ -139,12 +195,20 @@ export const getDocumentsByCategory = async (
139195 orderBy : { createdAt : "desc" } ,
140196 skip,
141197 take : limit ,
198+ include : {
199+ createdBy : {
200+ select : {
201+ id : true ,
202+ fullName : true ,
203+ } ,
204+ } ,
205+ } ,
142206 } ) ;
143207
144208 if ( documents . length === 0 && page === 1 ) {
145209 return response . errorResponse (
146210 res ,
147- "No documents found in this category."
211+ "No accessible documents found in this category."
148212 ) ;
149213 }
150214
0 commit comments