1- import { Controller , Get , Patch , Param , Body , Query , ValidationPipe , Req } 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,7 +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' ;
14- import { Request } from 'express ' ;
24+ import { RequestWithUser } from '../auth/auth.middleware ' ;
1525
1626@ApiTags ( 'reports' )
1727@Controller ( 'reports' )
@@ -22,19 +32,19 @@ export class ReportsController {
2232 @ApiOperation ( { summary : 'Get all reports' } )
2333 @ApiResponse ( {
2434 status : 200 ,
25- description : 'Returns all reports' ,
35+ description : 'Returns all reports for the authenticated user ' ,
2636 type : [ Report ] ,
2737 } )
2838 @Get ( )
29- async findAll ( @Req ( ) request : Request ) : Promise < Report [ ] > {
39+ async findAll ( @Req ( ) request : RequestWithUser ) : Promise < Report [ ] > {
3040 const userId = this . extractUserId ( request ) ;
3141 return this . reportsService . findAll ( userId ) ;
3242 }
3343
3444 @ApiOperation ( { summary : 'Get latest reports' } )
3545 @ApiResponse ( {
3646 status : 200 ,
37- description : 'Returns the latest reports' ,
47+ description : 'Returns the latest reports for the authenticated user ' ,
3848 type : [ Report ] ,
3949 } )
4050 @ApiQuery ( {
@@ -45,7 +55,7 @@ export class ReportsController {
4555 @Get ( 'latest' )
4656 async findLatest (
4757 @Query ( ValidationPipe ) queryDto : GetReportsQueryDto ,
48- @Req ( ) request : Request ,
58+ @Req ( ) request : RequestWithUser ,
4959 ) : Promise < Report [ ] > {
5060 const userId = this . extractUserId ( request ) ;
5161 return this . reportsService . findLatest ( queryDto , userId ) ;
@@ -66,7 +76,7 @@ export class ReportsController {
6676 description : 'Report ID' ,
6777 } )
6878 @Get ( ':id' )
69- async getReport ( @Param ( 'id' ) id : string , @Req ( ) request : Request ) : Promise < Report > {
79+ async getReport ( @Param ( 'id' ) id : string , @Req ( ) request : RequestWithUser ) : Promise < Report > {
7080 const userId = this . extractUserId ( request ) ;
7181 return this . reportsService . findOne ( id , userId ) ;
7282 }
@@ -89,19 +99,18 @@ export class ReportsController {
8999 async updateStatus (
90100 @Param ( 'id' ) id : string ,
91101 @Body ( ValidationPipe ) updateDto : UpdateReportStatusDto ,
92- @Req ( ) request : Request ,
102+ @Req ( ) request : RequestWithUser ,
93103 ) : Promise < Report > {
94104 const userId = this . extractUserId ( request ) ;
95105 return this . reportsService . updateStatus ( id , updateDto , userId ) ;
96106 }
97107
98- private extractUserId ( request : Request ) : string {
99- console . log ( request ) ;
100- // The user object is attached to the request by the AuthGuard
101- const user = request . user as any ;
108+ private extractUserId ( request : RequestWithUser ) : string {
109+ // The user object is attached to the request by our middleware
110+ const user = request . user ;
102111
103112 if ( ! user || ! user . sub ) {
104- throw new Error ( 'User ID not found in token ' ) ;
113+ throw new UnauthorizedException ( 'User ID not found in request ' ) ;
105114 }
106115
107116 return user . sub ;
0 commit comments