@@ -34,7 +34,7 @@ import { SanitizeTrimPipe } from '../common/pipes/sanitize-trim.pipe';
34
34
* updated and delete an entity protected by the Role Base ACL and the api documentaion.
35
35
*/
36
36
export function ControllerFactory <
37
- Entity extends BaseEntity ,
37
+ Entity extends BaseEntity & { userId ?: string } ,
38
38
CreateDTO extends DeepPartial < Entity > ,
39
39
UpdateDTO extends QueryDeepPartialEntity < Entity > ,
40
40
> (
@@ -64,7 +64,7 @@ export function ControllerFactory<
64
64
) ;
65
65
66
66
class BaseController <
67
- Entity extends BaseEntity ,
67
+ Entity extends BaseEntity & { userId ?: string } ,
68
68
CreateDTO extends DeepPartial < Entity > ,
69
69
UpdateDTO extends QueryDeepPartialEntity < Entity > ,
70
70
> implements IBaseController < Entity , CreateDTO , UpdateDTO >
@@ -149,8 +149,6 @@ export function ControllerFactory<
149
149
id,
150
150
userId : user . id ,
151
151
} as unknown as FindOptionsWhere < Entity > ) ;
152
- // We don't wont to give much info, so always return not found
153
- // even if the user is trying to get a resource of another user
154
152
if ( ! entity ) throw new NotFoundException ( ) ;
155
153
156
154
return entity ;
@@ -224,14 +222,9 @@ export function ControllerFactory<
224
222
@Body ( ) dto : UpdateDTO ,
225
223
@CurrentUser ( ) user : User ,
226
224
) : Promise < void > {
227
- const entity = await this . service . findById ( id ) ;
228
- // We don't wont to give much info, so always return not found
229
- // even if the user is trying to update a resource of another user
230
- if ( ! entity || ( entity . userId && entity . userId !== user . id ) )
231
- throw new NotFoundException ( ) ;
232
-
233
225
// Update owned resource
234
- await this . service . updateById ( id , dto , user . id ) ;
226
+ const result = await this . service . updateById ( id , dto , user . id ) ;
227
+ if ( result . affected === 0 ) throw new NotFoundException ( ) ;
235
228
}
236
229
237
230
/**
@@ -266,15 +259,9 @@ export function ControllerFactory<
266
259
@Param ( 'id' , ParseUUIDPipe ) id : string ,
267
260
@CurrentUser ( ) user : User ,
268
261
) : Promise < void > {
269
- const entity = await this . service . findById ( id ) ;
270
-
271
- // We don't wont to give much info, so always return not found
272
- // even if the user is trying to delete a resource of another user
273
- if ( ! entity || ( entity . userId && entity . userId !== user . id ) )
274
- throw new NotFoundException ( ) ;
275
-
276
262
// Delete owned resource
277
- await this . service . deleteById ( id , user . id ) ;
263
+ const result = await this . service . deleteById ( id , user . id ) ;
264
+ if ( result . affected === 0 ) throw new NotFoundException ( ) ;
278
265
}
279
266
}
280
267
0 commit comments