1- import { Controller , Get , Patch , Param , Body , Query , ValidationPipe } from '@nestjs/common' ;
1+ import {
2+ Controller ,
3+ Get ,
4+ Patch ,
5+ Param ,
6+ Body ,
7+ Query ,
8+ ValidationPipe ,
9+ Req ,
10+ UnauthorizedException ,
11+ } from '@nestjs/common' ;
212import {
313 ApiTags ,
414 ApiOperation ,
@@ -11,6 +21,7 @@ import { ReportsService } from './reports.service';
1121import { Report } from './models/report.model' ;
1222import { GetReportsQueryDto } from './dto/get-reports.dto' ;
1323import { UpdateReportStatusDto } from './dto/update-report-status.dto' ;
24+ import { RequestWithUser } from '../auth/auth.middleware' ;
1425
1526@ApiTags ( 'reports' )
1627@Controller ( 'reports' )
@@ -21,18 +32,19 @@ export class ReportsController {
2132 @ApiOperation ( { summary : 'Get all reports' } )
2233 @ApiResponse ( {
2334 status : 200 ,
24- description : 'Returns all reports' ,
35+ description : 'Returns all reports for the authenticated user ' ,
2536 type : [ Report ] ,
2637 } )
2738 @Get ( )
28- async findAll ( ) : Promise < Report [ ] > {
29- return this . reportsService . findAll ( ) ;
39+ async findAll ( @Req ( ) request : RequestWithUser ) : Promise < Report [ ] > {
40+ const userId = this . extractUserId ( request ) ;
41+ return this . reportsService . findAll ( userId ) ;
3042 }
3143
3244 @ApiOperation ( { summary : 'Get latest reports' } )
3345 @ApiResponse ( {
3446 status : 200 ,
35- description : 'Returns the latest reports' ,
47+ description : 'Returns the latest reports for the authenticated user ' ,
3648 type : [ Report ] ,
3749 } )
3850 @ApiQuery ( {
@@ -41,14 +53,18 @@ export class ReportsController {
4153 description : 'Maximum number of reports to return' ,
4254 } )
4355 @Get ( 'latest' )
44- async findLatest ( @Query ( ValidationPipe ) queryDto : GetReportsQueryDto ) : Promise < Report [ ] > {
45- return this . reportsService . findLatest ( queryDto ) ;
56+ async findLatest (
57+ @Query ( ValidationPipe ) queryDto : GetReportsQueryDto ,
58+ @Req ( ) request : RequestWithUser ,
59+ ) : Promise < Report [ ] > {
60+ const userId = this . extractUserId ( request ) ;
61+ return this . reportsService . findLatest ( queryDto , userId ) ;
4662 }
4763
4864 @ApiOperation ( { summary : 'GET report' } )
4965 @ApiResponse ( {
5066 status : 200 ,
51- description : 'Report status updated successfully ' ,
67+ description : 'Report details ' ,
5268 type : Report ,
5369 } )
5470 @ApiResponse ( {
@@ -60,8 +76,9 @@ export class ReportsController {
6076 description : 'Report ID' ,
6177 } )
6278 @Get ( ':id' )
63- async getReport ( @Param ( 'id' ) id : string ) : Promise < Report > {
64- return this . reportsService . findOne ( id ) ;
79+ async getReport ( @Param ( 'id' ) id : string , @Req ( ) request : RequestWithUser ) : Promise < Report > {
80+ const userId = this . extractUserId ( request ) ;
81+ return this . reportsService . findOne ( id , userId ) ;
6582 }
6683
6784 @ApiOperation ( { summary : 'Update report status' } )
@@ -82,7 +99,20 @@ export class ReportsController {
8299 async updateStatus (
83100 @Param ( 'id' ) id : string ,
84101 @Body ( ValidationPipe ) updateDto : UpdateReportStatusDto ,
102+ @Req ( ) request : RequestWithUser ,
85103 ) : Promise < Report > {
86- return this . reportsService . updateStatus ( id , updateDto ) ;
104+ const userId = this . extractUserId ( request ) ;
105+ return this . reportsService . updateStatus ( id , updateDto , userId ) ;
106+ }
107+
108+ private extractUserId ( request : RequestWithUser ) : string {
109+ // The user object is attached to the request by our middleware
110+ const user = request . user ;
111+
112+ if ( ! user || ! user . sub ) {
113+ throw new UnauthorizedException ( 'User ID not found in request' ) ;
114+ }
115+
116+ return user . sub ;
87117 }
88118}
0 commit comments