1
- import { Request , Response } from 'express' ;
1
+ import { Response } from 'express' ;
2
2
import jwt , { JwtPayload } from 'jsonwebtoken' ;
3
3
import historyEntryModel from '../models/HistoryEntry' ;
4
- import { Question } from 'models/Question ' ;
4
+ import { AuthenticatedRequest } from 'middlewares/auth ' ;
5
5
6
6
const getErrorMessage = ( error : unknown ) : string => {
7
7
if ( error instanceof Error ) return error . message ;
8
8
return 'An unexpected error occurred' ;
9
9
} ;
10
10
11
- const extractUserIdFromToken = ( req : Request ) : string | null => {
12
- const authHeader = req . headers . authorization ;
13
- if ( ! authHeader ) {
14
- console . error ( 'Authorization header missing' ) ;
15
- return null ;
16
- }
17
-
18
- const token = authHeader . split ( ' ' ) [ 1 ] ;
19
- if ( ! token ) {
20
- console . error ( 'Token missing from authorization header' ) ;
21
- return null ;
22
- }
23
-
24
- try {
25
- const decodedToken = jwt . verify ( token , process . env . JWT_ACCESS_TOKEN_SECRET as string ) as JwtPayload ;
26
- if ( decodedToken && typeof decodedToken === 'object' && 'id' in decodedToken ) {
27
- return decodedToken . id as string ;
28
- } else {
29
- console . error ( 'Token payload does not contain user ID' ) ;
30
- return null ;
31
- }
32
- } catch ( error ) {
33
- console . error ( 'Token verification failed:' , error ) ;
11
+ const extractUserIdFromToken = ( req : AuthenticatedRequest ) : string | null => {
12
+ const userId = req . userId ;
13
+ if ( ! userId ) {
14
+ console . error ( 'userId missing - Token is likely invalid' ) ;
34
15
return null ;
35
16
}
17
+ return userId
36
18
} ;
37
19
38
- export const getUserHistoryEntries = async ( req : Request , res : Response ) => {
20
+ export const getUserHistoryEntries = async ( req : AuthenticatedRequest , res : Response ) => {
39
21
try {
40
22
const userId = extractUserIdFromToken ( req ) ;
41
23
@@ -68,24 +50,20 @@ export const getUserHistoryEntries = async (req: Request, res: Response) => {
68
50
}
69
51
} ;
70
52
71
- export const createOrUpdateUserHistoryEntry = async ( req : Request , res : Response ) => {
53
+ export const createOrUpdateUserHistoryEntry = async ( req : AuthenticatedRequest , res : Response ) => {
72
54
try {
73
- // Extract userId from the token
74
55
const userId = extractUserIdFromToken ( req ) ;
75
56
76
57
if ( ! userId ) {
77
58
return res . status ( 401 ) . json ( { error : 'Invalid or missing token' } ) ;
78
59
}
79
60
80
- // Destructure required fields from the request body
81
61
const { questionId, roomId, attemptStartedAt, attemptCompletedAt, collaboratorId, attemptCode } = req . body ;
82
62
83
- // Validate required fields (optional but recommended)
84
63
if ( ! roomId ) {
85
64
return res . status ( 400 ) . json ( { error : 'roomId is required' } ) ;
86
65
}
87
66
88
- // Attempt to find an existing history entry with the same userId and roomId
89
67
const existingEntry = await historyEntryModel . findOne ( { userId, roomId } ) ;
90
68
91
69
if ( existingEntry ) {
@@ -118,7 +96,7 @@ export const createOrUpdateUserHistoryEntry = async (req: Request, res: Response
118
96
}
119
97
} ;
120
98
121
- export const deleteUserHistoryEntry = async ( req : Request , res : Response ) => {
99
+ export const deleteUserHistoryEntry = async ( req : AuthenticatedRequest , res : Response ) => {
122
100
try {
123
101
const userId = extractUserIdFromToken ( req ) ;
124
102
@@ -140,7 +118,7 @@ export const deleteUserHistoryEntry = async (req: Request, res: Response) => {
140
118
}
141
119
} ;
142
120
143
- export const deleteUserHistoryEntries = async ( req : Request , res : Response ) => {
121
+ export const deleteUserHistoryEntries = async ( req : AuthenticatedRequest , res : Response ) => {
144
122
try {
145
123
const userId = extractUserIdFromToken ( req ) ;
146
124
@@ -160,7 +138,7 @@ export const deleteUserHistoryEntries = async (req: Request, res: Response) => {
160
138
}
161
139
} ;
162
140
163
- export const deleteAllUserHistoryEntries = async ( req : Request , res : Response ) => {
141
+ export const deleteAllUserHistoryEntries = async ( req : AuthenticatedRequest , res : Response ) => {
164
142
try {
165
143
const userId = extractUserIdFromToken ( req ) ;
166
144
0 commit comments